网上教程还是挺多的,但难免有不清楚的地方,我根据我自身的情况说一下吧,基本上是根据下面这个博文来的。

链接

主要想说明的是,我的hexo版本是3.3.1,在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
'use strict';

var pagination = require('hexo-pagination');

module.exports = function(locals) {
var config = this.config;
var posts = locals.posts.sort(config.index_generator.order_by);

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
}
});
};

保存后,在文章的front-matter中添加top: 1这一样就可以实现文章置顶了,“1”也可以用其他数字替换,数字越大,越前面。