完成帮助中心的改造

This commit is contained in:
chenjinyang
2026-01-26 13:49:43 +08:00
parent c878dc19d7
commit 4dd824d296
34 changed files with 1471 additions and 44 deletions

View File

@@ -0,0 +1,26 @@
const isWin = process.platform === 'win32';
// 如果是 windows 平台
if (isWin) {
const {dev: devScriptStr, build: buildScriptStr} = require('../package.json').scripts
const args = process.argv.slice(2)
const scriptType = args[0]
const fRed = "\x1b[31m"
const warnFn = (type) => {
console.log(fRed,
`\n[vdoing] 提示:由于您使用的是 windows 系统,请使用 ${type}:win 运行,否则运行失败。 \n`
)
process.exit(1)
}
// 当前运行的是dev脚本 且 脚本前端是'export'
if (scriptType === 'dev' && devScriptStr.startsWith('export')) {
warnFn('dev')
}
// 当前运行的是build脚本 且 脚本前端是'export'
if (scriptType === 'build' && buildScriptStr.startsWith('export')) {
warnFn('build')
}
}

View File

@@ -0,0 +1,14 @@
#批量添加和修改、删除front matter配置文件
# 需要批量处理的路径docs文件夹内的文件夹 (数组。映射路径docs/arr[0]/arr[1] ... )
path:
- docs # 第一个成员必须是docs
# 要删除的字段 (数组)
delete:
# - test
# - tags
# 要添加、修改front matter的数据 front matter中没有的数据则添加已有的数据则覆盖
data:
article: false

View File

@@ -0,0 +1,92 @@
/**
* 批量添加和修改front matter ,需要配置 ./config.yml 文件。
*/
const fs = require('fs'); // 文件模块
const path = require('path'); // 路径模块
const matter = require('gray-matter'); // front matter解析器 https://github.com/jonschlinkert/gray-matter
const jsonToYaml = require('json2yaml')
const yamlToJs = require('yamljs')
const inquirer = require('inquirer') // 命令行操作
const chalk = require('chalk') // 命令行打印美化
const readFileList = require('./modules/readFileList');
const { type, repairDate} = require('./modules/fn');
const log = console.log
const configPath = path.join(__dirname, 'config.yml') // 配置文件的路径
main();
/**
* 主体函数
*/
async function main() {
const promptList = [{
type: "confirm",
message: chalk.yellow('批量操作frontmatter有修改数据的风险确定要继续吗'),
name: "edit",
}];
let edit = true;
await inquirer.prompt(promptList).then(answers => {
edit = answers.edit
})
if(!edit) { // 退出操作
return
}
const config = yamlToJs.load(configPath) // 解析配置文件的数据转为js对象
if (type(config.path) !== 'array') {
log(chalk.red('路径配置有误path字段应该是一个数组'))
return
}
if (config.path[0] !== 'docs') {
log(chalk.red("路径配置有误path数组的第一个成员必须是'docs'"))
return
}
const filePath = path.join(__dirname, '..', ...config.path); // 要批量修改的文件路径
const files = readFileList(filePath); // 读取所有md文件数据
files.forEach(file => {
let dataStr = fs.readFileSync(file.filePath, 'utf8');// 读取每个md文件的内容
const fileMatterObj = matter(dataStr) // 解析md文件的front Matter。 fileMatterObj => {content:'剔除frontmatter后的文件内容字符串', data:{<frontmatter对象>}, ...}
let matterData = fileMatterObj.data; // 得到md文件的front Matter
let mark = false
// 删除操作
if (config.delete) {
if( type(config.delete) !== 'array' ) {
log(chalk.yellow('未能完成删除操作delete字段的值应该是一个数组'))
} else {
config.delete.forEach(item => {
if (matterData[item]) {
delete matterData[item]
mark = true
}
})
}
}
// 添加、修改操作
if (type(config.data) === 'object') {
Object.assign(matterData, config.data) // 将配置数据合并到front Matter对象
mark = true
}
// 有操作时才继续
if (mark) {
if(matterData.date && type(matterData.date) === 'date') {
matterData.date = repairDate(matterData.date) // 修复时间格式
}
const newData = jsonToYaml.stringify(matterData).replace(/\n\s{2}/g,"\n").replace(/"/g,"") + '---\r\n' + fileMatterObj.content;
fs.writeFileSync(file.filePath, newData); // 写入
log(chalk.green(`update frontmatter${file.filePath} `))
}
})
}

View File

@@ -0,0 +1,21 @@
// 类型判断
exports.type = function (o){
var s = Object.prototype.toString.call(o)
return s.match(/\[object (.*?)\]/)[1].toLowerCase()
}
// 修复date时区格式的问题
exports.repairDate = function (date) {
date = new Date(date);
return `${date.getUTCFullYear()}-${zero(date.getUTCMonth()+1)}-${zero(date.getUTCDate())} ${zero(date.getUTCHours())}:${zero(date.getUTCMinutes())}:${zero(date.getUTCSeconds())}`;
}
// 日期的格式
exports.dateFormat = function (date) {
return `${date.getFullYear()}-${zero(date.getMonth()+1)}-${zero(date.getDate())} ${zero(date.getHours())}:${zero(date.getMinutes())}:${zero(date.getSeconds())}`
}
// 小于10补0
function zero(d){
return d.toString().padStart(2,'0')
}

View File

@@ -0,0 +1,43 @@
/**
* 读取所有md文件数据
*/
const fs = require('fs'); // 文件模块
const path = require('path'); // 路径模块
const docsRoot = path.join(__dirname, '..', '..', 'docs'); // docs文件路径
function readFileList(dir = docsRoot, filesList = []) {
const files = fs.readdirSync(dir);
files.forEach( (item, index) => {
let filePath = path.join(dir, item);
const stat = fs.statSync(filePath);
if (stat.isDirectory() && item !== '.vuepress') {
readFileList(path.join(dir, item), filesList); //递归读取文件
} else {
if(path.basename(dir) !== 'docs'){ // 过滤docs目录级下的文件
const fileNameArr = path.basename(filePath).split('.')
let name = null, type = null;
if (fileNameArr.length === 2) { // 没有序号的文件
name = fileNameArr[0]
type = fileNameArr[1]
} else if (fileNameArr.length === 3) { // 有序号的文件
name = fileNameArr[1]
type = fileNameArr[2]
} else { // 超过两个‘.’的
log(chalk.yellow(`warning: 该文件 "${filePath}" 没有按照约定命名,将忽略生成相应数据。`))
return
}
if(type === 'md'){ // 过滤非md文件
filesList.push({
name,
filePath
});
}
}
}
});
return filesList;
}
module.exports = readFileList;