hexo-plugin-aurora
Version:
A plugin for Hexo Aurora theme
83 lines (70 loc) • 1.86 kB
JavaScript
const pagination = require('hexo-pagination');
const { tagMapper, tagPageMapper, postListMapper } = require('../helpers/mapper');
class TagGenerator {
data = [];
posts = [];
configs = {};
constructor(tags, posts, configs) {
this.data = tags ?? [];
this.posts = posts;
this.configs = configs;
if (this.data.length < 1) return;
this.reduceTags();
for (let tag of this.data) {
tag.data.postlist.sort(function (a, b) {
return a.date < b.date ? 1 : -1;
});
}
}
sortTags() {
this.data = Object.assign(this.data, []).sort(function (a, b) {
return b.posts.length - a.posts.length;
});
}
reduceTags() {
if (this.count() <= 0) return;
const tags = this.data;
const posts = this.posts;
const configs = this.configs;
this.data = tags.reduce(function (result, item) {
if (!item.length) return result;
return result.concat(
pagination(item.path, posts, {
perPage: 0,
data: {
name: item.name,
slug: item.slug,
count: item.count ?? item.posts.length,
path: 'api/tags/' + item.slug + '.json',
postlist: item.posts.map((post) => {
return postListMapper(post, configs);
})
}
})
);
}, []);
}
addTags(data) {
if (this.count() <= 0) {
data.push({
path: 'api/tags.json',
data: JSON.stringify([])
});
} else {
data.push({
path: 'api/tags.json',
data: JSON.stringify(
this.data.map(tagMapper).sort(function (a, b) {
return b.count - a.count;
})
)
});
data = data.concat(this.data.map(tagPageMapper));
}
return data;
}
count() {
return this.data.length;
}
}
module.exports = TagGenerator;