开始之初

在此,我将分享如何配置 hexo-hide-posts 插件,使其在不改变原有代码的情况下,优雅地隐藏文章。

安装与配置

首先,于项目目录中执行以下命令,以安装此插件:

1
npm install hexo-hide-posts --save

安装完成后,在项目根目录的 _config.yml 中,添加如下内容:

1
2
3
4
5
6
7
8
9
10
# 文章隐藏:https://github.com/prinsss/hexo-hide-posts
hide_posts:
enable: true # 是否启用 hexo-hide-posts
filter: hidden # 隐藏文章的标识,也可以改成其他你喜欢的名字
noindex: true # 为隐藏的文章添加 noindex meta 标签,阻止搜索引擎收录
# 设置白名单,白名单中的 generator 可以访问隐藏文章
allowlist_generators: []

# 设置黑名单,黑名单中的 generator 不可以访问隐藏文章
blocklist_generators: ['*']

使用之法

若在 _config.yml 中的配置为 filter: hidden,则在文章的 front-matter 中添加 hidden: true 即可隐藏文章。例如:

1
2
3
4
5
---
title: '被隐藏的文章'
date: '2024-07-20 00:21:14'
hidden: true
---

高级配置

插件提供了黑白名单控制,只需在 _config.yml 中配置 allowlist_generatorsblocklist_generators 参数即可。以下是一些示例:

  • 示例1:让所有隐藏文章在存档页面和分类页面中可见,其他地方不可见。

    1
    2
    3
    hide_posts:
    enable: true
    allowlist_generators: ['archive', 'category']
  • 示例2:仅在首页和 RSS 隐藏部分文章,其他地方可见。

    1
    2
    3
    4
    hide_posts:
    enable: true
    allowlist_generators: ['*']
    blocklist_generators: ['index', 'feed']

配置文件解说

在我的配置文件中:

  • enable: true 启用了 hexo-hide-posts 插件。
  • filter: hidden 指定了隐藏文章的标识符。
  • noindex: true 为隐藏文章添加了 noindex 标签,以阻止搜索引擎的收录。
  • allowlist_generators: [] 设置了白名单为空,即默认不允许任何生成器访问隐藏文章。
  • blocklist_generators: ['*'] 设置了黑名单为所有生成器,即所有生成器默认无法访问隐藏文章。
    如果同时设置了黑白名单,白名单优先级高于黑名单

通过这些设置,我可以优雅地管理博客的可见性,使其既保持内容的丰富,又不失整洁的观感。