Hexo添加字数统计、阅读时长、友情链接

字数统计

NexT主题默认已经集成了文章【字数统计】、【阅读时长】统计功能,如果我们需要使用,只需要在主题配置文件(Blog hemes ext_config.yml)中打开wordcount 统计功能即可

# Post wordcount display settings
# Dependencies: https://github.com/willin/hexo-wordcount
post_wordcount:
  item_text: true
  wordcount: true
  min2read: true

问题

如果仅仅只是打开开关,部署之后会发现文章的【字数统计】和【阅读时长】后面没有对应的xxx字,xx分钟等字样,只有光秃秃的数字在那里。如下图

解决方案

找到Blog hemes extlayout\_macropost.swig 文件

    字数统计

搜到找到如下代码

<span title="{
           
    { __(post.wordcount) }}">
     {
         
  { wordcount(post.content) }}
</span>

添加 “字”到{ { wordcount(post.content) }} 后面,修改后为:

<span title="{
           
    { __(post.wordcount) }}">
     {
         
  { wordcount(post.content) }} 字
    </span>
    阅读时长

找到如下代码

<span title="{
           
    { __(post.min2read) }}">
   {
         
  { min2read(post.content) }}
 </span>

添加 “分钟”到{ { min2read(post.content) }} 后面,修改后为:

<span title="{
           
    { __(post.min2read) }}">
   {
         
  { min2read(post.content) }} 分钟
 </span>

再次运行,就能得到正常的如“字数统计 1888字”“阅读时长 6分钟”这样的样式了,如下图:

添加友情链接

在主题配置文件(Blog hemes ext_config.yml)中打开links 开关即可

# Blog rolls 友情链接
links_title: Links
links_layout: block
links_layout: inline
links:
  test: http://www.example.com

问题

正常情况下本地部署,远程部署都没有问题,添加了友情链接之后,hexo s 本地部署的时候,就报出了如下异常

INFO  Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.
Unhandled rejection Error: ENOENT: no such file or directory, open D:Blog	hemes
extlayout\_scriptsschemes.swig
    at Error (native)
    at Object.fs.openSync (fs.js:641:18)
    at Object.fs.readFileSync (fs.js:509:33)
    at Object.ret.load (D:Blog
ode_modulesswiglibloadersfilesystem.js:55:15)
    at compileFile (D:Blog
ode_modulesswiglibswig.js:695:31)
    at Object.eval [as tpl] (eval at <anonymous> (D:Blog
ode_modulesswiglibswig.js:498:13), <anonymous>:842:18)
    at compiled (D:Blog
ode_modulesswiglibswig.js:619:18)
    at _compiled (D:Blog
ode_moduleshexolib	hemeview.js:127:30)
    at View.render (D:Blog
ode_moduleshexolib	hemeview.js:29:15)
    at D:Blog
ode_moduleshexolibhexoindex.js:388:25
    at tryCatcher (D:Blog
ode_modulesluebirdjs
eleaseutil.js:16:23)
...

提示说在themes extlayout\_scriptsschemes 目录下找不到.swig 这个文件,但是没有添加友链之前好像也没什么问题啊?一头雾水。。。

解决方案

在 中也没有找到自己想要的解决方案,而且google下这个问题,遇到的人好像也不多,就想到可能只是个简单的小配置问题。

于是又回头看了看文件内容,想到了第一眼看到时就比较奇怪的一点:

links_layout: block
links_layout: inline

有两个layout配置,不觉得奇怪吗?

于是果断注释掉其中一个,再次运行hexo s ,完美运行!再次测试注释掉另外一个,依然完美运行!OK,就这么轻松愉快的解决了,猜测可能是多文件冲突导致的.swig 生成问题。

经验分享 程序员 微信小程序 职场和发展