hexo不去扫描模版语言

  1. 问题引入
  2. 解决

问题引入

在运行hexo g的时候,遇到了下面的问题

Nunjucks Error: _posts/Django-No.md [Line 659, Column 37] unknown block tag: with
    =====               Context Dump               =====
    === (line number probably different from source) ===
  654 | <pre><code class="language-python">{% with a=books.1 %}
  655 |     <p>{{ a }}</p>
  656 | {% endwith %}
  657 | </code></pre>
  658 | <p>在 <code>with</code> 语句中定义的变量,只能在 <code>{%with%}` `{%endwith%}</code> 中使用,不能在这个标签外面使用。</p>
  659 | <p>定义变量的时候,不能在等号左右两边留有空格。比如 &#96;{% with lisi = persons.1%}` 是错误的。
  660 | 
  661 | `books.1`:这是 **用点语法访问列表中的索引**,DTL 中没有 `books[1]` 这样的写法,只能用 `books.1`
  662 | 
  663 | 还有另外一种写法同样也是支持的:
  664 | 
    =====             Context Dump Ends            =====
    at formatNunjucksError (/Users/xieshaolin/Desktop/LinNote/node_modules/hexo/dist/extend/tag.js:145:15)
    at /Users/xieshaolin/Desktop/LinNote/node_modules/hexo/dist/extend/tag.js:213:46
    at tryCatcher (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/async.js:102:5)
    at Async.drainQueues [as _onImmediate] (/Users/xieshaolin/Desktop/LinNote/node_modules/bluebird/js/release/async.js:15:14)
    at process.processImmediate (node:internal/timers:476:21)

Hexo 默认使用 Nunjucks 作为模板引擎,而 Nunjucks 不支持 DTL 的 {% with %} 标签,因此解析时抛出了 unknown block tag: with 的错误。

Nunjucks 在渲染代码块时,误认为 {% with %} 是自己的模板标签,而非代码展示的一部分。

解决

  1. 进入你 Hexo 项目根目录

  2. 找到或新建 scripts 文件夹(没有就建一个)

  3. scripts 目录下新建 disable_nunjucks.js,内容如下

    // 在你的 Hexo 项目根目录下的 scripts 目录里建一个 js 文件,比如 disable_nunjucks.js
    
    const renderer = hexo.render.renderer.get('md');
    if (renderer) {
      renderer.disableNunjucks = true; // 关闭对 Nunjucks 标签的解析
      hexo.extend.renderer.register('md', 'html', renderer);
    }
    
  4. 重启 Hexo,执行 hexo clean && hexo g 重新生成


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1909773034@qq.com

×

喜欢就点赞,疼爱就打赏