-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-tags.js
66 lines (53 loc) · 1.9 KB
/
generate-tags.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
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const yaml = require("js-yaml");
const getReadingTime = require("./docs/.vitepress/helpers/getReadingTime.js")
const base = '/knowledge-base'
const generateTags = () => {
const tagsSet = new Set();
const articles = [];
const files = glob.sync(path.join(__dirname, "docs/src/**/*.md")); // 修改为实际路径
files.forEach((file) => {
// 跳过自定义页面(不算做文章)
if (file.includes("custom-pages")) {
return;
}
const content = fs.readFileSync(file, "utf8");
// 将所有tab字符替换为4个空格
const contentWithoutTabs = content.replace(/\t/g, " ");
const match = contentWithoutTabs.match(/^---\n([\s\S]*?)\n---/);
if (match) {
try {
const frontMatter = yaml.load(match[1]);
const filePath = file.replace(path.join(__dirname, "docs"), "");
// 从内容中提取标题
const titleMatch = contentWithoutTabs.match(/^#\s+(.+)/m);
const title = titleMatch ? titleMatch[1] : "Untitled";
if (frontMatter.tags) {
frontMatter.tags.forEach((tag) => tagsSet.add(tag));
articles.push({
path: base + filePath.replace(/\.md$/, ""),
title,
tags: frontMatter.tags,
publishDate: frontMatter.publishDate,
timeToRead: getReadingTime(content)
});
}
} catch (e) {
console.error(`Error parsing YAML front matter in file: ${file}`, e);
}
}
});
const tagsArray = Array.from(tagsSet).sort();
const data = { tags: tagsArray, articles };
const outputDir = path.join(__dirname, "docs/src/custom-pages/tag-page");
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(
path.join(outputDir, "tags.json"),
JSON.stringify(data, null, 2)
);
};
generateTags();