-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocudocs.mjs
97 lines (86 loc) · 2.81 KB
/
docudocs.mjs
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
/*!
* Copyright (c) Friendly Captcha GmbH 2023.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Rewrites the markdown docs files making them more suitable for docusaurus.
// Heavily inspired by the docs build script in the faast.js repository (Apache-2.0 license)
import fsExtra from "fs-extra";
import { createInterface } from "readline";
import { join, parse, dirname } from "path";
const { readdir, createReadStream, writeFile, mkdir, rm } = fsExtra;
async function main() {
const dir = "./dist/docs/markdown";
const outDir = "./dist/docs/docusaurus";
await mkdir(outDir, { recursive: true });
await writeFile(
join(outDir, "_category_.json"),
JSON.stringify({
label: "SDK Reference",
position: 100,
})
);
console.log(join(outDir, "_category_.json"));
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 outDocPath = join(outDir, id + ".md");
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 !== "friendly-captcha-sdk") {
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, "|");
}
if (!skip) {
output.push(line);
}
});
await new Promise((resolve) => lines.once("close", resolve));
input.close();
let hide = id.includes(".");
// console.log(id, hide)
const header = [
"---",
`id: ${id}`,
`title: ${title.split(" ")[0]}`,
`hide_title: true`,
...(hide ? [`sidebar_class_name: sidebar-hidden`] : []),
"---",
];
await writeFile(outDocPath, header.concat(output).join("\n"));
} catch (err) {
console.error(`Could not process ${docFile}: ${err}`);
}
}
}
main();