From 2300defc1fb8c2b68bfadbefcaa76232d3736ce6 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sun, 26 Apr 2020 23:26:33 +0800 Subject: [PATCH 1/3] Sort by a new `sticky` parameter --- lib/generator.js | 4 ++++ package-lock.json | 13 +++++++++++++ package.json | 3 ++- 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 package-lock.json diff --git a/lib/generator.js b/lib/generator.js index 6273128..29b0895 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -1,10 +1,14 @@ 'use strict'; const pagination = require('hexo-pagination'); +const _ = require('lodash'); module.exports = function(locals) { const config = this.config; const posts = locals.posts.sort(config.index_generator.order_by); + + posts.data = _.orderBy(posts.data, post => post.sticky || 0, "desc"); + const paginationDir = config.pagination_dir || 'page'; const path = config.index_generator.path || ''; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f24fde9 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "hexo-generator-index", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + } + } +} diff --git a/package.json b/package.json index c5494c8..1cd2fce 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "nyc": "^15.0.0" }, "dependencies": { - "hexo-pagination": "1.0.0" + "hexo-pagination": "1.0.0", + "lodash": "^4.17.15" } } From 4e5958a0fd9a439146950b6db0244e13a5cafcc0 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Mon, 27 Apr 2020 10:15:56 +0800 Subject: [PATCH 2/3] Use timsort instead of lodash --- lib/generator.js | 4 ++-- package-lock.json | 13 ------------- package.json | 2 +- 3 files changed, 3 insertions(+), 16 deletions(-) delete mode 100644 package-lock.json diff --git a/lib/generator.js b/lib/generator.js index 29b0895..6ec9415 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -1,13 +1,13 @@ 'use strict'; const pagination = require('hexo-pagination'); -const _ = require('lodash'); +const { sort } = require('timsort'); module.exports = function(locals) { const config = this.config; const posts = locals.posts.sort(config.index_generator.order_by); - posts.data = _.orderBy(posts.data, post => post.sticky || 0, "desc"); + sort(posts.data, (a, b) => (b.sticky || 0) - (a.sticky || 0)); const paginationDir = config.pagination_dir || 'page'; const path = config.index_generator.path || ''; diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index f24fde9..0000000 --- a/package-lock.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "hexo-generator-index", - "version": "1.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" - } - } -} diff --git a/package.json b/package.json index 1cd2fce..3f52e85 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,6 @@ }, "dependencies": { "hexo-pagination": "1.0.0", - "lodash": "^4.17.15" + "timsort": "^0.3.0" } } From 420361065ad5df94b06037aa134e7df0b5f421ba Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Fri, 12 Jun 2020 22:53:22 +0800 Subject: [PATCH 3/3] Update README.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 86e8df0..5965500 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ $ npm install hexo-generator-index --save ``` ## Options + Add or modify the following section to your root _config.yml file ``` yaml @@ -36,6 +37,18 @@ index_generator: - default: 'page' - `awesome-page` makes the URL ends with 'awesome-page/' 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.