注入器被用于将静态代码片段注入生成的 HTML 的 <head> 和/或 <body> 中。Hexo 将在 after_render:html 过滤器 之前 完成注入。
概要
hexo.extend.injector.register(entry, value, to) |
entry <string>
代码片段注入的位置,接受以下值:
head_begin: 注入在<head>之后(默认)head_end: 注入在</head>之前body_begin: 注入在<body>之后body_end: 注入在</body>之前
value <string> | <Function>
除了字符串,也支持返回值为字符串的函数
需要注入的代码片段。
to <string>
需要注入代码片段的页面类型,接受以下值:
default: 注入到每个页面(默认值)home: 只注入到主页(is_home()为true的页面)post: 只注入到文章页面(is_post()为true的页面)page: 只注入到独立页面(is_page()为true的页面)archive: 只注入到归档页面(is_archive()为true的页面)category: 只注入到分类页面(is_category()为true的页面)tag: 只注入到标签页面(is_tag()为true的页面)- 或是其他自定义 layout 名称,自定义 layout 参见 写作 - 布局(Layout)
注入器还有一些内部函数,如果你要使用它们,请参考 hexojs/hexo#4049。
样例
const css = hexo.extend.helper.get('css').bind(hexo); |
上述代码将会把 APlayer.min.css(<link> 标签)和 APlayer.min.js (<script> 标签)注入到所有 layout 为 music 的页面的 </head> 和 </body> 之前,以及将 jquery.js(<script> 标签)注入到每一个生成的页面的 </body> 之前。
Accessing user configuration
Use any of the following options:
1.
const css = hexo.extend.helper.get('css').bind(hexo); |
2.
/* global hexo */ |
module.exports = function () { |
function injectFn() { |
3.
/* global hexo */ |
module.exports = (hexo) => () => { |
const injectFn = (hexo) => { |