Skip to content

Commit

Permalink
feat: hide posts from index (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhac authored Dec 4, 2024
1 parent 83d121f commit 5cb1ecb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
5 changes: 3 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));

Expand Down

0 comments on commit 5cb1ecb

Please sign in to comment.