hexo-hide-posts
![hexo-version](https://img.shields.io/badge/hexo-%3E=4.0.0-blue?logo=hexo)
中文文档
A plugin to hide specific posts from your Hexo blog and make them only accessible by links.
Hide means your posts will not come up in article lists (homepage, archive, category, tag, feed, sitemap, whatever), or search results either (by telling search engines not to index these pages with a "noindex" meta tag). Only those who know the link can view the post, and you can share the link with anyone.
This means that posts marked as hidden could still be seen by anyone, but only if they guess the link.
Installation
npm install hexo-hide-posts
Usage
Add hidden: true
to the front-matter of posts which you want to hide.
e.g. Edit source/_posts/lorem-ipsum.md
:
---
title: 'Lorem Ipsum'
date: '2019/8/10 11:45:14'
hidden: true
---
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
This post will not be shown anywhere, but you can still access it by https://hexo.test/lorem-ipsum/
. (If you want to completely prevent a post from rendering, just set it as a draft.)
To get a list of hidden posts, you can run hexo hidden:list
from command line.
For developers, all_posts
and hidden_posts
added to Local Variables may be useful.
Config
In your site's _config.yml
:
hide_posts:
enable: true
filter: hidden
noindex: true
e.g. Set filter to secret
, so you can use secret: true
in front-matter instead.
Advanced Config
For power users, they can have a more fine-grained control on access to hidden posts by configuring blocklist and allowlist. This feature is available in version 0.3.0 or later.
Config Example 1: Hide the flagged posts everywhere, but make them visible on archives page and sitemap.
hide_posts:
enable: true
allowlist_generators: ['archive', 'sitemap']
Config Example 2: Hide the flagged posts only on homepage and RSS, and show them elsewhere.
hide_posts:
enable: true
allowlist_generators: ['*']
blocklist_generators: ['index', 'feed']
Note: although most of generator plugins respect a naming convention that they register
generator with the name in their package names, the generator name could be arbitrary.
For example, hexo-generator-searchdb
does not register
generators with name searchdb
, but xml
and json
.
For accurate generator name, you should check their source code.
Tips: Run hexo g --debug
command will show you all the generators installed with their names in the debug log.
Config Example 3: Pass a custom JavaScript function to determine which generator should be allowlisted.
This could be done by adding a plugin script to your Hexo blog.
hexo.config.hide_posts.allowlist_function = function (name) {
return /archive|feed/.test(name);
}
Custom ACL Function
For even more fine-grained control over which posts should be visible in which place, meet the most powerful feature of the plugin: Custom ACL (Access Control List) Function.
In version 0.4.0 and later, a custom JavaScript function could be configured to determine wether a generator could access a post or not, giving you the full control and inspection. The function accepts two arguments, the post
object and the current generatorName
. The global variable hexo
is also available in the context.
Here is an example. Use with caution!
const isGeneratorAllowed = require('hexo-hide-posts/lib/isGeneratorAllowed');
hexo.config.hide_posts.acl_function_per_post = function (post, generatorName) {
if (post.acl === 'no-rss') {
return generatorName !== 'atom' && generatorName !== 'sitemap';
}
if (post.acl === 'archive-only') {
return generatorName === 'archive';
}
if (post.categories.find({ name: 'news' }).length) {
return true;
}
if (post.date.year() < 2020) {
return false;
}
return isGeneratorAllowed(hexo.config.hide_posts, generatorName);
}
License
MIT