跳轉到內容

VitePress 添加 Giscu評論

安裝

sh
npm i vitepress-plugin-comment-with-giscus
sh
pnpm i vitepress-plugin-comment-with-giscus
sh
yarn add vitepress-plugin-comment-with-giscus

創建 index 文件

.vitepress/theme 下創建index.js 或者index.ts文件

配置 index.ts

giscus.ts
ts
// .vitepress/theme/index.js
import { useData, useRoute } from 'vitepress'
import giscusTalk from 'vitepress-plugin-comment-with-giscus'
import DefaultTheme from 'vitepress/theme'

export default {
  ...DefaultTheme,
  enhanceApp(ctx) {
    DefaultTheme.enhanceApp(ctx)
    // ...
  },
  setup() {
    // 获取前言和路由
    const { frontmatter } = useData()
    const route = useRoute()

    // 评论组件 - https://giscus.app/
    giscusTalk(
      {
        repo: 'xxxxxx/xxxxxx', //仓库地址
        repoId: 'xxxxxx', //仓库ID
        category: 'General', // 默认: `General`
        categoryId: 'xxxxxx', //分类ID
        mapping: 'title', // 默认: `pathname`
        strict: '0',
        reactionsEnabled: '0',
        emitMetadata: '0',
        inputPosition: 'top', // 默认: `top`
        lang: 'zh-CN' // 默认: `zh-CN`
      },
      {
        frontmatter,
        route
      }
    )
  }
}

giscus 獲取參數

giscus 配置獲取

拓展使用

如果文章前言添加以下代碼,則不會生成評論區

md
---
comment: false
---

最後更新於: