标签插件(Tag)

标签插件帮助开发者在文章中快速插入内容。

概要

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

标签函数会传入两个参数:argscontent,前者代表开发者在使用标签插件时传入的参数,而后者则是标签插件所覆盖的内容。

从 Hexo 3 开始,因为新增了非同步渲染功能,而改用 Nunjucks 作为渲染引擎,其行为可能会与过去使用的 Swig 有些许差异。

移除标签插件

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

1
hexo.extend.tag.unregister(name);

示例

1
2
3
4
5
6
7
8
9
const tagFn = (args, content) => {
content = 'something';
return content;
};

// https://hexo.io/docs/tag-plugins#YouTube
hexo.extend.tag.unregister('youtube');

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

选项

ends

使用结束标签,此选项默认为 false

async

开启非同步模式,此选项默认为 false

范例

没有结束标签

插入 Youtube 影片。

1
2
3
4
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>';
});

有结束标签

插入 pull quote。

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

非同步渲染

插入文件。

1
2
3
4
5
6
7
8
9
10
11
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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';
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
index.jshexo.extend.tag.register('foo', require('./lib/foo')(hexo));
lib/foo.jsmodule.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';
};
};

标签插件(Tag Plugins)

标签插件和 Front-matter 中的标签不同,它们是用于在文章中快速插入特定内容的插件。

虽然你可以使用任何格式书写你的文章,但是标签插件永远可用,且语法也都是一致的。

Tag plugins should not be wrapped inside Markdown syntax, e.g. []({% post_path lorem-ipsum %}) is not supported.

引用块

在文章中插入引言,可包含作者、来源和标题。

别号: quote

1
2
3
{% blockquote [author[, source]] [link] [source_link_title] %}
content
{% endblockquote %}

样例

没有提供参数,则只输出普通的 blockquote

1
2
3
{% blockquote %}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque hendrerit lacus ut purus iaculis feugiat. Sed nec tempor elit, quis aliquam neque. Curabitur sed diam eget dolor fermentum semper at eu lorem.
{% endblockquote %}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque hendrerit lacus ut purus iaculis feugiat. Sed nec tempor elit, quis aliquam neque. Curabitur sed diam eget dolor fermentum semper at eu lorem.

引用书上的句子

1
2
3
{% blockquote David Levithan, Wide Awake %}
Do not just seek happiness for yourself. Seek happiness for all. Through kindness. Through mercy.
{% endblockquote %}

Do not just seek happiness for yourself. Seek happiness for all. Through kindness. Through mercy.

David LevithanWide Awake

引用 Twitter

