Hexo隐藏和加密文章

只在首页隐藏

安装 hexo-generator-index2

1
2
$ npm install hexo-generator-index2 --save
$ npm uninstall hexo-generator-index --save

需要安装 hexo-generator-index2 并卸载 hexo-generator-index

修改配置文件

在博客根目录的 _config.yml 添加下列配置

1
2
3
4
5
6
7
index2_include_index: true
index2_generator:
per_page: 8
order_by: -date
exclude:
- tag hide
- category hide

这样子在首页就看不到标签或者分类为hide的文章了,但是在分类页或归档页依旧能看到隐藏的文章

彻底隐藏(以NexT主题为例)

打开 themes\hexo-theme-next-master\layout_macro\post-collapse.njk 文件

修改成以下代码

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
37
38
39
40
41
42
43
{% macro render(posts) %}
{%- set current_year = '1970' %}
{%- for post in posts.toArray() %}
{%- set year = date(post.date, 'YYYY') %}
{# 获取每一篇文章的标签数组 #}
{%- set tags = post.tags.toArray() %}
{# 如果标签为hide #}
{%- if tags[0].name != 'hide' %}
{%- if year !== current_year %}
{%- set current_year = year %}
<div class="collection-year">
<span class="collection-header">{{ current_year }}</span>
</div>
{%- endif %}
<article itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<div class="post-meta-container">
<time itemprop="dateCreated"
datetime="{{ moment(post.date).format() }}"
content="{{ date(post.date, config.date_format) }}">
{{ date(post.date, 'MM-DD') }}
</time>
</div>

<div class="post-title">
{%- if post.link %}{# Link posts #}
{%- set postTitleIcon = '<i class="fa fa-external-link-alt"></i>' %}
{%- set postText = post.title or post.link %}
{{ next_url(post.link, postText + postTitleIcon, {class: 'post-title-link post-title-link-external', itemprop: 'url'}) }}
{% else %}
<a class="post-title-link" href="{{ url_for(post.path) }}" itemprop="url">
<span itemprop="name">{{ post.title or __('post.untitled') }}</span>
</a>
{%- endif %}
</div>

{{ post_gallery(post.photos) }}
</header>
</article>
{%- endif %}
{%- endfor %}
{% endmacro %}

修改之后,在分类页,归档页也看不到隐藏的文章(但是也要做首页隐藏的配置),为了防止页面底部导航引导至隐藏的文章,还需要修改主题文件夹内的 _config.yml

1
2
3
# Show previous post and next post in post footer if exists
# Available values: left | right | false
post_navigation: false # 将此项改为false,关闭底部的导航

这样子文章就被彻底隐藏,需要指定文章url才可以看得到

加密文章

安装 hexo-blog-encrypt

1
npm install --save hexo-blog-encrypt

修改博客根目录 _config.yml 的配置

1
2
3
4
5
6
7
8
9
encrypt: # hexo-blog-encrypt
abstract: This article is encrypted!
message: This article is encrypted!
tags:
- {name: tagName, password: passwordA}
- {name: tagName, password: passwordB}
template: <div id="hexo-blog-encrypt" data-wpm="{{hbeWrongPassMessage}}" data-whm="{{hbeWrongHashMessage}}"><div class="hbe-input-container"><input type="password" id="hbePass" placeholder="{{hbeMessage}}" /><label>{{hbeMessage}}</label><div class="bottom-line"></div></div><script id="hbeData" type="hbeData" data-hmacdigest="{{hbeHmacDigest}}">{{hbeEncryptedData}}</script></div>
wrong_pass_message: Sorry,wrong password!
wrong_hash_message: Sorry,wrong hash!

需要使用密码的时候,在文章头部信息加入这个配置即可

1
password: password