@rspress/plugin-api-docgen 1.37.2 → 1.37.3
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/index.d.ts +68 -63
- package/dist/index.js +308 -321
- package/package.json +7 -5
- package/dist/index.js.map +0 -1
package/dist/index.d.ts
CHANGED
@@ -1,63 +1,68 @@
|
|
1
|
-
import {
|
2
|
-
import { ParserOptions } from 'react-docgen-typescript';
|
3
|
-
import {
|
4
|
-
|
5
|
-
type
|
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
|
-
* @zh
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
1
|
+
import type { CompilerOptions } from 'typescript';
|
2
|
+
import type { ParserOptions } from 'react-docgen-typescript';
|
3
|
+
import type { RspressPlugin } from '@rspress/shared';
|
4
|
+
|
5
|
+
declare type ApiParseTool = 'documentation' | 'react-docgen-typescript';
|
6
|
+
|
7
|
+
declare type DocumentationArgs = {
|
8
|
+
external?: Array<string>;
|
9
|
+
shallow?: boolean;
|
10
|
+
order?: Array<unknown>;
|
11
|
+
access?: Array<string>;
|
12
|
+
hljs?: {
|
13
|
+
highlightAuto?: boolean;
|
14
|
+
languages?: string;
|
15
|
+
};
|
16
|
+
inferPrivate?: string;
|
17
|
+
extension?: string | Array<string>;
|
18
|
+
noReferenceLinks?: boolean;
|
19
|
+
};
|
20
|
+
|
21
|
+
declare type Entries = Record<string, string>;
|
22
|
+
|
23
|
+
declare type ParseToolOptions = {
|
24
|
+
'react-docgen-typescript'?: ParserOptions & {
|
25
|
+
tsconfigPath?: Record<string, string>;
|
26
|
+
compilerOptions?: Record<string, CompilerOptions>;
|
27
|
+
};
|
28
|
+
documentation?: DocumentationArgs;
|
29
|
+
};
|
30
|
+
|
31
|
+
/**
|
32
|
+
* The plugin is used to generate api doc for files.
|
33
|
+
*/
|
34
|
+
export declare function pluginApiDocgen(options?: PluginOptions): RspressPlugin;
|
35
|
+
|
36
|
+
export declare type PluginOptions = {
|
37
|
+
/**
|
38
|
+
* Module entries
|
39
|
+
* @zh 传入自动生成文档的模块名称及相对路径
|
40
|
+
*/
|
41
|
+
entries?: Entries | ToolEntries;
|
42
|
+
/**
|
43
|
+
* apiParseTool
|
44
|
+
* @experimental
|
45
|
+
* @zh 解析工具
|
46
|
+
* @default 'react-docgen-typescript'
|
47
|
+
*/
|
48
|
+
apiParseTool?: ApiParseTool;
|
49
|
+
/**
|
50
|
+
* parserToolOptions
|
51
|
+
* @experimental
|
52
|
+
* @zh 解析器参数
|
53
|
+
*/
|
54
|
+
parseToolOptions?: ParseToolOptions;
|
55
|
+
/**
|
56
|
+
* appDirectory
|
57
|
+
* @zh 项目根目录
|
58
|
+
* @default process.cwd()
|
59
|
+
*/
|
60
|
+
appDir?: string;
|
61
|
+
};
|
62
|
+
|
63
|
+
declare type ToolEntries = {
|
64
|
+
documentation: Entries;
|
65
|
+
'react-docgen-typescript': Entries;
|
66
|
+
};
|
67
|
+
|
68
|
+
export { }
|
package/dist/index.js
CHANGED
@@ -1,335 +1,322 @@
|
|
1
1
|
"use strict";
|
2
|
-
var
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
-
var __export = (target, all) => {
|
9
|
-
for (var name in all)
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
-
};
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
-
for (let key of __getOwnPropNames(from))
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
-
}
|
18
|
-
return to;
|
2
|
+
var __webpack_modules__ = {
|
3
|
+
documentation: function(module) {
|
4
|
+
module.exports = import("documentation");
|
5
|
+
}
|
19
6
|
};
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
)
|
28
|
-
var
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
7
|
+
/************************************************************************/ // The module cache
|
8
|
+
var __webpack_module_cache__ = {};
|
9
|
+
// The require function
|
10
|
+
function __webpack_require__(moduleId) {
|
11
|
+
// Check if module is in cache
|
12
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
13
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
14
|
+
// Create a new module (and put it into the cache)
|
15
|
+
var module = __webpack_module_cache__[moduleId] = {
|
16
|
+
exports: {}
|
17
|
+
};
|
18
|
+
// Execute the module function
|
19
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
20
|
+
// Return the exports of the module
|
21
|
+
return module.exports;
|
22
|
+
}
|
23
|
+
/************************************************************************/ // webpack/runtime/compat_get_default_export
|
24
|
+
(()=>{
|
25
|
+
// getDefaultExport function for compatibility with non-ESM modules
|
26
|
+
__webpack_require__.n = function(module) {
|
27
|
+
var getter = module && module.__esModule ? function() {
|
28
|
+
return module['default'];
|
29
|
+
} : function() {
|
30
|
+
return module;
|
31
|
+
};
|
32
|
+
__webpack_require__.d(getter, {
|
33
|
+
a: getter
|
34
|
+
});
|
35
|
+
return getter;
|
36
|
+
};
|
37
|
+
})();
|
38
|
+
// webpack/runtime/define_property_getters
|
39
|
+
(()=>{
|
40
|
+
__webpack_require__.d = function(exports1, definition) {
|
41
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
42
|
+
enumerable: true,
|
43
|
+
get: definition[key]
|
44
|
+
});
|
45
|
+
};
|
46
|
+
})();
|
47
|
+
// webpack/runtime/has_own_property
|
48
|
+
(()=>{
|
49
|
+
__webpack_require__.o = function(obj, prop) {
|
50
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
51
|
+
};
|
52
|
+
})();
|
53
|
+
// webpack/runtime/make_namespace_object
|
54
|
+
(()=>{
|
55
|
+
// define __esModule on exports
|
56
|
+
__webpack_require__.r = function(exports1) {
|
57
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
58
|
+
value: 'Module'
|
59
|
+
});
|
60
|
+
Object.defineProperty(exports1, '__esModule', {
|
61
|
+
value: true
|
62
|
+
});
|
63
|
+
};
|
64
|
+
})();
|
65
|
+
/************************************************************************/ var __webpack_exports__ = {};
|
66
|
+
// ESM COMPAT FLAG
|
67
|
+
__webpack_require__.r(__webpack_exports__);
|
68
|
+
// EXPORTS
|
69
|
+
__webpack_require__.d(__webpack_exports__, {
|
70
|
+
pluginApiDocgen: ()=>/* binding */ pluginApiDocgen
|
34
71
|
});
|
35
|
-
|
36
|
-
var
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
var
|
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
|
-
disabled: "Whether to disable ",
|
79
|
-
required: "Required",
|
80
|
-
property: "Property",
|
81
|
-
description: "Description",
|
82
|
-
type: "Type",
|
83
|
-
defaultValue: "Default Value",
|
84
|
-
overview: "Overview"
|
85
|
-
}
|
86
|
-
};
|
87
|
-
|
88
|
-
// src/docgen.ts
|
89
|
-
var isToolEntries = (obj) => {
|
90
|
-
return obj.documentation || obj["react-docgen-typescript"];
|
91
|
-
};
|
92
|
-
var docgen = async ({
|
93
|
-
entries,
|
94
|
-
languages,
|
95
|
-
apiParseTool,
|
96
|
-
appDir,
|
97
|
-
parseToolOptions,
|
98
|
-
isProd
|
99
|
-
}) => {
|
100
|
-
const watchFileMap = {};
|
101
|
-
const genApiDoc = async (entry, tool) => {
|
102
|
-
if (Object.keys(entry).length === 0) {
|
103
|
-
return;
|
72
|
+
const external_node_path_namespaceObject = require("node:path");
|
73
|
+
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
|
74
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
75
|
+
var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
|
76
|
+
const logger_namespaceObject = require("@rspress/shared/logger");
|
77
|
+
const external_chokidar_namespaceObject = require("chokidar");
|
78
|
+
var external_chokidar_default = /*#__PURE__*/ __webpack_require__.n(external_chokidar_namespaceObject);
|
79
|
+
const external_react_docgen_typescript_namespaceObject = require("react-docgen-typescript");
|
80
|
+
const shared_namespaceObject = require("@rspress/shared");
|
81
|
+
external_node_path_default().join(__dirname, '..');
|
82
|
+
const apiDocMap = {};
|
83
|
+
const locales = {
|
84
|
+
zh: {
|
85
|
+
copy: '复制',
|
86
|
+
copied: '复制成功',
|
87
|
+
expand: '展开代码',
|
88
|
+
collapse: '收起代码',
|
89
|
+
className: '节点类名',
|
90
|
+
style: '节点样式',
|
91
|
+
children: '子节点',
|
92
|
+
disabled: '是否禁用',
|
93
|
+
required: '必填',
|
94
|
+
property: '属性',
|
95
|
+
description: '说明',
|
96
|
+
type: '类型',
|
97
|
+
defaultValue: '默认值',
|
98
|
+
overview: '概览'
|
99
|
+
},
|
100
|
+
en: {
|
101
|
+
copy: 'Copy',
|
102
|
+
copied: 'Copied Success!',
|
103
|
+
expand: 'Expand Code',
|
104
|
+
collapse: 'Collapse Code',
|
105
|
+
className: 'Additional css class',
|
106
|
+
style: 'Additional style',
|
107
|
+
children: 'Children',
|
108
|
+
disabled: 'Whether to disable ',
|
109
|
+
required: 'Required',
|
110
|
+
property: 'Property',
|
111
|
+
description: 'Description',
|
112
|
+
type: 'Type',
|
113
|
+
defaultValue: 'Default Value',
|
114
|
+
overview: 'Overview'
|
104
115
|
}
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
[moduleSourceFilePath],
|
117
|
-
{
|
118
|
-
...parseToolOptions.documentation
|
119
|
-
}
|
120
|
-
);
|
121
|
-
const apiDoc = await documentation.formats.md(documentationRes, {
|
122
|
-
noReferenceLinks: parseToolOptions.documentation?.noReferenceLinks ?? true
|
123
|
-
});
|
124
|
-
apiDocMap[key] = apiDoc;
|
125
|
-
} else {
|
126
|
-
const { tsconfigPath, compilerOptions, ...restOptions } = parseToolOptions?.["react-docgen-typescript"] ?? {};
|
127
|
-
const parserOpts = {
|
128
|
-
// https://github.com/styleguidist/react-docgen-typescript/blob/master/README.md?plain=1#L111
|
129
|
-
propFilter: (prop) => {
|
130
|
-
if (prop.declarations !== void 0 && prop.declarations.length > 0) {
|
131
|
-
const hasPropAdditionalDescription = prop.declarations.find(
|
132
|
-
(declaration) => {
|
133
|
-
return !declaration.fileName.includes("node_modules");
|
134
|
-
}
|
135
|
-
);
|
136
|
-
return Boolean(hasPropAdditionalDescription);
|
137
|
-
}
|
138
|
-
return true;
|
139
|
-
},
|
140
|
-
...restOptions
|
116
|
+
};
|
117
|
+
const isToolEntries = (obj)=>obj.documentation || obj['react-docgen-typescript'];
|
118
|
+
const docgen = async ({ entries, languages, apiParseTool, appDir, parseToolOptions, isProd })=>{
|
119
|
+
const watchFileMap = {};
|
120
|
+
const genApiDoc = async (entry, tool)=>{
|
121
|
+
if (0 === Object.keys(entry).length) return;
|
122
|
+
await Promise.all(Object.entries(entry).map(async ([key, value])=>{
|
123
|
+
const moduleSourceFilePath = external_node_path_default().resolve(appDir, value);
|
124
|
+
watchFileMap[moduleSourceFilePath] = {
|
125
|
+
apiParseTool,
|
126
|
+
moduleName: key
|
141
127
|
};
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
128
|
+
try {
|
129
|
+
if ('documentation' === tool) {
|
130
|
+
var _parseToolOptions_documentation;
|
131
|
+
const documentation = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "documentation"));
|
132
|
+
const documentationRes = await documentation.build([
|
133
|
+
moduleSourceFilePath
|
134
|
+
], {
|
135
|
+
...parseToolOptions.documentation
|
136
|
+
});
|
137
|
+
const apiDoc = await documentation.formats.md(documentationRes, {
|
138
|
+
noReferenceLinks: (null === (_parseToolOptions_documentation = parseToolOptions.documentation) || void 0 === _parseToolOptions_documentation ? void 0 : _parseToolOptions_documentation.noReferenceLinks) ?? true
|
139
|
+
});
|
140
|
+
apiDocMap[key] = apiDoc;
|
141
|
+
} else {
|
142
|
+
const { tsconfigPath, compilerOptions, ...restOptions } = (null == parseToolOptions ? void 0 : parseToolOptions['react-docgen-typescript']) ?? {};
|
143
|
+
const parserOpts = {
|
144
|
+
// https://github.com/styleguidist/react-docgen-typescript/blob/master/README.md?plain=1#L111
|
145
|
+
propFilter: (prop)=>{
|
146
|
+
if (void 0 !== prop.declarations && prop.declarations.length > 0) {
|
147
|
+
const hasPropAdditionalDescription = prop.declarations.find((declaration)=>!declaration.fileName.includes('node_modules'));
|
148
|
+
return Boolean(hasPropAdditionalDescription);
|
149
|
+
}
|
150
|
+
return true;
|
151
|
+
},
|
152
|
+
...restOptions
|
153
|
+
};
|
154
|
+
let fileParser = (0, external_react_docgen_typescript_namespaceObject.withDefaultConfig)(parserOpts);
|
155
|
+
if (null == tsconfigPath ? void 0 : tsconfigPath[key]) fileParser = (0, external_react_docgen_typescript_namespaceObject.withCustomConfig)(tsconfigPath[key], parserOpts);
|
156
|
+
else if (null == compilerOptions ? void 0 : compilerOptions[key]) fileParser = (0, external_react_docgen_typescript_namespaceObject.withCompilerOptions)(compilerOptions[key], parserOpts);
|
157
|
+
const componentDoc = fileParser.parse(moduleSourceFilePath);
|
158
|
+
if (0 === componentDoc.length) logger_namespaceObject.logger.warn('[module-doc-plugin]', `Unable to parse API document in ${moduleSourceFilePath}`);
|
159
|
+
if (languages.length > 0) languages.forEach((language)=>{
|
160
|
+
apiDocMap[`${key}-${language}`] = generateTable(componentDoc, language);
|
161
|
+
});
|
162
|
+
else apiDocMap[key] = generateTable(componentDoc, 'en');
|
163
|
+
}
|
164
|
+
} catch (e) {
|
165
|
+
if (e instanceof Error) logger_namespaceObject.logger.error('[module-doc-plugin]', `Generate API table error: ${e.message}`);
|
157
166
|
}
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
+
}));
|
168
|
+
};
|
169
|
+
logger_namespaceObject.logger.info('[module-doc-plugin]', 'Start to generate API table...');
|
170
|
+
if (isToolEntries(entries)) {
|
171
|
+
const reactEntries = entries['react-docgen-typescript'];
|
172
|
+
const documentationEntries = entries.documentation;
|
173
|
+
await Promise.all([
|
174
|
+
genApiDoc(reactEntries, 'react-docgen-typescript'),
|
175
|
+
genApiDoc(documentationEntries, 'documentation')
|
176
|
+
]);
|
177
|
+
} else await genApiDoc(entries, apiParseTool);
|
178
|
+
if (!isProd) {
|
179
|
+
const watcher = external_chokidar_default().watch(Object.keys(watchFileMap), {
|
180
|
+
ignoreInitial: true,
|
181
|
+
ignorePermissionErrors: true,
|
182
|
+
ignored: [
|
183
|
+
/node_modules/
|
184
|
+
]
|
185
|
+
});
|
186
|
+
let isUpdate = false;
|
187
|
+
watcher.on('change', (changed)=>{
|
188
|
+
if (isUpdate) return;
|
189
|
+
isUpdate = true;
|
190
|
+
logger_namespaceObject.logger.info('[module-doc-plugin]', 'updating API');
|
191
|
+
const watchFileInfo = watchFileMap[changed];
|
192
|
+
if (watchFileInfo) {
|
193
|
+
const { apiParseTool, moduleName } = watchFileInfo;
|
194
|
+
const updateSiteData = ()=>{
|
195
|
+
const siteDataPath = external_node_path_default().join(process.cwd(), 'node_modules', shared_namespaceObject.RSPRESS_TEMP_DIR, 'runtime', 'virtual-site-data.mjs');
|
196
|
+
import(siteDataPath).then((siteData)=>{
|
197
|
+
const data = {
|
198
|
+
...siteData.default
|
199
|
+
};
|
200
|
+
data.pages.forEach((page)=>{
|
201
|
+
page.apiDocMap = apiDocMap;
|
202
|
+
});
|
203
|
+
external_node_fs_default().writeFileSync(siteDataPath, `export default ${JSON.stringify(data)}`);
|
204
|
+
isUpdate = false;
|
205
|
+
});
|
206
|
+
};
|
207
|
+
genApiDoc({
|
208
|
+
[moduleName]: changed
|
209
|
+
}, apiParseTool).then(updateSiteData);
|
167
210
|
}
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
import_logger.logger.error(
|
172
|
-
"[module-doc-plugin]",
|
173
|
-
`Generate API table error: ${e.message}`
|
174
|
-
);
|
175
|
-
}
|
176
|
-
}
|
177
|
-
})
|
178
|
-
);
|
179
|
-
};
|
180
|
-
import_logger.logger.info("[module-doc-plugin]", "Start to generate API table...");
|
181
|
-
if (isToolEntries(entries)) {
|
182
|
-
const reactEntries = entries["react-docgen-typescript"];
|
183
|
-
const documentationEntries = entries.documentation;
|
184
|
-
await Promise.all([
|
185
|
-
genApiDoc(reactEntries, "react-docgen-typescript"),
|
186
|
-
genApiDoc(documentationEntries, "documentation")
|
187
|
-
]);
|
188
|
-
} else {
|
189
|
-
await genApiDoc(entries, apiParseTool);
|
190
|
-
}
|
191
|
-
if (!isProd) {
|
192
|
-
const watcher = import_chokidar.default.watch(Object.keys(watchFileMap), {
|
193
|
-
ignoreInitial: true,
|
194
|
-
ignorePermissionErrors: true,
|
195
|
-
ignored: [/node_modules/]
|
196
|
-
});
|
197
|
-
let isUpdate = false;
|
198
|
-
watcher.on("change", (changed) => {
|
199
|
-
if (isUpdate) {
|
200
|
-
return;
|
201
|
-
}
|
202
|
-
isUpdate = true;
|
203
|
-
import_logger.logger.info("[module-doc-plugin]", "updating API");
|
204
|
-
const watchFileInfo = watchFileMap[changed];
|
205
|
-
if (watchFileInfo) {
|
206
|
-
const { apiParseTool: apiParseTool2, moduleName } = watchFileInfo;
|
207
|
-
const updateSiteData = () => {
|
208
|
-
const siteDataPath = import_node_path2.default.join(
|
209
|
-
process.cwd(),
|
210
|
-
"node_modules",
|
211
|
-
import_shared.RSPRESS_TEMP_DIR,
|
212
|
-
"runtime",
|
213
|
-
"virtual-site-data.mjs"
|
214
|
-
);
|
215
|
-
import(siteDataPath).then((siteData) => {
|
216
|
-
const data = { ...siteData.default };
|
217
|
-
data.pages.forEach((page) => {
|
218
|
-
page.apiDocMap = apiDocMap;
|
219
|
-
});
|
220
|
-
import_node_fs.default.writeFileSync(
|
221
|
-
siteDataPath,
|
222
|
-
`export default ${JSON.stringify(data)}`
|
223
|
-
);
|
224
|
-
isUpdate = false;
|
225
|
-
});
|
226
|
-
};
|
227
|
-
genApiDoc({ [moduleName]: changed }, apiParseTool2).then(updateSiteData);
|
228
|
-
}
|
229
|
-
});
|
230
|
-
}
|
231
|
-
import_logger.logger.success("[module-doc-plugin]", "Generate API table successfully!");
|
211
|
+
});
|
212
|
+
}
|
213
|
+
logger_namespaceObject.logger.success('[module-doc-plugin]', 'Generate API table successfully!');
|
232
214
|
};
|
233
215
|
function generateTable(componentDoc, language) {
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
216
|
+
return componentDoc.map((param)=>{
|
217
|
+
const { props } = param;
|
218
|
+
const t = locales[language];
|
219
|
+
const PROP_TABLE_HEADER = `|${t.property}|${t.description}|${t.type}|${t.defaultValue}|\n|:---:|:---:|:---:|:---:|`;
|
220
|
+
const tableContent = Object.keys(props).filter((propName)=>{
|
221
|
+
const { name, description } = props[propName];
|
222
|
+
return description || [
|
223
|
+
'className',
|
224
|
+
'style',
|
225
|
+
'disabled',
|
226
|
+
'children'
|
227
|
+
].indexOf(name) > -1;
|
228
|
+
}).map((propName)=>{
|
229
|
+
const { defaultValue, description, name, required, type } = props[propName];
|
230
|
+
const getType = ()=>`\`${type.name.replace(/\|/g, '\\|')}\`${required ? ` **(${t.required})**` : ''}`;
|
231
|
+
const getDefaultValue = ()=>`\`${(null == defaultValue ? void 0 : defaultValue.value) || '-'}\``;
|
232
|
+
const getDescription = ()=>{
|
233
|
+
switch(name){
|
234
|
+
case 'className':
|
235
|
+
return description || t.className;
|
236
|
+
case 'style':
|
237
|
+
return description || t.style;
|
238
|
+
case 'children':
|
239
|
+
return description || t.children;
|
240
|
+
case 'disabled':
|
241
|
+
return description || t.disabled;
|
242
|
+
default:
|
243
|
+
return description;
|
244
|
+
}
|
245
|
+
};
|
246
|
+
const formattedDescription = getDescription() // allow newline
|
247
|
+
.replace(/\n/g, ' ');
|
248
|
+
return `|${[
|
249
|
+
name,
|
250
|
+
formattedDescription,
|
251
|
+
getType(),
|
252
|
+
getDefaultValue()
|
253
|
+
].map((str)=>str.replace(/(?<!\\)\|/g, '|')).join('|')}|`;
|
254
|
+
});
|
255
|
+
return `
|
256
|
+
${param.displayName ? `### ${param.displayName}\n` : ''}
|
257
|
+
${param.description ? `**${param.description}**\n` : ''}
|
268
258
|
${PROP_TABLE_HEADER}
|
269
|
-
${tableContent.join(
|
259
|
+
${tableContent.join('\n')}
|
270
260
|
`;
|
271
|
-
|
261
|
+
}).join('\n');
|
272
262
|
}
|
273
|
-
|
274
|
-
|
275
|
-
function pluginApiDocgen(options) {
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
}
|
328
|
-
};
|
263
|
+
/**
|
264
|
+
* The plugin is used to generate api doc for files.
|
265
|
+
*/ function pluginApiDocgen(options) {
|
266
|
+
const { entries = {}, apiParseTool = 'react-docgen-typescript', appDir = process.cwd(), parseToolOptions = {} } = options || {};
|
267
|
+
return {
|
268
|
+
name: '@modern-js/doc-plugin-api-docgen',
|
269
|
+
config (config) {
|
270
|
+
config.markdown = config.markdown || {};
|
271
|
+
config.markdown.mdxRs = false;
|
272
|
+
return config;
|
273
|
+
},
|
274
|
+
async beforeBuild (config, isProd) {
|
275
|
+
var _config_themeConfig_locales, _config_themeConfig, _config_locales;
|
276
|
+
// only support zh and en
|
277
|
+
const languages = ((null === (_config_themeConfig = config.themeConfig) || void 0 === _config_themeConfig ? void 0 : null === (_config_themeConfig_locales = _config_themeConfig.locales) || void 0 === _config_themeConfig_locales ? void 0 : _config_themeConfig_locales.map((locale)=>locale.lang)) || (null === (_config_locales = config.locales) || void 0 === _config_locales ? void 0 : _config_locales.map((locale)=>locale.lang)) || [
|
278
|
+
config.lang
|
279
|
+
]).filter((lang)=>'zh' === lang || 'en' === lang);
|
280
|
+
await docgen({
|
281
|
+
entries,
|
282
|
+
apiParseTool,
|
283
|
+
languages,
|
284
|
+
appDir,
|
285
|
+
parseToolOptions,
|
286
|
+
isProd
|
287
|
+
});
|
288
|
+
},
|
289
|
+
async modifySearchIndexData (pages) {
|
290
|
+
// Update the search index of module doc which includes `<API moduleName="foo" />` and `<API moduleName="foo" ></API>
|
291
|
+
const apiCompRegExp = /(<API\s+moduleName=['"](\S+)['"]\s*(.*)?\/>)|(<API\s+moduleName=['"](\S+)['"]\s*(.*)?>(.*)?<\/API>)/;
|
292
|
+
await Promise.all(pages.map(async (page)=>{
|
293
|
+
const { _filepath, lang } = page;
|
294
|
+
let content = await external_node_fs_default().promises.readFile(_filepath, 'utf-8');
|
295
|
+
let matchResult = new RegExp(apiCompRegExp).exec(content);
|
296
|
+
if (!matchResult) return;
|
297
|
+
while(null !== matchResult){
|
298
|
+
const [matchContent, moduleName] = matchResult;
|
299
|
+
const apiDoc = apiDocMap[moduleName] || apiDocMap[`${moduleName}-${lang}`];
|
300
|
+
content = content.replace(matchContent, apiDoc);
|
301
|
+
matchResult = new RegExp(apiCompRegExp).exec(content);
|
302
|
+
}
|
303
|
+
page.content = content;
|
304
|
+
}));
|
305
|
+
},
|
306
|
+
extendPageData (pageData) {
|
307
|
+
pageData.apiDocMap = {
|
308
|
+
...apiDocMap
|
309
|
+
};
|
310
|
+
},
|
311
|
+
markdown: {
|
312
|
+
globalComponents: [
|
313
|
+
external_node_path_default().join(__dirname, '..', 'static', 'global-components', 'API.tsx')
|
314
|
+
]
|
315
|
+
}
|
316
|
+
};
|
329
317
|
}
|
330
|
-
|
331
|
-
|
332
|
-
|
318
|
+
var __webpack_export_target__ = exports;
|
319
|
+
for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
|
320
|
+
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
|
321
|
+
value: true
|
333
322
|
});
|
334
|
-
|
335
|
-
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@rspress/plugin-api-docgen",
|
3
|
-
"version": "1.37.
|
3
|
+
"version": "1.37.3",
|
4
4
|
"description": "A plugin for rspress to generate api doc.",
|
5
5
|
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
6
6
|
"repository": {
|
@@ -21,9 +21,11 @@
|
|
21
21
|
"react-docgen-typescript": "2.2.2",
|
22
22
|
"react-markdown": "8.0.7",
|
23
23
|
"remark-gfm": "3.0.1",
|
24
|
-
"@rspress/shared": "1.37.
|
24
|
+
"@rspress/shared": "1.37.3"
|
25
25
|
},
|
26
26
|
"devDependencies": {
|
27
|
+
"@microsoft/api-extractor": "^7.48.0",
|
28
|
+
"@rslib/core": "0.1.0",
|
27
29
|
"@types/mdast": "^4.0.4",
|
28
30
|
"@types/node": "^18.11.17",
|
29
31
|
"@types/react": "^18.3.12",
|
@@ -39,7 +41,7 @@
|
|
39
41
|
"peerDependencies": {
|
40
42
|
"react": ">=17.0.0",
|
41
43
|
"react-router-dom": "^6.8.1",
|
42
|
-
"@rspress/core": "^1.37.
|
44
|
+
"@rspress/core": "^1.37.3",
|
43
45
|
"typescript": "^5.5.3"
|
44
46
|
},
|
45
47
|
"peerDependenciesMeta": {
|
@@ -58,8 +60,8 @@
|
|
58
60
|
"registry": "https://registry.npmjs.org/"
|
59
61
|
},
|
60
62
|
"scripts": {
|
61
|
-
"dev": "
|
62
|
-
"build": "
|
63
|
+
"dev": "rslib build -w",
|
64
|
+
"build": "rslib build",
|
63
65
|
"reset": "rimraf ./**/node_modules",
|
64
66
|
"test": "vitest run --passWithNoTests"
|
65
67
|
}
|
package/dist/index.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,oBAAiB;AAEjB,IAAAC,kBAAe;;;ACFf,IAAAD,oBAAiB;AAEjB,qBAAe;AACf,oBAAuB;AACvB,sBAAqB;AACrB,qCAIO;AACP,oBAAiC;;;ACVjC,uBAAiB;AAEV,IAAM,eAAe,iBAAAE,QAAK,KAAK,WAAW,IAAI;AAE9C,IAAM,YAAoC,CAAC;;;ACJ3C,IAAM,UAAU;AAAA,EACrB,IAAI;AAAA,IACF,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,WAAW;AAAA,IACX,OAAO;AAAA,IACP,UAAU;AAAA,IACV,UAAU;AAAA,IACV,UAAU;AAAA,IACV,UAAU;AAAA,IACV,aAAa;AAAA,IACb,MAAM;AAAA,IACN,cAAc;AAAA,IACd,UAAU;AAAA,EACZ;AAAA,EACA,IAAI;AAAA,IACF,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,WAAW;AAAA,IACX,OAAO;AAAA,IACP,UAAU;AAAA,IACV,UAAU;AAAA,IACV,UAAU;AAAA,IACV,UAAU;AAAA,IACV,aAAa;AAAA,IACb,MAAM;AAAA,IACN,cAAc;AAAA,IACd,UAAU;AAAA,EACZ;AACF;;;A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AAA,IACF;AAAA,EACF;AACF","names":["import_node_path","import_node_fs","path","chokidar","apiParseTool","fs"],"ignoreList":[],"sources":["../src/index.ts","../src/docgen.ts","../src/constants.ts","../src/locales.ts"],"sourcesContent":["import path from 'node:path';\nimport type { RspressPlugin } from '@rspress/shared';\nimport fs from 'node:fs';\nimport type {\n PluginOptions,\n SupportLanguages,\n ExtendedPageData,\n} from './types';\nimport { docgen } from './docgen';\nimport { apiDocMap } from './constants';\n\n/**\n * The plugin is used to generate api doc for files.\n */\nexport function pluginApiDocgen(options?: PluginOptions): RspressPlugin {\n const {\n entries = {},\n apiParseTool = 'react-docgen-typescript',\n appDir = process.cwd(),\n parseToolOptions = {},\n } = options || {};\n return {\n name: '@modern-js/doc-plugin-api-docgen',\n config(config) {\n config.markdown = config.markdown || {};\n config.markdown.mdxRs = false;\n return config;\n },\n async beforeBuild(config, isProd) {\n // only support zh and en\n const languages = (\n config.themeConfig?.locales?.map(locale => locale.lang) ||\n config.locales?.map(locale => locale.lang) || [config.lang]\n ).filter(lang => lang === 'zh' || lang === 'en') as SupportLanguages[];\n await docgen({\n entries,\n apiParseTool,\n languages,\n appDir,\n parseToolOptions,\n isProd,\n });\n },\n async modifySearchIndexData(pages) {\n // Update the search index of module doc which includes `<API moduleName=\"foo\" />` and `<API moduleName=\"foo\" ></API>\n const apiCompRegExp =\n /(<API\\s+moduleName=['\"](\\S+)['\"]\\s*(.*)?\\/>)|(<API\\s+moduleName=['\"](\\S+)['\"]\\s*(.*)?>(.*)?<\\/API>)/;\n await Promise.all(\n pages.map(async page => {\n const { _filepath, lang } = page;\n let content = await fs.promises.readFile(_filepath, 'utf-8');\n let matchResult = new RegExp(apiCompRegExp).exec(content);\n if (!matchResult) {\n return;\n }\n while (matchResult !== null) {\n const [matchContent, moduleName] = matchResult;\n const apiDoc =\n apiDocMap[moduleName] || apiDocMap[`${moduleName}-${lang}`];\n content = content.replace(matchContent, apiDoc);\n matchResult = new RegExp(apiCompRegExp).exec(content);\n }\n page.content = content;\n }),\n );\n },\n extendPageData(pageData) {\n (pageData as ExtendedPageData).apiDocMap = { ...apiDocMap };\n },\n markdown: {\n globalComponents: [\n path.join(__dirname, '..', 'static', 'global-components', 'API.tsx'),\n ],\n },\n };\n}\n\nexport type { PluginOptions };\n","import path from 'node:path';\nimport type { ComponentDoc, PropItem } from 'react-docgen-typescript';\nimport fs from 'node:fs';\nimport { logger } from '@rspress/shared/logger';\nimport chokidar from 'chokidar';\nimport {\n withDefaultConfig,\n withCustomConfig,\n withCompilerOptions,\n} from 'react-docgen-typescript';\nimport { RSPRESS_TEMP_DIR } from '@rspress/shared';\nimport { apiDocMap } from './constants';\nimport { locales } from './locales';\nimport type {\n DocGenOptions,\n Entries,\n ToolEntries,\n ApiParseTool,\n WatchFileInfo,\n} from './types';\n\nconst isToolEntries = (obj: Record<string, any>): obj is ToolEntries => {\n return obj.documentation || obj['react-docgen-typescript'];\n};\n\nexport const docgen = async ({\n entries,\n languages,\n apiParseTool,\n appDir,\n parseToolOptions,\n isProd,\n}: DocGenOptions) => {\n const watchFileMap: Record<string, WatchFileInfo> = {};\n const genApiDoc = async (entry: Entries, tool: ApiParseTool) => {\n if (Object.keys(entry).length === 0) {\n return;\n }\n await Promise.all(\n Object.entries(entry).map(async ([key, value]) => {\n const moduleSourceFilePath = path.resolve(appDir, value);\n watchFileMap[moduleSourceFilePath] = {\n apiParseTool,\n moduleName: key,\n };\n try {\n if (tool === 'documentation') {\n const documentation = await import('documentation');\n\n const documentationRes = await documentation.build(\n [moduleSourceFilePath],\n {\n ...parseToolOptions.documentation,\n },\n );\n const apiDoc = await documentation.formats.md(documentationRes, {\n noReferenceLinks:\n parseToolOptions.documentation?.noReferenceLinks ?? true,\n });\n apiDocMap[key] = apiDoc;\n } else {\n const { tsconfigPath, compilerOptions, ...restOptions } =\n parseToolOptions?.['react-docgen-typescript'] ?? {};\n const parserOpts = {\n // https://github.com/styleguidist/react-docgen-typescript/blob/master/README.md?plain=1#L111\n propFilter: (prop: PropItem) => {\n if (\n prop.declarations !== undefined &&\n prop.declarations.length > 0\n ) {\n const hasPropAdditionalDescription = prop.declarations.find(\n declaration => {\n return !declaration.fileName.includes('node_modules');\n },\n );\n\n return Boolean(hasPropAdditionalDescription);\n }\n\n return true;\n },\n ...restOptions,\n };\n let fileParser = withDefaultConfig(parserOpts);\n if (tsconfigPath?.[key]) {\n fileParser = withCustomConfig(tsconfigPath[key], parserOpts);\n } else if (compilerOptions?.[key]) {\n fileParser = withCompilerOptions(\n compilerOptions[key],\n parserOpts,\n );\n }\n const componentDoc = fileParser.parse(moduleSourceFilePath);\n if (componentDoc.length === 0) {\n logger.warn(\n '[module-doc-plugin]',\n `Unable to parse API document in ${moduleSourceFilePath}`,\n );\n }\n if (languages.length > 0) {\n languages.forEach(language => {\n apiDocMap[`${key}-${language}`] = generateTable(\n componentDoc,\n language,\n );\n });\n } else {\n apiDocMap[key] = generateTable(componentDoc, 'en');\n }\n }\n } catch (e) {\n if (e instanceof Error) {\n logger.error(\n '[module-doc-plugin]',\n `Generate API table error: ${e.message}`,\n );\n }\n }\n }),\n );\n };\n logger.info('[module-doc-plugin]', 'Start to generate API table...');\n\n if (isToolEntries(entries)) {\n const reactEntries = entries['react-docgen-typescript'];\n const documentationEntries = entries.documentation;\n await Promise.all([\n genApiDoc(reactEntries, 'react-docgen-typescript'),\n genApiDoc(documentationEntries, 'documentation'),\n ]);\n } else {\n await genApiDoc(entries, apiParseTool);\n }\n if (!isProd) {\n const watcher = chokidar.watch(Object.keys(watchFileMap), {\n ignoreInitial: true,\n ignorePermissionErrors: true,\n ignored: [/node_modules/],\n });\n let isUpdate = false;\n watcher.on('change', changed => {\n if (isUpdate) {\n return;\n }\n isUpdate = true;\n logger.info('[module-doc-plugin]', 'updating API');\n const watchFileInfo = watchFileMap[changed];\n if (watchFileInfo) {\n const { apiParseTool, moduleName } = watchFileInfo;\n\n const updateSiteData = () => {\n const siteDataPath = path.join(\n process.cwd(),\n 'node_modules',\n RSPRESS_TEMP_DIR,\n 'runtime',\n 'virtual-site-data.mjs',\n );\n import(siteDataPath).then(siteData => {\n const data = { ...siteData.default };\n data.pages.forEach((page: Record<string, unknown>) => {\n page.apiDocMap = apiDocMap;\n });\n fs.writeFileSync(\n siteDataPath,\n `export default ${JSON.stringify(data)}`,\n );\n isUpdate = false;\n });\n };\n genApiDoc({ [moduleName]: changed }, apiParseTool).then(updateSiteData);\n }\n });\n }\n\n logger.success('[module-doc-plugin]', 'Generate API table successfully!');\n};\n\nfunction generateTable(componentDoc: ComponentDoc[], language: 'zh' | 'en') {\n return componentDoc\n .map(param => {\n const { props } = param;\n const t = locales[language];\n const PROP_TABLE_HEADER = `|${t.property}|${t.description}|${t.type}|${t.defaultValue}|\\n|:---:|:---:|:---:|:---:|`;\n\n const tableContent = Object.keys(props)\n .filter(propName => {\n const { name, description } = props[propName];\n return (\n description ||\n ['className', 'style', 'disabled', 'children'].indexOf(name) > -1\n );\n })\n .map(propName => {\n const { defaultValue, description, name, required, type } =\n props[propName];\n const getType = () =>\n `\\`${type.name.replace(/\\|/g, '\\\\|')}\\`${\n required ? ` **(${t.required})**` : ''\n }`;\n const getDefaultValue = () => `\\`${defaultValue?.value || '-'}\\``;\n\n const getDescription = () => {\n switch (name) {\n case 'className':\n return description || t.className;\n case 'style':\n return description || t.style;\n case 'children':\n return description || t.children;\n case 'disabled':\n return description || t.disabled;\n default:\n return description;\n }\n };\n\n const formattedDescription = getDescription()\n // allow newline\n .replace(/\\n/g, ' ');\n\n return `|${[name, formattedDescription, getType(), getDefaultValue()]\n .map(str => str.replace(/(?<!\\\\)\\|/g, '|'))\n .join('|')}|`;\n });\n\n return `\n ${param.displayName ? `### ${param.displayName}\\n` : ''}\n ${param.description ? `**${param.description}**\\n` : ''}\n ${PROP_TABLE_HEADER}\n ${tableContent.join('\\n')}\n `;\n })\n .join('\\n');\n}\n","import path from 'node:path';\n\nexport const PACKAGE_ROOT = path.join(__dirname, '..');\n\nexport const apiDocMap: Record<string, string> = {};\n","export const locales = {\n zh: {\n copy: '复制',\n copied: '复制成功',\n expand: '展开代码',\n collapse: '收起代码',\n className: '节点类名',\n style: '节点样式',\n children: '子节点',\n disabled: '是否禁用',\n required: '必填',\n property: '属性',\n description: '说明',\n type: '类型',\n defaultValue: '默认值',\n overview: '概览',\n },\n en: {\n copy: 'Copy',\n copied: 'Copied Success!',\n expand: 'Expand Code',\n collapse: 'Collapse Code',\n className: 'Additional css class',\n style: 'Additional style',\n children: 'Children',\n disabled: 'Whether to disable ',\n required: 'Required',\n property: 'Property',\n description: 'Description',\n type: 'Type',\n defaultValue: 'Default Value',\n overview: 'Overview',\n },\n};\n"]}
|