{% if site.relatedpostsenabled %}

<div class="relatedposts">

{% if site.tagrelationlevel %}
  {% assign tagrelationlevel = site.tagrelationlevel %}
{% else %}
  {% assign tagrelationlevel = 2 %}
{% endif %}

{% if site.maxrelatedposts %}
  {% assign maxrelatedposts = site.maxrelatedposts %}
{% else %}
  {% assign maxrelatedposts = 3 %}
{% endif %}

{% assign maxRelatedCounter = 0 %}

<ul>
  {% for post in site.posts %}

    {% assign sameTagCount = 0 %}
    {% assign commonTags = '' %}

    {% for tag in post.tags %}
      {% if post.url != page.url %}
        {% if page.tags contains tag %}
          {% assign sameTagCount = sameTagCount | plus: 1 %}
          {% capture tagmarkup %} {{ tag }} {% endcapture %}
          {% assign commonTags = commonTags | append: tagmarkup %}
        {% endif %}
      {% endif %}
    {% endfor %}

    {% if sameTagCount >= tagrelationlevel %}
      <li>Related reading: <a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a></li>
      {% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}
      {% if maxRelatedCounter >= maxrelatedposts %}
        {% break %}
      {% endif %}
    {% endif %}

  {% endfor %}
</ul>

</div>

{% endif %}