Mike Slinn

jekyll_reading_time

Published 2020-10-03. Last modified 2023-05-18.
Time to read: 1 minutes.

This page is part of the jekyll_plugins collection.

jekyll_reading_time is a Jekyll plugin that provides a Liquid filter that intelligently counts the number of words in a piece of HTML and estimates how long the text will take to read.

Only readable words contribute toward the count. HTML tags are not counted, and words are also not counted if they are contained within any of the following HTML elements: area, audio, canvas, code, embed, footer, form, img, map, math, nav, object, pre, script, svg, table, track, and video.

The plugin assumes a reading speed of 270 words per minute. Wikipedia cites 250 to 300 words per minute as a typical range.

Usage

Two functions are provided:

  • reading_time – This function gives an estimate of the amount of time it will take to read the input text. The return value is an integer number of minutes. The input should be HTML (i.e. the text should have already been run through your Markdown or Textile filter). For example, you could use it in a _layout file like this:
    HTML or markdown
    {% capture time %}{{ content | reading_time }}{% endcapture %}
    <p>
      This article will take
      {{ time }} {% if time == '1' %}minute{% else %}minutes{% endif %}
      to read.
    </p>

    Even better, use the pluralize filter. Because it is a dependency, it is automatically installed when this gem is installed:
    HTML or markdown
    <p>
      This article will take
      {{ content | reading_time | pluralize: "minute" }}
      to read.
    </p>
  • count_words – This function returns the number of words in the input. Like reading_time, this function accepts HTML.

Installation

Add this line to your application's Gemfile:

Gemfile
$ gem 'jekyll_reading_time'

And then execute:

Shell
$ bundle
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.