|
| 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(); |
0 commit comments