Related links in Github Pages

Jekyll comes with a built in ‘Related Links’ feature, however - if you run your Jekyll blog on Github Pages then it’s just the latest N posts.

Related posts by common tags would take about 3 lines of ruby, but because Github Pages is so locked down1 I must do it in Liquid. This really isn’t what liquid is for, but it doesn’t have to be fast or pretty - it’s only run once per deploy.

Here is my Liquid-only related posts script. It reminds me of the dark horror-filled days of Smarty templates. *shudder*

{% assign limit = '4' %}
{% assign matched = '' %}
{% for matching_tag_count in (1..page.tags.size) reversed %}
  {% for tag in page.tags %}
    {% for tag_page in site.tags[tag] %}
      {% if limit == '0' %}
        {% break %}
      {% else %}
      {% unless tag_page.url == page.url %}
      {% capture url_compare %}$${{tag_page.url}}$${% endcapture %}
      {% unless matched contains url_compare %}
      {% assign appearances = '0' %}
      {% for tag_page_tag in tag_page.tags %}
        {% if page.tags contains tag_page_tag %}
          {% capture appearances %}{{ appearances | plus:1 }}{% endcapture %}
        {% endif %}
      {% endfor %}
      {% capture difference %}{{ appearances | minus:matching_tag_count }}{% endcapture %}
      {% if difference == '0' %}
        {% capture matched %}{{matched}}{{url_compare}}{% endcapture %}
        {% capture limit %}{{limit | minus:1}}{% endcapture %}
        {% assign archive_page = tag_page %}
        {% include archive_row.html %}
      {% endif %}
      {% endunless %}
      {% endunless %}
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endfor %}

I was going to go through and explain every line but liquid leaves me cold. I don’t understand it enough to know its limitations properly - such as why arithmetic works better with strings than numbers - I just do what it wants and be sad about the verbosity and inefficiency.