添加Gitalk
评论插件
前言
看到很多博客,都有留言功能,hexo
搭建的静态博客是不是也可以实现类似的功能。一查发现有很多类似插件,但是评论比较好的是Gitalk
插件,需要评论者使用github
账号登录,而且访问速度也比较快。
插件介绍
Gitalk
使用github
账号登录,支持MarkDown
语法。
Gitalk
是利用Github API
,基于Github issue
和Preact
开发的评论插件,也就是如果有用户进行评论,其实就是在对应的issue
上提问题。
插件使用
最标准的还是看官方教程说明
说明很简洁,也就3步。
增加一个
div
在我们的页面(page
)上<div id="gitalk-container"></div>
然后使用
javascript
代码生成gitalk
插件const gitalk = new Gitalk({ clientID: 'GitHub Application Client ID', clientSecret: 'GitHub Application Client Secret', repo: 'GitHub repo', owner: 'GitHub repo owner', admin: ['GitHub repo owner and collaborators, only these guys can initialize github issues'], id: location.pathname, // Ensure uniqueness and length less than 50 distractionFreeMode: false // Facebook-like distraction free mode }) gitalk.render('gitalk-container')
配置参数有很多,大部分时候,只要关注主要的几个即可。
clientID
:GitHub Application Client ID
【需要一个 Github Application,后面说】clientSecret
:GitHub Application Client Secret
repo
:Github 仓库名
【只要写参考名即可!存储评论issue的Github参考名(建议直接用GitHubPage的仓库名)】owner
:Github 用户名
admin
: [Github 用户名
] 【这个仓库的管理员,可以有多个,用数组表示,一般写自己】id
:location.pathname
【页面的唯一标识,gitalk会根据这个标识制动创建issue的标签,我们使用页面的相对路径作为标识即可】
上面提到必须有一个
Github Application
Github Application
, 申请地址填写好参数:
Application name
: 就是App的名字,任意起我这里写
BY Blog
Homepage URL
: App的地址,写自己博客仓库地址就好。我这里写
https://github.com/lmwings/lmwings.github.io.git
Application description
: 简单写下描述我这里写
BY Blog
Authorization callback URL
: 回调地址,也写自己博客地址就好我这里写
https://www.lmwings.top
点击
Register application
创建应用。生成的页面会有
(数字做了伪造处理)
Client ID 0123esadf950656f3339b6 Client Secret 7ed5373372d9gecf3be09c4sde6724015bwweb1
设置好参数提交,应该就可以出现评论框了。
我的使用
因为我用的是hexo
,使用的主题是matery
, 本身主题支持了这个插件,所以我不需要去关注代码层面的东西,只要把生成的Client ID
和Client Secret
等配置好就好。
在主题的_config.yml
下,修改# Gitalk 评论模块的配置,默认为不激活
# the Gitalk config,default disabled
# Gitalk 评论模块的配置,默认为不激活
gitalk:
enable: true
owner: lmwings
repo: lmwings.github.io
oauth:
clientId: 0123e932340656f3c89b6
clientSecret: 7ed52348272d23412cf3be09c3342401532b200b1
admin: lmwings
把代码上传到参考,会自动触发travis ci
, 然后部署
问题
启用以后,需要给每一篇都建一个issus
,否则没地方评论。手动建立也是很麻烦的,有没有更加方便的方法呢?