Hexo 7.3.0 使用 hexo server 报错“TypeError: tab.repeat is not a function at \node_modules\hexo-util\dist\highlight.js:73:44”如何解决

🕘
⏱️
👁️ * 阅读

本文主要应对 hexo 6.3.0 代码片段渲染错误和 hexo 7.3.0 TypeError: tab.repeat 错误。

详细的报错信息:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
TypeError: tab.repeat is not a function
    at C:\Users\yjl\Documents\hexo-yijile\node_modules\hexo-util\dist\highlight.js:73:44        
    at String.replace (<anonymous>)
    at replaceTabs (C:\Users\yjl\Documents\hexo-yijile\node_modules\hexo-util\dist\highlight.js:73:16)
    at highlightUtil (C:\Users\yjl\Documents\hexo-yijile\node_modules\hexo-util\dist\highlight.js:35:20)
    at Hexo.highlightFilter (C:\Users\yjl\Documents\hexo-yijile\node_modules\hexo\dist\plugins\highlight\highlight.js:41:12)
    at SyntaxHighlight.exec (C:\Users\yjl\Documents\hexo-yijile\node_modules\hexo\dist\extend\syntax_highlight.js:21:24)
    at C:\Users\yjl\Documents\hexo-yijile\node_modules\hexo\dist\plugins\filter\before_post_render\backtick_code_block.js:49:44
    at String.replace (<anonymous>)
    at Hexo.backtickCodeBlock (C:\Users\yjl\Documents\hexo-yijile\node_modules\hexo\dist\plugins\filter\before_post_render\backtick_code_block.js:11:36)
    ...

经过几番折腾才发现是 highlight 配置错误,虽然一极乐没有启用 highlightenable: false)但是 tab_replace 值写法错误,不确定是一直就写错了还是升级后不能这样写。

错误写法

1
2
3
4
5
6

highlight:
  enable: false
  line_number: true
  auto_detect: true
  tab_replace: true

正确写法,如果你有使用 highlight 请将 enable 的值改为 true,本文提及的报错主要是 tab_replace 所致,只需要重点看 tab_replace 即可。

1
2
3
4
5
highlight:
  enable: false
  line_number: true
  auto_detect: true
  tab_replace: '    ' 

一极乐是直接使用 highlight.js 实现代码高亮的,因为 hexo 集成的 highlight 高亮的配色不好看,所以用了 highlight.js,然后使用 highlight.js 可以自行控制哪些页面加载相关资源(没有使用到代码片段的页面不加载相关资源)。

因为之前使用 6.3.0 可以正常构建(hexo server or hexo generate),所以一直都没有改变 _config.yml 中的配置,直到今天发现 hexo 将代码片段中的 HTML 内容渲染解析了,然后一番搜索无果,又去 hexo 的 github 提问,然后看到有选项提示是否最新版,就安装了最新的吧 hexo(hexo 7.3.0),安装以后就无法正常构建(hexo server or hexo generate)了,又是各种搜索无果,hexo github 提问区补充描述等结果,在此之间chat了gpt,虽然没有明确的答案,但是提供了一些思路,后来发现就是 highlight 配置导致的。

这里不得不吐槽 hexo,功能未启用时配置错误也会导致无法构建,构建的错误也没有标明具体问题和提供相应方案,官方文档评论区也是一片吐槽。

附,一极乐当前使用的 highlight.js 版本:

1
/*! highlight.js v9.17.1 | BSD3 License | git.io/hljslicense */

转载请注明转自:一极乐https://yijile.com/zh/hexo-7-3-0-server-type-error-highlight-tab-replace/

CC BY-NC-SA 4.0 声明
本文采用 CC BY-NC-SA 4.0方式授权。
转载请注明出处和本文链接,说明是否进行修改,不得用于商业用途,使用相同方式共享。