-
Notifications
You must be signed in to change notification settings - Fork 11
/
.markdown-magic.config.js
66 lines (59 loc) · 1.74 KB
/
.markdown-magic.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const fs = require("fs");
const path = require("path");
const markdownMagic = require("markdown-magic");
const config = {
transforms: {
RENDERDOCS(_, options) {
const lines = fs
.readFileSync(options.path, { encoding: "UTF8" })
.split("\n");
const interfaces = {};
for (let line of lines) {
const match = line.match(/\* \[(.*)\]\[(InterfaceDeclaration-.*)\]/);
if (match) {
interfaces[match[1]] = `[${match[1]}][${match[2]}]`;
}
if (line.startsWith("##")) {
break;
}
}
const interfaceRegExp = `.*(${Object.keys(interfaces).join(
"|"
)}) | undefined.*`;
let isCode = false;
return lines
.map((line) => {
if (line.startsWith("#")) return `##${line}`;
if (line.startsWith("|")) {
const match = line.match(interfaceRegExp);
if (match) {
const interface = match[1];
line = line.replace(interface, interfaces[interface]);
}
line = line.replace("| undefined", "");
} else if (line === "```typescript") {
isCode = true;
} else if (line === "```") {
isCode = false;
} else if (isCode) {
line = line.replace(" | undefined", "");
}
return line;
})
.join("\n")
.replace(/index.md/g, "README.md");
},
EXAMPLE_CODE(_, options) {
return [
"```ts",
...fs
.readFileSync(options.src, { encoding: "UTF8" })
.trim()
.split("\n"),
"```",
].join("\n");
},
},
};
const markdownPath = path.join(__dirname, "README.md");
markdownMagic(markdownPath, config);