Skip to content

Commit

Permalink
feat: mapping zh folder
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyunWan committed Oct 16, 2024
1 parent 1b8fa15 commit b8708ac
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 63 additions & 0 deletions docusaurus/script/buildAfter.js
Original file line number Diff line number Diff line change
@@ -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);
74 changes: 74 additions & 0 deletions docusaurus/script/buildBefore.js
Original file line number Diff line number Diff line change
@@ -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); // 设置延时以确保所有读取操作完成
19 changes: 19 additions & 0 deletions docusaurus/script/folderMappings.json
Original file line number Diff line number Diff line change
@@ -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": "分析接口"
}

0 comments on commit b8708ac

Please sign in to comment.