Skip to content

Commit

Permalink
refactor(nunjucks): fix data.path & change filter name
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 3, 2020
1 parent eb1a31d commit 821e075
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
42 changes: 25 additions & 17 deletions lib/plugins/renderer/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const nunjucks = require('nunjucks');
const fs = require('hexo-fs');
const { readFileSync } = require('hexo-fs');
const { dirname } = require('path');

function toArray(value) {
Expand Down Expand Up @@ -32,31 +32,39 @@ function safeJsonStringify(json, spacer = undefined) {
return '""';
}

const nunjucksCfg = {
autoescape: false,
throwOnUndefined: false,
trimBlocks: false,
lstripBlocks: false
};

const nunjucksAddFilter = env => {
env.addFilter('toarray', toArray);
env.addFilter('safedump', safeJsonStringify);
};

function njkCompile(data) {
const env = nunjucks.configure(dirname(data.path), {
autoescape: false,
throwOnUndefined: false,
trimBlocks: false,
lstripBlocks: false
});
env.addFilter('toArray', toArray);
env.addFilter('safeDump', safeJsonStringify);

return nunjucks.compile(
'text' in data ? data.text : fs.readFileSync(data.path),
env,
data.path
);
let env;
if (data.path) {
env = nunjucks.configure(dirname(data.path), nunjucksCfg);
} else {
env = nunjucks.configure(nunjucksCfg);
}
nunjucksAddFilter(env);

const text = 'text' in data ? data.text : readFileSync(data.path);

return nunjucks.compile(text, env, data.path);
}

function njkRenderer(data, locals) {
return njkCompile(data).render(locals);
}

njkRenderer.compile = data => {
const compiled = njkCompile(data);
// Need a closure to keep the compiled template.
return locals => compiled.render(locals);
return locals => njkCompile(data).render(locals);
};

module.exports = njkRenderer;
46 changes: 23 additions & 23 deletions test/scripts/renderers/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ describe('nunjucks', () => {

describe('nunjucks filters', () => {
const forLoop = [
'{% for x in arr | toArray %}',
'{% for x in arr | toarray %}',
'{{ x }}',
'{% endfor %}'
].join('');

it('toArray can iterate on Warehouse collections', () => {
it('toarray can iterate on Warehouse collections', () => {
const data = {
arr: {
toArray() {
Expand All @@ -66,40 +66,40 @@ describe('nunjucks', () => {
r({ text: forLoop }, data).should.eql('123');
});

it('toArray can iterate on plain array', () => {
it('toarray can iterate on plain array', () => {
const data = {
arr: [1, 2, 3]
};

r({ text: forLoop }, data).should.eql('123');
});

it('toArray can iterate on string', () => {
it('toarray can iterate on string', () => {
const data = {
arr: '123'
};

r({ text: forLoop }, data).should.eql('123');
});

// https://github.com/lodash/lodash/blob/master/test/toArray.test.js
it('toArray can iterate on objects', () => {
// https://github.com/lodash/lodash/blob/master/test/toarray.test.js
it('toarray can iterate on objects', () => {
const data = {
arr: { a: '1', b: '2', c: '3' }
};

r({ text: forLoop }, data).should.eql('123');
});

it('toArray can iterate on object string', () => {
it('toarray can iterate on object string', () => {
const data = {
arr: Object('123')
};

r({ text: forLoop }, data).should.eql('123');
});

it('toArray can iterate on Map', () => {
it('toarray can iterate on Map', () => {
const data = {
arr: new Map()
};
Expand All @@ -111,7 +111,7 @@ describe('nunjucks', () => {
r({ text: forLoop }, data).should.eql('123');
});

it('toArray can iterate on Set', () => {
it('toarray can iterate on Set', () => {
const data = {
arr: new Set()
};
Expand All @@ -123,38 +123,38 @@ describe('nunjucks', () => {
r({ text: forLoop }, data).should.eql('123');
});

it('safeDump undefined', () => {
it('safedump undefined', () => {
const text = [
'{{ items | safeDump }}'
'{{ items | safedump }}'
].join('\n');

r({ text }).should.eql('""');
});

it('safeDump null', () => {
it('safedump null', () => {
const text = [
'{% set items = null %}',
'{{ items | safeDump }}'
'{{ items | safedump }}'
].join('\n');

r({ text }).should.eql('\n""');
});

// Adapt from nunjucks test cases
// https://github.com/mozilla/nunjucks/blob/9a0ce364effd28fcdb3ab922fcffa9343b7b3630/tests/filters.js#L98
it('safeDump default', () => {
it('safedump default', () => {
const text = [
'{% set items = ["a", 1, { b : true}] %}',
'{{ items | safeDump }}'
'{{ items | safedump }}'
].join('\n');

r({ text }).should.eql('\n["a",1,{"b":true}]');
});

it('safeDump spacer - 2', () => {
it('safedump spacer - 2', () => {
const text = [
'{% set items = ["a", 1, { b : true}] %}',
'{{ items | safeDump(2) }}'
'{{ items | safedump(2) }}'
].join('\n');

r({ text }).should.eql([
Expand All @@ -169,10 +169,10 @@ describe('nunjucks', () => {
].join('\n'));
});

it('safeDump spacer - 2', () => {
it('safedump spacer - 2', () => {
const text = [
'{% set items = ["a", 1, { b : true}] %}',
'{{ items | safeDump(2) }}'
'{{ items | safedump(2) }}'
].join('\n');

r({ text }).should.eql([
Expand All @@ -187,10 +187,10 @@ describe('nunjucks', () => {
].join('\n'));
});

it('safeDump spacer - 4', () => {
it('safedump spacer - 4', () => {
const text = [
'{% set items = ["a", 1, { b : true}] %}',
'{{ items | safeDump(4) }}'
'{{ items | safedump(4) }}'
].join('\n');

r({ text }).should.eql([
Expand All @@ -205,10 +205,10 @@ describe('nunjucks', () => {
].join('\n'));
});

it('safeDump spacer - \\t', () => {
it('safedump spacer - \\t', () => {
const text = [
'{% set items = ["a", 1, { b : true}] %}',
'{{ items | safeDump(\'\t\') }}'
'{{ items | safedump(\'\t\') }}'
].join('\n');

r({ text }).should.eql([
Expand Down

0 comments on commit 821e075

Please sign in to comment.