-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmake-docs.js
109 lines (98 loc) · 2.67 KB
/
make-docs.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const { readdir, createReadStream, writeFile } = require("fs-extra");
const { createInterface } = require("readline");
const { join, parse } = require("path");
const { exec } = require("child_process");
async function runApiExtractor() {
return new Promise((resolve, reject) => {
exec(
'api-extractor run --local',
(err, stdout, stderr) => {
console.log(stdout);
console.error(stderr);
if (err) {
reject(err);
} else {
resolve();
}
}
);
});
}
async function runApiDocumenter() {
return new Promise((resolve, reject) => {
exec(
'api-documenter markdown --input-folder temp --output-folder documentation/src/api',
(err, stdout, stderr) => {
console.log(stdout);
console.error(stderr);
if (err) {
reject(err);
} else {
resolve();
}
}
);
});
}
async function main() {
await runApiExtractor();
await runApiDocumenter();
const dir = "./documentation/src/api";
const docFiles = await readdir(dir);
for (const docFile of docFiles) {
try {
const { name: id, ext } = parse(docFile);
if (ext !== ".md") {
continue;
}
const docPath = join(dir, docFile);
const input = createReadStream(docPath);
const output = [];
const lines = createInterface({
input,
crlfDelay: Infinity
});
let title = "";
lines.on("line", line => {
let skip = false;
if (!title) {
const titleLine = line.match(/## (.*)/);
if (titleLine) {
title = titleLine[1];
}
}
const homeLink = line.match(/\[Home\]\(.\/index\.md\) > (.*)/);
if (homeLink) {
// Skip the breadcrumb for the toplevel index file.
if (id !== "filecoin.js") {
output.push(homeLink[1]);
}
skip = true;
}
// api-documenter expects \| to escape table
// column delimiters, but docusaurus uses a markdown processor
// that doesn't support this. Replace with an escape sequence
// that renders |.
if (line.startsWith("|")) {
line = line.replace(/\\\|/g, "|");
}
if (!skip) {
output.push(line);
}
});
await new Promise(resolve => lines.once("close", resolve));
input.close();
const header = [
"---",
`id: ${id}`,
`title: ${title}`,
`hide_title: true`,
"---"
];
await writeFile(docPath, header.concat(output).join("\n"));
} catch (err) {
console.error(`Could not process ${docFile}: ${err}`);
}
}
}
main();