hexo增加utterances评论系统

在配置utteranc评论系统时,按照大多数网上搜索到的办法:

1、GitHub install utterances;
2、在next _config.yml中配置相关配置。
在重启hexo后发现并为生效,原因是因为当前版本的next并未内置utterances的相关配置模版。

参考原文地址

解决办法:

在GitHub安装好utterances插件之后再根据以下步骤操作

创建utterances.swig

  • 在layout/_third-party/comments里创建utterances.swig,内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{%- if page.comments %}
<script>
NexT.utils.loadComments(document.querySelector('#utterances-container'), () => {
// if (typeof parcelRequire === 'function') { return; }
var js = document.createElement('script');
js.type = 'text/javascript';
js.src = 'https://utteranc.es/client.js';
js.async = true;
js.crossorigin = 'anonymous';
js.setAttribute('repo', '{{ theme.utterances.repo }}');
js.setAttribute('issue-term', '{{ theme.utterances.issue_term }}');
js.setAttribute('theme', '{{ theme.utterances.theme }}');
document.getElementById('utterances-container').appendChild(js);
});
</script>
{%- endif %}

创建utterances.js

  • 在scripts/filters/comment下创建utterances.js,内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* global hexo */

'use strict';

const path = require('path');

// Add comment
hexo.extend.filter.register('theme_inject', injects => {
let theme = hexo.theme.config;
if (!theme.utterances.enable) return;

injects.comment.raw('utterances', '<div class="comments" id="utterances-container"></div>', {}, {cache: true});

injects.bodyEnd.file('utterances', path.join(hexo.theme_dir, 'layout/_third-party/comments/utterances.swig'));

});

修改主题配置文件

  • 在主题配置文件中添加
1
2
3
4
5
utterances: 
enable: true
repo: "你的repo地址"
issue_term: "pathname"
theme: "github-light"
  • 在主配置_config.yml文件中的url中配置好博客的地址,比如:url: https://yourdomain.com

最后重启博客系统就能够看到评论模块了

-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道