Skip to content

Commit af8d0a6

Browse files
committed
Release
1 parent 6df23f6 commit af8d0a6

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"build": "rspress build",
77
"dev": "rspress dev",
88
"preview": "rspress preview",
9-
"gen-docs": "bun scripts/docs.js"
9+
"gen-docs": "rm -rf docs/en/guide/docs/* && rm -rf docs/zh/guide/docs/* && bun scripts/docs.js"
1010
},
1111
"dependencies": {
1212
"rspress": "^1.40.2"

scripts/docs.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const resourcePath = path.join(__dirname, "..", "resources", "Scripting Documentation");
5+
const docsPath = path.join(__dirname, "..", "docs");
6+
7+
const readFile = (filePath) => {
8+
return fs.readFileSync(filePath, "utf-8");
9+
};
10+
11+
const writeFile = (filePath, content) => {
12+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
13+
fs.writeFileSync(filePath, content, "utf-8");
14+
};
15+
16+
// 处理文件
17+
const processDocItem = (item, parentPath = "", language = "en") => {
18+
const { title, subtitle, keywords, example, readme, children } = item;
19+
20+
const folderName = title[language]; // 使用当前语言来选择标题
21+
const basePath = path.join(docsPath, language, "guide", "docs", parentPath);
22+
23+
// 如果有子文档,递归处理
24+
if (children && children.length > 0) {
25+
writeFile(path.join(basePath, folderName + ".md"), `# ${folderName}`);
26+
children.forEach((child) => processDocItem(child, path.join(parentPath, folderName), language));
27+
} else {
28+
// 没有子文档的文件,处理 example 和 readme
29+
if (example) {
30+
// 处理 example,将其转换为 MD 文件
31+
const tsxContent = readFile(path.join(resourcePath, example + ".tsx"));
32+
const exampleMd = `---
33+
title: ${language === "zh" ? "示例" : "Example"}
34+
---
35+
\`\`\`tsx
36+
${tsxContent}
37+
\`\`\``;
38+
// 写入 index.md,文件名固定为 index.md
39+
writeFile(path.join(basePath, "example.md"), exampleMd);
40+
}
41+
42+
if (readme) {
43+
// 处理 readme 文件,直接用对应语言的 .md 文件内容
44+
const readmePath = path.join(resourcePath, readme, language + ".md");
45+
try {
46+
const readmeContent = readFile(readmePath);
47+
48+
// 为 readme 添加 title 块
49+
const readmeMd = `---
50+
title: ${folderName}
51+
---
52+
${readmeContent}`;
53+
54+
// 生成固定路径的 index.md 文件
55+
writeFile(path.join(basePath, "index.md"), readmeMd);
56+
} catch (err) {
57+
console.error(`Error reading readme file at ${readmePath}:`, err);
58+
}
59+
}
60+
}
61+
};
62+
63+
// 处理多语言
64+
const processLanguages = (docItem) => {
65+
processDocItem(docItem, "", "en");
66+
processDocItem(docItem, "", "zh");
67+
};
68+
69+
// 读取 JSON 配置并开始处理
70+
const processDocs = () => {
71+
const docJsonPath = path.join(resourcePath, "doc.json");
72+
const docJson = JSON.parse(readFile(docJsonPath));
73+
74+
docJson.forEach((item) => processLanguages(item));
75+
};
76+
77+
processDocs();

scripts/pompt.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
在scripts/docs.js写一个package.json脚本,用于读取resources/Scripting Documentation/doc.json并根据json定义:
1+
在scripts/docs.js写一个package.json脚本(只用自带库),用于读取resources/Scripting Documentation/doc.json并根据json定义:
22

33
export type DocItem = {
44
title: Record<string, string>,

0 commit comments

Comments
 (0)