-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoc-summary.ts
executable file
·47 lines (36 loc) · 1.2 KB
/
toc-summary.ts
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
#!/usr/bin/env -S deno run -A
import { preprocess } from "#/lib/mdbook-preprocessor.ts";
import { chapters, slugify } from "#/lib/mdbook.ts";
const scriptsDir = new URL("../scripts", import.meta.url).pathname;
const outputDir = scriptsDir + "/dist";
const outputFile = outputDir + "/subsectionindex.json";
const reSubsection = /^## (.*)$/gm;
export type SubsectionsOutput = Section[];
export type Section = {
name: string;
link: string;
subsections?: Section[];
};
await preprocess(async (context, book) => {
if (context.renderer != "html") {
return;
}
const output: SubsectionsOutput = [];
for (const chapter of chapters(book)) {
const processedSection: Section = {
name: chapter.name,
link: "/" + chapter.path.replace("src/", "").replace(".md", ".html"),
subsections: [],
};
for (const match of chapter.content.matchAll(reSubsection)) {
const subsection = match[1];
const slug = slugify(subsection);
processedSection.subsections!.push({
name: subsection,
link: `${processedSection.link}#${slug}`,
});
}
output.push(processedSection);
}
await Deno.writeTextFile(outputFile, JSON.stringify(output, null, 2));
});