Wordcount returns inaccurate numbers

Hello,

I have this code in a shortcode:

{{ range $.Page.Site.Pages }}
  
    {{ .Title }} - {{ .WordCount }} - {{ .FuzzyWordCount }} <br/>

{{ end }}

It returns:

Article 1 - 0 - 0
Article 2 - 7 - 100
Article 3 - 9 - 100
Article 4 - 0 - 0
Article 5 - 0 - 0
Article 6 - 0 - 0
Article 7 - 0 - 0
Article 8 - 0 - 0 

But the actual word count per article is as follows:

Article 1 - 1
Article 2 - 6
Article 3 - 9
Article 4 - 1189
Article 5 - 0
Article 6 - 508
Article 7 - 0
Article 8 - 872

What am I missing?

Thanks for any insights,

(Hugo v0.14 on Windows 7 SP1)

What language is the articles written in?

EDIT: There is a known issue about wordcount and autogenerated summaries for CJK languages. I added the RuneCount method a while ago, but there is an open discussion about it on GitHub.

English, with no special characters as far as I can tell (not even a dollar or euro sign).

The files themselves are saved as .md files and created with hugo new.

The problem is with looping through the pages in the shortcode. If I put this code in my template file, it works correctly:

{{ range $.Site.Pages }}
  
    {{ .Title }} - {{ .WordCount }} - {{ .FuzzyWordCount }} <br/>

{{ end }}

$.Site.Pages can, however, not be used in a shortcode and triggers the *Data is not a field of struct type hugolib.ShortcodeWithPage error message. I know of pages and nodes, but not of the ShortcodeWithPage type.

So how to access all pages in a shortcode correctly?

You can not. This isn’t what the shortcodes are built for. This is the chicken and the egg; it should be documented better what part of the page’s state and the site’s state that is available in the shortcode – but the surrounding content isn’t.

Thanks for clarifying Bep. Is there a particular reason why we can’t pull .WordCount in a shortcode but variables like .Title, .IsDraft, .Section, and .Params.categories are no problem? Or is this latter something that will be deprecated in future versions since shortcodes aren’t built for that?

To calculate the word count you need the content of the page. The content of the page includes shortcode(s).

You can read more about this problem here: