Skip to content

配置示例 ​

为了启用主题的所有功能(加密、草稿、隐藏、描述、信息等),还需要做一些配置。

.vitepress/config.ts
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]
});
公众号
关注 “只抄” 获取最新技术干货与动态
扫码关注扫码关注
优化线路
Evoxt

Evoxt

最便宜的亚太优化线路

马来西亚 电信 GIA / 联通 9929
优惠码:AFF2377-DEV

1C / 512MB / 5GB SSD / 150GB 流量

$3.32/月
优化线路
搬瓦工

搬瓦工

15 个机房自由切换

洛杉矶 DC6 机房
电信 / 联通 GIA + 移动 CMIN2

2C / 2GB / 40GB SSD / 1TB 流量

$49.99/季