0%

Hexo 博客 添加评论功能 (来必力 livere)& Template render error (Hexo BUG)

前言

作为一个完整的博客,评论功能自然是必不可少的。

而可供选择的第三方评论系统有很多,但不是被墙(如 Disqus)就是停止服务(如友言、网易云跟帖),或者需要域名备案(如畅言)。

所以只能把目光投向一款来自韩国的评论系统 —— 来必力 (livere)。

总的来说,这个评论系统有以下好处:

1. 有中文网站
2. 界面还算美观
3. 国内本土化做得很好,支持如下账号登录:

![支持账号](login.png)

来必力网站配置

  • 进入官网 https://livere.com , 邮箱注册登录

  • 点击安装,选择 City 免费版,点击现在安装

  • 获得安装代码并复制

![安装代码](install_code.jpg)
  • 到此为止,网站的配置到此结束;当然,如果需要修改登录选项和评论界面,可以在设置中更改

博客的配置

  • 打开博客主题的配置文件_config.yml,加上如下代码:
1
2
3
# Support for LiveRe comments system.
# You can get your uid from https://livere.com/insight/myCode (General web site)
livere_uid:
  • livere_uid: 后加上安装代码中 data-uid 的内容,以便以后修改

  • 打开博客主题的 layout 文件夹

  • includes 文件夹中新建一个 livere.swig 文件,在其中写入如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{% if not (theme.duoshuo and theme.duoshuo.shortname) and not theme.duoshuo_shortname and not theme.disqus_shortname and not theme.hypercomments_id and not theme.gentie_productKey %}
{% if theme.livere_uid %}
<script type="text/javascript">
(function(d, s) {
var j, e = d.getElementsByTagName(s)[0];
if (typeof LivereTower === 'function') { return; }
j = d.createElement(s);
j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
j.async = true;
e.parentNode.insertBefore(j, e);
})(document, 'script');
</script>
{% endif %}
{% endif %}
  • 打开 post.swig 文件,在 </article> 前加上如下代码:
1
2
3
4
5
6
7
{% if page.photos.length %}
{% include 'includes/gallery.swig' %}
{% endif %}
{% autoescape false %}{{ page.content }}{% endautoescape %}
{% if theme.livere_uid %}
<div id="lv-container" data-id="city" data-uid="{{ theme.livere_uid }}"></div>
{% endif %}
  • </article> 后加上 {% include 'includes/livere.swig' %}

  • 最后,将博客重新 deploy 即可

![最终效果](result.png)

BUG!!!

在写完这篇博客后,再 hexo g 发生错误,提示 Template render error:(unknown path), 后经 google 发现,hexo 对特殊符号 {% %}{{ }} 的解析有错误,其被当成 hexo 模板中的标签,所以出现错误无法找到路径 includes/livere.swig


目前的解决办法为在这两个特殊字符之外添加如下代码:

1
2
3
{% raw %}
特殊字符
{% endrow %}

使 hexo 不解析其中的特殊字符

同时注意,对于用三个 ``` 包围的代码块则不存在这样的问题

Thank you for your reward !