安装

文章字数和阅读时长的统计都是借助 hexo-wordcount 插件实现,可以使用 npm 或者 yarn 安装:

1
2
3
yarn add hexo-wordcount
# or
npm i --save hexo-wordcount

Node 版本在 7.6.0 之前,请安装 2.x 版本:

1
npm i --save hexo-wordcount@2

配置

在主题的配置文件 _config.yml 中添加如下 hexo-wordcount 插件的配置说明:

1
2
3
4
5
6
7
# Post wordcount display settings
# Dependencies: https://github.com/willin/hexo-wordcount
post_wordcount:
item_text: true
wordcount: true # 单篇 字数统计
min2read: true # 单篇 阅读时长
totalcount: false # 网站 字数统计

可以同时在主题配置文件中添加如下的控制配置,实现利用配置文件控制是否显示字数统计和阅读时长等。

1
2
post_wordcount_enable: true
post_min2read_enable: true

添加到 post 页面

我使用的是 Chic 主题,post 的 layout 文件在 themes/Chic/layout/_page/post.ejs。其他主题的 layout 文件位置应该类似。

在其中修改添加如下 code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Category:
<% page.categories.forEach(item=>{ %>
<a href="<%- url_for(item.path) %>"><%- item.name %></a>
<% }) %>
</span>
<% } %>
// 下面为添加的新代码
<% if(theme.post_wordcount_enable){ %>
<span class="post-count">
Words:
<a href=""><%= wordcount(page.content) %></a>
</span>
<% } %>
<% if(theme.post_min2read_enable){ %>
<span class="post-count">
Time:
<a href=""><%= min2read(post.content) %>min</a>
</span>
<% } %>

Chic 使用的是 Ejs,其他如 SwagJade 的写法可参照 hexo-wordcount 仓库中的说明。

参考样例可看本文的 head 部分🤖

Reference