-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildPage.js
55 lines (46 loc) · 1.51 KB
/
buildPage.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
import postList from './src/postList.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// 获取当前文件所在目录
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function formatTitle(variableName) {
return variableName
.split('-')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join('-');
}
// 创建目录
function makePageDir(filePath) {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
makePageDir(dirname);
fs.mkdirSync(dirname);
}
function generateAstroFile(variableName) {
const capitalizedVariableName = formatTitle(variableName);
const dirPath = path.join(__dirname, `./src/pages/${variableName}`);
const filePath = path.join(dirPath, `index.astro`);
makePageDir(filePath);
const content = `---
import BlogLayout from "../../layouts/Blog.astro";
import Code from "./${variableName}_code.md";
import Comment from "./${variableName}_comment.md";
import Head from "./${variableName}_combined_comments.md";
---
<BlogLayout title="Nodejs by Example: ${capitalizedVariableName}">
<span slot="title"> ${capitalizedVariableName}</span>
<Head slot="head" />
<Comment slot="comment" />
<Code slot="code" />
</BlogLayout>
`;
fs.writeFileSync(filePath, content, 'utf8');
console.log(`File ${filePath} has been generated.`);
}
// 循环遍历postList并为每一项调用generateAstroFile
postList.forEach(post => {
generateAstroFile(post.url);
});