Jekyll article sell-by dates

Written by Kevin Hougasian
3
This post is days old.

So much work has gone into rimdev.io, and we like to keep content fresh, or at least let you know you may be reading an old or outdated article. Enter the freshness scale.

Leading every article on rimdev.io is a heart rate icon (hover over it for our rating scale). Doesn’t seem that difficult, so how did we get there using Jekyll?

Get the dates

Get values for today and posted; subtract posted from today. If you’re new to liquid syntax, it’s the minus pipe.

{% capture today %}{{ "now" | date: "%s" }}{% endcapture %}
{% capture posted %}{{ page.date | date: "%s" }}{% endcapture %}
{% capture freshness %}{{ today | minus: posted | date: "%s" | date: "%j" | floor }}{% endcapture %}

Gotcha

So this worked great, we had our freshness date, right? What about articles a post dated for the same day, today? Let’s see if they’re the same day of the year %j (1-365). Note freshness | times: 1, freshness is a string until here.

{% capture compareToday %}{{ today | date: "%j" | floor }}{% endcapture %}
{% capture comparePosted %}{{ posted | date: "%j" | floor }}{% endcapture %}

{% if compareToday == comparePosted %}
  {% assign freshness = 0 %}
{% else %}
  {% assign freshness = freshness | times: 1 %}
{% endif %}

Apply freshness stamp how you see fit

From here, we assigned classes to determine freshness, you can handle it how you see fit!

{% if freshness <= 30 %}
  {% assign class = "new" %}
{% endif %}
{% if freshness > 31 and freshness < 190 %}
  {% assign class = "old" %}
{% endif %}
{% if freshness > 191 and freshness < 364 %}
  {% assign class = "stale" %}
{% endif %}
{% if freshness > 365 %}
  {% assign class = "rubbish" %}
{% endif %}
Suggested reading

Comments