本文记录如何在基于 Hexo 的博客里添加 gitalk comment 评论系统。
GITALK :一个基于 Github Issue 和 Preact 开发的评论插件。
步骤
gitalk 是基于 GitHub Issue 来操作评论的,那就需要我们在 GitHub 上注册指定的 Application 来申请操作权限!
1. Register Application
点击 https://github.com/settings/applications/new 注册链接去自己 GitHub 上注册新应用。
说明:
- Application name # 随便写,这里就起个 Gitalk 吧
- Homepage URL # 网站 URL,写自己的博客地址,类似:https://jolly336.github.io 或 https://nelsonblog.me 我自己做了博客 CANME
- Authorization callback URL # 当评论系统出错时,跳转地址,这里和 homepage URL 保持一致就行
- Application description,可选,随便写点啥
填写完成后点击注册,会生成一个 GitHub Application,点击这里 的 OAuth Apps 列表会看到创建好的 App,如果后面评论调试出错了,还需要回这里修改的。
注册完记录下生成的 Client ID 和 Client Secret 两个 key,待会修改主题配置需要用到。
2. 修改 next 主题
提示:以下修改都在自己本地博客 next 主题目录! 找到 next 主题目录,我自己博客对应的是 jolly336.github.io/themes/next 目录。
- gitalk.swig
新建 /layout/_third-party/comments/gitalk.swig 文件,并添加内容:
1 | {% if page.comments && theme.gitalk.enable %} |
- comments.swig
修改 /layout/_partials/comments.swig,添加内容如下:
打开文件找到这个代码块,在里面添加 gitalk 元素,注意别写错位置了!,博主就是一开始写错位置,导致 gitalk 评论死活不显示,最后通过 console 才定位到是这的问题!
- 添加之前:
1 | {% elseif theme.valine.appid and theme.valine.appkey %} |
- 要添加的 gitalk 元素代码块:
1 | {% elseif theme.gitalk.enable %} |
- 添加之后:
1 | {% elseif theme.valine.appid and theme.valine.appkey %} |
- index.swig
修改 layout/_third-party/comments/index.swig,在最后一行添加内容:
1 | {% include 'gitalk.swig' %} |
- gitalk.styl
新建 /source/css/_common/components/third-party/gitalk.styl 文件,添加内容:
1 | .gt-header a, .gt-comments a, .gt-popup a |
- third-party.styl
修改 /source/css/_common/components/third-party/third-party.styl,在最后一行上添加内容,引入样式:
1 | @import "gitalk"; |
- _config.yml
在主题配置文件 next/_config.yml 中添加如下内容:
1 | gitalk: |
以上就是 NexT 中添加 gitalk 评论的配置,博客上传到 GitHub 上后,打开页面进入某一博客内容下,就可看到我们增加的 Gitalk 评论了。
看见自己创建好的 Gitalk 评论系统,是不是有点小激动呢?:)
下一节分享如何统计博客阅读量,敬请期待~