An injector is used to add static code snippet to the <head>
or/and <body>
of generated HTML files. Hexo run injector before after_render:html
filter is executed.
Synopsis
hexo.extend.injector.register(entry, value, to); |
entry <string>
Where the code will be injected inside the HTML.
Support those values:
head_begin
: Inject code snippet right after<head>
(Default).head_end
: Inject code snippet right before</head>
.body_begin
: Inject code snippet right after<body>
.body_end
: Inject code snippet right before</body>
.
value <string> | <Function>
A function that returns string is supported.
The code snippet to be injected.
to <string>
Which page will code snippets being injected.
default
: Inject to every page (Default).home
: Only inject to home page (which hasis_home()
helper beingtrue
)post
: Only inject to post pages (which hasis_post()
helper beingtrue
)page
: Only inject to pages (which hasis_page()
helper beingtrue
)archive
: Only inject to archive pages (which hasis_archive()
helper beingtrue
)category
: Only inject to category pages (which hasis_category()
helper beingtrue
)tag
: Only inject to tag pages (which hasis_tag()
helper beingtrue
)- Custom layout name could be used as well, see Writing - Layout.
There are other internal functions, see hexojs/hexo#4049 for more details.
Example
const css = hexo.extend.helper.get("css").bind(hexo); |
Above setup will inject APlayer.min.css
(<link>
tag) to the </head>
of any page which layout is music
, and APlayer.min.js
(<script>
tag) to the </body>
of those pages. Also, jquery.js
(<script>
tag) will be injected to </body>
of every page generated.
Accessing user configuration
Use any of the following options:
const css = hexo.extend.helper.get("css").bind(hexo); |
/* global hexo */ |
module.exports = function () { |
function injectFn() { |
/* global hexo */ |
module.exports = (hexo) => () => { |
const injectFn = (hexo) => { |