I added “X min read” (a la Medium), functionality to my Hugo-generated blog today. If you’re not sure what that means, just take a look under the title of this post – it says “1 min read” next to the post date.

Adding this feature was pretty easy. The Golang snippet below can be used to display the read time in minutes. It reads the number of words in .Content, which is the post body, divides it by 250, and rounds it to the nearest integer.

(math.Round (div (countwords .Content) 250.0)

With the snippet above, I integrated it into my Hugo template. I ended up with two conditionals. The first one checks for the post type because I only want “X min read” to show on posts, links, and notes. The second conditional forces the minimum read time to one minute.

{{- if or (eq .Section "post") (eq .Section "link") (eq .Section "note") -}}
&nbsp;&middot;&nbsp;<span class="read-time">{{ if lt (math.Round (div (countwords .Content) 250.0)) 1 }}1{{ else }}{{ (math.Round (div (countwords .Content) 250.0)) }}{{ end }} min read</span>{{- end -}}

I know what you’re thinking. No, I am not trying to make a Medium clone.