@rspress/plugin-typedoc 0.0.1
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +303 -0
- package/dist/index.js.map +1 -0
- package/package.json +71 -0
- package/src/.eslintrc.cjs +9 -0
- package/src/constants.ts +2 -0
- package/src/index.ts +87 -0
- package/src/modern-app-env.d.ts +2 -0
- package/src/sidebar.ts +179 -0
- package/src/utils.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @rspress/plugin-typedoc
|
package/dist/index.d.ts
ADDED
|
@@ -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/dist/index.js
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import path2 from "path";
|
|
3
|
+
import { Application, TSConfigReader } from "typedoc";
|
|
4
|
+
import { load } from "typedoc-plugin-markdown";
|
|
5
|
+
|
|
6
|
+
// src/constants.ts
|
|
7
|
+
var API_DIR = "api";
|
|
8
|
+
var ROUTE_PREFIX = `/${API_DIR}`;
|
|
9
|
+
|
|
10
|
+
// src/sidebar.ts
|
|
11
|
+
import path from "path";
|
|
12
|
+
import fs from "@modern-js/utils/fs-extra";
|
|
13
|
+
|
|
14
|
+
// src/utils.ts
|
|
15
|
+
function transformModuleName(name) {
|
|
16
|
+
return name.replace(/\//g, "_").replace(/-/g, "_");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/sidebar.ts
|
|
20
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
21
|
+
try {
|
|
22
|
+
var info = gen[key](arg);
|
|
23
|
+
var value = info.value;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
reject(error);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (info.done) {
|
|
29
|
+
resolve(value);
|
|
30
|
+
} else {
|
|
31
|
+
Promise.resolve(value).then(_next, _throw);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function _async_to_generator(fn) {
|
|
35
|
+
return function() {
|
|
36
|
+
var self = this, args = arguments;
|
|
37
|
+
return new Promise(function(resolve, reject) {
|
|
38
|
+
var gen = fn.apply(self, args);
|
|
39
|
+
function _next(value) {
|
|
40
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
41
|
+
}
|
|
42
|
+
function _throw(err) {
|
|
43
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
44
|
+
}
|
|
45
|
+
_next(void 0);
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function patchLinks(outputDir) {
|
|
50
|
+
return _patchLinks.apply(this, arguments);
|
|
51
|
+
}
|
|
52
|
+
function _patchLinks() {
|
|
53
|
+
_patchLinks = _async_to_generator(function* (outputDir) {
|
|
54
|
+
const normlizeLinksInFile = function() {
|
|
55
|
+
var _ref = _async_to_generator(function* (filePath) {
|
|
56
|
+
const content = yield fs.readFile(filePath, "utf-8");
|
|
57
|
+
const newContent = content.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_match, p1, p2) => {
|
|
58
|
+
return `[${p1}](./${p2})`;
|
|
59
|
+
});
|
|
60
|
+
yield fs.writeFile(filePath, newContent);
|
|
61
|
+
});
|
|
62
|
+
return function normlizeLinksInFile2(filePath) {
|
|
63
|
+
return _ref.apply(this, arguments);
|
|
64
|
+
};
|
|
65
|
+
}();
|
|
66
|
+
const traverse = function() {
|
|
67
|
+
var _ref = _async_to_generator(function* (dir) {
|
|
68
|
+
const files = yield fs.readdir(dir);
|
|
69
|
+
for (const file of files) {
|
|
70
|
+
const filePath = path.join(dir, file);
|
|
71
|
+
const stat = yield fs.stat(filePath);
|
|
72
|
+
if (stat.isDirectory()) {
|
|
73
|
+
yield traverse(filePath);
|
|
74
|
+
} else if (stat.isFile() && /\.mdx?/.test(file)) {
|
|
75
|
+
yield normlizeLinksInFile(filePath);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return function traverse2(dir) {
|
|
80
|
+
return _ref.apply(this, arguments);
|
|
81
|
+
};
|
|
82
|
+
}();
|
|
83
|
+
yield traverse(outputDir);
|
|
84
|
+
});
|
|
85
|
+
return _patchLinks.apply(this, arguments);
|
|
86
|
+
}
|
|
87
|
+
function resolveSidebarForSingleEntry(jsonFile) {
|
|
88
|
+
return _resolveSidebarForSingleEntry.apply(this, arguments);
|
|
89
|
+
}
|
|
90
|
+
function _resolveSidebarForSingleEntry() {
|
|
91
|
+
_resolveSidebarForSingleEntry = _async_to_generator(function* (jsonFile) {
|
|
92
|
+
const result = [];
|
|
93
|
+
const data = JSON.parse(yield fs.readFile(jsonFile, "utf-8"));
|
|
94
|
+
if (!data.children || data.children.length <= 0) {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
const symbolMap = /* @__PURE__ */ new Map();
|
|
98
|
+
data.groups.forEach((group) => {
|
|
99
|
+
const groupItem = {
|
|
100
|
+
text: group.title,
|
|
101
|
+
items: []
|
|
102
|
+
};
|
|
103
|
+
group.children.forEach((id) => {
|
|
104
|
+
const dataItem = data.children.find((item) => item.id === id);
|
|
105
|
+
if (dataItem) {
|
|
106
|
+
let fileName = dataItem.name;
|
|
107
|
+
if (symbolMap.has(dataItem.name)) {
|
|
108
|
+
const count = symbolMap.get(dataItem.name) + 1;
|
|
109
|
+
symbolMap.set(dataItem.name, count);
|
|
110
|
+
fileName = `${dataItem.name}-${count}`;
|
|
111
|
+
} else {
|
|
112
|
+
symbolMap.set(dataItem.name.toLocaleLowerCase(), 0);
|
|
113
|
+
}
|
|
114
|
+
groupItem.items.push({
|
|
115
|
+
text: dataItem.name,
|
|
116
|
+
link: `${ROUTE_PREFIX}/${group.title.toLocaleLowerCase()}/${fileName}`
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
result.push(groupItem);
|
|
121
|
+
});
|
|
122
|
+
yield patchLinks(path.dirname(jsonFile));
|
|
123
|
+
return result;
|
|
124
|
+
});
|
|
125
|
+
return _resolveSidebarForSingleEntry.apply(this, arguments);
|
|
126
|
+
}
|
|
127
|
+
function resolveSidebarForMultiEntry(jsonFile) {
|
|
128
|
+
return _resolveSidebarForMultiEntry.apply(this, arguments);
|
|
129
|
+
}
|
|
130
|
+
function _resolveSidebarForMultiEntry() {
|
|
131
|
+
_resolveSidebarForMultiEntry = _async_to_generator(function* (jsonFile) {
|
|
132
|
+
const result = [];
|
|
133
|
+
const data = JSON.parse(yield fs.readFile(jsonFile, "utf-8"));
|
|
134
|
+
if (!data.children || data.children.length <= 0) {
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
function getModulePath(name) {
|
|
138
|
+
return path.join(`${ROUTE_PREFIX}/modules`, `${transformModuleName(name)}`).replace(/\\/g, "/");
|
|
139
|
+
}
|
|
140
|
+
function getClassPath(moduleName, className) {
|
|
141
|
+
return path.join(`${ROUTE_PREFIX}/classes`, `${transformModuleName(moduleName)}.${className}`).replace(/\\/g, "/");
|
|
142
|
+
}
|
|
143
|
+
function getInterfacePath(moduleName, interfaceName) {
|
|
144
|
+
return path.join(`${ROUTE_PREFIX}/interfaces`, `${transformModuleName(moduleName)}.${interfaceName}`).replace(/\\/g, "/");
|
|
145
|
+
}
|
|
146
|
+
function getFunctionPath(moduleName, functionName) {
|
|
147
|
+
return path.join(`${ROUTE_PREFIX}/functions`, `${transformModuleName(moduleName)}.${functionName}`).replace(/\\/g, "/");
|
|
148
|
+
}
|
|
149
|
+
data.children.forEach((module) => {
|
|
150
|
+
const moduleNames = module.name.split("/");
|
|
151
|
+
const name = moduleNames[moduleNames.length - 1];
|
|
152
|
+
const moduleConfig = {
|
|
153
|
+
text: `Module:${name}`,
|
|
154
|
+
items: [
|
|
155
|
+
{
|
|
156
|
+
text: "Overview",
|
|
157
|
+
link: getModulePath(module.name)
|
|
158
|
+
}
|
|
159
|
+
]
|
|
160
|
+
};
|
|
161
|
+
if (!module.children || module.children.length <= 0) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
module.children.forEach((sub) => {
|
|
165
|
+
var _module_groups_find;
|
|
166
|
+
const kindString = (_module_groups_find = module.groups.find((item) => item.children.includes(sub.id))) === null || _module_groups_find === void 0 ? void 0 : _module_groups_find.title.slice(0, -1);
|
|
167
|
+
if (!kindString) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
switch (kindString) {
|
|
171
|
+
case "Class":
|
|
172
|
+
moduleConfig.items.push({
|
|
173
|
+
text: `Class:${sub.name}`,
|
|
174
|
+
link: getClassPath(module.name, sub.name)
|
|
175
|
+
});
|
|
176
|
+
break;
|
|
177
|
+
case "Interface":
|
|
178
|
+
moduleConfig.items.push({
|
|
179
|
+
text: `Interface:${sub.name}`,
|
|
180
|
+
link: getInterfacePath(module.name, sub.name)
|
|
181
|
+
});
|
|
182
|
+
break;
|
|
183
|
+
case "Function":
|
|
184
|
+
moduleConfig.items.push({
|
|
185
|
+
text: `Function:${sub.name}`,
|
|
186
|
+
link: getFunctionPath(module.name, sub.name)
|
|
187
|
+
});
|
|
188
|
+
break;
|
|
189
|
+
default:
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
result.push(moduleConfig);
|
|
194
|
+
});
|
|
195
|
+
yield patchLinks(path.dirname(jsonFile));
|
|
196
|
+
return result;
|
|
197
|
+
});
|
|
198
|
+
return _resolveSidebarForMultiEntry.apply(this, arguments);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/index.ts
|
|
202
|
+
function asyncGeneratorStep2(gen, resolve, reject, _next, _throw, key, arg) {
|
|
203
|
+
try {
|
|
204
|
+
var info = gen[key](arg);
|
|
205
|
+
var value = info.value;
|
|
206
|
+
} catch (error) {
|
|
207
|
+
reject(error);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (info.done) {
|
|
211
|
+
resolve(value);
|
|
212
|
+
} else {
|
|
213
|
+
Promise.resolve(value).then(_next, _throw);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function _async_to_generator2(fn) {
|
|
217
|
+
return function() {
|
|
218
|
+
var self = this, args = arguments;
|
|
219
|
+
return new Promise(function(resolve, reject) {
|
|
220
|
+
var gen = fn.apply(self, args);
|
|
221
|
+
function _next(value) {
|
|
222
|
+
asyncGeneratorStep2(gen, resolve, reject, _next, _throw, "next", value);
|
|
223
|
+
}
|
|
224
|
+
function _throw(err) {
|
|
225
|
+
asyncGeneratorStep2(gen, resolve, reject, _next, _throw, "throw", err);
|
|
226
|
+
}
|
|
227
|
+
_next(void 0);
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function pluginTypeDoc(options) {
|
|
232
|
+
let docRoot;
|
|
233
|
+
const { entryPoints = [], outDir = API_DIR } = options;
|
|
234
|
+
return {
|
|
235
|
+
name: "doc-plugin-typedoc",
|
|
236
|
+
addPages() {
|
|
237
|
+
return _async_to_generator2(function* () {
|
|
238
|
+
return [
|
|
239
|
+
{
|
|
240
|
+
routePath: `${outDir.replace(/\/$/, "")}/`,
|
|
241
|
+
filepath: path2.join(docRoot, outDir, "README.md")
|
|
242
|
+
}
|
|
243
|
+
];
|
|
244
|
+
})();
|
|
245
|
+
},
|
|
246
|
+
config(config) {
|
|
247
|
+
return _async_to_generator2(function* () {
|
|
248
|
+
const app = new Application();
|
|
249
|
+
docRoot = config.root;
|
|
250
|
+
app.options.addReader(new TSConfigReader());
|
|
251
|
+
load(app);
|
|
252
|
+
app.bootstrap({
|
|
253
|
+
name: config.title,
|
|
254
|
+
entryPoints,
|
|
255
|
+
theme: "markdown",
|
|
256
|
+
disableSources: true,
|
|
257
|
+
readme: "none",
|
|
258
|
+
githubPages: false,
|
|
259
|
+
requiredToBeDocumented: [
|
|
260
|
+
"Class",
|
|
261
|
+
"Function",
|
|
262
|
+
"Interface"
|
|
263
|
+
],
|
|
264
|
+
plugin: [
|
|
265
|
+
"typedoc-plugin-markdown"
|
|
266
|
+
],
|
|
267
|
+
// @ts-expect-error MarkdownTheme has no export
|
|
268
|
+
hideBreadcrumbs: true,
|
|
269
|
+
hideMembersSymbol: true,
|
|
270
|
+
allReflectionsHaveOwnDocument: true
|
|
271
|
+
});
|
|
272
|
+
const project = app.convert();
|
|
273
|
+
if (project) {
|
|
274
|
+
const absoluteOutputdir = path2.join(docRoot, outDir);
|
|
275
|
+
yield app.generateDocs(project, absoluteOutputdir);
|
|
276
|
+
const jsonDir = path2.join(absoluteOutputdir, "documentation.json");
|
|
277
|
+
yield app.generateJson(project, jsonDir);
|
|
278
|
+
config.themeConfig = config.themeConfig || {};
|
|
279
|
+
config.themeConfig.nav = config.themeConfig.nav || [];
|
|
280
|
+
const apiIndexLink = `/${outDir.replace(/(^\/)|(\/$)/, "")}/`;
|
|
281
|
+
config.themeConfig.nav.push({
|
|
282
|
+
text: "API",
|
|
283
|
+
link: apiIndexLink
|
|
284
|
+
});
|
|
285
|
+
config.themeConfig.sidebar = config.themeConfig.sidebar || {};
|
|
286
|
+
config.themeConfig.sidebar[apiIndexLink] = entryPoints.length > 1 ? yield resolveSidebarForMultiEntry(jsonDir) : yield resolveSidebarForSingleEntry(jsonDir);
|
|
287
|
+
config.themeConfig.sidebar[apiIndexLink].unshift({
|
|
288
|
+
text: "Overview",
|
|
289
|
+
link: `${apiIndexLink}README`
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
config.route = config.route || {};
|
|
293
|
+
config.route.exclude = config.route.exclude || [];
|
|
294
|
+
return config;
|
|
295
|
+
})();
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
export {
|
|
300
|
+
pluginTypeDoc
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
//# sourceMappingUR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Q,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: 'doc-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"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rspress/plugin-typedoc",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A plugin for rspress to integrate typedoc",
|
|
5
|
+
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/web-infra-dev/rspress",
|
|
9
|
+
"directory": "packages/plugin-typedoc"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"jsnext:source": "./src/index.ts",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/node/index.js",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=14.17.6"
|
|
18
|
+
},
|
|
19
|
+
"eslintIgnore": [
|
|
20
|
+
"node_modules/",
|
|
21
|
+
"dist/"
|
|
22
|
+
],
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@modern-js/tsconfig": "2.30.0",
|
|
25
|
+
"@types/node": "^18.11.17",
|
|
26
|
+
"@types/react": "^18",
|
|
27
|
+
"@types/react-dom": "^18",
|
|
28
|
+
"typescript": "^5",
|
|
29
|
+
"vitest": "0.33.0",
|
|
30
|
+
"react": "^17",
|
|
31
|
+
"@rspress/shared": "0.0.1"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"rspress": "*"
|
|
35
|
+
},
|
|
36
|
+
"sideEffects": [
|
|
37
|
+
"*.css",
|
|
38
|
+
"*.less",
|
|
39
|
+
"*.sass",
|
|
40
|
+
"*.scss"
|
|
41
|
+
],
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"src"
|
|
45
|
+
],
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"provenance": true,
|
|
49
|
+
"registry": "https://registry.npmjs.org/"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@modern-js/utils": "2.30.0",
|
|
53
|
+
"typedoc": "0.24.8",
|
|
54
|
+
"typedoc-plugin-markdown": "3.15.3"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"dev": "modern dev",
|
|
58
|
+
"build": "modern build",
|
|
59
|
+
"reset": "rimraf ./**/node_modules",
|
|
60
|
+
"lint": "modern lint",
|
|
61
|
+
"change": "modern change",
|
|
62
|
+
"bump": "modern bump",
|
|
63
|
+
"pre": "modern pre",
|
|
64
|
+
"change-status": "modern change-status",
|
|
65
|
+
"gen-release-note": "modern gen-release-note",
|
|
66
|
+
"release": "modern release",
|
|
67
|
+
"new": "modern new",
|
|
68
|
+
"test": "vitest run --passWithNoTests",
|
|
69
|
+
"upgrade": "modern upgrade"
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/constants.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { Application, TSConfigReader } from 'typedoc';
|
|
3
|
+
import type { RspressPlugin } from '@rspress/shared';
|
|
4
|
+
import { load } from 'typedoc-plugin-markdown';
|
|
5
|
+
import { API_DIR } from './constants';
|
|
6
|
+
import {
|
|
7
|
+
resolveSidebarForMultiEntry,
|
|
8
|
+
resolveSidebarForSingleEntry,
|
|
9
|
+
} from './sidebar';
|
|
10
|
+
|
|
11
|
+
export interface PluginTypeDocOptions {
|
|
12
|
+
/**
|
|
13
|
+
* The entry points of modules.
|
|
14
|
+
* @default []
|
|
15
|
+
*/
|
|
16
|
+
entryPoints: string[];
|
|
17
|
+
/**
|
|
18
|
+
* The output directory.
|
|
19
|
+
* @default 'api'
|
|
20
|
+
*/
|
|
21
|
+
outDir?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function pluginTypeDoc(options: PluginTypeDocOptions): RspressPlugin {
|
|
25
|
+
let docRoot: string | undefined;
|
|
26
|
+
const { entryPoints = [], outDir = API_DIR } = options;
|
|
27
|
+
return {
|
|
28
|
+
name: 'doc-plugin-typedoc',
|
|
29
|
+
async addPages() {
|
|
30
|
+
return [
|
|
31
|
+
{
|
|
32
|
+
routePath: `${outDir.replace(/\/$/, '')}/`,
|
|
33
|
+
filepath: path.join(docRoot!, outDir, 'README.md'),
|
|
34
|
+
},
|
|
35
|
+
];
|
|
36
|
+
},
|
|
37
|
+
async config(config) {
|
|
38
|
+
const app = new Application();
|
|
39
|
+
docRoot = config.root;
|
|
40
|
+
app.options.addReader(new TSConfigReader());
|
|
41
|
+
load(app);
|
|
42
|
+
app.bootstrap({
|
|
43
|
+
name: config.title,
|
|
44
|
+
entryPoints,
|
|
45
|
+
theme: 'markdown',
|
|
46
|
+
disableSources: true,
|
|
47
|
+
readme: 'none',
|
|
48
|
+
githubPages: false,
|
|
49
|
+
requiredToBeDocumented: ['Class', 'Function', 'Interface'],
|
|
50
|
+
plugin: ['typedoc-plugin-markdown'],
|
|
51
|
+
// @ts-expect-error MarkdownTheme has no export
|
|
52
|
+
hideBreadcrumbs: true,
|
|
53
|
+
hideMembersSymbol: true,
|
|
54
|
+
allReflectionsHaveOwnDocument: true,
|
|
55
|
+
});
|
|
56
|
+
const project = app.convert();
|
|
57
|
+
|
|
58
|
+
if (project) {
|
|
59
|
+
// 1. Generate module doc by typedoc
|
|
60
|
+
const absoluteOutputdir = path.join(docRoot!, outDir);
|
|
61
|
+
await app.generateDocs(project, absoluteOutputdir);
|
|
62
|
+
const jsonDir = path.join(absoluteOutputdir, 'documentation.json');
|
|
63
|
+
await app.generateJson(project, jsonDir);
|
|
64
|
+
// 2. Generate sidebar
|
|
65
|
+
config.themeConfig = config.themeConfig || {};
|
|
66
|
+
config.themeConfig.nav = config.themeConfig.nav || [];
|
|
67
|
+
const apiIndexLink = `/${outDir.replace(/(^\/)|(\/$)/, '')}/`;
|
|
68
|
+
config.themeConfig.nav.push({
|
|
69
|
+
text: 'API',
|
|
70
|
+
link: apiIndexLink,
|
|
71
|
+
});
|
|
72
|
+
config.themeConfig.sidebar = config.themeConfig.sidebar || {};
|
|
73
|
+
config.themeConfig.sidebar[apiIndexLink] =
|
|
74
|
+
entryPoints.length > 1
|
|
75
|
+
? await resolveSidebarForMultiEntry(jsonDir)
|
|
76
|
+
: await resolveSidebarForSingleEntry(jsonDir);
|
|
77
|
+
config.themeConfig.sidebar[apiIndexLink].unshift({
|
|
78
|
+
text: 'Overview',
|
|
79
|
+
link: `${apiIndexLink}README`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
config.route = config.route || {};
|
|
83
|
+
config.route.exclude = config.route.exclude || [];
|
|
84
|
+
return config;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
package/src/sidebar.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from '@modern-js/utils/fs-extra';
|
|
3
|
+
import type { SidebarGroup } from '@rspress/shared';
|
|
4
|
+
import { transformModuleName } from './utils';
|
|
5
|
+
import { ROUTE_PREFIX } from './constants';
|
|
6
|
+
|
|
7
|
+
interface ModuleItem {
|
|
8
|
+
id: number;
|
|
9
|
+
name: string;
|
|
10
|
+
children: {
|
|
11
|
+
id: number;
|
|
12
|
+
name: string;
|
|
13
|
+
}[];
|
|
14
|
+
groups: {
|
|
15
|
+
title: string;
|
|
16
|
+
children: number[];
|
|
17
|
+
}[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function patchLinks(outputDir: string) {
|
|
21
|
+
// Patch links in markdown files
|
|
22
|
+
// Scan all the markdown files in the output directory
|
|
23
|
+
// replace [xxx](yyy) -> [xxx](./yyy)
|
|
24
|
+
const normlizeLinksInFile = async (filePath: string) => {
|
|
25
|
+
const content = await fs.readFile(filePath, 'utf-8');
|
|
26
|
+
// replace: [xxx](yyy) -> [xxx](./yyy)
|
|
27
|
+
const newContent = content.replace(
|
|
28
|
+
/\[([^\]]+)\]\(([^)]+)\)/g,
|
|
29
|
+
(_match, p1, p2) => {
|
|
30
|
+
return `[${p1}](./${p2})`;
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
await fs.writeFile(filePath, newContent);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const traverse = async (dir: string) => {
|
|
37
|
+
const files = await fs.readdir(dir);
|
|
38
|
+
for (const file of files) {
|
|
39
|
+
const filePath = path.join(dir, file);
|
|
40
|
+
const stat = await fs.stat(filePath);
|
|
41
|
+
if (stat.isDirectory()) {
|
|
42
|
+
await traverse(filePath);
|
|
43
|
+
} else if (stat.isFile() && /\.mdx?/.test(file)) {
|
|
44
|
+
await normlizeLinksInFile(filePath);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
await traverse(outputDir);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function resolveSidebarForSingleEntry(
|
|
52
|
+
jsonFile: string,
|
|
53
|
+
): Promise<SidebarGroup[]> {
|
|
54
|
+
const result: SidebarGroup[] = [];
|
|
55
|
+
const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));
|
|
56
|
+
if (!data.children || data.children.length <= 0) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
const symbolMap = new Map<string, number>();
|
|
60
|
+
data.groups.forEach((group: { title: string; children: number[] }) => {
|
|
61
|
+
const groupItem: SidebarGroup = {
|
|
62
|
+
text: group.title,
|
|
63
|
+
items: [],
|
|
64
|
+
};
|
|
65
|
+
group.children.forEach((id: number) => {
|
|
66
|
+
const dataItem = data.children.find((item: ModuleItem) => item.id === id);
|
|
67
|
+
if (dataItem) {
|
|
68
|
+
// Note: we should handle the case that classes and interfaces have the same name
|
|
69
|
+
// Such as class `Env` and variable `env`
|
|
70
|
+
// The final output file name should be `classes/Env.md` and `variables/env-1.md`
|
|
71
|
+
let fileName = dataItem.name;
|
|
72
|
+
if (symbolMap.has(dataItem.name)) {
|
|
73
|
+
const count = symbolMap.get(dataItem.name)! + 1;
|
|
74
|
+
symbolMap.set(dataItem.name, count);
|
|
75
|
+
fileName = `${dataItem.name}-${count}`;
|
|
76
|
+
} else {
|
|
77
|
+
symbolMap.set(dataItem.name.toLocaleLowerCase(), 0);
|
|
78
|
+
}
|
|
79
|
+
groupItem.items.push({
|
|
80
|
+
text: dataItem.name,
|
|
81
|
+
link: `${ROUTE_PREFIX}/${group.title.toLocaleLowerCase()}/${fileName}`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
result.push(groupItem);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
await patchLinks(path.dirname(jsonFile));
|
|
89
|
+
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function resolveSidebarForMultiEntry(
|
|
94
|
+
jsonFile: string,
|
|
95
|
+
): Promise<SidebarGroup[]> {
|
|
96
|
+
const result: SidebarGroup[] = [];
|
|
97
|
+
const data = JSON.parse(await fs.readFile(jsonFile, 'utf-8'));
|
|
98
|
+
if (!data.children || data.children.length <= 0) {
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function getModulePath(name: string) {
|
|
103
|
+
return path
|
|
104
|
+
.join(`${ROUTE_PREFIX}/modules`, `${transformModuleName(name)}`)
|
|
105
|
+
.replace(/\\/g, '/');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function getClassPath(moduleName: string, className: string) {
|
|
109
|
+
return path
|
|
110
|
+
.join(
|
|
111
|
+
`${ROUTE_PREFIX}/classes`,
|
|
112
|
+
`${transformModuleName(moduleName)}.${className}`,
|
|
113
|
+
)
|
|
114
|
+
.replace(/\\/g, '/');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function getInterfacePath(moduleName: string, interfaceName: string) {
|
|
118
|
+
return path
|
|
119
|
+
.join(
|
|
120
|
+
`${ROUTE_PREFIX}/interfaces`,
|
|
121
|
+
`${transformModuleName(moduleName)}.${interfaceName}`,
|
|
122
|
+
)
|
|
123
|
+
.replace(/\\/g, '/');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function getFunctionPath(moduleName: string, functionName: string) {
|
|
127
|
+
return path
|
|
128
|
+
.join(
|
|
129
|
+
`${ROUTE_PREFIX}/functions`,
|
|
130
|
+
`${transformModuleName(moduleName)}.${functionName}`,
|
|
131
|
+
)
|
|
132
|
+
.replace(/\\/g, '/');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
data.children.forEach((module: ModuleItem) => {
|
|
136
|
+
const moduleNames = module.name.split('/');
|
|
137
|
+
const name = moduleNames[moduleNames.length - 1];
|
|
138
|
+
const moduleConfig = {
|
|
139
|
+
text: `Module:${name}`,
|
|
140
|
+
items: [{ text: 'Overview', link: getModulePath(module.name) }],
|
|
141
|
+
};
|
|
142
|
+
if (!module.children || module.children.length <= 0) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
module.children.forEach(sub => {
|
|
146
|
+
const kindString = module.groups
|
|
147
|
+
.find(item => item.children.includes(sub.id))
|
|
148
|
+
?.title.slice(0, -1);
|
|
149
|
+
if (!kindString) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
switch (kindString) {
|
|
153
|
+
case 'Class':
|
|
154
|
+
moduleConfig.items.push({
|
|
155
|
+
text: `Class:${sub.name}`,
|
|
156
|
+
link: getClassPath(module.name, sub.name),
|
|
157
|
+
});
|
|
158
|
+
break;
|
|
159
|
+
case 'Interface':
|
|
160
|
+
moduleConfig.items.push({
|
|
161
|
+
text: `Interface:${sub.name}`,
|
|
162
|
+
link: getInterfacePath(module.name, sub.name),
|
|
163
|
+
});
|
|
164
|
+
break;
|
|
165
|
+
case 'Function':
|
|
166
|
+
moduleConfig.items.push({
|
|
167
|
+
text: `Function:${sub.name}`,
|
|
168
|
+
link: getFunctionPath(module.name, sub.name),
|
|
169
|
+
});
|
|
170
|
+
break;
|
|
171
|
+
default:
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
result.push(moduleConfig);
|
|
176
|
+
});
|
|
177
|
+
await patchLinks(path.dirname(jsonFile));
|
|
178
|
+
return result;
|
|
179
|
+
}
|
package/src/utils.ts
ADDED