From c08424aa55ce322c5dc066ca672413d76f9c7e8d Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Wed, 14 Dec 2022 02:10:52 +0900 Subject: [PATCH] Improve tags layout template --- _layouts/categories.html | 2 ++ _layouts/tags.html | 18 +++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/_layouts/categories.html b/_layouts/categories.html index f8dc790..e7226ba 100644 --- a/_layouts/categories.html +++ b/_layouts/categories.html @@ -19,6 +19,8 @@ layout: archive {% endfor %} +{% comment %} Make sure that all collections are listed in the _config.yml. {% endcomment %} + {% assign entries_layout = page.entries_layout | default: 'list' %} {% for label in site.collections_order %} {% assign collection = site.collections | where: "label", label | first %} diff --git a/_layouts/tags.html b/_layouts/tags.html index 158b47d..a150506 100644 --- a/_layouts/tags.html +++ b/_layouts/tags.html @@ -4,18 +4,17 @@ layout: archive {{ content }} -{% assign unique_tags = "" | split: "" %} -{% assign nested_docs = site.collections | map: "docs" %} -{% assign docs = "" | split: "" %} +{% comment %} Flatten all documents in all categories {% endcomment %} -{% for doc in nested_docs %} - {% assign docs = docs | concat: doc %} +{% assign docs = "" | split: "" %} +{% for collection in site.collections %} + {% assign docs = docs | concat: collection.docs %} {% endfor %} -{% assign found_tags = docs | map: "tags" %} +{% comment %} Extract all tags {% endcomment %} -{% for tags in found_tags %} - {% assign all_tags = all_tags | concat: tags %} +{% for doc in docs %} + {% assign all_tags = all_tags | concat: doc.tags %} {% endfor %} {% assign unique_tags = all_tags | uniq %} @@ -26,6 +25,7 @@ layout: archive {% for tag in unique_tags %} {% comment %} Count the number of tag occurrence in all docs {% endcomment %} + {% assign count = 0 %} {% for t in all_tags %} {% if t == tag %} @@ -35,6 +35,7 @@ layout: archive {% assign tags_count = tags_count | push: count %} {% comment %} Group docs by tags {% endcomment %} + {% assign found = docs | where_exp: "item", "item.tags contains tag" %} {% assign tagged_docs = tagged_docs | push: found %} @@ -43,7 +44,6 @@ layout: archive {% endif %} {% endfor %} -