From 0008e6e73fda50638a66d5d4ec10b0a1c513ee64 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sun, 12 Mar 2023 15:06:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=9A=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=20data=20loaders=20=E9=87=8D=E6=9E=84=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=89=80=E6=9C=89=E6=96=87=E7=AB=A0=E6=95=B0=E6=8D=AE=EF=BC=88?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E8=AF=B7=E5=8F=82=E9=98=85=EF=BC=9Avuejs/vit?= =?UTF-8?q?epress#96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +-- article.data.js | 25 ++++++++++++++++++++ collect-article-data.mjs | 20 ---------------- docs/.vitepress/theme/components/Archive.vue | 2 +- docs/.vitepress/theme/components/Tag.vue | 2 +- package.json | 6 ++--- 6 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 article.data.js delete mode 100644 collect-article-data.mjs 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 @@