ReadingTime Define

By the way, you can also calculate the reading time yourself if you want. That way it’s possible to use a custom words per minutes to estimate reading time. Or use seconds in the output (like the readingtime front matter variable from the first post seems to do).

This Hugo template code outputs the reading time in minutes and seconds, assuming 220 words per minute:

{{ $readTime := mul (div (countwords .Content) 220.0) 60 }}

{{ $minutes := math.Floor (div $readTime 60) }}
{{ $seconds := mod $readTime 60 }}

<p>Reading time: {{ $minutes }} {{ cond (eq $minutes 1) "minute" "minutes" }} and
    {{ $seconds }} {{ cond (eq $seconds 1) "second" "seconds" }}.</p>

The output is something like (depending on your content’s length):

Reading time: 1 minute and 35 seconds.

Reading time: 6 minutes and 45 seconds.

Reading time: 0 minutes and 22 seconds.

Reading time: 33 minutes and 0 seconds.

I wrote a blog post about this approach yesterday. You can find it here: calculate reading time of a Hugo string.

5 Likes