Giscus 配置速览:5 步启用评论系统


这篇文章只关注 Giscus 接入流程,不重复组件总览中的其他内容。

第 1 步:准备仓库

  • 确保仓库是公开仓库。
  • 在 GitHub 仓库设置中启用 Discussions。
  • 建议先创建一个用于评论的分类,例如 AnnouncementsGeneral

第 2 步:安装 Giscus App

完成后你会拿到以下关键值:

  • repoowner/repo
  • repoId:仓库 ID
  • category
  • categoryId

第 3 步:写入 consts.ts

src/consts.tsCOMMENTS 中填入配置:

export const COMMENTS = {
	enabled: true,
	provider: 'giscus',
	repo: 'kevynf/logflow-theme',
	repoId: '你的 repoId',
	category: 'Announcements',
	categoryId: '你的 categoryId',
	mapping: 'pathname',
	themeLight: 'light',
	themeDark: 'dark',
	lang: 'zh-CN',
};

第 4 步:确认文章页开启评论

当前主题在文章详情页通过布局参数控制评论显示:

<BlogPost {...post.data} enableComments={true}>
	<Content />
</BlogPost>

如果改成 false,即使 COMMENTS.enabledtrue 也不会渲染评论区。

第 5 步:常见问题排查

  • 页面显示“Comments are not configured yet”
    • 检查 reporepoIdcategoryId 是否为空。
  • 评论区加载失败或无内容
    • 检查仓库是否公开、Discussions 是否开启、分类是否匹配。
  • 主题切换后评论区颜色不正确
    • 检查 themeLight / themeDark 是否为 Giscus 支持的主题名。

可选参数建议

  • mapping
    • 推荐 pathname,按 URL 路径绑定讨论串。
    • 若希望按文章标题绑定,可使用 title
  • lang
    • 可设置 zh-CNen 等。
  • themeLight / themeDark
    • 可从 giscus 官方主题列表中选择,例如 lightdarktransparent_dark

Comments