Add "group by array" include to simplify archive pages

This commit is contained in:
Michael Rose 2016-03-04 21:45:30 -05:00
parent cf42731352
commit 239c949298
4 changed files with 96 additions and 36 deletions

View file

@ -0,0 +1,47 @@
<!--
# Jekyll Group-By-Array 0.1.0
# https://github.com/mushishi78/jekyll-group-by-array
# © 2015 Max White <mushishi78@gmail.com>
# MIT License
-->
<!-- Initialize -->
{% assign __empty_array = '' | split: ',' %}
{% assign group_names = __empty_array %}
{% assign group_items = __empty_array %}
<!-- Map -->
{% assign __names = include.collection | map: include.field %}
<!-- Flatten -->
{% assign __names = __names | join: ',' | join: ',' | split: ',' %}
<!-- Uniq -->
{% assign __names = __names | sort %}
{% for name in __names | sort %}
<!-- If not equal to previous then it must be unique as sorted -->
{% unless name == previous %}
<!-- Push to group_names -->
{% assign group_names = group_names | push: name %}
{% endunless %}
{% assign previous = name %}
{% endfor %}
<!-- group_items -->
{% for name in group_names %}
<!-- Collect if contains -->
{% assign __item = __empty_array %}
{% for __element in include.collection %}
{% if __element[include.field] contains name %}
{% assign __item = __item | push: __element %}
{% endif %}
{% endfor %}
<!-- Push to group_items -->
{% assign group_items = group_items | push: __item %}
{% endfor %}

View file

@ -4,24 +4,19 @@ permalink: /category-archive/
title: "Posts by Category"
---
{% include absolute-url.liquid %}
{% capture site_categories %}{% for category in site.categories %}{{ category | first }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}
{% assign category_list = site_categories | split:',' | sort %}
{% include group-by-array.html collection=site.posts field='categories' %}
{% for item in (0..site.categories.size) %}
{% unless forloop.last %}
{% capture category_name %}{{ category_list[item] | strip_newlines }}{% endcapture %}
<h3>{{ category_name }}</h3>
{% for post in site.categories[category_name] %}
{% if post.title != null %}
<article itemscope itemtype="http://schema.org/CreativeWork">
{% if post.link %}
<h2 class="link-post" itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a> <a href="{{ post.link }}" target="_blank" title="{{ post.title }}"><i class="fa fa-link"></i></a></h2>
{% else %}
<h2 itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a></h2>
<p itemprop="description">{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}</p>
{% endif %}
</article>
{% for category in group_names %}
{% assign posts = group_items[forloop.index0] %}
<h3>{{ category }}</h3>
{% for post in posts %}
<article itemscope itemtype="http://schema.org/CreativeWork">
{% if post.link %}
<h2 class="link-post" itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a> <a href="{{ post.link }}" target="_blank" title="{{ post.title }}"><i class="fa fa-link"></i></a></h2>
{% else %}
<h2 itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a></h2>
<p itemprop="description">{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}</p>
{% endif %}
{% endfor %}
{% endunless %}
</article>
{% endfor %}
{% endfor %}

View file

@ -0,0 +1,22 @@
---
layout: archive
title: "Recipes Archive"
permalink: /recipes-archive/
---
{% include absolute-url.liquid %}
{% include group-by-array.html collection=site.recipes field='categories' %}
{% for category in group_names %}
{% assign posts = group_items[forloop.index0] %}
<h3>{{ category }}</h3>
{% for post in posts %}
<article itemscope itemtype="http://schema.org/CreativeWork">
{% if post.link %}
<h2 class="link-post" itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a> <a href="{{ post.link }}" target="_blank" title="{{ post.title }}"><i class="fa fa-link"></i></a></h2>
{% else %}
<h2 itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a></h2>
<p itemprop="description">{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}</p>
{% endif %}
</article>
{% endfor %}
{% endfor %}

View file

@ -4,23 +4,19 @@ permalink: /tag-archive/
title: "Posts by Tags"
---
{% include absolute-url.liquid %}
{% capture site_tags %}{% for tag in site.tags %}{{ tag | first }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}
{% assign tag_list = site_tags | split:',' | sort %}
{% include group-by-array.html collection=site.posts field='tags' %}
{% for item in (0..site.tags.size) %}
{% unless forloop.last %}
{% capture tag_name %}{{ tag_list[item] | strip_newlines }}{% endcapture %}
<h3>{{ tag_name }}</h3>
{% for post in site.tags[tag_name] %}{% if post.title != null %}
<article itemscope itemtype="http://schema.org/CreativeWork">
{% if post.link %}
<h2 class="link-post" itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a> <a href="{{ post.link }}" target="_blank" title="{{ post.title }}"><i class="fa fa-link"></i></a></h2>
{% else %}
<h2 itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a></h2>
<p itemprop="description">{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}</p>
{% endif %}
</article>
{% endif %}
{% endfor %}
{% endunless %}
{% for tag in group_names %}
{% assign posts = group_items[forloop.index0] %}
<h3>{{ tag }}</h3>
{% for post in posts %}
<article itemscope itemtype="http://schema.org/CreativeWork">
{% if post.link %}
<h2 class="link-post" itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a> <a href="{{ post.link }}" target="_blank" title="{{ post.title }}"><i class="fa fa-link"></i></a></h2>
{% else %}
<h2 itemprop="headline"><a href="{{ absurl }}{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a></h2>
<p itemprop="description">{{ post.excerpt | markdownify | strip_html | strip_newlines | escape_once }}</p>
{% endif %}
</article>
{% endfor %}
{% endfor %}