From de9280d9e72e3a4383777f86b8e8b68036e52865 Mon Sep 17 00:00:00 2001 From: Han <1655466387@qq.com> Date: Sat, 22 Feb 2025 18:28:14 +0800 Subject: [PATCH] =?UTF-8?q?=20=E6=96=B0=E5=A2=9E=20=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 ++- script/newpost.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 script/newpost.js diff --git a/package.json b/package.json index 5665edb..470d0a0 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "dev": "rm -rf .astro && astro dev", "build": "astro build", "preview": "astro preview", - "astro": "astro" + "astro": "astro", + "newpost": "node ./script/newpost.js" }, "dependencies": { "@astrojs/mdx": "^4.0.8", diff --git a/script/newpost.js b/script/newpost.js new file mode 100644 index 0000000..cdbfea8 --- /dev/null +++ b/script/newpost.js @@ -0,0 +1,41 @@ + +import path from 'path'; +import dayjs from 'dayjs'; +import crypto from 'crypto'; +import { fileURLToPath } from 'url'; +import { promises as fs } from 'fs'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +// 获取命令行参数 +const articleName = process.argv.slice(2).join(''); +const articleID = crypto.createHash('md5').update(String(dayjs().valueOf())).digest('hex'); +if (!articleName) { + console.error('请提供文章名称,例如:pnpm newpost "第一篇文章"'); + process.exit(1); +} +const ArticleContent = `--- +title: ${articleName} +categories: 分类 +tags: + - 标签 +id: "${articleID.slice(0, 16)}" +date: ${dayjs().format('YYYY-MM-DD HH:mm:ss')} +cover: "封面图URL (为空默认随机内置封面 /public/assets/images/banner)" +--- + +:::note +文章描述 +::: + +### 标题1 + +::btn[按钮]{link="链接" type="info"}`; +const init = async () => { + // 写文件 + const now = dayjs(); + const targetDir = path.join(__dirname, '../src/content/blog', `${now.year()}/${now.format('MM')}`); + await fs.mkdir(path.dirname(targetDir), { recursive: true }); + await fs.writeFile(path.join(targetDir, `${articleName}.md`), ArticleContent, 'utf8'); + console.log(`文章 ${articleName} 已创建`); +} +init(); \ No newline at end of file