hexo-theme-amane/includes/generators/category.js

34 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-10-16 05:28:42 +00:00
const pagination = require('hexo-pagination');
module.exports = function (hexo) {
// ATTENTION: This will override the default category generator!
hexo.extend.generator.register('category', function(locals) {
const config = this.config;
const perPage = config.category_generator.per_page;
const paginationDir = config.pagination_dir || 'page';
function findParent(category) {
let parents = [];
if (category && category.hasOwnProperty('parent')) {
const parent = locals.categories.filter(cat => cat._id === category.parent).first();
parents = [parent].concat(findParent(parent));
}
return parents;
}
return locals.categories.reduce(function(result, category){
const posts = category.posts.sort('-date');
const data = pagination(category.path, posts, {
perPage: perPage,
layout: ['category', 'archive', 'index'],
format: paginationDir + '/%d/',
data: {
category: category.name,
parents: findParent(category)
}
});
return result.concat(data);
}, []);
});
}