タグを使用すると、ユーザーはスニペットを投稿に素早く簡単に挿入できます。
概要
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 }, );
|
フロントマターとユーザー設定
次のオプションはいずれも有効です
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'; }; };
|