Count Word function customized to exclude code

Hi all,

I was wondering if it was possible for the Hugo wordcount function to exclude any code blocks when calculating the number of words in a post and the corresponding reading time.

{{ $wordCount := replaceRE `(?s)<div class="highlight">.*?</div>` "" .Content | countwords }}
{{ $readingtTime := div (float $wordCount) 213 | math.Ceil }}

Code fences are excluded.
Inline code is not excluded.

Reading time is expressed in minutes.

When Hugo calculates reading time, it uses 213 wpm for non-CJK languages.

1 Like

thanks! so I’m assuming that my new script would be

{{ if .Site.Params.readingTime }}
 |  {{ i18n “readingTime”}}{{ .readingTime }} {{ i18n “readTime” }}
{{ end }}
{{ if .Site.Params.wordCount }}
 |  {{ .wordCount }} {{ i18n “words” }}
{{ end }}

after pasting

{{ $wordCount := replaceRE `(?s)<div class="highlight">.*?</div>` "" .Content | countwords }}
{{ $readingtTime := div (float $wordCount) 213 | math.Ceil }}

at the top

No. Insert the variable name, including the dollar sign.

1 Like

Thank you!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.