文章列表
文章列表由 usePosts 函数生成,用于获取博客的所有文章并生成文章列表,可以传入一个对象进行自定义配置,控制每页文章数、文章列表路径等。
配置项
| 参数名 | 默认值 | 备注 |
|---|---|---|
| pageSize | 10 | 每页文章数 |
| homepage | true | 是否将文章列表设置为主页 |
| srcDir | posts | 文章文件夹 |
| outDir | / | 文章列表、分类页 / 标签页 / 归档页输出路径 |
| lang | zh | 语言 |
| excerpt | 0 | 自动摘要,值为摘要字数 |
| permalink | posts | 永久链接前缀,填入 '' 空字符串可以关闭永久链接 |
| nav | false | 启用 VitePress 默认主题的上一页 / 下一页(自动添加 Frontmatter) |
| slot | / | 文章列表页插槽,支持 doc-before 与 doc-after |
| custom | / | 文章列表页自定义代码 |
示例代码
- posts:文章列表
- hiddenPosts:隐藏文章列表
- excludePosts:排除文章列表
- descriptionMap:文章描述 Map
- rewrites:路由重写数组
ts
const slot = `
<template #doc-after>
<AdItem :custom="ads" type="doc" />
</template>
`;
const custom = `
<script lang="ts" setup>
import AdItem from '/src/components/AdItem.vue';
import { ads } from '/.vitepress/theme/ads.ts';
</script>
`;
const { posts, hiddenPosts, excludePosts, descriptionMap, rewrites } = await usePosts({
pageSize: 10,
homepage: true,
srcDir: 'posts',
outDir: '',
lang: 'zh',
excerpt: 150,
permalink: 'posts',
nav: true,
slot,
custom
});


