Skip to content

Commit

Permalink
Optimize ci script normalize zh-CN lang file and config
Browse files Browse the repository at this point in the history
  • Loading branch information
ihavecoke committed Nov 3, 2023
1 parent d38d154 commit 4e9c569
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 78 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"scripts": {
"cp_feishu_pages_to_vitepress": "./scripts/cp_feishu_to_vitepress.sh",
"setup:vitepress:workspace": "./scripts/setup_vitepress_workspace.sh",
"export": "./scripts/export",
"autocort": "bun run autocorrect --fix locales/",
"translate": "node ./scripts/hk2cn.js",
"normalize:slug": "node ./scripts/normalize-docs-slug.js",
"build": "vitepress build",
"dev": "vitepress dev",
"clean:vitepress": "rm -rf .vitepress/dist && rm -rf docs/*",
"clean:export": "rm -rf dist/*",
"clean": "bun run clean:export && bun run clean:vitepress",
"local:build": "bun run export && bun run translate && bun run build",
"setup:vitepress": "bun run cp_feishu_pages_to_vitepress && bun run autocort && bun run translate"
"setup:vitepress": "bun run setup:vitepress:workspace && bun run autocort && bun run translate && bun run normalize:slug"
},
"dependencies": {
"autocorrect-node": "^2.8.4",
Expand Down
5 changes: 0 additions & 5 deletions scripts/cp_feishu_to_vitepress.sh

This file was deleted.

111 changes: 40 additions & 71 deletions scripts/hk2cn.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,66 @@
// Iter *.md in docs/zh-CN/**/*.md and use OpenCC to convert to docs/zh-HK/**/*.md
// Usage: node scripts/cn2hk.js

const fs = require('fs');
const path = require('path');
const glob = require('glob');
const OpenCC = require('opencc-js');

const converter = OpenCC.Converter({ from: 'hk', to: 'cn' });

function isFile(filePath) {
try {
const stats = fs.statSync(filePath);
return stats.isFile();
} catch (err) {
// An error occurred, which likely means the path doesn't exist or is not accessible.
return false;
}
}
/**
* Iterator all .md file in docs/zh-CN/ and convert to docs/zh-HK/
*/
const convert = () => {
// if(!fs.existsSync(path.resolve(__dirname,'../docs/zh-HK.md'))) return
// let content = fs.readFileSync(path.resolve(__dirname,'../docs/zh-HK.md'), 'utf8');
// let newContent = converter(content);
// fs.writeFileSync(path.resolve(__dirname,'../docs/zh-CN.md'), newContent);

// 翻译 CN to HK
let files = glob.sync(path.resolve(__dirname,'../locales/zh-HK/**/*.md'));
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const OpenCC = require("opencc-js");
const converter = OpenCC.Converter({ from: "hk", to: "cn" });

// 翻译 MD 文件
const convertMDHK2CN = () => {
console.log("--> Convert feishu pages markdown file to zh-CN");
let files = glob.sync(path.resolve(__dirname, "../locales/zh-CN/**/*.md"));
files.forEach((file) => {
let content = fs.readFileSync(file, 'utf8');
let newContent = converter(content);
let newFile = file.replace('zh-HK', 'zh-CN');
let folder = path.dirname(newFile);
fs.mkdirSync(folder, { recursive: true });
fs.writeFileSync(newFile, newContent);
let content = fs.readFileSync(file, "utf8");
fs.writeFileSync(file, converter(content));
});

// 复制非 md 文件
let assetsFiles = glob.sync(path.join(__dirname, '..', 'locales', 'zh-HK', '**', '!(*.md)'));
assetsFiles.forEach((file) => {
if(isFile(file)){
console.log('copy file:', file)
const destinationDirectory = file.replace('zh-HK', 'zh-CN').replace(path.basename(file), '');
fs.mkdirSync(destinationDirectory, { recursive: true });
fs.copyFileSync(file, file.replace('zh-HK', 'zh-CN'));
}
})
};

const deepConvertDocIntoZhCN = (doc) => {
const convertDocsContentHK2CN = (doc) => {
let newDoc = Object.assign({}, doc);

newDoc.title = converter(newDoc.title);
newDoc.slug = newDoc.slug.replace('zh-HK', 'zh-CN');
newDoc.filename = newDoc.filename.replace('zh-HK', 'zh-CN');
newDoc.slug = newDoc.slug.replace("zh-HK", "zh-CN");
if (newDoc.meta) {
newDoc.meta.slug = newDoc.meta.slug.replace("zh-HK", "zh-CN");
}
newDoc.filename = newDoc.filename.replace("zh-HK", "zh-CN");

if (newDoc.children) {
newDoc.children = newDoc.children.map((child) => {
return deepConvertDocIntoZhCN(child);
return convertDocsContentHK2CN(child);
});
}

return newDoc;
};

/**
* Parse docs.json and duplicate zh-HK to zh-CN by use OpenCC to convert
*/
// 翻译 docs.json
const convertDocsJSON = () => {
let docs = require('../locales/docs.json');
let newDocs = [];
let found = false;
console.log("--> Convert feishu pages docs data to zh-CN");
let docs = require("../feishu-pages/docs.json");
let newDocs = docs;

docs.forEach((doc) => {
doc.slug = doc.slug.replace('zh-HK', 'zh-CN');
if (doc.slug === 'zh-HK' && !found) {
newDocs.push(doc);
let newDoc = Object.assign({}, doc);
const HKDocs = docs.find((doc) => doc.slug === "zh-HK");
if (HKDocs) {
const { children: hkChildren, ...resetHKDocs } = HKDocs;

newDoc.title = '繁体中文';
const cnDocsChildren = [];
Array.from(Array.from(hkChildren)).forEach((child) => {
cnDocsChildren.push(convertDocsContentHK2CN(child));
});

newDoc = deepConvertDocIntoZhCN(newDoc);
const cnDocsReset = convertDocsContentHK2CN(resetHKDocs);
const cnDocs = {
...cnDocsReset,
children: cnDocsChildren,
title: "__简体中文__"
};

newDocs.push(newDoc);
found = true;
} else {
newDocs.push(doc);
}
});

fs.writeFileSync('locales/docs.json', JSON.stringify(newDocs, null, 2));
newDocs.push(cnDocs);
}
fs.writeFileSync("locales/docs.json", JSON.stringify(newDocs, null, 2));
};

convert();
convertMDHK2CN();
convertDocsJSON();
14 changes: 14 additions & 0 deletions scripts/normalize-docs-slug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require("path");
const glob = require("glob");
const fs = require("fs");

function normalizeDocsSlug() {
console.log('normalize all markdown file slug join /doc prefix')
const rawMDFiles = glob.sync(path.resolve(__dirname, "../locales/**/*.md"));
rawMDFiles.forEach((file) => {
let content = fs.readFileSync(file, "utf8");
fs.writeFileSync(file, content.replace(/slug: (zh-HK|zh-CN|en)\/(.*)/, "slug: $1/docs/$2"));
});
}

normalizeDocsSlug();
19 changes: 19 additions & 0 deletions scripts/setup_vitepress_workspace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# prepare vitepress workspace
mkdir -p locales/en/docs locales/zh-HK/docs locales/zh-CN/docs
cp -r feishu-pages/docs/en/* locales/en/docs/
cp -r feishu-pages/docs/zh-HK/* locales/zh-HK/docs/
cp -r feishu-pages/docs/zh-HK/* locales/zh-CN/docs/

# cp feishu pages meta config to vitepress workspace
cp feishu-pages/docs.json locales/

# 将 /docs/guides 默认成 /docs 的首页内容
cp locales/en/docs/guides.md locales/en/docs/index.md
cp locales/en/docs/guides.md locales/en/docs.md

cp locales/zh-CN/docs/guides.md locales/zh-CN/docs/index.md
cp locales/zh-CN/docs/guides.md locales/zh-CN/docs.md

cp locales/zh-HK/docs/guides.md locales/zh-HK/docs/index.md
cp locales/zh-HK/docs/guides.md locales/zh-HK/docs.md

0 comments on commit 4e9c569

Please sign in to comment.