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

Sort by a new sticky parameter #51

Merged
merged 3 commits into from
Jun 12, 2020
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $ npm install hexo-generator-index --save
```

## Options

Add or modify the following section to your root _config.yml file

``` yaml
Expand All @@ -36,6 +37,18 @@ index_generator:
- default: 'page'
- `awesome-page` makes the URL ends with 'awesome-page/<page number>' for second page and beyond.

## Usage

The `sticky` parameter in the post [Front-matter](https://hexo.io/docs/front-matter) will be used to pin the post to the top of the index page.

```markdown
---
title: Hello World
date: 2013/7/13 20:46:25
sticky: 100
stevenjoezhang marked this conversation as resolved.
Show resolved Hide resolved
---
```

## 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
4 changes: 4 additions & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use strict';

const pagination = require('hexo-pagination');
const { sort } = require('timsort');

module.exports = function(locals) {
const config = this.config;
const posts = locals.posts.sort(config.index_generator.order_by);

sort(posts.data, (a, b) => (b.sticky || 0) - (a.sticky || 0));

const paginationDir = config.pagination_dir || 'page';
const path = config.index_generator.path || '';

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"nyc": "^15.0.0"
},
"dependencies": {
"hexo-pagination": "1.0.0"
"hexo-pagination": "1.0.0",
"timsort": "^0.3.0"
}
}