隐藏博客中某些特定的文章应该算是一个比较常见的需求,毕竟谁都有些不希望让别人看到的东西。当然啦,你可能会说,「不想被看到就不要放到博客上来嘛」。话是没错,不过有时候人就是这么别扭,想让某些文章被看到,却又不想被所有人看到,又或者是某些文章只想和自己熟悉的人一起分享。

Hexo 插件 hexo-hide-posts 可以实现这个需求(网上也有一些关于 Hexo 隐藏文章的教程,不过一般都要求修改主题文件,还是这个插件更通用一些)。它的功能如下:

  • 在博客的所有文章列表中隐藏指定的文章(包括首页、存档页、分类标签、Feed 等)

  • 被隐藏的文章依然可以通过文章链接直接访问(比如 https://hexo.example/{slug}/)

  • 除非知道链接,任何人都无法找到这些被隐藏的文章。

安装 hexo-hide-posts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ yarn add hexo-hide-posts
yarn add v1.22.5
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not
to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this
warning, remove package-lock.json.
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@2.3.2: The platform "win32" is incompatible with this module.
info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 3 new dependencies.
info Direct dependencies
├─ hexo-generator-index-pin-top@0.2.2
└─ hexo-hide-posts@0.2.0
info All dependencies
├─ hexo-generator-index-pin-top@0.2.2
├─ hexo-hide-posts@0.2.0
└─ object-assign@4.1.1
Done in 19.27s.

配置

在站点目录下的_config.yml中如下配置:

1
2
3
4
5
6
7
8
9
10
# hexo-hide-posts
hide_posts:
enable: true
# Change the filter name to fit your need
filter: hidden
# Generators which you want to expose all posts (include hidden ones) to.
# Common generators: index, tag, category, archive, sitemap, feed, etc.
public_generators: []
# Add "noindex" meta tag to prevent hidden posts from being indexed by search engines
noindex: true

使用文章隐藏功能

安装插件后,在想要隐藏的文章的 front-matter(就是 Markdown 顶上的那个参数块)里添加一行 hidden: true 即可。

例如编辑文章: source/_posts/lorem-ipsum.md

1
2
3
4
5
6
7
8
---
title: 'Lorem Ipsum'
date: '2019/8/10 11:45:14'
hidden: true
---

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

这篇文章不会在任何地方显示,但仍然可以通过类似 https://hexo.test/lorem-ipsum/ 的网址访问它。