diff --git a/source/_data/sidebar.yml b/source/_data/sidebar.yml index a1f5e545e4..ec5ede54d5 100644 --- a/source/_data/sidebar.yml +++ b/source/_data/sidebar.yml @@ -46,6 +46,7 @@ api: filter: filter.html generator: generator.html helper: helper.html + injector: injector.html migrator: migrator.html processor: processor.html renderer: renderer.html diff --git a/source/api/injector.md b/source/api/injector.md new file mode 100644 index 0000000000..ffe668aba7 --- /dev/null +++ b/source/api/injector.md @@ -0,0 +1,63 @@ +title: Injector +--- + +An injector is used to add static code snippet to the `` or/and `` of generated HTML files. Hexo run injector **before** `after_render:html` filter is executed. + +## Synopsis + +```js +hexo.extend.injector.register(entry, value, to) +``` + +### entry `` + +Where the code will be injected inside the HTML. + +Support those values: + +- `head_begin`: Inject code snippet right after `` (Default). +- `head_end`: Inject code snippet right before ``. +- `body_begin`: Inject code snippet right after ``. +- `body_end`: Inject code snippet right before ``. + +### value ` | ` + +> A function that returns string is supported. + +The code snippet to be injected. + +### to `` + +Which page will code snippets being injected. + +- `default`: Inject to every page (Default). +- `home`: Only inject to home page (which has `is_home()` helper being `true`) +- `post`: Only inject to post pages (which has `is_post()` helper being `true`) +- `page`: Only inject to pages (which has `is_page()` helper being `true`) +- `archive`: Only inject to archive pages (which has `is_archive()` helper being `true`) +- `category`: Only inject to category pages (which has `is_category()` helper being `true`) +- `tag`: Only inject to tag pages (which has `is_tag()` helper being `true`) +- Custom layout name could be used as well, see [Writing - Layout](writing#Layout). + +---- + +There are other internal functions, see [hexojs/hexo#4049](https://github.com/hexojs/hexo/pull/4049) for more details. + +## Example + +```js +const css = hexo.extend.helper.get('css'); +const js = hexo.extend.helper.get('js'); + +hexo.extend.injector.register('head_end', () => { + return css('https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.css'); +}, 'music'); + +hexo.extend.injector.register('body_end', '