这两天用xx云笔记写东西感觉不方便,限制两个登陆设备。所以简单搭了一个 Vitepress 的环境,用来写点东西,内容也能直接上传到 Git。
但是使用过程中创建文章还得加上连接忒麻烦,所以写了个脚本,跟大家分享一下,做个参考。
修改 config.mts
在 /docs/.vitepress 下创建 menu.json 文件夹
json
[
{
"text": "20250418-测试",
"link": "/20250418-测试"
}
]
修改 /docs/.vitepress/config.mts 文件
javascript
import Menu from "./menu.json"
export default defineConfig({
themeConfig: {
sidebar: [
{
text: 'Examples',
items: Menu
}
],
}
})
编辑脚本
创建 scripts/create-post.js 文件
javascript
const fs = require('fs');
const path = require('path');
const readline = require('readline');
// 获取命令行输入
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('请输入文章名: ', (articleName) => {
// 格式化日期和文章名
const date = new Date();
const year = date.getFullYear();
let month = date.getMonth() + 1; // 月份从0开始,所以需要加1
let day = date.getDate();
// 确保月份和日期是两位数
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
// 创建文件名
const fileName = `${year}${month}${day}-${articleName}`;
const filePath = path.join(__dirname, '../docs', `${fileName}.md`);
// 写入空文件
fs.writeFile(filePath, '', (err) => {
if (err) throw err;
console.log(`"${fileName}.md" 已经创建在 docs 文件夹下.`);
});
// 更新 menu.json 文件
const menuFilePath = path.join(__dirname, '../docs/.vitepress/menu.json');
fs.readFile(menuFilePath, 'utf8', (err, data) => {
if (err) throw err;
const menuItems = JSON.parse(data);
const newItem = { text: fileName, link: `/${fileName}` };
// 添加新项
menuItems.push(newItem);
// 将更新后的数据写回到 menu.json 文件
fs.writeFile(menuFilePath, JSON.stringify(menuItems, null, 2), 'utf8', (err) => {
if (err) throw err;
console.log(`新文章链接已添加到 menu.json 文件中.`);
});
});
rl.close();
});
给 package.json 加上这个指令脚本就行了
json
{
"scripts": {
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"create": "node scripts/create-post.js" // 新加指令
},
"devDependencies": {
"vitepress": "^1.6.3"
}
}

实测好使,折腾呗~