Estimated Reading Times in Jekyll

 — 3 Min Read

As previously noted, I've migrated the engine behind this site to Jekyll. In just about every way this has been a positive move, except that I lost my estimated read time feature due to a lack of number formatting filters in Liquid.

I recently got around to restoring said feature and it was a little more involved than before. Due to Liquid's non-existent number formatting filters and crummy handling of math, I had to put together a small plugin to handle the display of the estimate. This time around, I decided to go one step further and round off the result, as it's only an estimate anyway. If you'd like to do the same, create a file at /_plugins/read-time.rb and drop in the following code:

module ReadTimeFilter

    def readtime(input)

        charcount = 4.5
        wpm = 180

        rt = (input.to_f/charcount/wpm).round
        rt = 1 if rt < 1
        rt

    end

    Liquid::Template.register_filter self

end

Just like last time, we're using 4.5 as the mean character count of an English word and an average reading speed of 180wpm.

The filter expects a single integer equal to the character count of your content, like so:

{{ content.size | readtime }}

All the rounding is done for you, and if the result is less than half a minute, we're hard-setting the result to 1. Of course, you could change this behaviour to whatever you like.

I should note that I'm hardly a programmer let alone experienced with Ruby, so if there's a better way to do this I'd love to know. However, this works for me, so I thought I'd share. Enjoy!