タグを使用することで、記事に簡単にスニペットを挿入できます。
概要
hexo.extend.tag.register(name, function(args, content){ }, options);
|
タグ関数にはargs
とcontent
の2つの引数が渡されます。args
はタグに渡された引数、content
はタグでラップされたコンテンツです。
Hexo 3での非同期レンダリングの導入以来、レンダリングにはNunjucksが使用されています。Swigの挙動とは少し異なる場合があります。
タグの登録解除
unregister()
を使用して、既存のタグプラグインをカスタム関数で置き換えます。
hexo.extend.tag.unregister(name);
|
例
const tagFn = (args, content) => { content = 'something'; return content; };
hexo.extend.tag.unregister('youtube');
hexo.extend.tag.register('youtube', tagFn);
|
オプション
ends
終了タグを使用します。デフォルトでfalse
です。
async
非同期モードを有効にします。デフォルトでfalse
です。
例
終了タグなし
Youtubeビデオを挿入します。
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>'; });
|
終了タグあり
プルクオートを挿入します。
hexo.extend.tag.register('pullquote', function(args, content){ var className = args.join(' '); return '<blockquote class="pullquote' + className + '">' + content + '</blockquote>'; }, {ends: true});
|
非同期レンダリング
ファイルを挿入します。
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とユーザー設定
以下の方法で利用できます:
hexo.extend.tag.register('foo', function (args) { const [firstArg] = args;
const { config } = hexo; const editor = config.author + firstArg;
const { config: themeCfg } = hexo.theme; if (themeCfg.fancybox)
const { title } = this;
const { _content } = this; const { content } = this;
return 'foo'; });
|
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)
const { title, _content, content } = this;
return 'foo'; }; };
|