Skip to content

Instantly share code, notes, and snippets.

@Markyparky56
Created June 19, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Markyparky56/f692b278f46219d3a7f5a6ffb8dee416 to your computer and use it in GitHub Desktop.
Save Markyparky56/f692b278f46219d3a7f5a6ffb8dee416 to your computer and use it in GitHub Desktop.
Jekyll Tag List
{% comment %} Get tags for each of the three collections: food, projects & journal {% endcomment %}
{% comment %} Then concatenate them into a single array {% endcomment %}
{% assign tags_food = (site.food | map: 'tags' | uniq) %}
{% assign tags_projects = (site.projects | map: 'tags' | uniq) %}
{% assign tags_journal = (site.journal | map: 'tags' | uniq) %}
{% assign tags = (tags_food | concat: tags_projects | concat: tags_journal | uniq) %}
{% comment %} Sort tag array alphabetically {% endcomment %}
{% assign sorted_tags = (tags | sort:0) %}
{% assign sorted_food_tags = (tags_food | sort:0) %}
{% assign sorted_projects_tags = (tags_projects | sort:0) %}
{% assign sorted_journal_tags = (tags_journal | sort:0) %}
{% comment %} map, flatten and sort {% endcomment %}
{% comment %} from:https://stackoverflow.com/questions/36479756/counting-collection-tags-in-jekyll {% endcomment %}
{% assign allFoodTags = (site.food | map: 'tags' | join: ',' | split: ',' | sort) %}
{% assign allProjectTags = (site.projects | map: 'tags' | join: ',' | split: ',' | sort) %}
{% assign allJournalTags = (site.journal | map: 'tags' | join: ',' | split: ',' | sort) %}
{% assign alltags = ((allFoodTags | concat: allProjectTags | concat: allJournalTags) | sort) %}
<hr/>
<div id="tags-page">
<div class="tags-list">
<h3>All Tagged Posts</h3>
{% for tag in sorted_tags %}
{% comment %} Count all instances of this tag in the alltags array {% endcomment %}
{% assign n = 0 %}
{% for tagToCheck in alltags %}
{% if tagToCheck == tag %}
{% assign n = n | plus: 1 %}
{% elsif n >= 1 %}
{% comment %} Since alltags is sorted we can assume that it has counted all instances if it reaches this point {% endcomment %}
{% break %}
{% endif %}
{% endfor %}
<a href="/tags/{{ tag }}" class="tag-link">
<span class="tag-name">{{ tag }}</span>
<span class="tag-count">({{ n }})</span>
</a>
{% unless tag == sorted_tags.last %} | {% endunless %}
{% endfor %}
</div>
<br>
<div class="tags-list">
<h3>All Tagged Food Posts</h3>
{% for tag in sorted_food_tags %}
{% comment %} Count all instances of this tag in the allFoodTags array {% endcomment %}
{% assign n = 0 %}
{% for tagToCheck in allFoodTags %}
{% if tagToCheck == tag %}
{% assign n = n | plus: 1 %}
{% elsif n >= 1 %}
{% comment %} Since allFoodTags is sorted we can assume that it has counted all instances if it reaches this point {% endcomment %}
{% break %}
{% endif %}
{% endfor %}
<a href="/tags/food/{{ tag }}" class="tag-link">
<span class="tag-name">{{ tag }}</span>
<span class="tag-count">({{ n }})</span>
</a>
{% unless tag == sorted_food_tags.last %} | {% endunless %}
{% endfor %}
</div>
<br>
<div class="tags-list">
<h3>All Tagged Project Posts</h3>
{% for tag in sorted_projects_tags %}
{% comment %} Count all instances of this tag in the allProjectsTags array {% endcomment %}
{% assign n = 0 %}
{% for tagToCheck in allProjectTags %}
{% if tagToCheck == tag %}
{% assign n = n | plus: 1 %}
{% elsif n >= 1 %}
{% comment %} Since allProjectsTags is sorted we can assume that it has counted all instances if it reaches this point {% endcomment %}
{% break %}
{% endif %}
{% endfor %}
<a href="/tags/projects/{{ tag }}" class="tag-link">
<span class="tag-name">{{ tag }}</span>
<span class="tag-count">({{ n }})</span>
</a>
{% unless tag == sorted_projects_tags.last %} | {% endunless %}
{% endfor %}
</div>
<br>
<div class="tags-list">
<h3>All Tagged Journal Posts</h3>
{% for tag in sorted_journal_tags %}
{% comment %} Count all instances of this tag in the allJournalTags array {% endcomment %}
{% assign n = 0 %}
{% for tagToCheck in allJournalTags %}
{% if tagToCheck == tag %}
{% assign n = n | plus: 1 %}
{% elsif n >= 1 %}
{% comment %} Since allJournalTags is sorted we can assume that it has counted all instances if it reaches this point {% endcomment %}
{% break %}
{% endif %}
{% endfor %}
<a href="/tags/journal/{{ tag }}" class="tag-link">
<span class="tag-name">{{ tag }}</span>
<span class="tag-count">({{ n }})</span>
</a>
{% unless tag == sorted_journal_tags.last %} | {% endunless %}
{% endfor %}
</div>
</div>
layout
page

Tags

{% include tags-list.html %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment