Docs(locales): add chinese locale support (#2772)

This commit is contained in:
a76yyyy
2023-07-14 22:20:15 +08:00
committed by GitHub
parent 24186a488a
commit c7e34bdc11
33 changed files with 2636 additions and 97 deletions

View File

@ -1,73 +1,9 @@
import { defineConfig } from 'vitepress'
import directoryTree from 'directory-tree'
import fs from 'fs'
import metadataParser from 'markdown-yaml-metadata-parser'
function getMetadataFromDoc(path: string): { sidebarTitle?: string, sidebarOrder?: number } {
const fileContents = fs.readFileSync(path, 'utf8')
return metadataParser(fileContents).metadata
}
function generateSidebarChapter(chapterDirName: string): any {
const chapterPath = `./${chapterDirName}`
const tree = directoryTree(chapterPath)
if (!tree || !tree.children) {
console.error(tree)
throw new Error(`Could not genereate sidebar: invalid chapter at ${chapterPath}`)
}
let items: { sidebarOrder: number, text: string, link: string }[] = []
// Look into files in the chapter
for (const doc of tree.children) {
// make sure it's a .md file
if (doc.children || !doc.name.endsWith('.md'))
continue
const { sidebarOrder, sidebarTitle } = getMetadataFromDoc(doc.path)
if (!sidebarOrder)
throw new Error('Cannot find sidebarOrder in doc metadata: ' + doc.path)
if (!sidebarTitle)
throw new Error('Cannot find sidebarTitle in doc metadata: ' + doc.path)
items.push({
sidebarOrder,
text: sidebarTitle,
link: "/" + doc.path
})
}
items = items.sort((a, b) => a.sidebarOrder - b.sidebarOrder)
// remove dash and capitalize first character of each word as chapter title
const text = chapterDirName.split('-').join(' ').replace(/\b\w/g, l => l.toUpperCase())
return {
text,
collapsed: false,
items,
}
}
const chapters = [
'introduction',
'configuration',
'premium',
'runtime',
'advanced-usages',
].map(generateSidebarChapter)
// Override index page link
chapters[0]['items'][0]['link'] = '/'
import locales from './locales'
// https://vitepress.dev/reference/site-config
export default defineConfig({
title: 'Clash',
description: 'Rule-based Tunnel',
base: '/clash/',
@ -78,37 +14,34 @@ export default defineConfig({
],
],
locales: locales.locales,
lastUpdated: true,
themeConfig: {
outline: 'deep',
search: {
provider: 'local'
},
provider: 'local',
options: {
locales: {
zh_CN: {
translations: {
button: {
buttonText: '搜索文档',
buttonAriaLabel: '搜索文档'
},
modal: {
noResultsText: '无法找到相关结果',
resetButtonTitle: '清除查询条件',
footer: {
selectText: '选择',
navigateText: '切换'
}
}
}
}
},
editLink: {
pattern: 'https://github.com/Dreamacro/clash/edit/master/docs/:path',
text: 'Edit this page on GitHub'
},
logo: '/logo.png',
// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: 'Home', link: '/' },
{ text: 'Configuration', link: '/configuration/configuration-reference' },
{
text: 'Download',
items: [
{ text: 'Open-source Edition', link: 'https://github.com/Dreamacro/clash/releases/' },
{ text: 'Premium Edition', link: 'https://github.com/Dreamacro/clash/releases/tag/premium' },
]
}
],
socialLinks: [
{ icon: 'github', link: 'https://github.com/Dreamacro/clash' },
],
sidebar: chapters
}
}
},
})