-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypedoc.ts
84 lines (69 loc) · 2.42 KB
/
typedoc.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
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
import { readFile, writeFile, mkdtemp, copyFile } from "fs/promises";
import { join } from "path";
import { tmpdir } from "os";
import type { TypeDocOptionMap } from "typedoc/dist/lib/utils/options/declaration";
import * as pagesConfig from "./pagesconfig.json" with { type: "json" };
type Page = {
title: string;
source?: string; // no source means it's a directory
childrenDir?: string;
childrenOutputDir?: string;
childrenSourceDir?: string;
moduleRoot?: boolean;
output?: string;
children?: Page[];
};
type PagesConfig = {
source?: string;
pages?: Page[];
};
const config = (pagesConfig as unknown as { default: PagesConfig }).default;
const tutorials: string[] = [];
type ConvertOptions = {
parent: string;
pages: Page[];
root?: string | undefined;
};
const tempDir = await mkdtemp(join(tmpdir(), "ts-library-template-"));
const shouldPathUseRoot = (path: string) => !path.startsWith("./") && !path.startsWith("../") && !path.startsWith("/");
const generateTitle = (title: string, group?: string) => {
const tit = title.replace(/"/gu, `\\"`);
const sub = (group ?? "").replace(/"/gu, `\\"`);
const finalTitle = [sub, tit].filter(Boolean).join("/");
return `---
title: ${finalTitle}
---
`; // leave the ending new line
};
let i = 0;
// eslint-disable-next-line @typescript-eslint/no-shadow
const convertPages = async ({ pages, parent, root }: ConvertOptions) => {
for (const page of pages) {
if (page.source) {
const fullPath = shouldPathUseRoot(page.source) ? join(root ?? "", page.source) : page.source;
const tempPath = join(tempDir, `${i++}.md`);
tutorials.push(tempPath);
await copyFile(fullPath, tempPath);
await writeFile(tempPath, generateTitle(page.title, parent) + await readFile(fullPath, "utf-8"));
}
if (page.children) {
await convertPages({
pages: page.children,
parent: parent ? parent + "/" + page.title : page.title,
root: page.moduleRoot ? join(root ?? "", page.childrenSourceDir ?? "") : root,
});
}
}
};
if (config.pages?.length) {
const root = config.source;
await convertPages({ pages: config.pages, parent: "", root: root });
}
console.info({
tutorials,
tempDir,
});
export default {
projectDocuments: tutorials,
sortEntryPoints: false,
} satisfies Partial<TypeDocOptionMap>;