💬 Giscus 留言系统
基于 GitHub Discussions 的博客评论系统,无需数据库,简洁高效。
一、功能特性
| 特性 | 说明 |
|---|---|
| 🚀 零维护 | 使用 GitHub Discussions 存储评论,无需独立数据库 |
| 🔒 安全可靠 | 评论数据存储在你自己仓库的 Discussions 中 |
| 🎨 主题适配 | 自动跟随网站明暗主题切换 |
| 🌐 无服务器 | 纯前端嵌入,GitHub Pages 天然支持 |
| 💬 GitHub 登录 | 访客使用 GitHub 账号评论,便于社区互动 |
二、工作原理
访客评论 → Giscus 前端 → GitHub API → 创建/更新 Discussion
↓
访客查看 ← Giscus 前端 ← GitHub API ← Discussion 数据
- 访客在博客评论 → Giscus 调用 GitHub API
- 系统在仓库中创建对应的 Discussion(每个页面一个 Discussion)
- 评论数据存储在 GitHub Discussions 中
- 其他访客查看评论时,从 Discussions 读取并展示
三、启用条件
3.1 仓库要求
| 要求 | 说明 |
|---|---|
| 公开仓库 | Giscus 需要访客能读取 Discussions |
| 安装 Giscus App | 将 Giscus App 安装到你的仓库 |
| 开启 Discussions | 在仓库 Settings → Features 中开启 |
3.2 安装 Giscus App
- 打开 https://github.com/apps/giscus
- 点击 "Install"
- 选择 "Only select repositories"
- 选择你的博客仓库(如
mornikar/mornikar.github.io) - 点击 "Install"
3.3 开启 Discussions
- 进入仓库 Settings
- 找到 Features 部分
- 勾选 "Discussions"
- 点击 "Start discussions" 确认
四、获取配置参数
4.1 访问 Giscus 配置页
4.2 配置参数
| 参数 | 设置值 | 说明 |
|---|---|---|
| 仓库 | mornikar/mornikar.github.io |
你的博客仓库 |
| 映射关系 | pathname |
页面路径映射到 Discussion |
| 分类 | Announcements |
评论所在的分类 |
| 主题 | preferred_color_scheme |
跟随系统明暗 |
4.3 创建 Discussion 分类
- 在 Giscus 页面的「分类」下拉框中
- 点击 "Create a new category"
- 名称输入:
Announcements - 类型选择:**"Announcements"**(公告类型)
- 点击 "Create"
💡 推荐使用公告类型分类:这样只有仓库维护者和 Giscus 机器人能创建新 Discussion,访客只能在现有 Discussion 下评论,避免垃圾评论。
4.4 复制配置
配置完成后,页面会显示生成的代码,例如:
<script src="https://giscus.app/client.js"
data-repo="mornikar/mornikar.github.io"
data-repo-id="R_kgDOPto6vw"
data-category="Announcements"
data-category-id="DIC_kwDOPto6v84C7YeI"
data-mapping="pathname"
data-strict="0"
data-reactions-enabled="1"
data-emit-metadata="1"
data-input-position="top"
data-theme="preferred_color_scheme"
data-lang="zh-CN"
data-loading="lazy"
crossorigin="anonymous"
async>
</script>
记录以下三个关键参数:
data-repo-iddata-category-id
五、配置博客
5.1 修改主题配置
编辑 themes/arknights/_config.yml,找到 giscus 配置:
# Giscus
giscus:
enable: true
repo: 你的用户名/仓库名
repo_id: 从 giscus.app 获取的仓库 ID
category: Announcements
category_id: 从 giscus.app 获取的分类 ID
mapping: pathname
input_position: top
theme: preferred_color_scheme
lang: zh-CN
loading: lazy
5.2 配置参数说明
| 参数 | 可选值 | 说明 |
|---|---|---|
enable |
true / false |
开启/关闭评论 |
repo |
用户名/仓库名 |
GitHub 仓库 |
repo_id |
R_xxxxx |
仓库唯一标识 |
category |
分类名称 | Discussion 分类 |
category_id |
DIC_xxxxx |
分类唯一标识 |
mapping |
pathname / url / title |
页面映射方式 |
input_position |
top / bottom |
评论框位置 |
theme |
preferred_color_scheme / light / dark |
主题 |
5.3 主题配置示例
方案一:跟随网站明暗(推荐)
theme: preferred_color_scheme
方案二:固定亮色
theme: light
方案三:固定暗色
theme: dark
方案四:明暗分开配置
theme_light: light
theme_dark: dark_dimmed
六、推送更新
# 提交更改
git add themes/arknights/_config.yml
git commit -m "feat: 启用 Giscus 留言功能"
git push origin source
# CI 自动构建部署,2 分钟后生效
七、验证效果
- 打开博客任意文章
- 滚动到文章底部
- 应能看到 Giscus 评论区
- 点击评论框,使用 GitHub 账号登录
- 尝试发表一条测试评论
八、常见问题
Q1: 评论没有出现?
- 确认
giscus.enable: true - 检查浏览器 Console 是否有报错
- 确认 Giscus App 已安装到仓库
- 确认 Discussions 已开启
Q2: 如何自定义评论样式?
Giscus 支持自定义 CSS,通过 theme 参数可以加载自定义样式文件:
theme: https://your-domain.com/custom-giscus.css
Q3: 可以关闭某篇文章的评论吗?
在文章 frontmatter 中添加:
comments: false
Q4: 评论数据存在哪里?
评论存储在 GitHub Discussions 中,可以直接访问仓库的 Discussions 页面查看和管理。
九、进阶配置
9.1 启用评论反应
reactions_enabled: 1
9.2 输出元数据
emit_metadata: 1
9.3 严格标题匹配
strict: 1
9.4 限制访问域名
origin: https://your-domain.com
十、相关链接
| 资源 | 链接 |
|---|---|
| Giscus 官网 | https://giscus.app |
| Giscus GitHub | https://github.com/giscus/giscus |
| Discussions 管理 | https://github.com/你的用户名/仓库名/discussions |