配置步骤

1. 注册 GitHub OAuth Application

  1. 进入 GitHub Settings -> Developer settings -> OAuth Apps
  2. 点击 “New OAuth App”
  3. 填写相关信息:
    • Application name: 随意填写
    • Homepage URL: 博客地址
    • Authorization callback URL: 博客地址

2. 安装 Gitalk

1
npm install --save gitalk

3. 修改主题配置

在主题的 _config.yml 中添加:

1
2
3
4
5
6
7
8
gitalk:
enable: true
clientID: 'GitHub Application Client ID'
clientSecret: 'GitHub Application Client Secret'
repo: 'GitHub repo name'
owner: 'GitHub repo owner'
admin: ['GitHub repo owner and collaborators']
distractionFreeMode: false

4. 添加评论组件

在 themes/your-theme/layout/_partial/article.ejs 中添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<% if (theme.gitalk.enable && page.comments) { %>
<div id="gitalk-container"></div>
<script>
const gitalk = new Gitalk({
clientID: '<%= theme.gitalk.clientID %>',
clientSecret: '<%= theme.gitalk.clientSecret %>',
repo: '<%= theme.gitalk.repo %>',
owner: '<%= theme.gitalk.owner %>',
admin: ['<%= theme.gitalk.admin %>'],
id: location.pathname,
distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<% } %>

注意事项

  1. 安全性

    • 不要泄露 Client Secret
    • 谨慎设置 admin 权限
  2. 评论初始化

    • 首次需要管理员登录初始化
    • 每篇文章都需要单独初始化
  3. 常见问题

    • Error: Not Found
    • Error: Validation Failed
    • 评论无法加载

参考资料