Skip to content

Commit 5a3d514

Browse files
committed
bug: hasChildren
1 parent 9f6ec32 commit 5a3d514

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

docs/en/guide/docs/_meta.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/zh/guide/docs/_meta.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

scripts/docs.js

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,58 @@ async function generateDocs(docEntries, basePath) {
99
}
1010

1111
// 处理单个文档条目
12+
// 修改 processEntry 函数中的路径逻辑
1213
async function processEntry(entry, currentPath) {
1314
const hasContent = entry.readme || entry.example;
1415
const hasChildren = entry.children?.length > 0;
1516

16-
// 生成两种语言的文档
17-
await generateForLanguage("zh", entry, currentPath, hasContent, hasChildren);
18-
await generateForLanguage("en", entry, currentPath, hasContent, hasChildren);
17+
// 修改:所有包含children的条目都使用其title作为子目录
18+
const newPath = hasChildren ? join(currentPath, entry.title.zh || entry.title.en) : currentPath;
19+
20+
await generateForLanguage("zh", entry, newPath, hasContent, hasChildren);
21+
await generateForLanguage("en", entry, newPath, hasContent, hasChildren);
1922

20-
// 递归处理子条目(修复路径生成逻辑)
2123
if (hasChildren) {
2224
for (const child of entry.children) {
23-
// 仅在父级没有内容时才创建子目录
24-
const childPath = hasContent
25-
? currentPath // 保持当前路径
26-
: join(currentPath, entry.title.zh || entry.title.en);
27-
await processEntry(child, childPath);
25+
// 子条目继续在当前路径下生成
26+
await processEntry(child, newPath);
2827
}
2928
}
3029
}
3130

31+
// 修改 generateForLanguage 中的路径生成逻辑
3232
async function generateForLanguage(lang, entry, path, hasContent, hasChildren) {
33-
// 修复路径生成逻辑:移除多余的 "guide/docs" 拼接
34-
const langPath =
35-
hasChildren && !hasContent
36-
? join(path, lang, "guide/docs", entry.title[lang] || entry.title.en)
37-
: join(path, lang, "guide/docs");
38-
33+
// 保持路径生成逻辑不变
34+
const langPath = join(path, lang, "guide/docs");
3935
const title = entry.title[lang] || entry.title.en;
4036

37+
if (!hasContent && hasChildren) {
38+
const dirMetaPath = join(langPath, "_meta.json");
39+
const existingDirMeta = (await safeReadJson(dirMetaPath)) || [];
40+
41+
// 添加目录类型元数据
42+
const newDirMeta = existingDirMeta.concat({
43+
type: "dir",
44+
name: title,
45+
label: title,
46+
children: entry.children.map((c) => ({
47+
type: "file",
48+
name: c.title[lang] || c.title.en,
49+
label: c.title[lang] || c.title.en,
50+
})),
51+
});
52+
await writeFileWithDir(dirMetaPath, JSON.stringify(newDirMeta, null, 2));
53+
}
54+
4155
if (hasContent) {
42-
// 生成 .mdx 文件(文件扩展名需从 .md 改为 .mdx)
43-
const mdContent = await generateMarkdown(entry, lang);
44-
await writeFileWithDir(join(langPath, `${title}.mdx`), mdContent);
56+
// 只有当 entry.readme 存在时才复制 Markdown 文件
57+
if (entry.readme) {
58+
const sourcePath = join("Scripting Documentation", entry.readme, `${lang}.md`);
59+
const targetPath = join(langPath, `${title}.md`);
60+
await copyMarkdownFile(sourcePath, targetPath);
61+
}
4562

46-
// 更新 _meta.json
63+
// 更新父级元数据
4764
const metaPath = join(langPath, "_meta.json");
4865
const existingMeta = (await safeReadJson(metaPath)) || [];
4966

@@ -52,15 +69,8 @@ async function generateForLanguage(lang, entry, path, hasContent, hasChildren) {
5269
name: title,
5370
label: title,
5471
});
55-
5672
await writeFileWithDir(metaPath, JSON.stringify(newMeta, null, 2));
5773
}
58-
59-
// 处理子目录元数据
60-
if (!hasContent && hasChildren) {
61-
// 确保目录存在
62-
await writeFileWithDir(join(langPath, "_placeholder"), "");
63-
}
6474
}
6575

6676
// 新增辅助函数:安全读取 JSON
@@ -73,15 +83,16 @@ async function safeReadJson(path) {
7383
}
7484

7585
// 生成 Markdown 内容
86+
// 修改生成 Markdown 内容中的导入路径
7687
async function generateMarkdown(entry, lang) {
7788
let content = "---\n";
7889
content += `title: ${entry.title[lang]}\n`;
7990
if (entry.subtitle) content += `subtitle: ${entry.subtitle[lang]}\n`;
8091
content += "---\n\n";
8192

8293
if (entry.readme) {
83-
// 生成 MDX 导入语句
84-
const importPath = join("Scripting Documentation", entry.readme, `${lang}.mdx`);
94+
// 修改为相对路径引用
95+
const importPath = `./${entry.title[lang] || entry.title.en}.md`;
8596
content += `import Content from '${importPath}';\n\n`;
8697
content += "<Content />\n";
8798
}
@@ -100,6 +111,16 @@ async function writeFileWithDir(path, content) {
100111
await writeFile(path, content);
101112
}
102113

114+
// 辅助函数:复制 Markdown 文件
115+
async function copyMarkdownFile(sourcePath, targetPath) {
116+
try {
117+
const content = await Bun.file(sourcePath).text();
118+
await writeFileWithDir(targetPath, content);
119+
} catch (error) {
120+
console.error(`Error copying file: ${sourcePath}`, error);
121+
}
122+
}
123+
103124
// 主执行流程
104125
const docJson = JSON.parse(await Bun.file("./doc.json").text());
105126
await generateDocs(docJson, "./docs");

0 commit comments

Comments
 (0)