Hexo Nunjucks Error解决方法

问题

更新博客,准备先在本地hexo s预览看看的,但是出错了

控制台输出的错误如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Nunjucks Error: _posts/学习记录/大三专业课上机实验碰到的问题合集.md [Line 289, Column 26] expected variable end
===== Context Dump =====
=== (line number probably different from source) ===
284 | <p>输出的东西就长这样,我不认识: <span class="math display">\[
285 | \begin{align*}
286 | \int\limits_{0}^{1} \begin{cases} 2 \sqrt{x} \log{\left(x \right)} &amp;
287 | \text{for}\: \frac{1}{x} &lt; 1 \wedge x &lt; 1 \\\sqrt{x} \log{\left(x
288 | \right)} &amp; \text{for}\: \frac{1}{x} &lt; 1
289 | \vee x &lt; 1 \\\frac{{G_{3, 3}^{0, 3}\left(\begin{matrix} \frac{3}{2}, \frac{5}{2}, 1 & \\ & \frac{3}{2}, \frac{3}{2}, 0 \end{matrix} \middle| {x} \right)} + \frac{3 {G_{3, 3}^{0, 3}\left(\begin{matrix} \frac{5}{2}, \frac{5}{2}, 1 & \\ & \frac{3}{2}, \frac{3}{2}, 0 \end{matrix} \middle| {x} \right)}}{2}}{x} -
290 | \frac{{G_{3, 3}^{2, 1}\left(\begin{matrix} 0 & \frac{5}{2}, \frac{5}{2} \\\frac{3}{2}, \frac{3}{2} & 0 \end{matrix} \middle| {x} \right)}}{x} &amp; \text{otherwise} \end{cases}\, dx
291 | \end{align*}
292 | \]</span></p>
293 | <p>我明明就只想要一个数值结果,你给我这玩意,我咋看啊?</p>
294 | <p>于是就可以用这个<code>evalf</code>函数直接求出数值结果:</p>
===== Context Dump Ends =====
at formatNunjucksError (E:\Blog\node_modules\hexo\lib\extend\tag.js:173:13)
at E:\Blog\node_modules\hexo\lib\extend\tag.js:249:29
at tryCatcher (E:\Blog\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (E:\Blog\node_modules\bluebird\js\release\promise.js:547:31)
at Promise._settlePromise (E:\Blog\node_modules\bluebird\js\release\promise.js:604:18)
at Promise._settlePromise0 (E:\Blog\node_modules\bluebird\js\release\promise.js:649:10)
at Promise._settlePromises (E:\Blog\node_modules\bluebird\js\release\promise.js:725:18)
at _drainQueueStep (E:\Blog\node_modules\bluebird\js\release\async.js:93:12)
at _drainQueue (E:\Blog\node_modules\bluebird\js\release\async.js:86:9)
at Async._drainQueues (E:\Blog\node_modules\bluebird\js\release\async.js:102:5)
at Async.drainQueues [as _onImmediate] (E:\Blog\node_modules\bluebird\js\release\async.js:15:14)
at process.processImmediate (node:internal/timers:471:21)

图片如下

还花花绿绿的,怪有意思

原因

我在整理sympy使用记录的过程中,用到了sympy输出\(\LaTeX\)函数,结果如下:

可以看到这个东西的表达式还是很复杂的:

我参考报错信息里的网站:Troubleshooting | Hexo,找到了这个叫做“Nunjucks Error”的错误

原文如下:

Escape Contents

Hexo uses [Nunjucks] to render posts ([Swig] was used in the older version, which shares a similar syntax). Content wrapped with {{ }} or {% %} will get parsed and may cause problems. You can skip the parsing by wrapping it with the raw tag plugin, a single backtick {{ }} or a triple backtick. Alternatively, Nunjucks tags can be disabled through the renderer’s option (if supported), API or front-matter.

1
2
3
4
5
6
>{% raw %}
>Hello {{ world }}
>{% endraw %}
>```
>Hello {{ world }}
>```


可知Hexo是用Nunjucks来渲染的

如果文章内容里出现了{{ }}或者{% %}这样的标签,它们会被解析为Nunjucks的语法,从而导致错误

解决方法

为了避免这个问题,需要使用:

1
`{% raw %}`和`{% endraw %}`

这两个标签来包裹这些敏感内容

这样,它们就不会被解析为Nunjucks的语法,而是会被原样输出

如图:

虽然在本地Markdown编辑器里会感觉奇怪,但是在网页上能正常显示了

结果

在编辑这篇博客时遇到的问题

这篇文章内容中也出现了{{ }}或者{% %}这样的标签

但是Hexo没有报错

可能因为代码段渲染时不考虑这些


在博客中不能正常显示`和`,被渲染出来的结果如下图所示

虽然行内代码无法正常显示,但是能正常在代码段环境下显示

1
{% raw %}和{% endraw %}

不过我们可以这样写,用_表示空格:

{_% raw %}{% endraw %} 这样就能正常渲染了

{ % raw %}{% endraw %}





参考资料

Troubleshooting | Hexo

Nunjucks Error: 解决方案