From 5cb1ecbc9e3f41a6c66e399362f7170d0eaf79fc Mon Sep 17 00:00:00 2001 From: Muhan Li Date: Tue, 3 Dec 2024 21:47:22 -0500 Subject: [PATCH] feat: hide posts from index (#125) --- README.md | 10 ++++++++++ lib/generator.js | 2 +- test/index.js | 5 +++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 49acdb5..6506a4a 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,16 @@ sticky: 100 --- ``` +The `hidden` parameter can be used to hide a post from the index page. When `hidden: true` is set, the post will not appear in the index but will still be accessible in other ways (e.g., the archive page or via a direct link). + +```yml +--- +title: Secret Post +date: 2024/11/11 11:11:11 +hidden: true +--- +``` + ## Note If your theme define a non-archive `index` layout (e.g. About Me page), this plugin would follow that layout instead and not generate an archive. In that case, use [hexo-generator-archive](https://github.com/hexojs/hexo-generator-archive) to generate an archive according to the `archive` layout. diff --git a/lib/generator.js b/lib/generator.js index 631bd5a..abc0014 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -4,7 +4,7 @@ const pagination = require('hexo-pagination'); module.exports = function(locals) { const config = this.config; - const posts = locals.posts.sort(config.index_generator.order_by); + const posts = locals.posts.filter(post => !post.hidden).sort(config.index_generator.order_by); posts.data.sort((a, b) => (b.sticky || 0) - (a.sticky || 0)); diff --git a/test/index.js b/test/index.js index 2d9a9cf..97e19f5 100644 --- a/test/index.js +++ b/test/index.js @@ -23,9 +23,10 @@ describe('Index generator', () => { before(() => hexo.init().then(() => Post.insert([ {source: 'foo', slug: 'foo', date: 1e8, order: 0}, {source: 'bar', slug: 'bar', date: 1e8 + 1, order: 10}, - {source: 'baz', slug: 'baz', date: 1e8 - 1, order: 1} + {source: 'baz', slug: 'baz', date: 1e8 - 1, order: 1}, + {source: 'qux', slug: 'qux', date: 1e8 - 8, order: 8, hidden: true} ])).then(data => { - posts = Post.sort('-date'); + posts = Post.slice(0, -1).sort('-date'); locals = hexo.locals.toObject(); }));