avatar

Gitalk使用MD5生成Id

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

  1. 引入js

MD5 js 下载地址

<script src="js/md5.min.js"></script>
  1. 修改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>
文章作者: liuzhihang
文章链接: https://liuzhihang.com/2019/07/20/gitalk-uses-md5-to-generate-an-id.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Notes

评论