hexojs / hexo-generator-index

Index generator plugin for Hexo.
MIT License
53 stars 45 forks source link

Sort by a new 'priority' parameter #26

Closed tomap closed 4 years ago

tomap commented 5 years ago

Bump minor, because there could be a minor change in behavior if some user already have a priority parameter on their posts

Based upon the work of #6

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-25.0%) to 75.0% when pulling 69992b01064f4d353f50030bdeb489fb4e276e4b on tomap:master into e4129aeb24bd7dbfee9b404eac14579d4dbadd83 on hexojs:master.

YunYouJun commented 5 years ago

I've been looking forward to this feature for a long time. The earlier merged, the better.


By the way, I found some theme such as hexo-theme-next, hexo-theme-melody etc. They had added sticky style. Maybe use sticky replace priority is ok?

stevenjoezhang commented 5 years ago

The parameter config.index_generator.order_by is ignored

curbengh commented 5 years ago

The parameter config.index_generator.order_by is ignored

That's the intention.

note that if your posts have a `priority` property, it will be used to order your post taking precedence over `order_by`.
stevenjoezhang commented 5 years ago

That's the intention.

note that if your posts have a priority property, it will be used to order your post taking precedence over order_by.

But how about posts with same priority? It does not match the description.

tomap commented 5 years ago

well, I just looked at the code again, and it is not what I intended... I was trying to reproduce the work of https://github.com/hexojs/hexo-generator-index/pull/6 But what we have here is something that will break the parameter config.index_generator.order_by in all cases, which is not good

I believe the configuration config.index_generator.order_by can be used to order by priority, if a priority has been defined on all posts What might be missing is a was to order by priority first, if there is a priority defined, it goes on top If not, the config.index_generator.order_by is used I'm not sure how to code that :)

stevenjoezhang commented 5 years ago

Yes, the issue is: When two or more posts have the same priority, the sorting should fall back to config.index_generator.order_by, but not

if(a.priority == b.priority) return b.date - a.date;
stevenjoezhang commented 4 years ago

But what we have here is something that will break the parameter config.index_generator.order_by in all cases, which is not good

@tomap I think you are talking about the stability of the sorting algorithm. Node.js 12+ uses stable sorting algorithms by default: https://v8.dev/features/stable-sort . Therefore, you can sort locals.posts by config.index_generator.order_by (e.g. '-date') first, and then sort locals.posts.data according to the value of sticky or priority parameter. For older Nodejs versions, lodash is required to implement this method.

curbengh commented 4 years ago

@stevenjoezhang

can you create another PR with your patch? This PR has been inactive for a while...

SukkaW commented 4 years ago

cc @stevenjoezhang

stevenjoezhang commented 4 years ago

@curbengh @SukkaW Superseded by https://github.com/hexojs/hexo-generator-index/pull/51