From 13e635465ce79ac2eb0c1658308e039c2d377ef9 Mon Sep 17 00:00:00 2001 From: Frost-ZX Date: Tue, 7 Oct 2025 18:27:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/.vitepress/config.mjs | 2 +- docs/.vitepress/content-list.data.mts | 117 ++++++++++++++++++++++++++ docs/.vitepress/content-list.json | 1 + docs/index.md | 19 +++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 docs/.vitepress/content-list.data.mts create mode 100644 docs/.vitepress/content-list.json diff --git a/docs/.vitepress/config.mjs b/docs/.vitepress/config.mjs index bbf1e70..d55952a 100644 --- a/docs/.vitepress/config.mjs +++ b/docs/.vitepress/config.mjs @@ -26,7 +26,7 @@ export default defineConfig({ logo: '/favicon.png', nav: [ { text: '主页', link: '/' }, - { text: '归档', link: '/archives' }, + // { text: '归档', link: '/archives' }, { text: '关于', link: '/about' }, { text: '留言板', link: '/comments' }, { text: '友情链接', link: '/links' }, diff --git a/docs/.vitepress/content-list.data.mts b/docs/.vitepress/content-list.data.mts new file mode 100644 index 0000000..d08d280 --- /dev/null +++ b/docs/.vitepress/content-list.data.mts @@ -0,0 +1,117 @@ +import { createContentLoader } from 'vitepress'; + +import contentList from './content-list.json'; + +/** 文章信息列表 */ +type ContentList = { + 'created-at': string; + 'is-hide': string; + 'slug': string; + 'title': string; + 'updated-at': string; + 'url': string; +}[]; + +/** 文章信息列表,按日期分类 */ +type ContentListByDateArray = { + year: string; + items: { + month: string; + items: ContentList; + }[]; +}[]; + +/** 文章信息列表,按日期分类(格式:`{ 年: { 月: [] } }`)*/ +type ContentListByDateObject = Record>; + +/** 文章信息列表,按 slug 分类 */ +type ContentListBySlug = Record + +let contentListFull: ContentList = JSON.parse(JSON.stringify(contentList)); +let contentListByDateArray: ContentListByDateArray = []; +let contentListByDateObject: ContentListByDateObject = {}; +let contentListBySlug: ContentListBySlug = {}; + +for (let i = 0; i < contentListFull.length; i++) { + + let item = contentListFull[i]; + let date = new Date(item['created-at']); + let year = String(date.getFullYear()); + let month = String(date.getMonth() + 1); + + contentListBySlug[item.slug] = item; + + // 初始化年数据 + if (!contentListByDateObject[year]) { + contentListByDateObject[year] = {}; + } + + // 初始化月数据 + if (!contentListByDateObject[year][month]) { + contentListByDateObject[year][month] = []; + } + + // 放到对应的月数据中 + contentListByDateObject[year][month].push(item); + +} + +// 将 contentListByDateObject 数据转换为数组 +for (let yearStr in contentListByDateObject) { + + let yearList = contentListByDateObject[yearStr]; + let children: ContentListByDateArray[number]['items'] = []; + + contentListByDateArray.push({ + year: yearStr, + items: children, + }); + + for (let monthStr in yearList) { + children.push({ + month: monthStr, + items: yearList[monthStr], + }); + } + + // 按月份降序排序 + children.sort((a, b) => { + return Number(b.month) - Number(a.month); + }); + +} + +// 按年份降序排序 +contentListByDateArray.sort((a, b) => { + return Number(b.year) - Number(a.year); +}); + +export default createContentLoader('content/*.md', { + excerpt: false, + includeSrc: false, + render: false, + transform(rawData) { + + if (rawData.length !== Object.keys(contentListBySlug).length) { + throw new Error('文章信息列表数据不完整'); + } + + // 提取信息 + rawData.forEach((item) => { + + let slug = item.url.replace(/(^\/content\/|\.html$)/g, ''); + let data = contentListBySlug[slug]; + + // 更新数据 + if (data) { + data.url = item.url; + } + + }); + + return { + contentListByDate: contentListByDateArray, + }; + + }, +}); diff --git a/docs/.vitepress/content-list.json b/docs/.vitepress/content-list.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/docs/.vitepress/content-list.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 2ef8588..9916cf4 100644 --- a/docs/index.md +++ b/docs/index.md @@ -8,3 +8,22 @@ hero: text: "" tagline: 一个分享技术、资源,记录学习与生活的博客。 --- + + + +