Gitalk简介

Gitalk 是一个基于GitHub Issue的评论插件。

项目地址:https://github.com/gitalk/gitalk

特点:

  • 使用 GitHub 登录
  • 兼容PC 移动端
  • 集成简单

新建gitalk.swig

在next主题/layout/_third-party/comments/目录中新建gitalk.swig文件,并添加内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{% if page.comments && theme.gitalk.enable %}
<link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
<script type="text/javascript">
var gitalk = new Gitalk({
clientID: '{{ theme.gitalk.ClientID }}',
clientSecret: '{{ theme.gitalk.ClientSecret }}',
repo: '{{ theme.gitalk.repo }}',
owner: '{{ theme.gitalk.githubID }}',
admin: ['{{ theme.gitalk.adminUser }}'],
id: location.pathname,
distractionFreeMode: '{{ theme.gitalk.distractionFreeMode }}'
})
gitalk.render('gitalk-container')
</script>
{% endif %}

修改comments.swig

修改/layout/_partials/comments.swig,添加内容如下,与前面的elseif同一级别上:

1
2
3
{% elseif theme.gitalk.enable %}
<div id="gitalk-container"></div>

修改index.swig

修改layout/_third-party/comments/index.swig,在最后一行添加内容:

1
{% include 'gitalk.swig' %}

新建gitalk.styl

新建/source/css/_common/components/third-party/gitalk.styl文件,添加内容:

1
2
3
4
.gt-header a, .gt-comments a, .gt-popup a
border-bottom: none;
.gt-container .gt-popup .gt-action.is--active:before
top: 0.7em;

third-party.styl

修改/source/css/_common/components/third-party/third-party.styl,在最后一行上添加内容,引入样式:

1
@import "gitalk";

配置_config.yml

在主题配置文件next/_config.yml中添加如下内容:

1
2
3
4
5
6
7
8
gitalk:
enable: true
githubID: github帐号
repo: 仓库名称
ClientID: Client ID
ClientSecret: Client Secret
adminUser: github帐号
distractionFreeMode: true

申请github应用

在GitHub上注册应用,https://github.com/settings/applications/new

参数说明:

  • Application name: # 应用名称,随便填写
  • Homepage URL: # 你博客部署的网站url
  • Application description # 描述,随便填写
  • Authorization callback URL:# 回调地址,就填写博客的网站url即可

常见问题及优化

本地加载js和css

为了提高加载速度,可以使用本地js和css文件,在next/source/lib下新建gitalk文件夹,里面放置gitalk.css和gitalk.min.js文件:

gitalk文件下载地址

1
2
<link rel="stylesheet" href="/lib/gitalk/gitalk.css">
<script src="/lib/gitalk/gitalk.min.js"></script>

初始化报错: Validation Failed

原因是由于Github限制labal长度不能超过50引起的,该问题解决方案来自Gitalk项目仓Issues115,通过MD5加密ID来缩短labal长度.

  1. 访问blueimp-md5,下载md5.min.js,放入next/source/lib/gitalk目录.

  2. 编辑/layout/_third-party/comments/gitalk.swig文件中,添加:

1
2
加载gitalk的js引用下即可:
<script src="/lib/gitalk/md5.min.js"></script>
  1. 修改id加载方式
1
'id: window.location.pathname',修改为'id: md5(window.location.pathname)',