From c2dad099f6217778a2143f79baf28fc51794696f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=8D=C9=AA=E1=B4=8D=C9=AA?= <1119186082@qq.com> Date: Fri, 4 Oct 2019 22:30:22 +0800 Subject: [PATCH] refactor: ignore categories / tags with zero posts (#3624) --- lib/hexo/index.js | 10 ++++++++-- lib/plugins/helper/tagcloud.js | 3 --- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 4060dc4228..065b7619d3 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -136,9 +136,15 @@ Hexo.prototype._bindLocals = function() { return db.model('Page').find(query); }); - locals.set('categories', () => db.model('Category')); + locals.set('categories', () => { + // Ignore categories with zero posts + return db.model('Category').filter(category => category.length); + }); - locals.set('tags', () => db.model('Tag')); + locals.set('tags', () => { + // Ignore tags with zero posts + return db.model('Tag').filter(tag => tag.length); + }); locals.set('data', () => { const obj = {}; diff --git a/lib/plugins/helper/tagcloud.js b/lib/plugins/helper/tagcloud.js index 9fd5bda421..d4ad0e3c75 100644 --- a/lib/plugins/helper/tagcloud.js +++ b/lib/plugins/helper/tagcloud.js @@ -36,9 +36,6 @@ function tagcloudHelper(tags, options) { tags = tags.sort(orderby, order); } - // Ignore tags with zero posts - tags = tags.filter(tag => tag.length); - // Limit the number of tags if (options.amount) { tags = tags.limit(options.amount);