From 4b19aa97d0a051bfb690e4948a27f85497ae3276 Mon Sep 17 00:00:00 2001 From: "Mr.J" <116749895@qq.com> Date: Sat, 13 Jul 2019 00:02:43 +0800 Subject: [PATCH] Revert "Avoid recalculating the number of tags and categories (#954)" (#969) This reverts commit a8e2aefd75d5b270cb6131ac8a00b443b1718849. --- layout/_macro/menu/menu-badge.swig | 4 ++-- layout/_macro/sidebar.swig | 16 ++++++++++++---- layout/page.swig | 16 ++++++++++++++-- scripts/merge-configs.js | 16 ---------------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/layout/_macro/menu/menu-badge.swig b/layout/_macro/menu/menu-badge.swig index 6547969f6d..b2dcce135d 100644 --- a/layout/_macro/menu/menu-badge.swig +++ b/layout/_macro/menu/menu-badge.swig @@ -2,8 +2,8 @@ {% set badges = { archives: site.posts.length, - categories: theme.visibleCategories, - tags: theme.visibleTags } + categories: site.categories.length, + tags: site.tags.length } %} {% for menu, count in badges %} {% if name == menu %} diff --git a/layout/_macro/sidebar.swig b/layout/_macro/sidebar.swig index b458ff4217..589f149bb7 100644 --- a/layout/_macro/sidebar.swig +++ b/layout/_macro/sidebar.swig @@ -50,7 +50,7 @@ {% endif %} - {% if theme.visibleCategories > 0 %} + {% if site.categories.length > 0 %} {% set categoriesPageQuery = site.pages.find({type: 'categories'}, {lean: true}) %} {% set hasCategoriesPage = categoriesPageQuery.length > 0 %}
@@ -61,13 +61,17 @@ {% endif %} {% endif %} - {{ theme.visibleCategories }} + {% set visibleCategories = 0 %} + {% for cat in site.categories %} + {% if cat.length %}{% set visibleCategories += 1 %}{% endif %} + {% endfor %} + {{ visibleCategories }} {{ __('state.categories') }} {% if hasCategoriesPage %}{% endif %}
{% endif %} - {% if theme.visibleTags > 0 %} + {% if site.tags.length > 0 %} {% set tagsPageQuery = site.pages.find({type: 'tags'}, {lean: true}) %} {% set hasTagsPage = tagsPageQuery.length > 0 %}
@@ -78,7 +82,11 @@ {% endif %} {% endif %} - {{ theme.visibleTags }} + {% set visibleTags = 0 %} + {% for tag in site.tags %} + {% if tag.length %}{% set visibleTags += 1 %}{% endif %} + {% endfor %} + {{ visibleTags }} {{ __('state.tags') }} {% if hasTagsPage %}{% endif %}
diff --git a/layout/page.swig b/layout/page.swig index 2358b6e404..b2c08712cc 100644 --- a/layout/page.swig +++ b/layout/page.swig @@ -33,7 +33,13 @@ {% if page.type === 'tags' %}
- {{ _p('counter.tag_cloud', theme.visibleTags) }} + {% set visibleTags = 0 %} + {% for tag in site.tags %} + {% if tag.length %} + {% set visibleTags += 1 %} + {% endif %} + {% endfor %} + {{ _p('counter.tag_cloud', visibleTags) }}
{% if not theme.tagcloud %} @@ -46,7 +52,13 @@ {% elif page.type === 'categories' %}
- {{ _p('counter.categories', theme.visibleCategories) }} + {% set visibleCategories = 0 %} + {% for cat in site.categories %} + {% if cat.length %} + {% set visibleCategories += 1 %} + {% endif %} + {% endfor %} + {{ _p('counter.categories', visibleCategories) }}
{{ list_categories() }} diff --git a/scripts/merge-configs.js b/scripts/merge-configs.js index 944be3a9a8..dab00ac670 100644 --- a/scripts/merge-configs.js +++ b/scripts/merge-configs.js @@ -46,20 +46,4 @@ hexo.on('generateBefore', function() { // Add filter type `theme_inject` require('./injects')(hexo); - // Fix an issue about the categories/tags count. - let visibleTags = 0; - hexo.locals.get('tags').forEach((tag) => { - if (tag.length) { - visibleTags += 1; - } - }); - hexo.theme.config.visibleTags = visibleTags; - let visibleCategories = 0; - hexo.locals.get('categories').forEach((categorie) => { - if (categorie.length) { - visibleCategories += 1; - } - }); - hexo.theme.config.visibleCategories = visibleCategories; - });