-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtypedoc.config.base.mjs
90 lines (81 loc) · 2.67 KB
/
typedoc.config.base.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
import fs from "node:fs";
import path from "node:path";
import { OptionDefaults } from "typedoc";
/** @type {Partial<import('typedoc').TypeDocOptions>} */
const config = {
sort: ["source-order"],
tsconfig: "tsconfig.json",
readme: "website-root.md",
treatWarningsAsErrors: true,
validation: {
notExported: true,
invalidLink: true,
notDocumented: false, // Not every enum member has a JSDoc comment.
},
excludePrivate: true,
plugin: [
"typedoc-plugin-markdown",
"typedoc-plugin-rename",
"@zamiell/typedoc-plugin-not-exported",
],
githubPages: false,
blockTags: [
...OptionDefaults.blockTags,
"@allowEmptyVariadic",
"@maximum",
"@minimum",
],
};
const configTypeDocPluginMarkdown = {
/// hideBreadcrumbs: true,
};
/**
* @param {string} packageDirectoryPath The path to the package directory.
* @returns {Partial<import('typedoc').TypeDocOptions>} The generated config.
*/
export function getTypeDocConfig(packageDirectoryPath) {
const packageName = path.basename(packageDirectoryPath);
const out = path.join(import.meta.dirname, "docs", packageName);
// We want one entry point for each export source file, which will correspond to one Markdown file
// for each source file.
const indexTSPath = path.join(packageDirectoryPath, "src", "index.ts");
const typeScriptFileExports = getIndexTSExports(indexTSPath);
const exportsWithSrcPrefix = typeScriptFileExports.map((entryPoint) =>
entryPoint.replaceAll("./", "./src/"),
);
const entryPoints = exportsWithSrcPrefix.map(
(entryPoint) => `${entryPoint}.ts`,
);
return {
...config,
...configTypeDocPluginMarkdown,
out,
entryPoints,
};
}
/**
* By default, TypeDoc will create a page for each individual function (even if the
* "entryPointStrategy" is set to "expand"). Instead, we want to create a page per function
* category.
*
* This function parses the "index.ts" file to find all of the individual pages.
*
* @param {string} typeScriptFilePath The path to the ".ts" file.
* @returns {readonly string[]} An array of exported file paths.
*/
function getIndexTSExports(typeScriptFilePath) {
const typeScriptFile = fs.readFileSync(typeScriptFilePath, "utf8");
const lines = typeScriptFile.split("\n");
const exportLines = lines.filter((line) => line.startsWith("export"));
return exportLines.map((line) => {
const match = line.match(/export (?:type )?\* from "(.+)";/);
if (match === null) {
throw new Error(`Failed to parse line: ${line}`);
}
const insideQuotes = match[1];
if (insideQuotes === undefined) {
throw new Error(`Failed to parse inside the quotes: ${line}`);
}
return insideQuotes;
});
}