配置示例
为了启用主题的所有功能(加密、草稿、隐藏、描述、信息等),还需要做一些配置。
ts
import { defineConfig } from 'vitepress';
import { usePosts } from '../src/composables/usePosts.ts';
import { installEncryptedMarkdown } from '../src/utils/encryptedMarkdown.ts';
import type { ThemeConfig } from '../src/types.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({
excerpt: 150,
slot,
custom
});
export default defineConfig<ThemeConfig>({
rewrites,
lastUpdated: true,
// 在 sitemap 中移除隐藏文章
sitemap: {
hostname: 'https://example.com',
transformItems: (items) => {
return items.filter((item) => !hiddenPosts.has(item.url.replace(/\.html$/, '')));
}
},
transformPageData(pageData) {
const { frontmatter, description } = pageData;
const { id } = frontmatter;
// 移除 Frontmatter 中的密码
delete frontmatter.password;
if (!description) {
pageData.description = descriptionMap.get(id) as string;
}
},
themeConfig: {
posts,
page: {
max: 5
},
classicCategory: false,
transition: true
},
markdown: {
lineNumbers: true,
config: (md) => {
// 文章信息
md.use((md) => {
md.renderer.rules.heading_close = (tokens, idx, options, env, slf) => {
let htmlResult = slf.renderToken(tokens, idx, options);
if (tokens[idx].tag === 'h1') htmlResult += `<PostMeta />`;
return htmlResult;
};
});
installEncryptedMarkdown(md);
}
},
// 草稿
srcExclude: [...excludePosts]
});


