Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hide posts from index #125

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
muhac marked this conversation as resolved.
Show resolved Hide resolved
locals = hexo.locals.toObject();
}));

Expand Down
Loading