Gitalk默认使用: location.pathname 作为 gitalk 的id, 但是location.path必须小于50位切换主题时, 每个主题使用的处理方式都不相同, 有可能会导致换了主题, 发现之前的评论不见了, 下面介绍使用MD5作为id, 同时在换主题时一定要修改这个id的规则.

Gitalk使用

使用Gitalk方法:

  1. var gitalk = new Gitalk({
  2. clientID: 'GitHub Application Client ID',
  3. clientSecret: 'GitHub Application Client Secret',
  4. repo: 'GitHub repo',
  5. owner: 'GitHub repo owner',
  6. admin: ['GitHub repo owner and collaborators, only these guys can initialize github issues'],
  7. id: location.pathname, // Ensure uniqueness and length less than 50
  8. distractionFreeMode: false // Facebook-like distraction free mode
  9. })
  10. gitalk.render('gitalk-container')

使用MD5生成id

  1. 引入js

MD5 js 下载地址

  1. <script src="js/md5.min.js"></script>
  1. 修改js
    1. <script>
    2. var gitalk_id = md5(location.pathname)
    3. if (<%- page.comments_type == '404' %>) {
    4. gitalk_id = md5('https://liuzhihang.com/404')
    5. }
    6. let gitalk = new Gitalk({
    7. clientID: '<%- theme.gitalk.oauth.clientId %>',
    8. clientSecret: '<%- theme.gitalk.oauth.clientSecret %>',
    9. repo: '<%- theme.gitalk.repo %>',
    10. owner: '<%- theme.gitalk.owner %>',
    11. admin: <%- JSON.stringify(theme.gitalk.admin) %>,
    12. id: gitalk_id,
    13. distractionFreeMode: false // Facebook-like distraction free mode
    14. });
    15. gitalk.render('gitalk-container');
    16. </script>