Adding 'Minutes to read' to your Jekyll blog

• 1 minute to read
🕐 This article is more than 7 years old. Its content might be outdated 🕐

I created a code snippet to calculate reading time on a given blog post for Jekyll.

I got this idea from Medium's interface, where you can see each post's reading time, as you can see below:

Medium reading time Medium, as elegant as ever.

I found online a basic snippet of code that runs like this:

 • {% capture words %}
      {{ page.content | number_of_words | minus: 180 }}
    {% endcapture %}
    {% unless words contains "-" %}
      {% words | plus: 180 | divided_by: 180 | append: " minutes to read." %}
    {% endunless %}

However, the snippet above does not differentiate between singular and plural, and if the number of minutes is 1, it writes "1 minutes to read".

Thus, I added a little bit more of logic to differentiate between the two cases:

 • {% capture words %}
      {{ page.content | number_of_words | minus: 180 }}
   {% endcapture %}
   {% unless words contains "-" %}
     {% assign minutes = words | plus: 180 | divided_by: 180 %}
        {{ minutes }}
     {% if minutes == 1 %}
        {{ " minute to read." }}
     {% else %}
        {{ " minutes to read." }}
     {% endif %}
   {% endunless %}

I tried to do it in a simpler way taking advantage of Liquid's Liquid's 'pluralize' tag but it doesn't seem to work, for whatever reason.

If you want to keep things more organised, create a file named post-readingtime.erb including this code snippet and save it in your _includes folder. Then, go to your post.html file and call it like this:

Medium reading time

Hope that was useful! If you happen to have why is 'pluralize' not working well here, let me know in the comments section below!

Now Playing: Manel - Un directiu em va acomiadar

Àlex Rodríguez Bacardit

Àlex Rodríguez Bacardit

CEO and Founder at MarsBased and Director at Startup Grind Barcelona. I run a team of 20 people, where I spearhead the sales and strategy areas. My background in consulting and development (ex-Deloitte, ex-VASS) and my international profile help me with the technical and the business perspective. I love loud guitars, cats, travelling and tacos.