hexo 自定义 front matter, 并通过其控制评论与分享功能的开关

[随着hexo5.0.0 以及icarus4.0的发布,该页面已失效,现在用户可以直接通过以下方法控制]

1
2
share: true     //是否打开文章分享? 不填写或删除此行默认打开
comments: true //是否打开文章评论? 不填写或删除此行默认打开

在写文章的时候, 有时候希望关闭一些文章的评论和分享功能。那么如何自定义文章评论与分享功能的开启与否呢?

我们需要对主题稍作修改, 我的主题是icarus, 得益于作者优秀的编码习惯, 修改起来十分的方便

注: 不同主题修改的方式不同, 如next主题下, front matter的自定义配置文件在next\scripts\filters\front-matter.js, 整个修改过程也是大相径庭, 基本上所有主题已经完成了一些自定义的front—mattter变量, 我们需要做的就Ctrl+F找到该变量, 然后依样画葫芦

修改

打开主题下icarus\includes\specs\article.spec.js, 添加变量

article.spec.js
1
2
3
4
5
6
7
8
9
10
share: {
[type]: 'boolean',
[doc]: 'share or not',
[defaultValue]: true
},// don't forget the comma
comment: {
[type]: 'boolean',
[doc]: 'comment or not',
[defaultValue]: true
}

打开文章样式文件icarus\layout\common\article.ejs, Ctrl+F 找到以下位置, 并修改

article.ejs first_line:74 diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-<% if (!index && has_config('share.type')) { %>
+<% if (!index && has_config('share.type') && get_config('article.readtime') == true) { %>
<%- _partial('share/' + get_config('share.type')) %>
<% } %>

-<% if (!index && has_config('comment.type')) { %>
+<% if (!index && has_config('comment.type') && get_config('article.comment') == true ) { %>
<div class="card">
<div class="card-content">
<h3 class="title is-5 has-text-weight-normal"><%= __('article.comments') %></h3>
<%- _partial('comment/' + get_config('comment.type')) %>
</div>
</div>
<% } %>

使用

在文章的font-matter处进行设置, 以本篇文章为例

hexo-costum-frontmatter.md
1
2
3
4
5
6
7
8
9
10
11
12
---
title: hexo 自定义 front matter, 并通过其控制评论与分享功能的开关
date: 2020-02-28 19:05:44
category:
- Web
- FrontEnd
tags:
- hexo
thumbnail: false
share: true //是否打开文章分享? 不填写或删除此行默认打开
comment: true //是否打开文章评论? 不填写或删除此行默认打开
---

hexo 自定义 front matter, 并通过其控制评论与分享功能的开关

https://matrix4f.com/Web/FrontEnd/hexo-costum-front-matter/

Author

oxine

Posted on

2020-02-28

Updated on

2020-08-18

Licensed under

Comments

昵称处填入QQ号,自动同步QQ头像与ID