-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateMd.js
62 lines (55 loc) · 1.75 KB
/
createMd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// createBlogPost.mjs
import fs from 'fs';
import path from 'path';
import readline from 'readline';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
// 获取当前文件的目录路径
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// 创建 readline 接口来读取用户输入
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function formatDate(date) {
const options = { year: 'numeric', month: 'short', day: 'numeric' };
return date.toLocaleDateString('en-US', options).replace(/,/, '');
}
// 询问文件名和内容
rl.question('请输入博客文章的文件名(不包含扩展名):', (fileName) => {
rl.question('title:', (title) => {
rl.question('description:', (description) => {
// 定义文件路径
const dirPath = path.join(__dirname, 'src', 'content', 'blog');
const filePath = path.join(dirPath, `${fileName}.md`);
const dateStr = formatDate(new Date())
// 定义 Markdown 文件的内容
const fileContent = `---
title: "${title}"
description: "${description}"
pubDate: "${dateStr}"
heroImage: "/blog-placeholder-4.jpg"
---
`;
// 确保目录存在,如果不存在则创建
fs.mkdir(dirPath, { recursive: true }, (err) => {
if (err) {
console.error('创建目录时出错:', err);
rl.close();
return;
}
// 写入文件
fs.writeFile(filePath, fileContent, (err) => {
if (err) {
console.error('写入文件时出错:', err);
} else {
console.log(`文件已成功创建: ${filePath}`);
}
// 关闭 readline 接口
rl.close();
});
});
});
});
});