mirror of
https://github.com/zhigang1992/react.git
synced 2026-03-26 22:42:51 +08:00
docs: support chinese
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const extractMetadata = require('extract-mdx-metadata')
|
||||
const metaLocales = require('./locales')
|
||||
const pagePrefix = path.join(__dirname, '../pages')
|
||||
const docsDir = path.join(__dirname, '../pages/docs')
|
||||
const targetPath = path.join(__dirname, '../lib/data/metadata.json')
|
||||
const weights = {
|
||||
'getting-started': 1,
|
||||
@@ -28,20 +28,51 @@ const getMetadata = async (files, parentPath) => {
|
||||
const url = filePath
|
||||
.replace(pagePrefix, '')
|
||||
.replace('.mdx', '')
|
||||
return { name: meta.title || file, url, index: meta.index || 100, meta }
|
||||
return { name: meta.title || file, url, index: meta.index || 100 }
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
try {
|
||||
const files = await fs.readdir(docsDir)
|
||||
const data = await getMetadata(files, docsDir)
|
||||
const sorted = data.sort((a, b) => {
|
||||
return weights[a.name] - weights[b.name]
|
||||
})
|
||||
const locales = (await fs.readdir(pagePrefix))
|
||||
.filter(name => {
|
||||
const fullPath = path.join(pagePrefix, name)
|
||||
if (name === 'docs') return false
|
||||
return fs.statSync(fullPath).isDirectory()
|
||||
})
|
||||
|
||||
const sortdMetaData = await Promise.all(locales.map(async name => {
|
||||
const currentLocale = metaLocales[name] || {}
|
||||
const dir = path.join(pagePrefix, name)
|
||||
const childDirs = await fs.readdir(dir)
|
||||
const data = await getMetadata(childDirs, dir)
|
||||
const sorted = data
|
||||
.sort((a, b) => weights[a.name] - weights[b.name])
|
||||
.map(item => {
|
||||
const localeName = currentLocale[item.name]
|
||||
if (!localeName) return item
|
||||
return {
|
||||
...item,
|
||||
localeName: localeName
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
name,
|
||||
content: sorted
|
||||
}
|
||||
}))
|
||||
|
||||
const jsonData = sortdMetaData.reduce((pre, current) => {
|
||||
return {
|
||||
...pre,
|
||||
[current.name]: current.content,
|
||||
}
|
||||
}, [])
|
||||
|
||||
await fs.ensureFile(targetPath)
|
||||
await fs.writeJson(targetPath, sorted)
|
||||
await fs.writeJson(targetPath, jsonData)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
process.exit(1)
|
||||
|
||||
10
scripts/locales.js
Normal file
10
scripts/locales.js
Normal file
@@ -0,0 +1,10 @@
|
||||
module.exports = {
|
||||
'zh-cn': {
|
||||
'getting-started': '快速开始',
|
||||
customization: '定制化',
|
||||
components: '所有组件',
|
||||
},
|
||||
'en-us': {
|
||||
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user