component-auto-docs 0.1.2 → 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/README.md
CHANGED
|
@@ -40,6 +40,7 @@ App 内组件详情页默认使用 Tab 分区:
|
|
|
40
40
|
- `docs/ai/components.quality.json`:文档质量报告。
|
|
41
41
|
- `src/docs/data.json`:App 内文档首页数据。
|
|
42
42
|
- `src/docs/components/*/data.json`:单组件详情页数据。
|
|
43
|
+
- `src/docs/data.js`、`src/docs/components/*/data.js`:App/小程序运行时使用的数据模块,避免部分小程序编译链把 JSON 字段提升成同名顶层变量。
|
|
43
44
|
- `src/docs/components/*/index.vue`:单组件交互文档页。
|
|
44
45
|
|
|
45
46
|
## 快速开始
|
|
@@ -117,6 +118,7 @@ DOCS_CONFIG=./docs.config.mjs pnpm exec component-auto-docs gen
|
|
|
117
118
|
- `componentRoot`:组件目录,默认 `src/components`。
|
|
118
119
|
- `metaFileName`:组件元数据文件名,默认 `docs.meta.json`。
|
|
119
120
|
- `output`:Markdown、AI 文档、页面数据和质量报告输出位置。
|
|
121
|
+
- `pageDataModuleFileName`:单组件页面运行时数据模块文件名,默认 `data.js`。
|
|
120
122
|
- `routes`:是否自动写入 `pages.json`,以及文档路由前缀、插入锚点、标题后缀。
|
|
121
123
|
- `runtime`:自动文档页使用的页面壳、hook 和组件导入别名。
|
|
122
124
|
- `quality`:质量检查规则,例如是否要求 App 内文档页存在。
|
|
@@ -20,6 +20,7 @@ const defaultDocsConfig = {
|
|
|
20
20
|
markdownDir: 'docs/components',
|
|
21
21
|
pageDataRoot: 'src/docs/components',
|
|
22
22
|
indexDataFile: 'src/docs/data.json',
|
|
23
|
+
pageDataModuleFileName: 'data.js',
|
|
23
24
|
catalogFile: 'components.catalog.json',
|
|
24
25
|
aiIndexFile: 'components.index.md',
|
|
25
26
|
llmsFile: 'llms.txt',
|
|
@@ -158,6 +159,57 @@ function writeText(filePath, content) {
|
|
|
158
159
|
fs.writeFileSync(filePath, `${content.trimEnd()}\n`);
|
|
159
160
|
}
|
|
160
161
|
|
|
162
|
+
function hashText(text) {
|
|
163
|
+
let hash = 5381;
|
|
164
|
+
|
|
165
|
+
for (const char of String(text)) {
|
|
166
|
+
hash = (hash * 33) ^ char.codePointAt(0);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return (hash >>> 0).toString(36);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function toDataModuleIdentifier(name, fallback) {
|
|
173
|
+
const words = String(name).match(/[A-Za-z0-9]+/g) || [];
|
|
174
|
+
const base = words
|
|
175
|
+
.map((word, index) => {
|
|
176
|
+
const lower = word.toLowerCase();
|
|
177
|
+
return index === 0 ? lower : lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
178
|
+
})
|
|
179
|
+
.join('');
|
|
180
|
+
const identifierBase = base && /^[A-Za-z_$]/.test(base) ? base : fallback;
|
|
181
|
+
|
|
182
|
+
return `${identifierBase}Data_${hashText(name)}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function serializeJsValue(value) {
|
|
186
|
+
return JSON.stringify(value, null, 2)
|
|
187
|
+
.replace(/\u2028/g, '\\u2028')
|
|
188
|
+
.replace(/\u2029/g, '\\u2029');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function renderDataModule(data, identifier) {
|
|
192
|
+
return `// @generated by ${generatedBy}. Keep this file in sync by running docs:gen.
|
|
193
|
+
const ${identifier} = ${serializeJsValue(data)};
|
|
194
|
+
|
|
195
|
+
export default ${identifier};
|
|
196
|
+
`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function writeDataModule(filePath, data, identifier) {
|
|
200
|
+
writeText(filePath, renderDataModule(data, identifier));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function getIndexDataModuleFile() {
|
|
204
|
+
const parsed = path.parse(indexDataFile);
|
|
205
|
+
|
|
206
|
+
return path.join(parsed.dir, `${parsed.name}.js`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function getPageDataModuleFileName() {
|
|
210
|
+
return docsConfig.output.pageDataModuleFileName || 'data.js';
|
|
211
|
+
}
|
|
212
|
+
|
|
161
213
|
function normalizeTypeText(typeText) {
|
|
162
214
|
return typeText
|
|
163
215
|
.replace(/\s+/g, ' ')
|
|
@@ -1532,6 +1584,7 @@ function renderAutoDocPage(componentName, componentFile) {
|
|
|
1532
1584
|
const importName = componentName
|
|
1533
1585
|
.replace(/(^|-)(\w)/g, (_, __, char) => char.toUpperCase())
|
|
1534
1586
|
.replace(/[^\w]/g, '');
|
|
1587
|
+
const dataModuleImport = `./${getPageDataModuleFileName()}`;
|
|
1535
1588
|
const {
|
|
1536
1589
|
componentDocPageImport,
|
|
1537
1590
|
autoDocHookImport,
|
|
@@ -1545,7 +1598,7 @@ function renderAutoDocPage(componentName, componentFile) {
|
|
|
1545
1598
|
import ${componentDocPageName} from '${componentDocPageImport}';
|
|
1546
1599
|
import { ${autoDocHookName} } from '${autoDocHookImport}';
|
|
1547
1600
|
import ${importName} from '${componentImportBase}/${componentName}/${fileName}';
|
|
1548
|
-
import doc from '
|
|
1601
|
+
import doc from '${dataModuleImport}';
|
|
1549
1602
|
|
|
1550
1603
|
const {
|
|
1551
1604
|
propControls,
|
|
@@ -1650,10 +1703,11 @@ ${renderGeneratedControlStyles()}
|
|
|
1650
1703
|
|
|
1651
1704
|
function renderGenericDocPage() {
|
|
1652
1705
|
const { componentDocPageImport, componentDocPageName } = docsConfig.runtime;
|
|
1706
|
+
const dataModuleImport = `./${getPageDataModuleFileName()}`;
|
|
1653
1707
|
|
|
1654
1708
|
return `<script setup lang="ts">
|
|
1655
1709
|
import ${componentDocPageName} from '${componentDocPageImport}';
|
|
1656
|
-
import doc from '
|
|
1710
|
+
import doc from '${dataModuleImport}';
|
|
1657
1711
|
</script>
|
|
1658
1712
|
|
|
1659
1713
|
<template>
|
|
@@ -2128,13 +2182,24 @@ function main() {
|
|
|
2128
2182
|
writeJson(path.join(aiDocsDir, docsConfig.output.catalogFile), catalog);
|
|
2129
2183
|
writeText(path.join(aiDocsDir, docsConfig.output.aiIndexFile), renderAiIndex(catalog));
|
|
2130
2184
|
writeText(path.join(aiDocsDir, docsConfig.output.llmsFile), renderLlmsTxt(catalog));
|
|
2131
|
-
|
|
2185
|
+
const pageIndex = buildPageIndex(components);
|
|
2186
|
+
writeJson(indexDataFile, pageIndex);
|
|
2187
|
+
writeDataModule(
|
|
2188
|
+
getIndexDataModuleFile(),
|
|
2189
|
+
pageIndex,
|
|
2190
|
+
toDataModuleIdentifier(docsConfig.projectName || 'docs-index', 'docsIndex'),
|
|
2191
|
+
);
|
|
2132
2192
|
const qualityReport = collectQualityReport(components);
|
|
2133
2193
|
writeJson(path.join(aiDocsDir, docsConfig.output.qualityFile), qualityReport);
|
|
2134
2194
|
|
|
2135
2195
|
for (const component of components) {
|
|
2136
2196
|
writeText(path.join(markdownDocsDir, `${component.name}.md`), renderComponentMarkdown(component));
|
|
2137
2197
|
writeJson(path.join(pageDataRoot, component.name, 'data.json'), component);
|
|
2198
|
+
writeDataModule(
|
|
2199
|
+
path.join(pageDataRoot, component.name, getPageDataModuleFileName()),
|
|
2200
|
+
component,
|
|
2201
|
+
toDataModuleIdentifier(component.name, 'componentDoc'),
|
|
2202
|
+
);
|
|
2138
2203
|
}
|
|
2139
2204
|
|
|
2140
2205
|
console.log(`Generated component docs for ${components.length} component(s): ${components.map((item) => item.name).join(', ')}`);
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
生成产物分为三类:
|
|
13
13
|
|
|
14
14
|
- 人类阅读:`docs/components/*.md`。
|
|
15
|
-
- App 内页面:`src/docs/data.json`、`src/docs/components/*/data.json`、`src/docs/components/*/index.vue`。
|
|
15
|
+
- App 内页面:`src/docs/data.json`、`src/docs/data.js`、`src/docs/components/*/data.json`、`src/docs/components/*/data.js`、`src/docs/components/*/index.vue`。
|
|
16
16
|
- AI/检索:`docs/ai/components.catalog.json`、`docs/ai/components.index.md`、`docs/ai/llms.txt`、`docs/ai/components.quality.json`。
|
|
17
17
|
|
|
18
18
|
## 新组件接入
|
|
@@ -80,6 +80,7 @@ pnpm exec component-auto-docs scaffold --force
|
|
|
80
80
|
- `componentRoot`:组件目录,例如 `src/components`。
|
|
81
81
|
- `metaFileName`:组件元数据文件名,默认 `docs.meta.json`。
|
|
82
82
|
- `output`:AI 文档、Markdown、页面数据和质量报告输出位置。
|
|
83
|
+
- `output.pageDataModuleFileName`:单组件页面运行时数据模块文件名,默认 `data.js`。
|
|
83
84
|
- `routes`:是否自动写入 `pages.json`、文档入口路由、组件文档路由前缀、插入锚点和标题后缀。
|
|
84
85
|
- `runtime`:自动文档页使用的页面壳、hook 和组件导入别名。
|
|
85
86
|
- `quality`:质量检查是否要求 App 内文档页,以及页面壳识别规则。
|
|
@@ -13,6 +13,7 @@ export default {
|
|
|
13
13
|
markdownDir: 'docs/components',
|
|
14
14
|
pageDataRoot: 'src/docs/components',
|
|
15
15
|
indexDataFile: 'src/docs/data.json',
|
|
16
|
+
pageDataModuleFileName: 'data.js',
|
|
16
17
|
catalogFile: 'components.catalog.json',
|
|
17
18
|
aiIndexFile: 'components.index.md',
|
|
18
19
|
llmsFile: 'llms.txt',
|