Skip to content

Commit

Permalink
Merge aedc3b5 into 1323f96
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Parisot committed Apr 3, 2019
2 parents 1323f96 + aedc3b5 commit 9e3c9ab
Show file tree
Hide file tree
Showing 10 changed files with 4,712 additions and 95 deletions.
16 changes: 16 additions & 0 deletions lib/plugins/helper/to_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var isObject = require('lodash/isObject.js');
var _toArray = require('lodash/toArray.js');

function toArray(value) {
if (isObject(value) && typeof value.toArray === 'function') {
return value.toArray();
} else if (Array.isArray(value)) {
return value;
}

return toArray(value);
}

module.exports = toArray;
6 changes: 5 additions & 1 deletion lib/plugins/renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ module.exports = ctx => {

renderer.register('json', 'json', require('./json'), true);

renderer.register('swig', 'html', require('./swig'), true);
const nunjucks = require('./nunjucks');

renderer.register('nunjucks', 'html', nunjucks, true);
renderer.register('njk', 'html', nunjucks, true);
renderer.register('j2', 'html', nunjucks, true);

const yaml = require('./yaml');

Expand Down
37 changes: 37 additions & 0 deletions lib/plugins/renderer/nunjucks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

var nunjucks = require('nunjucks');
var fs = require('hexo-fs');
var toArray = require('../helper/to_array');

var env = nunjucks.configure({
noCache: true,
autoescape: false,
throwOnUndefined: false,
trimBlocks: false,
lstripBlocks: false
});

env.addFilter('toArray', toArray);

function nunjucksRenderer(data, locals) {
if ('text' in data) {
return nunjucks.renderString(data.text, locals);
}

return nunjucks.render(data.path, locals);
}

nunjucksRenderer.compile = function(data) {
var compiled;

if ('text' in data) {
compiled = nunjucks.compile(data.text);
} else {
compiled = nunjucks.compile(fs.readFileSync(data.path));
}

return compiled.render.bind(compiled);
};

module.exports = nunjucksRenderer;
45 changes: 0 additions & 45 deletions lib/plugins/renderer/swig.js

This file was deleted.

0 comments on commit 9e3c9ab

Please sign in to comment.