前面做了gitalk评论之后,初始化几个都可以正常,今天打开以前的文章发现Error:Validation Failed。去gitalk issues看了一下果然有存在这个问题。

GitHub里issues的label长度上限50字符。特别是中文处理之后会更长。所以要得处理一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div id="gitalk-container"></div>
<link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
<script src="/js/md5.js"></script>

<script>
var gitalk = new Gitalk({
clientID: '<%= theme.comment.gitalk_client_id %>',
clientSecret: '<%= theme.comment.gitalk_client_secret %>',
repo: '<%= theme.comment.gitalk_repo %>',
owner: '<%= theme.comment.gitalk_owner %>',
admin: ['<%= theme.comment.gitalk_owner %>'],
id: md5(location.pathname),
distractionFreeMode: false
});
gitalk.render('gitalk-container');
</script>

新增 md5处理js文件。和 id: md5(location.pathname)

因为gitalk关联issue是通过number,你没有number的时候,它会直接利用你的id,而id的这个生成条件又被你修改了,因此你之前评论是无法和你的文章关联上了。

为了更好处理,把文件名改为英文名更友好些吧。

更新:最近换了volantis主题,刚开始我看着还以为是在volantis\layout_third-party\comments.ejs里修改,后来才发现生成了两个label。才知道是在volantis\layout_partial\scripts.ejs那里修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<% if (enableGitalk) { %>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script src="/js/md5.js"></script>
<script type="text/javascript">
var gitalk = new Gitalk({
clientID: "<%- theme.comments.gitalk.clientID %>",
clientSecret: "<%- theme.comments.gitalk.clientSecret %>",
repo: "<%- theme.comments.gitalk.repo %>",
owner: "<%- theme.comments.gitalk.owner %>",
admin: "<%- theme.comments.gitalk.admin %>",
<% if(page.gitalk && page.gitalk.id) { %>
id: md5(location.pathname),
<% } else { %>
id: md5(location.pathname), // Ensure uniqueness and length less than 50
<% } %>
distractionFreeMode: false // Facebook-like distraction free mode
});
gitalk.render('gitalk-container');
</script>
<% } %>