prinsss / hexo-hide-posts

A plugin to hide specific posts from your Hexo blog and make them only accessible by links. (隐藏 Hexo 文章)
MIT License
197 stars 19 forks source link

Hexo 7.0 下失去部分隐藏效果 #34

Closed bianyukun1213 closed 9 months ago

bianyukun1213 commented 11 months ago

经测试,Hexo 7.0 下,在分类和标签页面能看到隐藏文章对应的分类和标签。

prinsss commented 11 months ago

「能看到隐藏文章对应的分类和标签」,那么在这些页面上能看到被隐藏的文章吗?还是只能看到分类和标签?

我之前在 v7.0.0 (RC2) 中测试过,功能应该是正常的,不知道是不是正式版有什么改动。

为了方便排查问题,请你提供以下信息:

  1. 你所使用的 Hexo 版本、主题名称、主题版本
  2. 你的 Hexo 配置文件 _config.yml
  3. 如果有能复现的 GitHub Repo,也可以直接提供地址

谢谢。

bianyukun1213 commented 11 months ago

不能看到被隐藏的文章,只能看到对应的分类和标签,如图。

image

Hexo 7.0.0、landscape 1.0.0

_config.yml

# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/

# Site
title: Hexo
subtitle: ''
description: ''
keywords:
author: John Doe
language: en
timezone: ''

# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: http://example.com
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
  trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
  trailing_html: true # Set to false to remove trailing '.html' from permalinks

# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:

# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link:
  enable: true # Open external links in new tab
  field: site # Apply to the whole site
  exclude: ''
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
syntax_highlighter: highlight.js
highlight:
  line_number: true
  auto_detect: false
  tab_replace: ''
  wrap: true
  hljs: false
prismjs:
  preprocess: true
  line_number: true
  tab_replace: ''

# Home page setting
# path: Root path for your blogs index page. (default = '')
# per_page: Posts displayed per page. (0 = disable pagination)
# order_by: Posts order. (Order by date descending by default)
index_generator:
  path: ''
  per_page: 10
  order_by: -date

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Metadata elements
## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
meta_generator: true

# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss
## updated_option supports 'mtime', 'date', 'empty'
updated_option: 'mtime'

# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page

# Include / Exclude file(s)
## include:/exclude: options only apply to the 'source/' folder
include:
exclude:
ignore:

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: landscape

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
  type: ''

hide_posts:
  # Should hexo-hide-posts be enabled.
  enable: true

  # The front-matter key for flagging hidden posts.
  # You can change the filter name if you like.
  filter: hidden

  # Add "noindex" meta tag to prevent hidden posts from being indexed by search engines.
  noindex: true

  # Generators in the allowlist will have access to the hidden posts.
  # Common generators in Hexo: 'index', 'tag', 'category', 'archive', 'sitemap', 'feed'
  # allowlist_generators: []

  # Generators in the blocklist can *not* access the hidden posts.
  # The allowlist has higher priority than the blocklist, if both set.
  # blocklist_generators: ['*']
prinsss commented 11 months ago

点进去这两个页面(标签和分类),可以看到什么?

Snipaste_2023-12-16_18-47-54
bianyukun1213 commented 11 months ago

这个点进去是正常的,即什么也看不到。

比如 invisible 分类点进去是 Cannot GET /categories/invisible/

prinsss commented 11 months ago

本地复现了,Hexo 6.3.0 是正常的,升级到 7.0.0 就有问题。

我后面看下怎么修复,感谢反馈!

prinsss commented 11 months ago

定位到问题了,Hexo 那边做了个性能优化,影响了这部分功能,不知道上游会不会修复。

Related Issue/PR: https://github.com/hexojs/hexo/pull/5119 https://github.com/hexojs/hexo/issues/5380

可以用以下临时方法解决: 在博客的 scripts 目录下创建 fix.js 文件,放入以下内容:

// FILE: scripts/fix.js
hexo.extend.filter.register("before_generate", function () {
  // Ignore categories with zero posts
  this.locals.set("categories", () => {
    return this.database
      .model("Category")
      .filter((category) => category.posts.length);
  });

  // Ignore tags with zero posts
  this.locals.set("tags", () => {
    return this.database.model("Tag").filter((tag) => {
      // tag.length => 1
      // tag.posts.length => 0
      // See: https://github.com/hexojs/hexo/pull/5119

      // console.log(tag.name, tag.length, tag.posts.length);
      return tag.posts.length;
    });
  });
});

