This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathmake-python-docs.mjs
More file actions
109 lines (95 loc) · 3.02 KB
/
Copy pathmake-python-docs.mjs
File metadata and controls
109 lines (95 loc) · 3.02 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
import { join, parse } from "path";
import { createInterface } from "readline";
import pkg from "fs-extra";
const { readdir, createReadStream, writeFile, readFile } = pkg;
// This script is not part of faast.js, but rather a tool to rewrite some parts
// of the generated docs from api-generator and api-documenter so they work with
// the website generated by docusaurus.
const outDir = "./docs/python";
const packageRoot = "./submodules/python";
const inDir = packageRoot + "/docs/docs";
// This is intentionally not the README dir, because the actually package README
// has development environment instructions that we don't want
const readmePath = inDir + "/index.md";
pkg.rmdirSync(outDir, { recursive: true, force: true });
pkg.ensureDirSync(outDir);
async function main() {
const docFiles = await readdir(inDir);
for (const docFile of docFiles) {
try {
const { name: id, ext } = parse(docFile);
if (ext !== ".md") {
continue;
}
const docPath = join(inDir, docFile);
const docPathOut = join(outDir, 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 line altogether
return;
}
// 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 (line.includes("<b>")) {
line = line.replace(/<b>/g, "**");
}
if (line.includes("</b>")) {
line = line.replace(/<\/b>/g, "**");
}
if (line.includes("<!-- -->")) {
line = line.replace(/<!-- -->/g, "");
}
output.push(line);
});
await new Promise((resolve) => lines.once("close", resolve));
input.close();
const header = [
"---",
`slug: /${id}`,
`title: ${title}`,
`hide_title: true`,
`displayed_sidebar: python`,
"---",
];
await writeFile(docPathOut, header.concat(output).join("\n"));
} catch (err) {
console.error(`Could not process ${docFile}: ${err}`);
}
}
await copyReadMe();
}
async function copyReadMe() {
const header = [
"---",
`title: Thirdweb Python SDK`,
`hide_title: true`,
`displayed_sidebar: python`,
"---",
];
const fileContents = await readFile(readmePath, "utf8");
await writeFile(
join(outDir, "index.md"),
header.join("\n") + "\n" + fileContents,
);
}
main();