Getting the character count

I am trying to get the character count a post. Using {{ len .RawContent }} is producing character count plus 1. What should I do to get the exact count? Thanks!

What len .RawContent is getting you is the number of bytes in that string. I assume that what happens in your case is some multibyte character messing with the count.

The closet we have, I think, is strings.CountRunes – but that oddly counts runes (i.e. unicode characters) excluding whitespace. I suspect it was added so the CJK languages could get a word count.

Maybe @moorereason knows.

I don’t know the history of countrunes (it predates the template namespaces). It strips HTML tags and ignores white space, both of which don’t make much sense to me for a func named countrunes. Hindsight’s 20/20.

In Go stdlib, we have utf8.RuneCountInString, but we don’t provide an equivalent function in Hugo templates. Curiously, Go’s strings.Count(foo, "") returns RuneCount+1.

I could use a function that counts runes for a text output formatter I’m writing. Makes it easier to properly pad things with whitespace.

Yeah countrunes is for spaceless languages i.e. CJK ones

I’ve submitted a PR to add a runelen function that just returns the result from utf8.RuneCountInString.