diff --git a/.gitignore b/.gitignore index 1b13bf063..9b391c8fb 100644 --- a/.gitignore +++ b/.gitignore @@ -26,5 +26,4 @@ cache/ /src/client/shared.ts /src/node/shared.ts pnpm-global -TODOs.md -article-data.json \ No newline at end of file +TODOs.md \ No newline at end of file diff --git a/article.data.js b/article.data.js new file mode 100644 index 000000000..530924aea --- /dev/null +++ b/article.data.js @@ -0,0 +1,25 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import parseFrontmatter from 'gray-matter'; + +const excludedFiles = ['index.md', 'tags.md', 'archives.md', 'me.md']; + +export default { + watch: ['./docs/**/*.md'], + load(watchedFiles) { + // 排除不必要文件 + const articleFiles = watchedFiles.filter(file => { + const filename = path.basename(file); + return !excludedFiles.includes(filename); + }); + // 解析文章 Frontmatter + return articleFiles.map(articleFile => { + const articleContent = fs.readFileSync(articleFile, 'utf-8'); + const { data } = parseFrontmatter(articleContent); + return { + ...data, + path: articleFile.substring(articleFile.lastIndexOf('/docs/') + 6).replace(/\.md$/, ''), + } + }) + } +} \ No newline at end of file diff --git a/collect-article-data.mjs b/collect-article-data.mjs deleted file mode 100644 index fd82448fa..000000000 --- a/collect-article-data.mjs +++ /dev/null @@ -1,20 +0,0 @@ -import glob from 'fast-glob' -import matter from 'gray-matter' -import fs from 'node:fs/promises' - -const articleData = await Promise.all( - glob.sync('./docs/**/*.md', { - onlyFiles: true, - objectMode: true, - ignore: ['./docs/**/index.md', './docs/**/tags.md', './docs/**/archives.md', './docs/**/me.md'], // without ! - }).map(async (article) => { - const file = matter.read(`${article.path}`) - const { data, path } = file - return { - ...data, - path: path.replace(/\.md$/, '').replace('./docs/', '') - } - }) -) - -await fs.writeFile('./article-data.json', JSON.stringify(articleData), 'utf-8') \ No newline at end of file diff --git a/docs/.vitepress/theme/components/Archive.vue b/docs/.vitepress/theme/components/Archive.vue index 041b90449..36addb9e2 100644 --- a/docs/.vitepress/theme/components/Archive.vue +++ b/docs/.vitepress/theme/components/Archive.vue @@ -79,8 +79,8 @@