51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const visiblePostFilter = post => !post.hide;
|
|
|
|
const createPostQuery = hexo => {
|
|
const query = {};
|
|
|
|
if (!hexo.config.future) query.date = { $lte: Date.now() };
|
|
if (!hexo._showDrafts()) query.published = true;
|
|
|
|
return query;
|
|
};
|
|
|
|
hexo.extend.filter.register('after_init', () => {
|
|
const getAllPosts = () => hexo.database.model('Post').find(createPostQuery(hexo));
|
|
const getVisiblePosts = () => getAllPosts().filter(visiblePostFilter);
|
|
|
|
hexo.locals.set('posts', () => getVisiblePosts());
|
|
hexo.locals.set('categories', () => hexo.model('Category').filter(category => category.posts.length));
|
|
hexo.locals.set('tags', () => hexo.model('Tag').filter(tag => tag.posts.length));
|
|
|
|
hexo.extend.generator.register('post', () => {
|
|
const posts = getAllPosts().sort('-date').toArray();
|
|
const visiblePosts = getVisiblePosts().sort('-date').toArray();
|
|
const visibleIndex = new Map(visiblePosts.map((post, index) => [String(post._id), index]));
|
|
|
|
return posts.map(post => {
|
|
const { path, layout } = post;
|
|
const index = visibleIndex.get(String(post._id));
|
|
|
|
post.prev = null;
|
|
post.next = null;
|
|
|
|
if (index !== undefined) {
|
|
post.prev = index > 0 ? visiblePosts[index - 1] : null;
|
|
post.next = index < visiblePosts.length - 1 ? visiblePosts[index + 1] : null;
|
|
}
|
|
|
|
if (!layout || layout === 'false') return { path, data: post.content };
|
|
|
|
const layouts = ['post', 'page', 'index'];
|
|
|
|
if (layout !== 'post') layouts.unshift(layout);
|
|
|
|
post.__post = true;
|
|
|
|
return { path, layout: layouts, data: post };
|
|
});
|
|
});
|
|
});
|