渲染

Hexo 中有两种渲染文件或字符串的方法:异步 hexo.render.render 方法和同步 hexo.render.renderSync 方法。不出所料,这两种方法非常相似,因此以下段落将仅进一步讨论异步 hexo.render.render

¥There are two methods for rendering files or strings in Hexo: the asynchronous hexo.render.render method and the synchronous hexo.render.renderSync method. Unsurprisingly, the two methods are very similar so only the asynchronous hexo.render.render will be further discussed in the below paragraphs.

渲染字符串

¥Render a String

渲染字符串时,必须指定 engine 以让 Hexo 知道应该使用哪个渲染引擎。

¥When rendering a string, you must specify an engine to let Hexo know which rendering engine it should use.

hexo.render.render({ text: "example", engine: "swig" }).then(function (result) {
// ...
});

渲染文件

¥Render a File

渲染文件时,不需要指定 engine,因为 Hexo 会根据文件的扩展名自动检测相关的渲染引擎。当然,你也可以明确定义 engine

¥When rendering a file, it’s not necessary to specify an engine because Hexo will detect the relevant rendering engine automatically based on the extension of the file. Of course, you are also allowed to explicitly define the engine.

hexo.render.render({ path: "path/to/file.swig" }).then(function (result) {
// ...
});

渲染选项

¥Render Options

你可以传入一个选项对象作为第二个参数。

¥You can pass in an options object as the second argument.

hexo.render.render({ text: "" }, { foo: "foo" }).then(function (result) {
// ...
});

after_render 过滤器

¥after_render Filters

渲染完成后,Hexo 将执行相应的 after_render 过滤器。例如,我们可以使用此功能实现 JavaScript 压缩器。

¥When rendering is complete, Hexo will execute the corresponding after_render filters. For example, we can use this feature to implement a JavaScript minifier.

var UglifyJS = require("uglify-js");

hexo.extend.filter.register("after_render:js", function (str, data) {
var result = UglifyJS.minify(str);
return result.code;
});

检查文件是否可渲染

¥Check Whether a File is Renderable

你可以使用 isRenderableisRenderableSync 方法来检查文件路径是否可渲染。仅当注册了相应的渲染器时,此方法才会返回 true。

¥You can use the isRenderable or isRenderableSync method to check whether a file path is renderable. Only when a corresponding renderer has been registered will this method return true.

hexo.render.isRenderable("layout.swig"); // true
hexo.render.isRenderable("image.png"); // false

获取输出扩展

¥Get the Output Extension

使用 getOutput 方法获取渲染输出的扩展名。如果文件不可渲染,该方法将返回一个空字符串。

¥Use the getOutput method to get the extension of the rendered output. If a file is not renderable, the method will return an empty string.

hexo.render.getOutput("layout.swig"); // html
hexo.render.getOutput("image.png"); // '''

禁用 Nunjucks 标签

¥Disable Nunjucks tags

如果你没有使用 标签插件 并想在帖子中使用 {{ }}{% %} 而不使用内容 escaping,你可以通过以下方式禁用现有渲染器中对 Nunjucks 标签的处理:

¥If you are not using a tag plugin and want to use {{ }} or {% %} in your post without using content escaping, you can disable processing of Nunjucks tag in existing renderer by:

// following example only applies to '.md' file extension
// you may need to cover other extensions, e.g. '.markdown', '.mkd'
const renderer = hexo.render.renderer.get("md");
if (renderer) {
renderer.disableNunjucks = true;
hexo.extend.renderer.register("md", "html", renderer);
}