Gitalk默认使用: location.pathname 作为 gitalk 的id, 但是location.path必须小于50位切换主题时, 每个主题使用的处理方式都不相同, 有可能会导致换了主题, 发现之前的评论不见了, 下面介绍使用MD5作为id, 同时在换主题时一定要修改这个id的规则.
Gitalk使用
使用Gitalk方法:
var 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')
使用MD5生成id
- 引入js
MD5 js 下载地址
<script src="js/md5.min.js"></script>
- 修改js
<script>
var gitalk_id = md5(location.pathname)
if (<%- page.comments_type == '404' %>) {
gitalk_id = md5('https://liuzhihang.com/404')
}
let gitalk = new Gitalk({
clientID: '<%- theme.gitalk.oauth.clientId %>',
clientSecret: '<%- theme.gitalk.oauth.clientSecret %>',
repo: '<%- theme.gitalk.repo %>',
owner: '<%- theme.gitalk.owner %>',
admin: <%- JSON.stringify(theme.gitalk.admin) %>,
id: gitalk_id,
distractionFreeMode: false // Facebook-like distraction free mode
});
gitalk.render('gitalk-container');
</script>