@rspress/plugin-typedoc 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/index.js.map +1 -0
- package/dist/lib/index.d.ts +17 -0
- package/dist/lib/index.js +278 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/types/index.d.ts +17 -0
- package/package.json +5 -5
- package/dist/index.js.map +0 -1
- /package/dist/{index.d.ts → es/index.d.ts} +0 -0
- /package/dist/{index.js → es/index.js} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"mappings":";AAAA,OAAOA,WAAU;AACjB,SAASC,aAAaC,sBAAsB;AAE5C,SAASC,YAAY;;;ACHd,IAAMC,UAAU;AAChB,IAAMC,eAAe,IAAID;;;ACDhC,OAAOJ,UAAU;AACjB,OAAOM,QAAQ;;;ACDR,SAASC,oBAAoBC,MAAY;AAC9C,SAAOA,KAAKC,QAAQ,OAAO,KAAKA,QAAQ,MAAM;AAChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SDiBeC,WAAWC,WAAiB;SAA5BD;;SAAAA;AAAAA,gBAAf,+BAA0BC,WAAiB;AAIzC,UAAMC;iBAAsB,+BAAOC;AACjC,cAAMC,UAAU,MAAMR,GAAGS,SAASF,UAAU;AAE5C,cAAMG,aAAaF,QAAQL,QACzB,4BACA,CAACQ,QAAQC,IAAIC;AACX,iBAAO,IAAID,SAASC;QACtB;AAEF,cAAMb,GAAGc,UAAUP,UAAUG;MAC/B;sBAVMJ,qBAA6BC;;;;AAYnC,UAAMQ;iBAAW,+BAAOC;AACtB,cAAMC,QAAQ,MAAMjB,GAAGkB,QAAQF;AAC/B,mBAAWG,QAAQF,OAAO;AACxB,gBAAMV,WAAWb,KAAK0B,KAAKJ,KAAKG;AAChC,gBAAME,OAAO,MAAMrB,GAAGqB,KAAKd;AAC3B,cAAIc,KAAKC,YAAW,GAAI;AACtB,kBAAMP,SAASR;UACjB,WAA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ID,SAAS;AAEX,gBAAME,oBAAoBvG,MAAK0B,KAAKsD,SAAUE;AAC9C,gBAAMM,IAAIgB,aAAaH,SAASE;AAChC,gBAAME,UAAUzG,MAAK0B,KAAK6E,mBAAmB;AAC7C,gBAAMf,IAAIkB,aAAaL,SAASI;AAEhClB,iBAAOoB,cAAcpB,OAAOoB,eAAe,CAAC;AAC5CpB,iBAAOoB,YAAYC,MAAMrB,OAAOoB,YAAYC,OAAO;AACnD,gBAAMC,eAAe,IAAI3B,OAAOzE,QAAQ,eAAe;AACvD8E,iBAAOoB,YAAYC,IAAIlD,KAAK;YAC1Bb,MAAM;YACNc,MAAMkD;UACR;AACAtB,iBAAOoB,YAAYG,UAAUvB,OAAOoB,YAAYG,WAAW,CAAC;AAC5DvB,iBAAOoB,YAAYG,QAAQD,gBACzB5B,YAAY3C,SAAS,IACjB,MAAMuB,4BAA4B4C,WAClC,MAAM1E,6BAA6B0E;AACzClB,iBAAOoB,YAAYG,QAAQD,cAAcE,QAAQ;YAC/ClE,MAAM;YACNc,MAAM,GAAGkD;UACX;QACF;AACAtB,eAAOyB,QAAQzB,OAAOyB,SAAS,CAAC;AAChCzB,eAAOyB,MAAMC,UAAU1B,OAAOyB,MAAMC,WAAW;AAC/C,eAAO1B;MACT;;EACF;AACF;","names":["path","Application","TSConfigReader","load","API_DIR","ROUTE_PREFIX","fs","transformModuleName","name","replace","patchLinks","outputDir","normlizeLinksInFile","filePath","content","readFile","newContent","_match","p1","p2","writeFile","traverse","dir","files","readdir","file","join","stat","isDirectory","isFile","test","resolveSidebarForSingleEntry","jsonFile","result","data","JSON","parse","children","length","symbolMap","Map","groups","forEach","group","groupItem","text","title","items","id","dataItem","find","item","fileName","has","count","get","set","toLocaleLowerCase","push","link","dirname","resolveSidebarForMultiEntry","getModulePath","getClassPath","moduleName","className","getInterfacePath","interfaceName","getFunctionPath","functionName","module","moduleNames","split","moduleConfig","sub","kindString","includes","slice","pluginTypeDoc","options","docRoot","entryPoints","outDir","addPages","_async_to_generator","routePath","filepath","config","app","root","addReader","bootstrap","theme","disableSources","readme","githubPages","requiredToBeDocumented","plugin","hideBreadcrumbs","hideMembersSymbol","allReflectionsHaveOwnDocument","project","convert","absoluteOutputdir","generateDocs","jsonDir","generateJson","themeConfig","nav","apiIndexLink","sidebar","unshift","route","exclude"],"sources":["../../src/home/runner/work/rspress/rspress/packages/plugin-typedoc/src/index.ts","../../src/home/runner/work/rspress/rspress/packages/plugin-typedoc/src/constants.ts","../../src/home/runner/work/rspress/rspress/packages/plugin-typedoc/src/sidebar.ts","../../src/home/runner/work/rspress/rspress/packages/plugin-typedoc/src/utils.ts"],"sourcesContent":["import path from 'path';\nimport { Application, TSConfigReader } from 'typedoc';\nimport type { RspressPlugin } from '@rspress/shared';\nimport { load } from 'typedoc-plugin-markdown';\nimport { API_DIR } from './constants';\nimport {\n resolveSidebarForMultiEntry,\n resolveSidebarForSingleEntry,\n} from './sidebar';\n\nexport interface PluginTypeDocOptions {\n /**\n * The entry points of modules.\n * @default []\n */\n entryPoints: string[];\n /**\n * The output directory.\n * @default 'api'\n */\n outDir?: string;\n}\n\nexport function pluginTypeDoc(options: PluginTypeDocOptions): RspressPlugin {\n let docRoot: string | undefined;\n const { entryPoints = [], outDir = API_DIR } = options;\n return {\n name: '@rspress/plugin-typedoc',\n async addPages() {\n return [\n {\n routePath: `${outDir.replace(/\\/$/, '')}/`,\n filepath: path.join(docRoot!, outDir, 'README.md'),\n },\n ];\n },\n async config(config) {\n const app = new Application();\n docRoot = config.root;\n app.options.addReader(new TSConfigReader());\n load(app);\n app.bootstrap({\n name: config.title,\n entryPoints,\n theme: 'markdown',\n disableSources: true,\n readme: 'none',\n githubPages: false,\n requiredToBeDocumented: ['Class', 'Function', 'Interface'],\n plugin: ['typedoc-plugin-markdown'],\n // @ts-expect-error MarkdownTheme has no export\n hideBreadcrumbs: true,\n hideMembersSymbol: true,\n allReflectionsHaveOwnDocument: true,\n });\n const project = app.convert();\n\n if (project) {\n // 1. Generate module doc by typedoc\n const absoluteOutputdir = path.join(docRoot!, outDir);\n await app.generateDocs(project, absoluteOutputdir);\n const jsonDir = path.join(absoluteOutputdir, 'documentation.json');\n await app.generateJson(project, jsonDir);\n // 2. Generate sidebar\n config.themeConfig = config.themeConfig || {};\n config.themeConfig.nav = config.themeConfig.nav || [];\n const apiIndexLink = `/${outDir.replace(/(^\\/)|(\\/$)/, '')}/`;\n config.themeConfig.nav.push({\n text: 'API',\n link: apiIndexLink,\n });\n config.themeConfig.sidebar = config.themeConfig.sidebar || {};\n config.themeConfig.sidebar[apiIndexLink] =\n entryPoints.length > 1\n ? await resolveSidebarForMultiEntry(jsonDir)\n : await resolveSidebarForSingleEntry(jsonDir);\n config.themeConfig.sidebar[apiIndexLink].unshift({\n text: 'Overview',\n link: `${apiIndexLink}README`,\n });\n }\n config.route = config.route || {};\n config.route.exclude = config.route.exclude || [];\n return config;\n },\n };\n}\n","export const API_DIR = 'api';\nexport const ROUTE_PREFIX = `/${API_DIR}`;\n","import path from 'path';\nimport fs from '@modern-js/utils/fs-extra';\nimport type { SidebarGroup } from '@rspress/shared';\nimport { transformModuleName } from './utils';\nimport { ROUTE_PREFIX } from './constants';\n\ninterface ModuleItem {\n id: number;\n name: string;\n children: {\n id: number;\n name: string;\n }[];\n groups: {\n title: string;\n children: number[];\n }[];\n}\n\nasync function patchLinks(outputDir: string) {\n // Patch links in markdown files\n // Scan all the markdown files in the output directory\n // replace [xxx](yyy) -> [xxx](./yyy)\n const normlizeLinksInFile = async (filePath: string) => {\n const content = await fs.readFile(filePath, 'utf-8');\n // replace: [xxx](yyy) -> [xxx](./yyy)\n const newContent = content.replace(\n /\\[([^\\]]+)\\]\\(([^)]+)\\)/g,\n (_match, p1, p2) => {\n return `[${p1}](./${p2})`;\n },\n );\n await fs.writeFile(filePath, newContent);\n };\n\n const traverse = async (dir: string) => {\n const files = await fs.readdir(dir);\n for (const file of files) {\n const filePath = path.join(dir, file);\n const stat = await fs.stat(filePath);\n if (stat.isDirectory()) {\n await traverse(filePath);\n } else if (stat.isFile() && /\\.mdx?/.test(file)) {\n await normlizeLinksInFile(filePath);\n }\n }\n };\n await traverse(outputDir);\n}\n\nexport async function resolveSidebarForSingleEntry(\n jsonFile: string,\n): Promise<SidebarGroup[]> {\n const result: SidebarGroup[] = [];\n const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));\n if (!data.children || data.children.length <= 0) {\n return [];\n }\n const symbolMap = new Map<string, number>();\n data.groups.forEach((group: { title: string; children: number[] }) => {\n const groupItem: SidebarGroup = {\n text: group.title,\n items: [],\n };\n group.children.forEach((id: number) => {\n const dataItem = data.children.find((item: ModuleItem) => item.id === id);\n if (dataItem) {\n // Note: we should handle the case that classes and interfaces have the same name\n // Such as class `Env` and variable `env`\n // The final output file name should be `classes/Env.md` and `variables/env-1.md`\n let fileName = dataItem.name;\n if (symbolMap.has(dataItem.name)) {\n const count = symbolMap.get(dataItem.name)! + 1;\n symbolMap.set(dataItem.name, count);\n fileName = `${dataItem.name}-${count}`;\n } else {\n symbolMap.set(dataItem.name.toLocaleLowerCase(), 0);\n }\n groupItem.items.push({\n text: dataItem.name,\n link: `${ROUTE_PREFIX}/${group.title.toLocaleLowerCase()}/${fileName}`,\n });\n }\n });\n result.push(groupItem);\n });\n\n await patchLinks(path.dirname(jsonFile));\n\n return result;\n}\n\nexport async function resolveSidebarForMultiEntry(\n jsonFile: string,\n): Promise<SidebarGroup[]> {\n const result: SidebarGroup[] = [];\n const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));\n if (!data.children || data.children.length <= 0) {\n return result;\n }\n\n function getModulePath(name: string) {\n return path\n .join(`${ROUTE_PREFIX}/modules`, `${transformModuleName(name)}`)\n .replace(/\\\\/g, '/');\n }\n\n function getClassPath(moduleName: string, className: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/classes`,\n `${transformModuleName(moduleName)}.${className}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n function getInterfacePath(moduleName: string, interfaceName: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/interfaces`,\n `${transformModuleName(moduleName)}.${interfaceName}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n function getFunctionPath(moduleName: string, functionName: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/functions`,\n `${transformModuleName(moduleName)}.${functionName}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n data.children.forEach((module: ModuleItem) => {\n const moduleNames = module.name.split('/');\n const name = moduleNames[moduleNames.length - 1];\n const moduleConfig = {\n text: `Module:${name}`,\n items: [{ text: 'Overview', link: getModulePath(module.name) }],\n };\n if (!module.children || module.children.length <= 0) {\n return;\n }\n module.children.forEach(sub => {\n const kindString = module.groups\n .find(item => item.children.includes(sub.id))\n ?.title.slice(0, -1);\n if (!kindString) {\n return;\n }\n switch (kindString) {\n case 'Class':\n moduleConfig.items.push({\n text: `Class:${sub.name}`,\n link: getClassPath(module.name, sub.name),\n });\n break;\n case 'Interface':\n moduleConfig.items.push({\n text: `Interface:${sub.name}`,\n link: getInterfacePath(module.name, sub.name),\n });\n break;\n case 'Function':\n moduleConfig.items.push({\n text: `Function:${sub.name}`,\n link: getFunctionPath(module.name, sub.name),\n });\n break;\n default:\n break;\n }\n });\n result.push(moduleConfig);\n });\n await patchLinks(path.dirname(jsonFile));\n return result;\n}\n","export function transformModuleName(name: string) {\n return name.replace(/\\//g, '_').replace(/-/g, '_');\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RspressPlugin } from '@rspress/shared';
|
|
2
|
+
|
|
3
|
+
interface PluginTypeDocOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The entry points of modules.
|
|
6
|
+
* @default []
|
|
7
|
+
*/
|
|
8
|
+
entryPoints: string[];
|
|
9
|
+
/**
|
|
10
|
+
* The output directory.
|
|
11
|
+
* @default 'api'
|
|
12
|
+
*/
|
|
13
|
+
outDir?: string;
|
|
14
|
+
}
|
|
15
|
+
declare function pluginTypeDoc(options: PluginTypeDocOptions): RspressPlugin;
|
|
16
|
+
|
|
17
|
+
export { PluginTypeDocOptions, pluginTypeDoc };
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var __async = (__this, __arguments, generator) => {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
var fulfilled = (value) => {
|
|
31
|
+
try {
|
|
32
|
+
step(generator.next(value));
|
|
33
|
+
} catch (e) {
|
|
34
|
+
reject(e);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var rejected = (value) => {
|
|
38
|
+
try {
|
|
39
|
+
step(generator.throw(value));
|
|
40
|
+
} catch (e) {
|
|
41
|
+
reject(e);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
45
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/index.ts
|
|
50
|
+
var src_exports = {};
|
|
51
|
+
__export(src_exports, {
|
|
52
|
+
pluginTypeDoc: () => pluginTypeDoc
|
|
53
|
+
});
|
|
54
|
+
module.exports = __toCommonJS(src_exports);
|
|
55
|
+
var import_path2 = __toESM(require("path"));
|
|
56
|
+
var import_typedoc = require("typedoc");
|
|
57
|
+
var import_typedoc_plugin_markdown = require("typedoc-plugin-markdown");
|
|
58
|
+
|
|
59
|
+
// src/constants.ts
|
|
60
|
+
var API_DIR = "api";
|
|
61
|
+
var ROUTE_PREFIX = `/${API_DIR}`;
|
|
62
|
+
|
|
63
|
+
// src/sidebar.ts
|
|
64
|
+
var import_path = __toESM(require("path"));
|
|
65
|
+
var import_fs_extra = __toESM(require("@modern-js/utils/fs-extra"));
|
|
66
|
+
|
|
67
|
+
// src/utils.ts
|
|
68
|
+
function transformModuleName(name) {
|
|
69
|
+
return name.replace(/\//g, "_").replace(/-/g, "_");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/sidebar.ts
|
|
73
|
+
function patchLinks(outputDir) {
|
|
74
|
+
return __async(this, null, function* () {
|
|
75
|
+
const normlizeLinksInFile = (filePath) => __async(this, null, function* () {
|
|
76
|
+
const content = yield import_fs_extra.default.readFile(filePath, "utf-8");
|
|
77
|
+
const newContent = content.replace(
|
|
78
|
+
/\[([^\]]+)\]\(([^)]+)\)/g,
|
|
79
|
+
(_match, p1, p2) => {
|
|
80
|
+
return `[${p1}](./${p2})`;
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
yield import_fs_extra.default.writeFile(filePath, newContent);
|
|
84
|
+
});
|
|
85
|
+
const traverse = (dir) => __async(this, null, function* () {
|
|
86
|
+
const files = yield import_fs_extra.default.readdir(dir);
|
|
87
|
+
for (const file of files) {
|
|
88
|
+
const filePath = import_path.default.join(dir, file);
|
|
89
|
+
const stat = yield import_fs_extra.default.stat(filePath);
|
|
90
|
+
if (stat.isDirectory()) {
|
|
91
|
+
yield traverse(filePath);
|
|
92
|
+
} else if (stat.isFile() && /\.mdx?/.test(file)) {
|
|
93
|
+
yield normlizeLinksInFile(filePath);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
yield traverse(outputDir);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
function resolveSidebarForSingleEntry(jsonFile) {
|
|
101
|
+
return __async(this, null, function* () {
|
|
102
|
+
const result = [];
|
|
103
|
+
const data = JSON.parse(yield import_fs_extra.default.readFile(jsonFile, "utf-8"));
|
|
104
|
+
if (!data.children || data.children.length <= 0) {
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
const symbolMap = /* @__PURE__ */ new Map();
|
|
108
|
+
data.groups.forEach((group) => {
|
|
109
|
+
const groupItem = {
|
|
110
|
+
text: group.title,
|
|
111
|
+
items: []
|
|
112
|
+
};
|
|
113
|
+
group.children.forEach((id) => {
|
|
114
|
+
const dataItem = data.children.find((item) => item.id === id);
|
|
115
|
+
if (dataItem) {
|
|
116
|
+
let fileName = dataItem.name;
|
|
117
|
+
if (symbolMap.has(dataItem.name)) {
|
|
118
|
+
const count = symbolMap.get(dataItem.name) + 1;
|
|
119
|
+
symbolMap.set(dataItem.name, count);
|
|
120
|
+
fileName = `${dataItem.name}-${count}`;
|
|
121
|
+
} else {
|
|
122
|
+
symbolMap.set(dataItem.name.toLocaleLowerCase(), 0);
|
|
123
|
+
}
|
|
124
|
+
groupItem.items.push({
|
|
125
|
+
text: dataItem.name,
|
|
126
|
+
link: `${ROUTE_PREFIX}/${group.title.toLocaleLowerCase()}/${fileName}`
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
result.push(groupItem);
|
|
131
|
+
});
|
|
132
|
+
yield patchLinks(import_path.default.dirname(jsonFile));
|
|
133
|
+
return result;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function resolveSidebarForMultiEntry(jsonFile) {
|
|
137
|
+
return __async(this, null, function* () {
|
|
138
|
+
const result = [];
|
|
139
|
+
const data = JSON.parse(yield import_fs_extra.default.readFile(jsonFile, "utf-8"));
|
|
140
|
+
if (!data.children || data.children.length <= 0) {
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
function getModulePath(name) {
|
|
144
|
+
return import_path.default.join(`${ROUTE_PREFIX}/modules`, `${transformModuleName(name)}`).replace(/\\/g, "/");
|
|
145
|
+
}
|
|
146
|
+
function getClassPath(moduleName, className) {
|
|
147
|
+
return import_path.default.join(
|
|
148
|
+
`${ROUTE_PREFIX}/classes`,
|
|
149
|
+
`${transformModuleName(moduleName)}.${className}`
|
|
150
|
+
).replace(/\\/g, "/");
|
|
151
|
+
}
|
|
152
|
+
function getInterfacePath(moduleName, interfaceName) {
|
|
153
|
+
return import_path.default.join(
|
|
154
|
+
`${ROUTE_PREFIX}/interfaces`,
|
|
155
|
+
`${transformModuleName(moduleName)}.${interfaceName}`
|
|
156
|
+
).replace(/\\/g, "/");
|
|
157
|
+
}
|
|
158
|
+
function getFunctionPath(moduleName, functionName) {
|
|
159
|
+
return import_path.default.join(
|
|
160
|
+
`${ROUTE_PREFIX}/functions`,
|
|
161
|
+
`${transformModuleName(moduleName)}.${functionName}`
|
|
162
|
+
).replace(/\\/g, "/");
|
|
163
|
+
}
|
|
164
|
+
data.children.forEach((module2) => {
|
|
165
|
+
const moduleNames = module2.name.split("/");
|
|
166
|
+
const name = moduleNames[moduleNames.length - 1];
|
|
167
|
+
const moduleConfig = {
|
|
168
|
+
text: `Module:${name}`,
|
|
169
|
+
items: [{ text: "Overview", link: getModulePath(module2.name) }]
|
|
170
|
+
};
|
|
171
|
+
if (!module2.children || module2.children.length <= 0) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
module2.children.forEach((sub) => {
|
|
175
|
+
var _a;
|
|
176
|
+
const kindString = (_a = module2.groups.find((item) => item.children.includes(sub.id))) == null ? void 0 : _a.title.slice(0, -1);
|
|
177
|
+
if (!kindString) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
switch (kindString) {
|
|
181
|
+
case "Class":
|
|
182
|
+
moduleConfig.items.push({
|
|
183
|
+
text: `Class:${sub.name}`,
|
|
184
|
+
link: getClassPath(module2.name, sub.name)
|
|
185
|
+
});
|
|
186
|
+
break;
|
|
187
|
+
case "Interface":
|
|
188
|
+
moduleConfig.items.push({
|
|
189
|
+
text: `Interface:${sub.name}`,
|
|
190
|
+
link: getInterfacePath(module2.name, sub.name)
|
|
191
|
+
});
|
|
192
|
+
break;
|
|
193
|
+
case "Function":
|
|
194
|
+
moduleConfig.items.push({
|
|
195
|
+
text: `Function:${sub.name}`,
|
|
196
|
+
link: getFunctionPath(module2.name, sub.name)
|
|
197
|
+
});
|
|
198
|
+
break;
|
|
199
|
+
default:
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
result.push(moduleConfig);
|
|
204
|
+
});
|
|
205
|
+
yield patchLinks(import_path.default.dirname(jsonFile));
|
|
206
|
+
return result;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/index.ts
|
|
211
|
+
function pluginTypeDoc(options) {
|
|
212
|
+
let docRoot;
|
|
213
|
+
const { entryPoints = [], outDir = API_DIR } = options;
|
|
214
|
+
return {
|
|
215
|
+
name: "@rspress/plugin-typedoc",
|
|
216
|
+
addPages() {
|
|
217
|
+
return __async(this, null, function* () {
|
|
218
|
+
return [
|
|
219
|
+
{
|
|
220
|
+
routePath: `${outDir.replace(/\/$/, "")}/`,
|
|
221
|
+
filepath: import_path2.default.join(docRoot, outDir, "README.md")
|
|
222
|
+
}
|
|
223
|
+
];
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
config(config) {
|
|
227
|
+
return __async(this, null, function* () {
|
|
228
|
+
const app = new import_typedoc.Application();
|
|
229
|
+
docRoot = config.root;
|
|
230
|
+
app.options.addReader(new import_typedoc.TSConfigReader());
|
|
231
|
+
(0, import_typedoc_plugin_markdown.load)(app);
|
|
232
|
+
app.bootstrap({
|
|
233
|
+
name: config.title,
|
|
234
|
+
entryPoints,
|
|
235
|
+
theme: "markdown",
|
|
236
|
+
disableSources: true,
|
|
237
|
+
readme: "none",
|
|
238
|
+
githubPages: false,
|
|
239
|
+
requiredToBeDocumented: ["Class", "Function", "Interface"],
|
|
240
|
+
plugin: ["typedoc-plugin-markdown"],
|
|
241
|
+
// @ts-expect-error MarkdownTheme has no export
|
|
242
|
+
hideBreadcrumbs: true,
|
|
243
|
+
hideMembersSymbol: true,
|
|
244
|
+
allReflectionsHaveOwnDocument: true
|
|
245
|
+
});
|
|
246
|
+
const project = app.convert();
|
|
247
|
+
if (project) {
|
|
248
|
+
const absoluteOutputdir = import_path2.default.join(docRoot, outDir);
|
|
249
|
+
yield app.generateDocs(project, absoluteOutputdir);
|
|
250
|
+
const jsonDir = import_path2.default.join(absoluteOutputdir, "documentation.json");
|
|
251
|
+
yield app.generateJson(project, jsonDir);
|
|
252
|
+
config.themeConfig = config.themeConfig || {};
|
|
253
|
+
config.themeConfig.nav = config.themeConfig.nav || [];
|
|
254
|
+
const apiIndexLink = `/${outDir.replace(/(^\/)|(\/$)/, "")}/`;
|
|
255
|
+
config.themeConfig.nav.push({
|
|
256
|
+
text: "API",
|
|
257
|
+
link: apiIndexLink
|
|
258
|
+
});
|
|
259
|
+
config.themeConfig.sidebar = config.themeConfig.sidebar || {};
|
|
260
|
+
config.themeConfig.sidebar[apiIndexLink] = entryPoints.length > 1 ? yield resolveSidebarForMultiEntry(jsonDir) : yield resolveSidebarForSingleEntry(jsonDir);
|
|
261
|
+
config.themeConfig.sidebar[apiIndexLink].unshift({
|
|
262
|
+
text: "Overview",
|
|
263
|
+
link: `${apiIndexLink}README`
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
config.route = config.route || {};
|
|
267
|
+
config.route.exclude = config.route.exclude || [];
|
|
268
|
+
return config;
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
274
|
+
0 && (module.exports = {
|
|
275
|
+
pluginTypeDoc
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAiB;AACjB,qBAA4C;AAE5C,qCAAqB;;;ACHd,IAAM,UAAU;AAChB,IAAM,eAAe,IAAI;;;ACDhC,kBAAiB;AACjB,sBAAe;;;ACDR,SAAS,oBAAoB,MAAc;AAChD,SAAO,KAAK,QAAQ,OAAO,GAAG,EAAE,QAAQ,MAAM,GAAG;AACnD;;;ADiBA,SAAe,WAAW,WAAmB;AAAA;AAI3C,UAAM,sBAAsB,CAAO,aAAqB;AACtD,YAAM,UAAU,MAAM,wBAAG,SAAS,UAAU,OAAO;AAEnD,YAAM,aAAa,QAAQ;AAAA,QACzB;AAAA,QACA,CAAC,QAAQ,IAAI,OAAO;AAClB,iBAAO,IAAI,SAAS;AAAA,QACtB;AAAA,MACF;AACA,YAAM,wBAAG,UAAU,UAAU,UAAU;AAAA,IACzC;AAEA,UAAM,WAAW,CAAO,QAAgB;AACtC,YAAM,QAAQ,MAA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index.ts","../../src/constants.ts","../../src/sidebar.ts","../../src/utils.ts"],"sourcesContent":["import path from 'path';\nimport { Application, TSConfigReader } from 'typedoc';\nimport type { RspressPlugin } from '@rspress/shared';\nimport { load } from 'typedoc-plugin-markdown';\nimport { API_DIR } from './constants';\nimport {\n resolveSidebarForMultiEntry,\n resolveSidebarForSingleEntry,\n} from './sidebar';\n\nexport interface PluginTypeDocOptions {\n /**\n * The entry points of modules.\n * @default []\n */\n entryPoints: string[];\n /**\n * The output directory.\n * @default 'api'\n */\n outDir?: string;\n}\n\nexport function pluginTypeDoc(options: PluginTypeDocOptions): RspressPlugin {\n let docRoot: string | undefined;\n const { entryPoints = [], outDir = API_DIR } = options;\n return {\n name: '@rspress/plugin-typedoc',\n async addPages() {\n return [\n {\n routePath: `${outDir.replace(/\\/$/, '')}/`,\n filepath: path.join(docRoot!, outDir, 'README.md'),\n },\n ];\n },\n async config(config) {\n const app = new Application();\n docRoot = config.root;\n app.options.addReader(new TSConfigReader());\n load(app);\n app.bootstrap({\n name: config.title,\n entryPoints,\n theme: 'markdown',\n disableSources: true,\n readme: 'none',\n githubPages: false,\n requiredToBeDocumented: ['Class', 'Function', 'Interface'],\n plugin: ['typedoc-plugin-markdown'],\n // @ts-expect-error MarkdownTheme has no export\n hideBreadcrumbs: true,\n hideMembersSymbol: true,\n allReflectionsHaveOwnDocument: true,\n });\n const project = app.convert();\n\n if (project) {\n // 1. Generate module doc by typedoc\n const absoluteOutputdir = path.join(docRoot!, outDir);\n await app.generateDocs(project, absoluteOutputdir);\n const jsonDir = path.join(absoluteOutputdir, 'documentation.json');\n await app.generateJson(project, jsonDir);\n // 2. Generate sidebar\n config.themeConfig = config.themeConfig || {};\n config.themeConfig.nav = config.themeConfig.nav || [];\n const apiIndexLink = `/${outDir.replace(/(^\\/)|(\\/$)/, '')}/`;\n config.themeConfig.nav.push({\n text: 'API',\n link: apiIndexLink,\n });\n config.themeConfig.sidebar = config.themeConfig.sidebar || {};\n config.themeConfig.sidebar[apiIndexLink] =\n entryPoints.length > 1\n ? await resolveSidebarForMultiEntry(jsonDir)\n : await resolveSidebarForSingleEntry(jsonDir);\n config.themeConfig.sidebar[apiIndexLink].unshift({\n text: 'Overview',\n link: `${apiIndexLink}README`,\n });\n }\n config.route = config.route || {};\n config.route.exclude = config.route.exclude || [];\n return config;\n },\n };\n}\n","export const API_DIR = 'api';\nexport const ROUTE_PREFIX = `/${API_DIR}`;\n","import path from 'path';\nimport fs from '@modern-js/utils/fs-extra';\nimport type { SidebarGroup } from '@rspress/shared';\nimport { transformModuleName } from './utils';\nimport { ROUTE_PREFIX } from './constants';\n\ninterface ModuleItem {\n id: number;\n name: string;\n children: {\n id: number;\n name: string;\n }[];\n groups: {\n title: string;\n children: number[];\n }[];\n}\n\nasync function patchLinks(outputDir: string) {\n // Patch links in markdown files\n // Scan all the markdown files in the output directory\n // replace [xxx](yyy) -> [xxx](./yyy)\n const normlizeLinksInFile = async (filePath: string) => {\n const content = await fs.readFile(filePath, 'utf-8');\n // replace: [xxx](yyy) -> [xxx](./yyy)\n const newContent = content.replace(\n /\\[([^\\]]+)\\]\\(([^)]+)\\)/g,\n (_match, p1, p2) => {\n return `[${p1}](./${p2})`;\n },\n );\n await fs.writeFile(filePath, newContent);\n };\n\n const traverse = async (dir: string) => {\n const files = await fs.readdir(dir);\n for (const file of files) {\n const filePath = path.join(dir, file);\n const stat = await fs.stat(filePath);\n if (stat.isDirectory()) {\n await traverse(filePath);\n } else if (stat.isFile() && /\\.mdx?/.test(file)) {\n await normlizeLinksInFile(filePath);\n }\n }\n };\n await traverse(outputDir);\n}\n\nexport async function resolveSidebarForSingleEntry(\n jsonFile: string,\n): Promise<SidebarGroup[]> {\n const result: SidebarGroup[] = [];\n const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));\n if (!data.children || data.children.length <= 0) {\n return [];\n }\n const symbolMap = new Map<string, number>();\n data.groups.forEach((group: { title: string; children: number[] }) => {\n const groupItem: SidebarGroup = {\n text: group.title,\n items: [],\n };\n group.children.forEach((id: number) => {\n const dataItem = data.children.find((item: ModuleItem) => item.id === id);\n if (dataItem) {\n // Note: we should handle the case that classes and interfaces have the same name\n // Such as class `Env` and variable `env`\n // The final output file name should be `classes/Env.md` and `variables/env-1.md`\n let fileName = dataItem.name;\n if (symbolMap.has(dataItem.name)) {\n const count = symbolMap.get(dataItem.name)! + 1;\n symbolMap.set(dataItem.name, count);\n fileName = `${dataItem.name}-${count}`;\n } else {\n symbolMap.set(dataItem.name.toLocaleLowerCase(), 0);\n }\n groupItem.items.push({\n text: dataItem.name,\n link: `${ROUTE_PREFIX}/${group.title.toLocaleLowerCase()}/${fileName}`,\n });\n }\n });\n result.push(groupItem);\n });\n\n await patchLinks(path.dirname(jsonFile));\n\n return result;\n}\n\nexport async function resolveSidebarForMultiEntry(\n jsonFile: string,\n): Promise<SidebarGroup[]> {\n const result: SidebarGroup[] = [];\n const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));\n if (!data.children || data.children.length <= 0) {\n return result;\n }\n\n function getModulePath(name: string) {\n return path\n .join(`${ROUTE_PREFIX}/modules`, `${transformModuleName(name)}`)\n .replace(/\\\\/g, '/');\n }\n\n function getClassPath(moduleName: string, className: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/classes`,\n `${transformModuleName(moduleName)}.${className}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n function getInterfacePath(moduleName: string, interfaceName: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/interfaces`,\n `${transformModuleName(moduleName)}.${interfaceName}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n function getFunctionPath(moduleName: string, functionName: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/functions`,\n `${transformModuleName(moduleName)}.${functionName}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n data.children.forEach((module: ModuleItem) => {\n const moduleNames = module.name.split('/');\n const name = moduleNames[moduleNames.length - 1];\n const moduleConfig = {\n text: `Module:${name}`,\n items: [{ text: 'Overview', link: getModulePath(module.name) }],\n };\n if (!module.children || module.children.length <= 0) {\n return;\n }\n module.children.forEach(sub => {\n const kindString = module.groups\n .find(item => item.children.includes(sub.id))\n ?.title.slice(0, -1);\n if (!kindString) {\n return;\n }\n switch (kindString) {\n case 'Class':\n moduleConfig.items.push({\n text: `Class:${sub.name}`,\n link: getClassPath(module.name, sub.name),\n });\n break;\n case 'Interface':\n moduleConfig.items.push({\n text: `Interface:${sub.name}`,\n link: getInterfacePath(module.name, sub.name),\n });\n break;\n case 'Function':\n moduleConfig.items.push({\n text: `Function:${sub.name}`,\n link: getFunctionPath(module.name, sub.name),\n });\n break;\n default:\n break;\n }\n });\n result.push(moduleConfig);\n });\n await patchLinks(path.dirname(jsonFile));\n return result;\n}\n","export function transformModuleName(name: string) {\n return name.replace(/\\//g, '_').replace(/-/g, '_');\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RspressPlugin } from '@rspress/shared';
|
|
2
|
+
|
|
3
|
+
interface PluginTypeDocOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The entry points of modules.
|
|
6
|
+
* @default []
|
|
7
|
+
*/
|
|
8
|
+
entryPoints: string[];
|
|
9
|
+
/**
|
|
10
|
+
* The output directory.
|
|
11
|
+
* @default 'api'
|
|
12
|
+
*/
|
|
13
|
+
outDir?: string;
|
|
14
|
+
}
|
|
15
|
+
declare function pluginTypeDoc(options: PluginTypeDocOptions): RspressPlugin;
|
|
16
|
+
|
|
17
|
+
export { PluginTypeDocOptions, pluginTypeDoc };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rspress/plugin-typedoc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A plugin for rspress to integrate typedoc",
|
|
5
5
|
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
|
6
6
|
"repository": {
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
},
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"jsnext:source": "./src/index.ts",
|
|
13
|
-
"types": "./dist/index.d.ts",
|
|
14
|
-
"main": "./dist/index.js",
|
|
15
|
-
"module": "./dist/
|
|
13
|
+
"types": "./dist/types/index.d.ts",
|
|
14
|
+
"main": "./dist/lib/index.js",
|
|
15
|
+
"module": "./dist/es/index.js",
|
|
16
16
|
"engines": {
|
|
17
17
|
"node": ">=14.17.6"
|
|
18
18
|
},
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"typescript": "^5",
|
|
29
29
|
"vitest": "0.33.0",
|
|
30
30
|
"react": "^18",
|
|
31
|
-
"@rspress/shared": "0.
|
|
31
|
+
"@rspress/shared": "0.2.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"rspress": "*"
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"mappings":";AAAA,OAAOA,WAAU;AACjB,SAASC,aAAaC,sBAAsB;AAE5C,SAASC,YAAY;;;ACHd,IAAMC,UAAU;AAChB,IAAMC,eAAe,IAAID;;;ACDhC,OAAOJ,UAAU;AACjB,OAAOM,QAAQ;;;ACDR,SAASC,oBAAoBC,MAAY;AAC9C,SAAOA,KAAKC,QAAQ,OAAO,KAAKA,QAAQ,MAAM;AAChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SDiBeC,WAAWC,WAAiB;SAA5BD;;SAAAA;AAAAA,gBAAf,+BAA0BC,WAAiB;AAIzC,UAAMC;iBAAsB,+BAAOC;AACjC,cAAMC,UAAU,MAAMR,GAAGS,SAASF,UAAU;AAE5C,cAAMG,aAAaF,QAAQL,QACzB,4BACA,CAACQ,QAAQC,IAAIC;AACX,iBAAO,IAAID,SAASC;QACtB;AAEF,cAAMb,GAAGc,UAAUP,UAAUG;MAC/B;sBAVMJ,qBAA6BC;;;;AAYnC,UAAMQ;iBAAW,+BAAOC;AACtB,cAAMC,QAAQ,MAAMjB,G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O;UACPC,gBAAgB;UAChBC,QAAQ;UACRC,aAAa;UACbC,wBAAwB;YAAC;YAAS;YAAY;;UAC9CC,QAAQ;YAAC;;;UAETC,iBAAiB;UACjBC,mBAAmB;UACnBC,+BAA+B;QACjC;AACA,cAAMC,UAAUb,IAAIc,QAAO;AAE3B,YAAID,SAAS;AAEX,gBAAME,oBAAoBvG,MAAK0B,KAAKsD,SAAUE;AAC9C,gBAAMM,IAAIgB,aAAaH,SAASE;AAChC,gBAAME,UAAUzG,MAAK0B,KAAK6E,mBAAmB;AAC7C,gBAAMf,IAAIkB,aAAaL,SAASI;AAEhClB,iBAAOoB,cAAcpB,OAAOoB,eAAe,CAAC;AAC5CpB,iBAAOoB,YAAYC,MAAMrB,OAAOoB,YAAYC,OAAO;AACnD,gBAAMC,eAAe,IAAI3B,OAAOzE,QAAQ,eAAe;AACvD8E,iBAAOoB,YAAYC,IAAIlD,KAAK;YAC1Bb,MAAM;YACNc,MAAMkD;UACR;AACAtB,iBAAOoB,YAAYG,UAAUvB,OAAOoB,YAAYG,WAAW,CAAC;AAC5DvB,iBAAOoB,YAAYG,QAAQD,gBACzB5B,YAAY3C,SAAS,IACjB,MAAMuB,4BAA4B4C,WAClC,MAAM1E,6BAA6B0E;AACzClB,iBAAOoB,YAAYG,QAAQD,cAAcE,QAAQ;YAC/ClE,MAAM;YACNc,MAAM,GAAGkD;UACX;QACF;AACAtB,eAAOyB,QAAQzB,OAAOyB,SAAS,CAAC;AAChCzB,eAAOyB,MAAMC,UAAU1B,OAAOyB,MAAMC,WAAW;AAC/C,eAAO1B;MACT;;EACF;AACF;","names":["path","Application","TSConfigReader","load","API_DIR","ROUTE_PREFIX","fs","transformModuleName","name","replace","patchLinks","outputDir","normlizeLinksInFile","filePath","content","readFile","newContent","_match","p1","p2","writeFile","traverse","dir","files","readdir","file","join","stat","isDirectory","isFile","test","resolveSidebarForSingleEntry","jsonFile","result","data","JSON","parse","children","length","symbolMap","Map","groups","forEach","group","groupItem","text","title","items","id","dataItem","find","item","fileName","has","count","get","set","toLocaleLowerCase","push","link","dirname","resolveSidebarForMultiEntry","getModulePath","getClassPath","moduleName","className","getInterfacePath","interfaceName","getFunctionPath","functionName","module","moduleNames","split","moduleConfig","sub","kindString","includes","slice","pluginTypeDoc","options","docRoot","entryPoints","outDir","addPages","_async_to_generator","routePath","filepath","config","app","root","addReader","bootstrap","theme","disableSources","readme","githubPages","requiredToBeDocumented","plugin","hideBreadcrumbs","hideMembersSymbol","allReflectionsHaveOwnDocument","project","convert","absoluteOutputdir","generateDocs","jsonDir","generateJson","themeConfig","nav","apiIndexLink","sidebar","unshift","route","exclude"],"sources":["../src/home/runner/work/rspress/rspress/packages/plugin-typedoc/src/index.ts","../src/home/runner/work/rspress/rspress/packages/plugin-typedoc/src/constants.ts","../src/home/runner/work/rspress/rspress/packages/plugin-typedoc/src/sidebar.ts","../src/home/runner/work/rspress/rspress/packages/plugin-typedoc/src/utils.ts"],"sourcesContent":["import path from 'path';\nimport { Application, TSConfigReader } from 'typedoc';\nimport type { RspressPlugin } from '@rspress/shared';\nimport { load } from 'typedoc-plugin-markdown';\nimport { API_DIR } from './constants';\nimport {\n resolveSidebarForMultiEntry,\n resolveSidebarForSingleEntry,\n} from './sidebar';\n\nexport interface PluginTypeDocOptions {\n /**\n * The entry points of modules.\n * @default []\n */\n entryPoints: string[];\n /**\n * The output directory.\n * @default 'api'\n */\n outDir?: string;\n}\n\nexport function pluginTypeDoc(options: PluginTypeDocOptions): RspressPlugin {\n let docRoot: string | undefined;\n const { entryPoints = [], outDir = API_DIR } = options;\n return {\n name: '@rspress/plugin-typedoc',\n async addPages() {\n return [\n {\n routePath: `${outDir.replace(/\\/$/, '')}/`,\n filepath: path.join(docRoot!, outDir, 'README.md'),\n },\n ];\n },\n async config(config) {\n const app = new Application();\n docRoot = config.root;\n app.options.addReader(new TSConfigReader());\n load(app);\n app.bootstrap({\n name: config.title,\n entryPoints,\n theme: 'markdown',\n disableSources: true,\n readme: 'none',\n githubPages: false,\n requiredToBeDocumented: ['Class', 'Function', 'Interface'],\n plugin: ['typedoc-plugin-markdown'],\n // @ts-expect-error MarkdownTheme has no export\n hideBreadcrumbs: true,\n hideMembersSymbol: true,\n allReflectionsHaveOwnDocument: true,\n });\n const project = app.convert();\n\n if (project) {\n // 1. Generate module doc by typedoc\n const absoluteOutputdir = path.join(docRoot!, outDir);\n await app.generateDocs(project, absoluteOutputdir);\n const jsonDir = path.join(absoluteOutputdir, 'documentation.json');\n await app.generateJson(project, jsonDir);\n // 2. Generate sidebar\n config.themeConfig = config.themeConfig || {};\n config.themeConfig.nav = config.themeConfig.nav || [];\n const apiIndexLink = `/${outDir.replace(/(^\\/)|(\\/$)/, '')}/`;\n config.themeConfig.nav.push({\n text: 'API',\n link: apiIndexLink,\n });\n config.themeConfig.sidebar = config.themeConfig.sidebar || {};\n config.themeConfig.sidebar[apiIndexLink] =\n entryPoints.length > 1\n ? await resolveSidebarForMultiEntry(jsonDir)\n : await resolveSidebarForSingleEntry(jsonDir);\n config.themeConfig.sidebar[apiIndexLink].unshift({\n text: 'Overview',\n link: `${apiIndexLink}README`,\n });\n }\n config.route = config.route || {};\n config.route.exclude = config.route.exclude || [];\n return config;\n },\n };\n}\n","export const API_DIR = 'api';\nexport const ROUTE_PREFIX = `/${API_DIR}`;\n","import path from 'path';\nimport fs from '@modern-js/utils/fs-extra';\nimport type { SidebarGroup } from '@rspress/shared';\nimport { transformModuleName } from './utils';\nimport { ROUTE_PREFIX } from './constants';\n\ninterface ModuleItem {\n id: number;\n name: string;\n children: {\n id: number;\n name: string;\n }[];\n groups: {\n title: string;\n children: number[];\n }[];\n}\n\nasync function patchLinks(outputDir: string) {\n // Patch links in markdown files\n // Scan all the markdown files in the output directory\n // replace [xxx](yyy) -> [xxx](./yyy)\n const normlizeLinksInFile = async (filePath: string) => {\n const content = await fs.readFile(filePath, 'utf-8');\n // replace: [xxx](yyy) -> [xxx](./yyy)\n const newContent = content.replace(\n /\\[([^\\]]+)\\]\\(([^)]+)\\)/g,\n (_match, p1, p2) => {\n return `[${p1}](./${p2})`;\n },\n );\n await fs.writeFile(filePath, newContent);\n };\n\n const traverse = async (dir: string) => {\n const files = await fs.readdir(dir);\n for (const file of files) {\n const filePath = path.join(dir, file);\n const stat = await fs.stat(filePath);\n if (stat.isDirectory()) {\n await traverse(filePath);\n } else if (stat.isFile() && /\\.mdx?/.test(file)) {\n await normlizeLinksInFile(filePath);\n }\n }\n };\n await traverse(outputDir);\n}\n\nexport async function resolveSidebarForSingleEntry(\n jsonFile: string,\n): Promise<SidebarGroup[]> {\n const result: SidebarGroup[] = [];\n const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));\n if (!data.children || data.children.length <= 0) {\n return [];\n }\n const symbolMap = new Map<string, number>();\n data.groups.forEach((group: { title: string; children: number[] }) => {\n const groupItem: SidebarGroup = {\n text: group.title,\n items: [],\n };\n group.children.forEach((id: number) => {\n const dataItem = data.children.find((item: ModuleItem) => item.id === id);\n if (dataItem) {\n // Note: we should handle the case that classes and interfaces have the same name\n // Such as class `Env` and variable `env`\n // The final output file name should be `classes/Env.md` and `variables/env-1.md`\n let fileName = dataItem.name;\n if (symbolMap.has(dataItem.name)) {\n const count = symbolMap.get(dataItem.name)! + 1;\n symbolMap.set(dataItem.name, count);\n fileName = `${dataItem.name}-${count}`;\n } else {\n symbolMap.set(dataItem.name.toLocaleLowerCase(), 0);\n }\n groupItem.items.push({\n text: dataItem.name,\n link: `${ROUTE_PREFIX}/${group.title.toLocaleLowerCase()}/${fileName}`,\n });\n }\n });\n result.push(groupItem);\n });\n\n await patchLinks(path.dirname(jsonFile));\n\n return result;\n}\n\nexport async function resolveSidebarForMultiEntry(\n jsonFile: string,\n): Promise<SidebarGroup[]> {\n const result: SidebarGroup[] = [];\n const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));\n if (!data.children || data.children.length <= 0) {\n return result;\n }\n\n function getModulePath(name: string) {\n return path\n .join(`${ROUTE_PREFIX}/modules`, `${transformModuleName(name)}`)\n .replace(/\\\\/g, '/');\n }\n\n function getClassPath(moduleName: string, className: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/classes`,\n `${transformModuleName(moduleName)}.${className}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n function getInterfacePath(moduleName: string, interfaceName: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/interfaces`,\n `${transformModuleName(moduleName)}.${interfaceName}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n function getFunctionPath(moduleName: string, functionName: string) {\n return path\n .join(\n `${ROUTE_PREFIX}/functions`,\n `${transformModuleName(moduleName)}.${functionName}`,\n )\n .replace(/\\\\/g, '/');\n }\n\n data.children.forEach((module: ModuleItem) => {\n const moduleNames = module.name.split('/');\n const name = moduleNames[moduleNames.length - 1];\n const moduleConfig = {\n text: `Module:${name}`,\n items: [{ text: 'Overview', link: getModulePath(module.name) }],\n };\n if (!module.children || module.children.length <= 0) {\n return;\n }\n module.children.forEach(sub => {\n const kindString = module.groups\n .find(item => item.children.includes(sub.id))\n ?.title.slice(0, -1);\n if (!kindString) {\n return;\n }\n switch (kindString) {\n case 'Class':\n moduleConfig.items.push({\n text: `Class:${sub.name}`,\n link: getClassPath(module.name, sub.name),\n });\n break;\n case 'Interface':\n moduleConfig.items.push({\n text: `Interface:${sub.name}`,\n link: getInterfacePath(module.name, sub.name),\n });\n break;\n case 'Function':\n moduleConfig.items.push({\n text: `Function:${sub.name}`,\n link: getFunctionPath(module.name, sub.name),\n });\n break;\n default:\n break;\n }\n });\n result.push(moduleConfig);\n });\n await patchLinks(path.dirname(jsonFile));\n return result;\n}\n","export function transformModuleName(name: string) {\n return name.replace(/\\//g, '_').replace(/-/g, '_');\n}\n"]}
|
|
File without changes
|
|
File without changes
|