标签

标签允许用户快速轻松地将代码片段插入到他们的帖子中。

¥A tag allows users to quickly and easily insert snippets into their posts.

概要

¥Synopsis

hexo.extend.tag.register(
name,
function (args, content) {
// ...
},
options,
);

两个参数将传递到标签函数中:argscontentargs 包含传递到标签插件的参数,content 是来自标签插件的封装内容。

¥Two arguments will be passed into the tag function: args and content. args contains the arguments passed into the tag plugin and content is the wrapped content from the tag plugin.

自从 Hexo 3 引入异步渲染以来,我们就使用 Nunjucks 进行渲染。行为可能与 Swig 中的行为略有不同。

¥Since the introduction of asynchronous rendering in Hexo 3, we are using Nunjucks for rendering. The behavior may be somewhat different from that in Swig.

取消注册标签

¥Unregister Tags

使用 unregister() 用自定义函数替换现有的 标签插件

¥Use unregister() to replace existing tag plugins with custom functions.

hexo.extend.tag.unregister(name);

示例

¥Example

const tagFn = (args, content) => {
content = "something";
return content;
};

// https://hexo.nodejs.cn/docs/tag-plugins#YouTube
hexo.extend.tag.unregister("youtube");

hexo.extend.tag.register("youtube", tagFn);

选项

¥Options

ends

使用结束标签。默认情况下,此选项为 false

¥Use end tags. This option is false by default.

async

启用异步模式。默认情况下,此选项为 false

¥Enable async mode. This option is false by default.

示例

¥Examples

不带结束标签

¥Without End Tags

插入 Youtube 视频。

¥Insert a Youtube video.

hexo.extend.tag.register("youtube", function (args) {
var id = args[0];
return (
'<div class="video-container"><iframe width="560" height="315" src="http://www.youtube.com/embed/' +
id +
'" frameborder="0" allowfullscreen></iframe></div>'
);
});

带结束标签

¥With End Tags

插入引言。

¥Insert a pull quote.

hexo.extend.tag.register(
"pullquote",
function (args, content) {
var className = args.join(" ");
return (
'<blockquote class="pullquote' +
className +
'">' +
content +
"</blockquote>"
);
},
{ ends: true },
);

异步渲染

¥Async Rendering

插入文件。

¥Insert a file.

var fs = require("hexo-fs");
var pathFn = require("path");

hexo.extend.tag.register(
"include_code",
function (args) {
var filename = args[0];
var path = pathFn.join(hexo.source_dir, filename);

return fs.readFile(path).then(function (content) {
return "<pre><code>" + content + "</code></pre>";
});
},
{ async: true },
);

前言和用户配置

¥Front-matter and user configuration

以下任何选项均有效:

¥Any of the following options is valid:

hexo.extend.tag.register('foo', function (args) {
const [firstArg] = args;

// User config
const { config } = hexo;
const editor = config.author + firstArg;

// Theme config
const { config: themeCfg } = hexo.theme;
if (themeCfg.fancybox) // do something...

// Front-matter
const { title } = this; // article's (post/page) title

// Article's content
const { _content } = this; // original content
const { content } = this; // HTML-rendered content

return 'foo';
});
index.js
hexo.extend.tag.register("foo", require("./lib/foo")(hexo));
lib/foo.js
module.exports = hexo => {
return function fooFn(args) {
const [firstArg] = args;

const { config } = hexo;
const editor = config.author + firstArg;

const { config: themeCfg } = hexo.theme;
if (themeCfg.fancybox) // do something...

const { title, _content, content } = this;

return 'foo';
};
};