Hexo 评论系统 Valine 的使用

因为自己使用过,看了几篇文章;觉得有必要记录一下

cungudafa


关于valine升级的升级/魔改

Valine - 一款快速、简洁且高效的无后端评论系统。
目前已经更新到v1.5.0, 2022-06-24;旧版和新版可根据自己需求看文档更新日志选取,本章主要阐述升级方法


引入X.X版的js文件;可以在官网选取

位置在:
1
2
<script src="<%- theme.jsDelivr.url %><%- url_for('/libs/valine/av-min.js') %>"></script>
<script src="https://cdn.jsdelivr.net/gh/small-rose/small-rose.github.io/libs/valine/Valine.min.js"></script>

记得把主题自带的

1
<script src="<%- theme.jsDelivr.url %><%- url_for(theme.libs.js.valine) %>"></script>

删掉,或直接剪切到别的地方备份着。
将文件放你自己的仓库

下载我的 Valine.min.js 文件,直接替换你主题目录 /source/libs/valine/ 下的 Valine.min.js 文件。

注意,如果你担心替换有问题,可以先备份一下你自己的 Valine.min.js 文件。

增加valine的配置:

1.4的版本有些属性调整了,主题下的_config.ymlvaline属性如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
valine:
enable: true
appId: iTxfqh5e9IaRfiiVOTbIWoKa-XXXXXX
appKey: C5s5xGFErD1EtXXXXXXXX
verify: true # 是否启用防垃圾验证
notify: true # 是否开启邮件提醒(https://valine.js.org/notify.html)
visitor: true
avatar: monsterid # 头像样式(https://valine.js.org/avatar.html)
pageSize: 10
placeholder: 'ヾノ≧∀≦)o来啊,快活啊!' # Comment Box placeholder
background: /medias/comment_bg.png #背景图
count: true
enableQQ: 970175021
recordIP: true
requiredFields:
- nick
- mail
guest_info:
- nick
- mail
- link
master:
- 123abc508165c8eba9a77f872xxxx046 # md5加密后的博主邮箱
metaPlaceholder: # 输入框的背景文字
nick: 昵称/QQ号(必填)
mail: 邮箱(必填)
link: 网址(https://)
lang: zh-CN
tagMeta: # The String Array of Words to show Flag.[Just Only xCss Style mode]
- 博主
- 小伙伴
- 访客
friends: # The MD5 String Array of friends Email to show friends Flag.[Just Only xCss Style mode]
- c08508165c8eba9a77f8c2853xxxx09e
- 901345d4c91ddfd8db0f175bbcfff0c8
- 1512958e18378c98b498d5effe3e76ff

复制代码注意缩进对齐,不对齐可能会报错,请自行检查对齐。

修改valine.ejs:

Matery 主题使用的ejs模板预编译,如果你使用了pug或者swig等其他的模板语言,请修改成对应语言语法即可。

原始的valine.ejs

1
2
3
4
5
6
7
8
9
10
11
12
new Valine({
el: '#vcomments',
appId: '<%- theme.valine.appId %>',
appKey: '<%- theme.valine.appKey %>',
notify: '<%- theme.valine.notify %>' === 'true',
verify: '<%- theme.valine.verify %>' === 'true',
visitor: '<%- theme.valine.visitor %>' === 'true',
avatar: '<%- theme.valine.avatar %>',
pageSize: '<%- theme.valine.pageSize %>',
lang: '<% if (config.language == "zh-CN") { %>zh-cn<% } else { %>en<% } %>',
placeholder: '<%= theme.valine.placeholder %>'
});

升级后的valine.ejs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let metaPlaceholder = <%-  JSON.stringify(theme.valine.metaPlaceholder) %> ;
//这里要换行
new Valine({
el: '#vcomments',
appId: '<%- theme.valine.appId %>',
appKey: '<%- theme.valine.appKey %>',
notify: '<%- theme.valine.notify %>' === 'true',
verify: '<%- theme.valine.verify %>' === 'true',
visitor: '<%- theme.valine.visitor %>' === 'true',
avatar: '<%- theme.valine.avatar %>',
pageSize: '<%- theme.valine.pageSize %>',
lang: '<%- theme.valine.lang %>',
placeholder: '<%= theme.valine.placeholder %>',
meta: <%- '["' + theme.valine.guest_info.join('", "') + '"]' %>,
recordIP: '<%- theme.valine.recordIP %>' === 'true',
enableQQ: '<%- theme.valine.avatar %>',
requiredFields: <%- '["' + theme.valine.master.join('", "') + '"]' %>,
master: <%- '["' + theme.valine.master.join('", "') + '"]' %>,
friends: <%- '["' + theme.valine.friends.join('", "') + '"]' %>,
tagMeta: <%- '["' + theme.valine.tagMeta.join('", "') + '"]' %>,
metaPlaceholder: metaPlaceholder,

});

如果需要验证昵称和邮箱可以加上以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
document.body.addEventListener('click', function(e) {
if (e.target.classList.contains('vsubmit')) {
const email = document.querySelector('input[type=email]');
const nick = document.querySelector('input[name=nick]');
const reg = /^[A-Za-z0-9_-\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
if (!email.value || !nick.value || !reg.test(email.value)) {
const str = `<div class="valert txt-center"><div class="vtext">请填写正确的昵称和邮箱!</div></div>`;
const vmark = document.querySelector('.vmark');
vmark.innerHTML = str;
vmark.style.display = 'block';

e.stopPropagation();

setTimeout(function() {
vmark.style.display = 'none';
vmark.innerHTML = '';
}, 2500);
}
}
}, true);

其它填写昵称邮箱和网址的地方如果折行了就按我的样式改一下就好:

在 valine.ejs 是上面对着改一下就好了:

1
2
3
4
.v[data-class="v"] .vwrap .vheader .vinput {
width: 32%;
border-bottom: 1px dashed #dedede;
}
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023 maojunzc
  • Visitors: | Views:
网站已运行:

请我喝杯咖啡吧~

支付宝
微信