-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
110 lines (101 loc) · 3.49 KB
/
build.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import fs from 'fs';
import path from 'path';
import jsYaml from "js-yaml";
const directoryPath = './articles';
import {marked} from "marked";
fs.mkdirSync('./public/articles/');
fs.mkdirSync('./public/articles/post/');
function generateRandomString(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let randomString = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
randomString += characters.charAt(randomIndex);
}
return randomString;
}
function clearFolder(folderPath) {
if (!fs.existsSync(folderPath)) {
console.log("Folder does not exist.");
return;
}
const files = fs.readdirSync(folderPath);
for (const file of files) {
const filePath = path.join(folderPath, file);
const fileStat = fs.statSync(filePath);
if (fileStat.isFile()) {
// 删除文件
fs.unlinkSync(filePath);
} else if (fileStat.isDirectory()) {
// 递归清空子文件夹
clearFolder(filePath);
// 删除空文件夹
fs.rmdirSync(filePath);
}
}
}
function parseCat(content){
const result = jsYaml.load(content)
if(result){
return result
}else{
return false
}
}
clearFolder('./public/articles/post/')
// Step 1: 获取目录中的文件列表
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error('无法读取目录:', err);
return;
}
const catFiles = files.filter((file) => path.extname(file) === '.md');
// Step 2: 获取每个文件的创建时间
const filesWithStats = catFiles.map((file) => {
// 现在,result是JavaScript对象,它包含XML文件的内容
// 您可以按需访问和处理对象中的数据
let content = fs.readFileSync('./articles/' + file, 'utf8')
const re = /---(.*?)---/gs
const t = re.exec(content)
const d = parseCat(t[1])
content = content.replace(t[0],'')
if(d !== false){
const filePath = path.join(directoryPath, file);
const fid = generateRandomString(8)
let md = marked.use({mangle: false, headerIds: false}).parse(content)
fs.writeFile('./public/articles/post/' + fid + '.cat', md,{}, (err) => {
if (err) {
console.error('无法写入文件:', err);
}
})
if(!('excerpt' in d)){
d['excerpt'] = md.replace(/(<([^>]+)>)/ig, '').slice(0, 50) + '...'
}
if('date' in d){
const date = d['date']
d['date'] = date.getTime()
}else{
const date = new Date()
d['date'] = date.getTime()
}
if('updated' in d){
const date = d['updated']
d['updated'] = date.getTime()
}else{
const date = new Date()
d['updated'] = date.getTime()
}
d['id'] = fid
return d
}
});
// Step 3: 使用创建时间对文件列表进行排序
filesWithStats.sort((a, b) => b.date - a.date);
fs.writeFile('./public/articles/index.json', JSON.stringify(filesWithStats),{}, (err) => {
if (err) {
console.error('无法写入文件:', err);
return;
}
console.log('OneCat->','文章索引已生成');
})
});