Hexo添加文章置顶功能
2016.10.22
Lany
前端
 热度
℃
考虑到之前的博客有置顶文章,所以需要置顶功能。
修改node_modules/hexo-generator-index/lib/generator.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 'use strict'; var pagination = require('hexo-pagination'); module.exports = function(locals){ var config = this.config; var 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; }); var paginationDir = config.pagination_dir || 'page'; return pagination('', posts, { perPage: config.index_generator.per_page, layout: ['index', 'archive'], format: paginationDir + '/%d/', data: { __index: true } }); };
|
修改generator.js
完成后只需要在 front-matter 中设置需要置顶文章的top
值,将会根据 top 值大小来选择置顶顺序。(大的在前面)
1 2 3 4 5
| title: date: tags: top: 0 ---
|
原文地址:http://www.netcan666.com/2015/11/22/解决Hexo置顶问题/