Skip to content

Commit

Permalink
feat: new sticky parameter (#51)
Browse files Browse the repository at this point in the history
* Sort by a new `sticky` parameter

* Use timsort instead of lodash

* Update README.md
  • Loading branch information
stevenjoezhang committed Jun 12, 2020
1 parent f136319 commit d1910fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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
---
```

## 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"
}
}

0 comments on commit d1910fe

Please sign in to comment.