1.0.0 • Published 7 years ago
hexo-renderer-njcks v1.0.0
hexo-renderer-njcks
Yet another Nunjucks renderer for Hexo.
Features 🎉
- Nunjucks 3
- Predefined filters
- Full Nunjucks renderer customization via Hexo Filter API
Installation
npm install hexo-renderer-njcksConfiguration
You can customize the Nunjucks renderer via the 2 following ways.
Config files
All configs are passed directly to Nunjucks#configure(), available options can be found here.
The following list shows the descending precedence of config files. (The first one presented is used.)
- Key
nunjucksin<hexo_root>/_config.yml - Key
theme_config.nunjucksin<hexo_root>/_config.yml - Key
nunjucksin<hexo_root>/themes/<theme_name>/_config.yml - Inline default config
Hexo Filter API
You can extend the before_render:nunjucks filter whose first argument is an Nunjucks Environment object, which is used later to render templates.
Note that the filter should be synchronized.
For example, you can use the following code in your Hexo script to add a new Nunjucks filter named split to split strings into arrays of strings.
hexo.extend.filter.register('before_render:nunjucks', function SplitFilter (env) {
env.addFilter('split', (str, del) => str.split(del))
})In the template, use the Nunjucks filter as usual.
{% for i in "1 2 3 4 5"|split(' ') %}#{{ i }}{% endfor %}Output should be
#1#2#3#4#5Built-in Filters
typeof
Template
{{ "test" | typeof }}Output
stringxmlattr
Hexo script
hexo.extend.filter.register('before_render:nunjucks', function GlobalStyleObj (env) {
env.addGlobal('DEFAULT_STYLE', {
someAttr: 'a',
other: 'b'
})
})Template
<div {{ DEFAULT_STYLE | xmlattr }}></div>Output
<div some-attr="a" other="b"></div>1.0.0
7 years ago