Adding 'Minutes to read' to your Jekyll blog
I created a code snippet to calculate reading time on a given blog post for Jekyll.

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:
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:
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 🤘🏻