Skip to content

Commit ada59ff

Browse files
committed
scripts and readme
1 parent c15c3e1 commit ada59ff

File tree

4 files changed

+73
-172
lines changed

4 files changed

+73
-172
lines changed

README.md

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
# Rspress website
1+
# Scripting 官方文档项目
22

3-
## Setup
3+
## 项目简介
44

5-
Install the dependencies:
5+
通过 `Scripting Documentation` (Scripting App 默认脚本) 生成官网的工程
66

7-
```bash
8-
npm install
9-
```
10-
11-
## Get started
12-
13-
Start the dev server:
14-
15-
```bash
16-
npm run dev
17-
```
18-
19-
Build the website for production:
20-
21-
```bash
22-
npm run build
23-
```
7+
## 快速开始
248

25-
Preview the production build locally:
9+
1.`Scripting Documentation` (Scripting App 默认脚本) 放入 `resources` 文件夹
10+
2. 运行文档生成命令:
2611

2712
```bash
28-
npm run preview
13+
bun run docs
2914
```

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"build": "rspress build",
77
"dev": "rspress dev",
88
"preview": "rspress preview",
9-
"gen-docs": "rm -rf docs/en/guide/docs/* && rm -rf docs/zh/guide/docs/* && bun scripts/docs.js",
10-
"docs-beta": "rm -rf docs/en/guide/docs/* && rm -rf docs/zh/guide/docs/* && bun scripts/docs-beta.js"
9+
"docs": "rm -rf docs/en/guide/docs/* && rm -rf docs/zh/guide/docs/* && bun scripts/docs.js"
1110
},
1211
"dependencies": {
1312
"rspress": "^1.40.2"

scripts/docs-beta.js

Lines changed: 0 additions & 115 deletions
This file was deleted.

scripts/docs.js

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,87 @@ const writeFile = (filePath, content) => {
1717
const processDocItem = (item, parentPath = "", language = "en") => {
1818
const { title, subtitle, keywords, example, readme, children } = item;
1919

20-
const folderName = title.en; // 使用语言来选择标题
21-
const basePath = path.join(docsPath, language, "guide", "docs");
20+
const folderName = title.en;
21+
const basePath = path.join(docsPath, language, "guide", "docs", parentPath, folderName);
2222

23+
// 生成 _meta.json
24+
const metaPath = path.join(path.join(docsPath, language, "guide", "docs", parentPath), "_meta.json");
25+
if (!fs.existsSync(metaPath)) {
26+
writeFile(metaPath, JSON.stringify([], null, 2));
27+
}
28+
const metaJson = JSON.parse(readFile(metaPath));
29+
30+
if (children) {
31+
metaJson.push({
32+
type: "dir",
33+
name: folderName,
34+
label: title[language],
35+
collapsible: true,
36+
collapsed: true,
37+
});
38+
writeFile(metaPath, JSON.stringify(metaJson, null, 2));
39+
} else {
40+
metaJson.push({
41+
type: "file",
42+
name: folderName,
43+
label: title[language],
44+
});
45+
writeFile(metaPath, JSON.stringify(metaJson, null, 2));
46+
}
47+
48+
// 生成文档
2349
if (children && children.length > 0) {
2450
children.forEach((child) => processDocItem(child, path.join(parentPath, folderName), language));
2551
} else {
26-
// 处理没有子文档的文件
27-
if (example) {
28-
// 如果有example,将其转换为MD文件
29-
const tsxContent = readFile(path.join(resourcePath, example + ".tsx"));
30-
31-
const exampleName = example.split("/").pop();
52+
if (readme) {
53+
if (!example) {
54+
const readmePath = path.join(resourcePath, readme, language + ".md");
55+
const readmeContent = readFile(readmePath);
56+
const readmeMd = `---
57+
title: ${title[language]}
58+
---\n${readmeContent}`;
59+
const cleanBasePath = basePath.endsWith(path.sep) ? basePath.slice(0, -1) : basePath;
60+
writeFile(cleanBasePath + ".md", readmeMd);
61+
} else {
62+
const readmePath = path.join(resourcePath, readme, language + ".md");
63+
const readmeContent = readFile(readmePath);
64+
const readmeMd = `---
65+
title: ${title[language]}
66+
---\n${readmeContent}`;
3267

33-
let exampleTitle = language === "en" ? "Example" : "示例";
34-
if (exampleName !== "index") {
35-
exampleTitle += " - " + exampleName;
68+
writeFile(path.join(basePath, "index.md"), readmeMd);
3669
}
70+
}
3771

38-
const exampleMd = `---
39-
title: ${exampleTitle}
72+
if (example) {
73+
if (!readme) {
74+
const tsxContent = readFile(path.join(resourcePath, example + ".tsx"));
75+
const exampleMd = `---
76+
title: ${title[language]}
4077
---
4178
\`\`\`tsx
4279
${tsxContent}
4380
\`\`\``;
44-
writeFile(path.join(basePath, example + "_example.md"), exampleMd); // 使用 index.md 作为文件名
45-
}
4681

47-
if (readme) {
48-
// 根据语言选择正确的 readme 文件,并保存为 index.md
49-
const readmePath = path.join(resourcePath, readme, language + ".md"); // 选择对应语言的 .md 文件
50-
try {
51-
const readmeContent = readFile(readmePath);
82+
const cleanBasePath = basePath.endsWith(path.sep) ? basePath.slice(0, -1) : basePath;
83+
writeFile(cleanBasePath + ".md", exampleMd);
84+
} else {
85+
const tsxContent = readFile(path.join(resourcePath, example + ".tsx"));
86+
let exampleName = example.split("/").pop();
87+
let exampleTitle = language === "en" ? "Example" : "示例";
5288

53-
// 为 readme 文件添加 title 和 --- 块
54-
const readmeMd = `---
55-
title: ${title[language]}
56-
---\n${readmeContent}`;
89+
const exampleMd = `---
90+
title: ${exampleTitle}
91+
---
92+
\`\`\`tsx
93+
${tsxContent}
94+
\`\`\``;
5795

58-
writeFile(path.join(basePath, readme, "index.md"), readmeMd); // 使用 index.md 作为文件名
59-
} catch (err) {
60-
console.error(`Error reading readme file at ${readmePath}:`, err);
96+
const fileSuffix = exampleName === "index" ? "_example" : "";
97+
writeFile(path.join(basePath, exampleName + fileSuffix + ".md"), exampleMd);
6198
}
6299
}
63100
}
64-
65-
// 处理 _meta.json
66-
// const metaPath = path.join(basePath, parentPath, folderName, "_meta.json");
67101
};
68102

69103
// 处理多语言
@@ -74,9 +108,7 @@ const processLanguages = (docItem) => {
74108

75109
// 读取 JSON 配置并开始处理
76110
const processDocs = () => {
77-
const docJsonPath = path.join(resourcePath, "doc.json");
78-
const docJson = JSON.parse(readFile(docJsonPath));
79-
111+
const docJson = JSON.parse(readFile(path.join(resourcePath, "doc.json")));
80112
docJson.forEach((item) => processLanguages(item));
81113
};
82114

0 commit comments

Comments
 (0)