Hexo博客通过插件实现文章置顶
通过改用hexo-generator-index-pin-top或修改hexo-generator-index源码实现文章置顶
更换文章排序插件
改用hexo-generator-index-pin-top插件
卸载默认排序插件hexo-generator-index
npm uninstall hexo-generator-index
更换为hexo-generator-index-pin-top插件
npm install hexo-generator-index-pin-top --save
在Front-matter中添加top: true即可开启置顶功能,置顶多篇文章时参数值越大排序越靠前
修改源码
修改hexo-generator-index插件
作为默认的博客文章排序插件却不支持文章置顶,有点可惜,但可以通过修改源码实现多篇文章按需置顶
const config = this.config;
- const posts = locals.posts.sort(config.index_generator.order_by);
+ const posts = locals.posts;
+ posts.data = posts.data.sort(function(a, b) {
+ if(a.top && b.top) {
+ if(a.top == b.top) return b.date - a.date;
+ else return b.top - a.top;
+ }
+ else if(a.top && !b.top) {
+ return -1;
+ }
+ else if(!a.top && b.top) {
+ return 1;
+ }
+ else return b.date - a.date;
+ });
- sort(posts.data, (a, b) => (b.sticky || 0) - (a.sticky || 0));
const paginationDir = config.pagination_dir || 'page';
const path = config.index_generator.path || '';
参考hexo-generator-index-pin-top做相应修改,若选用主题按date排序,需要对相应文件做修改,所以推荐改用hexo-generator-index-pin-top,一步到位
评论 ()
TwikooValine