-
Notifications
You must be signed in to change notification settings - Fork 34
/
toc.ts
35 lines (30 loc) · 935 Bytes
/
toc.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
import path from "path";
import fs from "fs";
import yaml from "js-yaml";
export interface RawChapter {
title: string;
local?: string;
new?: boolean;
local_fw?: { pt: string; tf: string; };
quiz?: string;
sections?: RawChapter[];
}
const flattener = (_flatChapters: RawChapter[], chapter: RawChapter): RawChapter[] => {
const sections = chapter?.sections as RawChapter[];
_flatChapters.push(chapter);
if (sections) {
for (const section of sections) {
flattener(_flatChapters, section);
}
}
return _flatChapters;
};
export async function get() {
const filepath = path.join(import.meta.url.replace('file://', ''), '../..', '_toctree.yml');
const content = (await fs.promises.readFile(filepath)).toString("utf-8");
const chapters: RawChapter[] = yaml.load(content) as RawChapter[];
const chaptersFlat: RawChapter[] = chapters.reduce(flattener, []);
return {
body: chaptersFlat
}
}