最近看到不少博主都在用一种很新的方式来展示站点的总字数:将全站总字数换算成对应书籍的篇幅来显示,我也想整一个。
对于 Hugo 静态博客,可以使用短代码的形式来实现这个小功能。此外,我的站点主要内容有两个大的分类:
/content/posts//content/weekly/后面我们调用短代码的时候传递一个参数即可实现对不同类别的统计,就像这样:
下文中的 \\ 用于转义 Hugo 短代码,使用时请把它去掉。
{{\\< wordCount "posts" >\\}}
{{\\< wordCount "weekly" >\\}}
{{\\< wordCount "all" >\\}}
posts 统计所有文章的字数weekly 统计所有周刊的字数all 统计全站总字数进入 Hugo 站点根目录,新建短代码:layouts/shortcodes/wordCount.html,参考 How to Calculate Global Word Count for a Hugo Static Site
,看下 Hugo 全站总字数统计的实现:
{{ $scratch := newScratch }}
{{ range (where .Site.RegularPages "Type" "in" (slice "blog")) }}
{{ $scratch.Add "wordcount" .WordCount }}
{{ end }}
{{ $scratch.Get "wordcount" | lang.NumFmt 0 }}
上面使用了 Hugo 内置的 .Scratch 函数 暂存
被操作的数据,遍历统计 blog 类别下的字数,最后将相加后的总字数格式化输出。
对于我的两个类别,可以根据输入的参数进行条件输出,我们还可以知道:全站总字数 = 所有文章字数 + 所有周刊字数:
{{ $scratch := newScratch }}
{{ if eq (.Get 0) "posts" }}
{{ range (where .Site.RegularPages "Type" "in" "posts") }}
{{ $scratch.Add "wordcount" .WordCount }}
{{ end }}
{{ else if eq (.Get 0) "weekly" }}
{{ range (where .Site.RegularPages "Type" "in" "weekly") }}
{{ $scratch.Add "wordcount" .WordCount }}
{{ end }}
{{ else if eq (.Get 0) "all" }}
{{ range (where .Site.RegularPages "Type" "in" (slice "posts" "weekly")) }}
{{ $scratch.Add "wordcount" .WordCount }}
{{ end }}
{{ end }}
{{ $scratch.Get "wordcount" | lang.NumFmt 0 }} 字
上面完成了统计各个类别的总字数功能,得到了输出的总字数,现在就可以使用条件语句匹配不同篇幅的书名并输出:
{{ $wordcount := $scratch.Get "wordcount" }}
{{ $output := "" }}
{{ if ge $wordcount 1000000 }}
{{ $output = "《战争与和平》" }}
{{ else if ge $wordcount 850000 }}
{{ $output = "《应物兄》" }}
{{ else if ge $wordcount 80000 }}
{{ $output = "《呐喊》" }}
{{ else if ge $wordcount 50000 }}
{{ $output = "《小王子》" }}
{{ else if gt $wordcount 4000 }}
{{ $output = "《断魂枪》" }}
{{ end }}
相当于一本 {{ $output }}
这个短代码就完成了。
在想要插入字数统计的地方插入短代码,比如:
文章总字数
「所有文章」加起来约 {{\\< wordCount "posts" >\\}}
效果如下:

周刊总字数
「Hacker News 周刊」加起来约 {{\\< wordCount "weekly" >\\}}
效果:

全站总字数
「Hacker News 周刊」加起来约 {{\\< wordCount "all" >\\}}
效果:

关于页面
- 文章写了约 {{\\< wordCount "posts" >\\}}
- 周刊写了约 {{\\< wordCount "weekly" >\\}}
- 全站共写了约 {{\\< wordCount "all" >\\}}
效果:

完成,请享受。🫣
最近看到不少博主都在用一种很新的方式来展示站点的总字数:将全站总字数换算成对应书籍的篇幅来显示,我也想整一个。
对于 Hugo 静态博客,可以使用短代码的形式来实现这个小功能。此外,我的站点主要内容有两个大的分类:
/content/posts//content/weekly/后面我们调用短代码的时候传递一个参数即可实现对不同类别的统计,就像这样:
下文中的 \\ 用于转义 Hugo 短代码,使用时请把它去掉。
{{\\< wordCount "posts" >\\}}
{{\\< wordCount "weekly" >\\}}
{{\\< wordCount "all" >\\}}
posts 统计所有文章的字数weekly 统计所有周刊的字数all 统计全站总字数进入 Hugo 站点根目录,新建短代码:layouts/shortcodes/wordCount.html,参考 How to Calculate Global Word Count for a Hugo Static Site
,看下 Hugo 全站总字数统计的实现:
{{ $scratch := newScratch }}
{{ range (where .Site.RegularPages "Type" "in" (slice "blog")) }}
{{ $scratch.Add "wordcount" .WordCount }}
{{ end }}
{{ $scratch.Get "wordcount" | lang.NumFmt 0 }}
上面使用了 Hugo 内置的 .Scratch 函数 暂存
被操作的数据,遍历统计 blog 类别下的字数,最后将相加后的总字数格式化输出。
对于我的两个类别,可以根据输入的参数进行条件输出,我们还可以知道:全站总字数 = 所有文章字数 + 所有周刊字数:
{{ $scratch := newScratch }}
{{ if eq (.Get 0) "posts" }}
{{ range (where .Site.RegularPages "Type" "in" "posts") }}
{{ $scratch.Add "wordcount" .WordCount }}
{{ end }}
{{ else if eq (.Get 0) "weekly" }}
{{ range (where .Site.RegularPages "Type" "in" "weekly") }}
{{ $scratch.Add "wordcount" .WordCount }}
{{ end }}
{{ else if eq (.Get 0) "all" }}
{{ range (where .Site.RegularPages "Type" "in" (slice "posts" "weekly")) }}
{{ $scratch.Add "wordcount" .WordCount }}
{{ end }}
{{ end }}
{{ $scratch.Get "wordcount" | lang.NumFmt 0 }} 字
上面完成了统计各个类别的总字数功能,得到了输出的总字数,现在就可以使用条件语句匹配不同篇幅的书名并输出:
{{ $wordcount := $scratch.Get "wordcount" }}
{{ $output := "" }}
{{ if ge $wordcount 1000000 }}
{{ $output = "《战争与和平》" }}
{{ else if ge $wordcount 850000 }}
{{ $output = "《应物兄》" }}
{{ else if ge $wordcount 80000 }}
{{ $output = "《呐喊》" }}
{{ else if ge $wordcount 50000 }}
{{ $output = "《小王子》" }}
{{ else if gt $wordcount 4000 }}
{{ $output = "《断魂枪》" }}
{{ end }}
相当于一本 {{ $output }}
这个短代码就完成了。
在想要插入字数统计的地方插入短代码,比如:
文章总字数
「所有文章」加起来约 {{\\< wordCount "posts" >\\}}
效果如下:

周刊总字数
「Hacker News 周刊」加起来约 {{\\< wordCount "weekly" >\\}}
效果:

全站总字数
「Hacker News 周刊」加起来约 {{\\< wordCount "all" >\\}}
效果:

关于页面
- 文章写了约 {{\\< wordCount "posts" >\\}}
- 周刊写了约 {{\\< wordCount "weekly" >\\}}
- 全站共写了约 {{\\< wordCount "all" >\\}}
效果:

完成,请享受。🫣