Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Compatibility] Incorrect Reading Time in Chinese #1112

Open
Adamska1008 opened this issue Jan 13, 2025 · 0 comments · May be fixed by #1113
Open

[Compatibility] Incorrect Reading Time in Chinese #1112

Adamska1008 opened this issue Jan 13, 2025 · 0 comments · May be fixed by #1113

Comments

@Adamska1008
Copy link

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:

      {{ if $showReadingTime }}
          <div>
              <time class="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" }}
                <time class="article-time--reading">
                    {{ T "article.readingTime" $readingTime }}
                </time>
            </div>
        {{ end }}

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

@Adamska1008 Adamska1008 linked a pull request Jan 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant