---
layout: archive
---

{{ content }}

{% comment %} Flatten all documents in all categories {% endcomment %}

{% assign docs = "" | split: "" %}
{% for collection in site.collections %}
  {% assign docs = docs | concat: collection.docs %}
{% endfor %}

{% comment %} Extract all tags {% endcomment %}

{% for doc in docs %}
  {% assign all_tags = all_tags | concat: doc.tags %}
{% endfor %}

{% assign unique_tags = all_tags | uniq %}

{% assign tags_count = "" | split: "" %}
{% assign tagged_docs = "" | split: "" %}
{% assign tags_max = 0 %}

{% 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 %}
      {% assign count = count | plus: 1 %}
    {% endif %}
  {% endfor %}
  {% 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 %}

  {% if count > tags_max %}
    {% assign tags_max = count %}
  {% endif %}
{% endfor %}

<ul class="taxonomy__index">
  {% for i in (1..tags_max) reversed %}
    {% for tag in unique_tags %}
      {% if tags_count[forloop.index0] == i %}
        <li>
          <a href="#{{ tag | slugify | downcase }}">
            <strong>{{ tag }}</strong> <span class="taxonomy__count">{{ i }}</span>
          </a>
        </li>
      {% endif %}
    {% endfor %}
  {% endfor %}
</ul>

{% assign entries_layout = page.entries_layout | default: 'list' %}
{% for i in (1..tags_max) reversed %}
  {% for tag in unique_tags %}
    {% if tags_count[forloop.index0] == i %}
      <section id="{{ tag | slugify | downcase }}" class="taxonomy__section">
        <h2 class="archive__subtitle">{{ tag }}</h2>
        <div class="entries-{{ entries_layout }}">
          {% for post in tagged_docs[forloop.index0] %}
            {% include archive-single.html type=entries_layout %}
          {% endfor %}
        </div>
        <a href="#page-title" class="back-to-top">{{ site.data.ui-text[site.locale].back_to_top | default: 'Back to Top' }} &uarr;</a>
      </section>
    {% endif %}
  {% endfor %}
{% endfor %}
