主题
使用 Giscus 为文档网站添加基于 GitHub Discussions 的评论系统。
pengyanhua/full-stack-roadmap
访问 Giscus App 并安装到你的仓库。
访问 Giscus 配置页面:
Announcements
preferred_color_scheme
复制页面底部生成的配置参数。
npm install @giscus/vue
编辑 docs/.vitepress/theme/index.ts:
docs/.vitepress/theme/index.ts
import DefaultTheme from 'vitepress/theme' import Comment from './components/Comment.vue' import { h } from 'vue' export default { extends: DefaultTheme, Layout: () => { return h(DefaultTheme.Layout, null, { 'doc-after': () => h(Comment) }) } }
编辑 docs/.vitepress/theme/components/Comment.vue,填入从 Giscus 网站获取的配置:
docs/.vitepress/theme/components/Comment.vue
<template> <div class="comment-container"> <Giscus repo="pengyanhua/full-stack-roadmap" repo-id="你的仓库ID" category="Announcements" category-id="你的分类ID" mapping="pathname" strict="0" reactions-enabled="1" emit-metadata="0" input-position="top" theme="preferred_color_scheme" lang="zh-CN" loading="lazy" /> </div> </template>
npm run docs:dev
打开任意文档页面,底部应该显示评论区!
评论区会显示在每篇文档底部:
所有评论都在 GitHub Discussions 中管理:
https://github.com/pengyanhua/full-stack-roadmap/discussions
在特定页面的 frontmatter 中添加:
--- comment: false ---
然后更新 Comment.vue:
Comment.vue
<script setup> import { useData } from 'vitepress' import { computed } from 'vue' const { frontmatter } = useData() const showComment = computed(() => frontmatter.value.comment !== false) </script> <template> <div v-if="showComment" class="comment-container"> <!-- Giscus 组件 --> </div> </template>
在 Comment.vue 中添加:
<style scoped> .comment-container { margin-top: 4rem; padding: 2rem; background: var(--vp-c-bg-soft); border-radius: 8px; } </style>
如果未来需要更复杂的功能,可以考虑:
但对于大多数文档站点,Giscus 已经足够!
添加评论功能
使用 Giscus 为文档网站添加基于 GitHub Discussions 的评论系统。
为什么选择 Giscus?
配置步骤
1. 启用 GitHub Discussions
pengyanhua/full-stack-roadmap2. 安装 Giscus App
访问 Giscus App 并安装到你的仓库。
3. 获取配置参数
访问 Giscus 配置页面:
pengyanhua/full-stack-roadmapAnnouncements(推荐)preferred_color_scheme(自动适配)复制页面底部生成的配置参数。
4. 安装依赖
5. 更新主题配置
编辑
docs/.vitepress/theme/index.ts:6. 更新评论组件
编辑
docs/.vitepress/theme/components/Comment.vue,填入从 Giscus 网站获取的配置:7. 测试
打开任意文档页面,底部应该显示评论区!
效果预览
评论区会显示在每篇文档底部:
管理评论
所有评论都在 GitHub Discussions 中管理:
https://github.com/pengyanhua/full-stack-roadmap/discussions高级配置
按页面禁用评论
在特定页面的 frontmatter 中添加:
然后更新
Comment.vue:自定义样式
在
Comment.vue中添加:迁移到其他评论系统
如果未来需要更复杂的功能,可以考虑:
但对于大多数文档站点,Giscus 已经足够!