@@ -17,53 +17,87 @@ const writeFile = (filePath, content) => {
1717const 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 配置并开始处理
76110const 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