-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocess-docs.js
More file actions
190 lines (160 loc) · 5.83 KB
/
process-docs.js
File metadata and controls
190 lines (160 loc) · 5.83 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// Script to process generated documentation markdown files.
//
// Thanks to Fasst.js.
//
const { readdir, createReadStream, writeFile, readFile, remove, ensureDir } = require("fs-extra");
const { createInterface } = require("readline");
const { join, parse } = require("path");
async function main() {
const jsonInputDir = "./input";
const docsInputDir = "./markdown";
const docsOutputDir = "./docs-src/docs";
await remove(docsOutputDir);
await ensureDir(docsOutputDir);
const jsonFiles = await readdir(jsonInputDir);
const typeLabels = {};
function processMember(member) {
if (member.name) {
const name = member.name.trim();
if (name.length > 0) {
typeLabels[name.toLowerCase()] = name;
}
}
if (member.members) {
for (const child of member.members) {
processMember(child);
}
}
}
for (const jsonFile of jsonFiles) {
const apiData = JSON.parse(await readFile(join("./input", jsonFile)));
processMember(apiData);
}
const docFiles = await readdir(docsInputDir);
const idTree = {
label: "API",
children: {},
};
for (const docFile of docFiles) {
const { name: id, ext } = parse(docFile);
if (ext !== ".md") {
continue;
}
if (id !== "index") {
const idParts = id.split(".");
let idsNode = idTree;
for (let i = 0; i < idParts.length; ++i) {
const idPart = idParts[i];
if (!idsNode.children[idPart]) {
idsNode.children[idPart] = {
label: typeLabels[idPart] || idPart,
partId: idPart,
isDoc: false,
children: {},
};
}
idsNode = idsNode.children[idPart];
if (i === idParts.length-1) {
idsNode.isDoc = true;
idsNode.fullId = id;
}
}
}
const docInputPath = join(docsInputDir, docFile);
const input = createReadStream(docInputPath);
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];
}
}
//TODO: do I need this?
// const homeLink = line.match(/\[Home\]\(.\/index\.md\) > (.*)/);
// if (homeLink) {
// // Skip the breadcrumb for the toplevel index file.
// if (id !== "index") {
// output.push(homeLink[1]);
// }
// skip = true;
// }
// See issue #4. 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, "|");
}
// Remove empty HTML comments that seem to cause issues for Docusaurus.
line = line.replace(/<!-- -->/g, "");
if (!skip) {
output.push(line);
}
});
await new Promise(resolve => lines.once("close", resolve));
input.close();
const titleParts = title.split(".");
const nestedTitleParts = titleParts[titleParts.length-1].split(" ");
const extractedTitle = nestedTitleParts[0];
const header = [
"---",
`id: ${id}`,
`hide_title: true`,
`title: ${id ==="index" ? "Package index" : extractedTitle}`,
`slug: ${id === "index" ? "/" : `/${id}`}`,
"---"
];
const docOutputPath = join(docsOutputDir, docFile);
console.log(`>> ${docOutputPath}`);
await writeFile(docOutputPath, header.concat(output).join("\n"));
}
const gettingStarted = [
"---",
`id: readme`,
`hide_title: true`,
`title: Readme`,
`slug: readme`,
"---",
await readFile("./README.md"),
];
const readmeOutputPath = join(docsOutputDir, "README.md");
console.log(`>> ${readmeOutputPath}`);
await writeFile(readmeOutputPath, gettingStarted.join("\n"));
function* processIdTree(idNode) {
for (const key of Object.keys(idNode.children)) {
const child = idNode.children[key];
if (child.isDoc) {
yield {
type: "doc",
id: child.fullId,
};
}
const numChildren = Object.keys(child.children).length;
if (numChildren > 0) {
yield {
type: "category",
label: child.label,
items: Array.from(processIdTree(child)),
}
}
}
}
const idsOutputFile = join(docsOutputDir, "ids.json");
console.log(`>> ${idsOutputFile}`);
await writeFile(idsOutputFile, JSON.stringify(Array.from(processIdTree(idTree)), null, 4));
// await writeFile(join(dir, "id-tree.json"), JSON.stringify(idTree, null, 4));
}
main()
.then(() => console.log("Done"))
.catch(err => {
console.error(`Error processing docs.`);
console.error(err && err.stack || err);
});