1
2
3
{% blockquote @DevDocs https://twitter.com/devdocs/status/356095192085962752 %}
NEW: DevDocs now comes with syntax highlighting. http://devdocs.io
{% endblockquote %}

NEW: DevDocs now comes with syntax highlighting. http://devdocs.io

@DevDocstwitter.com/devdocs/status/356095192085962752

引用网络上的文章

1
2
3
{% blockquote Seth Godin http://sethgodin.typepad.com/seths_blog/2009/07/welcome-to-island-marketing.html Welcome to Island Marketing %}
Every interaction is both precious and an opportunity to delight.
{% endblockquote %}

Every interaction is both precious and an opportunity to delight.

Seth GodinWelcome to Island Marketing

代码块

在文章中插入代码。

别名: code

1
2
3
{% codeblock [title] [lang:language] [url] [link text] [additional options] %}
code snippet
{% endcodeblock %}

Specify additional options in option:value format, e.g. line_number:false first_line:5.

Extra Options Description Default
line_number Show line number true
highlight Enable code highlighting true
first_line Specify the first line number 1
mark Line highlight specific line(s), each value separated by a comma. Specify number range using a dash Example: mark:1,4-7,10 will mark line 1, 4 to 7 and 10.
wrap Wrap the code block in `` true

样例

普通的代码块

1
2
3
4
{% codeblock %}
alert('Hello World!');
{% endcodeblock %}
alert('Hello World!');

指定语言

1
2
3
4
{% codeblock lang:objc %}
[rectangle setX: 10 y: 10 width: 20 height: 20];
{% endcodeblock %}
[rectangle setX: 10 y: 10 width: 20 height: 20];

附加说明

1
2
3
4
{% codeblock Array.map %}
array.map(callback[, thisArg])
{% endcodeblock %}
Array.maparray.map(callback[, thisArg])

附加说明和网址

1
2
3
4
5
6
{% codeblock _.compact http://underscorejs.org/#compact Underscore.js %}
_.compact([0, 1, false, 2, '', 3]);
=> [1, 2, 3]
{% endcodeblock %}
_.compactUnderscore.js_.compact([0, 1, false, 2, '', 3]);
=> [1, 2, 3]

反引号代码块

另一种形式的代码块,不同的是它使用三个反引号来包裹。

``` [language] [title] [url] [link text] code snippet ```

Pull Quote

在文章中插入 Pull quote。

1
2
3
{% pullquote [class] %}
content
{% endpullquote %}

jsFiddle

在文章中嵌入 jsFiddle。

1
{% jsfiddle shorttag [tabs] [skin] [width] [height] %}

Gist

在文章中嵌入 Gist。

1
{% gist gist_id [filename] %}

iframe

在文章中插入 iframe。

1
{% iframe url [width] [height] %}

Image

在文章中插入指定大小的图片。

1
{% img [class names] /path/to/image [width] [height] '"title text" "alt text"' %}

在文章中插入链接,并自动给外部链接添加 target="_blank" 属性。

1
{% link text url [external] [title] %}

Include Code

插入 source/downloads/code 文件夹内的代码文件。source/downloads/code 不是固定的,取决于你在配置文件中 code_dir 的配置。

1
{% include_code [title] [lang:language] [from:line] [to:line] path/to/file %}

样例

嵌入 test.js 文件全文

1
{% include_code lang:javascript test.js %}

只嵌入第 3 行

1
{% include_code lang:javascript from:3 to:3 test.js %}

嵌入第 5 行至第 8 行

1
{% include_code lang:javascript from:5 to:8 test.js %}

嵌入第 5 行至文件结束

1
{% include_code lang:javascript from:5 test.js %}

嵌入第 1 行至第 8 行

1
{% include_code lang:javascript to:8 test.js %}

Youtube

在文章中插入 Youtube 视频。

1
{% youtube video_id [type] [cookie] %}

Examples

视频

1
{% youtube lJIrF4YjHfQ %}

效果:

播放列表

1
{% youtube PL9hW1uS6HUfscJ9DHkOSoOX45MjXduUxo 'playlist' %}

效果:

隐私模式

禁止 YouTube cookie

1
2
{% youtube lJIrF4YjHfQ false %}
{% youtube PL9hW1uS6HUfscJ9DHkOSoOX45MjXduUxo 'playlist' false %}

Vimeo

在文章中插入 Vimeo 视频。

1
{% vimeo video_id %}

引用文章

引用其他文章的链接。

1
2
{% post_path filename %}
{% post_link filename [title] [escape] %}

在使用此标签时可以忽略文章文件所在的路径或者文章的永久链接信息、如语言、日期。

例如,在文章中使用 {% post_link how-to-bake-a-cake %} 时,只需有一个名为 how-to-bake-a-cake.md 的文章文件即可。即使这个文件位于站点文件夹的 source/posts/2015-02-my-family-holiday 目录下、或者文章的永久链接是 2018/en/how-to-bake-a-cake,都没有影响。

默认链接文字是文章的标题,你也可以自定义要显示的文本。

默认对文章的标题和自定义标题里的特殊字符进行转义。可以使用escape选项,禁止对特殊字符进行转义。

链接使用文章的标题

1
{% post_link hexo-3-8-released %}

Hexo 3.8.0 Released

链接使用自定义文字

1
{% post_link hexo-3-8-released '通往文章的链接' %}

通往文章的链接

对标题的特殊字符进行转义

1
{% post_link hexo-4-released 'How to use <b> tag in title' %}

How to use tag in title

禁止对标题的特殊字符进行转义

1
{% post_link hexo-4-released '<b>bold</b> custom title' false %}

bold custom title

引用资源

引用文章的资源。

1
2
3
{% asset_path filename %}
{% asset_img [class names] slug [width] [height] [title text [alt text]] %}
{% asset_link filename [title] [escape] %}

Embed image

hexo-renderer-marked 3.1.0+ can (optionally) resolves the post’s path of an image automatically, refer to this section on how to enable it.

“foo.jpg” is located at http://example.com/2020/01/02/hello/foo.jpg.

Default (no option)

1
2
{% asset_img foo.jpg %}
<img src="/2020/01/02/hello/foo.jpg">

Custom class

1
2
{% asset_img post-image foo.jpg %}
<img src="/2020/01/02/hello/foo.jpg" class="post-image">

Display size

1
2
{% asset_img foo.jpg 500 400 %}
<img src="/2020/01/02/hello/foo.jpg" width="500" height="400">

Title & Alt

1
2
{% asset_img logo.svg "lorem ipsum'dolor'" %}
<img src="/2020/01/02/hello/foo.jpg" title="lorem ipsum" alt="dolor">

Raw

如果您想在文章中插入 Swig 标签,可以尝试使用 Raw 标签,以免发生解析异常。

1
2
3
{% raw %}
content
{% endraw %}

文章摘要和截断

在文章中使用 <!-- more -->,那么 <!-- more --> 之前的文字将会被视为摘要。首页中将只出现这部分文字,同时这部分文字也会出现在正文之中。

例如:

1
2
3
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<!-- more -->
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

首页中将只会出现

1
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

正文中则会出现

1
2
3
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

注意,摘要可能会被 Front Matter 中的 excerpt 覆盖。