You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Stack uses built-in .ReadingTime function, which simply divides the word count by the words-per-minute value. However, chinese (and some other languages) does not use space to seperate words, and the whole paragraph may be counted as a single word. Thus the shown reading time may be too small. In the example starter code, I add a post with bunch of chinese words but 1 minute read.
The original template is:
{{ if $showReadingTime }}
<div>
<timeclass="article-time--reading">
{{ T "article.readingTime" .ReadingTime }}
</time>
</div>
{{ end }}
With simple modification we can do it right by counting runes:
{{ if $showReadingTime }}
<div>
{{ $readingTime := .ReadingTime }}
{{ if eq .Site.Language.LanguageCode "zh-cn"}}
{{ $wordsPerMinute := 500 }}
{{ $charCount := .Content | replaceRE "[\\pP]" "" | countrunes}}
{{ $readingTime = div (float $charCount) $wordsPerMinute | math.Ceil | int }}
{{ end }}
{{ partial "helper/icon" "clock" }}
<timeclass="article-time--reading">
{{ T "article.readingTime" $readingTime }}
</time>
</div>
{{ end }}
What happened?
Stack uses built-in
.ReadingTime
function, which simply divides the word count by the words-per-minute value. However, chinese (and some other languages) does not use space to seperate words, and the whole paragraph may be counted as a single word. Thus the shown reading time may be too small. In the example starter code, I add a post with bunch of chinese words but 1 minute read.The original template is:
With simple modification we can do it right by counting runes:
Hugo version
0.140.2
Theme version
3.29.0
What browsers are you seeing the problem on?
Chrome
More information about the browser
No response
Relevant log output
No response
Link to Minimal Reproducible Example
https://github.com/Adamska1008/hugo-theme-stack-starter
The text was updated successfully, but these errors were encountered: