Page

ReadingTime

返回给定页面的预估阅读时间(以分钟计)。

签名
PAGE.ReadingTime
返回类型
int

Hugo 用内容中的单词数除以每分钟 212 词的阅读速度,据此估算阅读时间。

{{ printf "Estimated reading time: %d minutes" .ReadingTime }}

阅读速度因语言而异。在多语言项目中,可以用站点参数为每种语言分别设置估算阅读时间。

[languages]
  [languages.de]
    contentDir = 'content/de'
    label = 'Deutsch'
    locale = 'de-DE'
    weight = 2
    [languages.de.params]
    reading_speed = 179
  [languages.en]
    contentDir = 'content/en'
    label = 'English'
    locale = 'en-US'
    weight = 1
    [languages.en.params]
      reading_speed = 228

然后在模板中:

{{ $readingTime := div (float .WordCount) .Site.Params.reading_speed }}
{{ $readingTime = math.Ceil $readingTime }}

我们把 .WordCount 转换成浮点数,这样除以阅读速度时就能得到浮点结果。然后再向上取整到最接近的整数。