然后再 hexo clean && hexo generate,应该就可以把空的 tag 和 category 隐藏掉了。

bianyukun1213 commented 11 months ago

感谢您的回复!

我觉得这个 issue 保持开启比较好——即使它是 Hexo 自身原因造成的——方便遇到同样问题的人找到您提出的解决方案。

prinsss commented 11 months ago

嗯,我会保持开启的。

我已经在上游 issue 中提交了相关信息,看看 Hexo 那边会不会修复吧。如果不修复那就插件这边兼容一下。

Raccoon-njuse commented 9 months ago
image

空分类已经使用临时方案隐藏了,但是butterfly主题会显示每个分类下的文章数量,这个数量依然算上了hidden的文章数量,请问这个有临时的解决办法吗

Raccoon-njuse commented 9 months ago

hexo-site@0.0.0 /Users/raccoon/blogs ├── hexo-blog-encrypt@3.1.9 ├── hexo-deployer-git@4.0.0 ├── hexo-generator-archive@2.0.0 ├── hexo-generator-category@2.0.0 ├── hexo-generator-index@3.0.0 ├── hexo-generator-tag@2.0.0 ├── hexo-hide-posts@0.4.0 ├── hexo-lazyload-image@1.0.13 ├── hexo-renderer-ejs@2.0.0 ├── hexo-renderer-marked@6.2.0 ├── hexo-renderer-pug@3.0.0 ├── hexo-renderer-stylus@3.0.1 ├── hexo-server@3.0.0 ├── hexo-theme-butterfly@4.12.0 ├── hexo-theme-fluid@1.9.7 ├── hexo-theme-landscape@1.0.0 ├── hexo-wordcount@6.0.1 └── hexo@7.1.0 这是我的npm依赖,

# hexo-hide-posts
hide_posts:
  filter: hidden
  public_generators: []
  noindex: true

这是_config.yml中的hidden配置

prinsss commented 9 months ago

Hi @Raccoon-njuse , 试试这个新脚本:

// FILE: scripts/fix.js
// This script will revert changes introduced in PR #5119
// See: https://github.com/hexojs/hexo/pull/5119/files

hexo.extend.filter.register('before_generate', function () {
  // Retrieve internal database models
  const Category = this.database.model('Category');
  const Tag = this.database.model('Tag');

  // Revert changes to lib/hexo/index.js
  this.locals.set('categories', () => {
    // Ignore categories with zero posts
    return Category.filter((category) => category.length);
  });

  this.locals.set('tags', () => {
    // Ignore tags with zero posts
    return Tag.filter((tag) => tag.length);
  });

  // Revert changes to lib/models/category.js
  Category.schema.virtual('length').get(function () {
    return this.posts.length;
  });

  // Revert changes to lib/models/tag.js
  Tag.schema.virtual('length').get(function() {
    return this.posts.length;
  });
});
Raccoon-njuse commented 9 months ago

Hi @Raccoon-njuse , 试试这个新脚本:

// FILE: scripts/fix.js
// This script will revert changes introduced in PR #5119
// See: https://github.com/hexojs/hexo/pull/5119/files

hexo.extend.filter.register('before_generate', function () {
  // Retrieve internal database models
  const Category = this.database.model('Category');
  const Tag = this.database.model('Tag');

  // Revert changes to lib/hexo/index.js
  this.locals.set('categories', () => {
    // Ignore categories with zero posts
    return Category.filter((category) => category.length);
  });

  this.locals.set('tags', () => {
    // Ignore tags with zero posts
    return Tag.filter((tag) => tag.length);
  });

  // Revert changes to lib/models/category.js
  Category.schema.virtual('length').get(function () {
    return this.posts.length;
  });

  // Revert changes to lib/models/tag.js
  Tag.schema.virtual('length').get(function() {
    return this.posts.length;
  });
});

@prinsss ,临时解决方案已生效!感谢您的回复!期待上游修复

prinsss commented 9 months ago

@Raccoon-njuse @bianyukun1213

先不等上游修复了,hexo-hide-posts 插件已经更新 0.4.2 版本,内置了对 Hexo 7.0 的兼容逻辑。更新之后就可以把 scripts 目录里的临时脚本删掉了,无需额外处理。:)