主题
为文档站点添加评论功能可以增强用户互动,收集反馈,构建社区。本教程对比三种主流评论方案,并详细讲解配置方法。
graph TD A[选择评论系统] --> B{目标用户} B -->|开发者/技术博客| C{GitHub 依赖} B -->|普通用户/企业| D{部署能力} C -->|已有 GitHub 项目| E[Giscus] C -->|不想依赖 GitHub| F[自托管 Waline] D -->|有服务器| F D -->|无服务器/零成本| G[Twikoo 云开发版本]
Giscus 基于 GitHub Discussions,是技术博客的首选方案。
进入仓库 Settings > Features,勾选 Discussions。
Settings > Features
Discussions
访问 giscus.app,填写仓库信息获取配置:
<script src="https://giscus.app/client.js" data-repo="your-username/your-repo" data-repo-id="R_xxxxx" data-category="Announcements" data-category-id="DIC_xxxxx" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="preferred_color_scheme" data-lang="zh-CN" crossorigin="anonymous" async> </script>
<!-- docs/.vitepress/components/GiscusComments.vue --> <script setup lang="ts"> import { useData } from 'vitepress' const { isDark } = useData() const giscusConfig = { repo: 'your-username/your-repo', repoId: 'R_xxxxx', category: 'Announcements', categoryId: 'DIC_xxxxx', mapping: 'pathname', reactionsEnabled: '1', inputPosition: 'top' as const, theme: 'preferred_color_scheme', lang: 'zh-CN' } </script> <template> <div class="giscus-container"> <script src="https://giscus.app/client.js" :data-repo="giscusConfig.repo" :data-repo-id="giscusConfig.repoId" :data-category="giscusConfig.category" :data-category-id="giscusConfig.categoryId" data-mapping="pathname" data-strict="0" :data-reactions-enabled="giscusConfig.reactionsEnabled" data-emit-metadata="0" :data-input-position="giscusConfig.inputPosition" :data-theme="isDark ? 'dark' : 'light'" :data-lang="giscusConfig.lang" crossorigin="anonymous" async /> </div> </template> <style scoped> .giscus-container { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--vp-c-divider); } </style>
// docs/.vitepress/theme/index.ts import DefaultTheme from 'vitepress/theme' import GiscusComments from './components/GiscusComments.vue' export default { extends: DefaultTheme, enhanceApp({ app }) { app.component('GiscusComments', GiscusComments) } }
<!-- docs/.vitepress/theme/Layout.vue --> <script setup lang="ts"> import DefaultTheme from 'vitepress/theme' import GiscusComments from './components/GiscusComments.vue' const { Layout } = DefaultTheme </script> <template> <Layout> <template #doc-after> <GiscusComments /> </template> </Layout> </template>
Waline 功能完善,适合需要完整评论管理后台的场景。
使用 MySQL/PostgreSQL/MongoDB:
# Docker 部署 docker run -d \ --name waline \ -p 8360:8360 \ -e MYSQL_HOST=db_host \ -e MYSQL_DB=waline \ -e MYSQL_USER=waline \ -e MYSQL_PASSWORD=password \ lizheming/waline
<!-- docs/.vitepress/components/WalineComments.vue --> <script setup lang="ts"> import { useData } from 'vitepress' import { init } from '@waline/client' import { onMounted, watch, ref } from 'vue' import '@waline/client/style' const { isDark } = useData() const walineRef = ref<HTMLElement>() const walineUrl = 'https://your-waline.vercel.app' let waline: ReturnType<typeof init> | null = null onMounted(() => { waline = init({ el: walineRef.value!, serverURL: walineUrl, lang: 'zh-CN', dark: 'html.dark', login: 'enable', meta: ['nick', 'mail', 'link'], requiredMeta: ['nick', 'mail'], pageSize: 10, imageUploader: false }) }) watch(isDark, () => { // Waline 自动响应主题变化 }) </script> <template> <div class="waline-container"> <div ref="walineRef" /> </div> </template> <style scoped> .waline-container { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--vp-c-divider); } </style>
npm install @waline/client
访问 https://your-waline.vercel.app/ui 进入管理后台,支持:
https://your-waline.vercel.app/ui
Twikoo 轻量级,适合个人博客,支持腾讯云开发零成本部署。
envId
# 或使用 CLI 部署 npm install tkserver npx tkserver deploy --envId your-env-id
# 克隆 Twikoo git clone https://github.com/twikoojs/twikoo.git cd twikoo # 部署到 Vercel vercel
<!-- docs/.vitepress/components/TwikooComments.vue --> <script setup lang="ts"> import { onMounted, ref } from 'vue' import { useData } from 'vitepress' const twikooEnvId = 'your-twikoo-env-id' onMounted(async () => { // 动态加载 Twikoo const twikoo = await import('twikoo') twikoo.init({ envId: twikooEnvId, el: '#twikoo-comment' }) }) </script> <template> <div class="twikoo-container"> <div id="twikoo-comment" /> </div> </template> <style scoped> .twikoo-container { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--vp-c-divider); } </style>
npm install twikoo
Waline 配置:
// 服务端环境变量 AKISMET_KEY=your-akismet-key // Akismet 反垃圾 TURING_TEST=true // 图灵测试
Twikoo 配置:
// 云函数配置 const config = { AKISMET_KEY: 'your-key', SCF_RECAPTCHA_TOKEN: 'your-token' }
<!-- docs/.vitepress/components/ConditionalComments.vue --> <script setup lang="ts"> import { useData } from 'vitepress' import GiscusComments from './GiscusComments.vue' const { frontmatter } = useData() </script> <template> <GiscusComments v-if="frontmatter.comments !== false" /> </template>
在 frontmatter 中禁用评论:
--- title: 关于页面 comments: false ---
Waline 兼容 Valine 数据,只需修改前端配置:
// Valine 配置 new Valine({ appId: 'xxx', appKey: 'xxx' }) // Waline 配置 init({ serverURL: 'https://your-waline.vercel.app' })
data-repo-id
data-category-id
检查 SMTP 配置:
SMTP_SERVICE=QQ SMTP_USER=your@qq.com SMTP_PASS=授权码 SITE_NAME=你的站点名 SITE_URL=https://your-site.com AUTHOR_EMAIL=你的邮箱
/* Waline 自定义样式 */ .waline-container { --waline-theme-color: var(--vp-c-brand-1); --waline-active-color: var(--vp-c-brand-2); --waline-font-size: 0.95rem; } /* Twikoo 自定义样式 */ .twikoo-container .tk-content { font-size: 0.95rem; line-height: 1.6; }
想要成为贡献者?
评论系统集成指南
为文档站点添加评论功能可以增强用户互动,收集反馈,构建社区。本教程对比三种主流评论方案,并详细讲解配置方法。
方案对比
方案选择建议
2
3
4
5
6
7
8
Giscus 配置
Giscus 基于 GitHub Discussions,是技术博客的首选方案。
前置条件
启用 Discussions
进入仓库
Settings > Features,勾选Discussions。自动配置
访问 giscus.app,填写仓库信息获取配置:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
创建 VitePress 组件
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
注册为全局组件
2
3
4
5
6
7
8
9
10
在布局中使用
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Waline 配置
Waline 功能完善,适合需要完整评论管理后台的场景。
部署方案
方案一:Vercel + LeanCloud(推荐)
方案二:独立数据库
使用 MySQL/PostgreSQL/MongoDB:
2
3
4
5
6
7
8
9
VitePress 集成
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
安装依赖
管理后台
访问
https://your-waline.vercel.app/ui进入管理后台,支持:Twikoo 配置
Twikoo 轻量级,适合个人博客,支持腾讯云开发零成本部署。
部署方案
方案一:腾讯云开发(免费)
envId2
3
方案二:Vercel 部署
2
3
4
5
6
VitePress 集成
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
安装依赖
进阶配置
评论通知
反垃圾策略
Waline 配置:
2
3
Twikoo 配置:
2
3
4
5
仅特定页面显示评论
2
3
4
5
6
7
8
9
10
11
在 frontmatter 中禁用评论:
2
3
4
迁移指南
从 Valine 迁移到 Waline
Waline 兼容 Valine 数据,只需修改前端配置:
2
3
4
5
6
7
8
9
10
从 Disqus 迁移
常见问题
Q: Giscus 评论不显示?
data-repo-id和data-category-id正确Q: Waline 邮件发送失败?
检查 SMTP 配置:
2
3
4
5
6
Q: 如何自定义评论样式?
2
3
4
5
6
7
8
9
10
11
12
下一步