From b8708ac29744abc5fd12cd4a70d61c7a6c607ea4 Mon Sep 17 00:00:00 2001 From: wanzhongyun Date: Wed, 16 Oct 2024 10:36:09 +0800 Subject: [PATCH] feat: mapping zh folder --- docusaurus/package.json | 2 +- docusaurus/script/buildAfter.js | 63 +++++++++++++++++++++++ docusaurus/script/buildBefore.js | 74 +++++++++++++++++++++++++++ docusaurus/script/folderMappings.json | 19 +++++++ 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 docusaurus/script/buildAfter.js create mode 100644 docusaurus/script/buildBefore.js create mode 100644 docusaurus/script/folderMappings.json diff --git a/docusaurus/package.json b/docusaurus/package.json index 11b756f9d4..d08ede0fdf 100644 --- a/docusaurus/package.json +++ b/docusaurus/package.json @@ -5,7 +5,7 @@ "scripts": { "docusaurus": "docusaurus", "start": "docusaurus start", - "build": "docusaurus build", + "build": "node script/buildBefore.js && docusaurus build && node script/buildAfter.js", "swizzle": "docusaurus swizzle", "deploy": "docusaurus deploy", "clear": "docusaurus clear", diff --git a/docusaurus/script/buildAfter.js b/docusaurus/script/buildAfter.js new file mode 100644 index 0000000000..6f6b3e710d --- /dev/null +++ b/docusaurus/script/buildAfter.js @@ -0,0 +1,63 @@ +/** + * file: build 后根据 foldMapping 重命名中文文档 + * author: Allen +*/ + +const fs = require('fs'); +const path = require('path'); + +// 读取文件夹映射的 JSON 文件 +const folderMappingsPath = path.resolve(__dirname, 'folderMappings.json'); +let folderMappings = {}; + +if (fs.existsSync(folderMappingsPath)) { + const data = fs.readFileSync(folderMappingsPath, 'utf-8'); + folderMappings = JSON.parse(data); +} + +// 递归函数用于遍历目录 +function renameDirectories(dirPath) { + if (!fs.existsSync(dirPath)) { + console.error(`Directory does not exist: ${dirPath}`); + return; + } + + const items = fs.readdirSync(dirPath, { withFileTypes: true }); + + for (const item of items) { + const itemPath = path.join(dirPath, item.name); + + if (item.isDirectory()) { + console.log(`Checking directory: ${item.name}`); + + // 检查是否在 folderMappings 中 + if (folderMappings.hasOwnProperty(item.name)) { + const newDirName = path.join(dirPath, folderMappings[item.name]); + + console.log(`Attempting to rename ${itemPath} to ${newDirName}`); + + // 重命名目录 + if (itemPath !== newDirName) { + try { + fs.renameSync(itemPath, newDirName); + console.log(`Directory renamed from ${itemPath} to ${newDirName}`); + // 更新 itemPath 以继续递归检查子目录 + renameDirectories(newDirName); + } catch (err) { + console.error(`Failed to rename ${itemPath} to ${newDirName}:`, err); + } + } else { + // 如果未重命名,继续递归检查子目录 + renameDirectories(itemPath); + } + } else { + // 递归检查子目录 + renameDirectories(itemPath); + } + } + } +} + +// 开始遍历 build/zh 目录 +const rootDir = path.resolve(__dirname, '../build/zh'); +renameDirectories(rootDir); diff --git a/docusaurus/script/buildBefore.js b/docusaurus/script/buildBefore.js new file mode 100644 index 0000000000..0825c958da --- /dev/null +++ b/docusaurus/script/buildBefore.js @@ -0,0 +1,74 @@ +/** + * file: build 前生成文档的映射文件 + * author: Allen +*/ + +const fs = require('fs'); +const path = require('path'); + +// 定义要遍历的根目录 +const rootDir = path.join(__dirname, '../../docs/zh-CN/source'); + +// 创建一个对象来存储映射 +const folderMappings = {}; + +// 递归读取目录中的文件夹 +const readDirectories = (dirPath) => { + fs.readdir(dirPath, (err, items) => { + if (err) { + console.error('无法读取目录', err); + return; + } + + // 遍历每个项目 + items.forEach((item) => { + const itemPath = path.join(dirPath, item); + + // 检查是否为文件夹 + fs.stat(itemPath, (err, stats) => { + if (err) { + console.error('无法读取文件夹信息', err); + return; + } + + if (stats.isDirectory()) { + // 检查 index.rst 文件是否存在 + const indexPath = path.join(itemPath, 'index.rst'); + fs.access(indexPath, fs.constants.F_OK, (err) => { + if (!err) { + // 读取 index.rst 文件的第一行 + fs.readFile(indexPath, 'utf8', (err, data) => { + if (err) { + console.error('无法读取文件', indexPath, err); + return; + } + + const firstLine = data.split('\n')[0].trim(); + const cleanKey = item.replace(/^\d+\./, ''); // 去掉前面的数字和点 + folderMappings[cleanKey] = firstLine; + }); + } + + // 递归读取子文件夹 + readDirectories(itemPath); + }); + } + }); + }); + }); +}; + +// 开始读取目录 +readDirectories(rootDir); + +// 输出文件夹映射到 JSON 文件 +setTimeout(() => { + const outputPath = path.join(__dirname, 'folderMappings.json'); + fs.writeFile(outputPath, JSON.stringify(folderMappings, null, 2), 'utf8', (err) => { + if (err) { + console.error('无法写入 JSON 文件', err); + } else { + console.log('文件夹名称映射已写入', outputPath); + } + }); +}, 3000); // 设置延时以确保所有读取操作完成 diff --git a/docusaurus/script/folderMappings.json b/docusaurus/script/folderMappings.json new file mode 100644 index 0000000000..85b36f5330 --- /dev/null +++ b/docusaurus/script/folderMappings.json @@ -0,0 +1,19 @@ +{ + "permission": "运维与权限管理", + "quality": "测试与质量保障", + "contributor-manual": "贡献者文档", + "best-practices": "最佳实践", + "introduction": "TuGraph入门", + "user-guide": "可视化操作指南", + "utility-tools": "实用工具", + "installation&running": "安装和运行", + "quick-start": "快速上手", + "query": "查询语言", + "client-tools": "客户端工具", + "olap&procedure": "存储过程和分析接口", + "characteristics": "产品特点", + "demo": "demo示例", + "procedure": "存储过程", + "learn": "图学习", + "olap": "分析接口" +} \ No newline at end of file