Vitepress利用createContentLoader配置归档功能
借助createContentLoader
,我们能轻松在Vitepress中实现博客文章的归档功能,具体配置如下:
按时间归档配置
按年份归档
要实现按年份对博客文章进行归档,可在createContentLoader
中对文章数据进行处理:
javascript
const loader = createContentLoader('path/to/your/blog/posts/*.md', {
transform: (posts) => {
const archiveByYear = {};
posts.forEach((post) => {
const year = new Date(post.frontmatter.date).getFullYear();
if (!archiveByYear[year]) {
archiveByYear[year] = [];
}
archiveByYear[year].push(post);
});
return {
archiveByYear
};
}
})