diff --git a/package.json b/package.json index 42783a48..6528cc1f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/cp_feishu_to_vitepress.sh b/scripts/cp_feishu_to_vitepress.sh deleted file mode 100755 index e8a9ba9c..00000000 --- a/scripts/cp_feishu_to_vitepress.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -mkdir -p locales/en/docs locales/zh-HK/docs -cp -r feishu-pages/docs/en/* locales/en/docs/ -cp -r feishu-pages/docs/zh-HK/* locales/zh-HK/docs/ -cp feishu-pages/docs.json locales/ diff --git a/scripts/hk2cn.js b/scripts/hk2cn.js index efc0c83e..683e4cb8 100755 --- a/scripts/hk2cn.js +++ b/scripts/hk2cn.js @@ -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(); diff --git a/scripts/normalize-docs-slug.js b/scripts/normalize-docs-slug.js new file mode 100644 index 00000000..b529a1ee --- /dev/null +++ b/scripts/normalize-docs-slug.js @@ -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(); diff --git a/scripts/setup_vitepress_workspace.sh b/scripts/setup_vitepress_workspace.sh new file mode 100755 index 00000000..f31adf74 --- /dev/null +++ b/scripts/setup_vitepress_workspace.sh @@ -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