阅读时间
Syntax
PAGE.ReadingTime
Returns
int
通过将内容中的单词数除以阅读速度来计算估计阅读时间。
默认情况下,Hugo假设阅读速度为每分钟212个单词。对于CJK语言,它假设每分钟阅读500个单词。
{{ printf "估计阅读时间:%d分钟" .ReadingTime }}
阅读速度因语言而异。使用站点参数在多语言网站上创建特定语言的估计阅读时间。
hugo.
languages:
de:
contentDir: content/de
languageCode: de-DE
languageName: 德语
params:
reading_speed: 179
weight: 2
en:
contentDir: content/en
languageCode: en-US
languageName: 英语
params:
reading_speed: 228
weight: 1
[languages]
[languages.de]
contentDir = 'content/de'
languageCode = 'de-DE'
languageName = '德语'
weight = 2
[languages.de.params]
reading_speed = 179
[languages.en]
contentDir = 'content/en'
languageCode = 'en-US'
languageName = '英语'
weight = 1
[languages.en.params]
reading_speed = 228
{
"languages": {
"de": {
"contentDir": "content/de",
"languageCode": "de-DE",
"languageName": "德语",
"params": {
"reading_speed": 179
},
"weight": 2
},
"en": {
"contentDir": "content/en",
"languageCode": "en-US",
"languageName": "英语",
"params": {
"reading_speed": 228
},
"weight": 1
}
}
}
然后在您的模板中:
{{ $readingTime := div (float .WordCount) .Site.Params.reading_speed }}
{{ $readingTime = math.Ceil $readingTime }}
我们将 .WordCount
强制转换为 float 类型,以在除以阅读速度时获得浮点数。然后将其四舍五入为最接近的整数。