本文记录了 Hexo 首页与归档博文排序如何自定义,想实现的功能是 index 按照修改日期排序,最近修改的文章置顶显示;Archives 归档则按照默认的创建时间前后排序。
Reference: 解决Hexo置顶问题
Hexo 目录下的 node_modules 中存放跟 hexo generator 命令相关的 hexo 模块,其中 /hexo-generator-index/lib/generator.js 还有 /hexo-generator-archive/lib/generator.js 分别用于生成 index 和 Archives 归档页面。
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
| '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) { return b.updated - a.updated; }); var paginationDir = config.pagination_dir || 'page'; return pagination('', posts, { perPage: config.index_generator.per_page, layout: ['index', 'archive'], format: paginationDir + '/%d/', data: { __index: true } }); };
|