From cf99c978dc8de3ec5dc79e6e90d1356771ac2cfc Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Mon, 11 Dec 2017 20:19:48 +0000 Subject: [PATCH 1/4] Replace built-in swig renderer with nunjucks renderer --- lib/extend/tag.js | 4 +-- lib/plugins/renderer/index.js | 3 ++- lib/plugins/renderer/swig.js | 45 --------------------------------- package.json | 4 +-- test/scripts/renderers/index.js | 1 - 5 files changed, 5 insertions(+), 52 deletions(-) delete mode 100644 lib/plugins/renderer/swig.js diff --git a/lib/extend/tag.js b/lib/extend/tag.js index 63ca80dd2c..864020d909 100644 --- a/lib/extend/tag.js +++ b/lib/extend/tag.js @@ -2,7 +2,7 @@ const { stripIndent } = require('hexo-util'); const { cyan } = require('chalk'); -const nunjucks = require('nunjucks'); +const { Environment } = require('hexo-renderer-nunjucks'); const Promise = require('bluebird'); const placeholder = '\uFFFC'; const rPlaceholder = /(?:<|<)!--\uFFFC(\d+)--(?:>|>)/g; @@ -178,7 +178,7 @@ const formatNunjucksError = (err, input) => { class Tag { constructor() { - this.env = new nunjucks.Environment(null, { + this.env = new Environment(null, { autoescape: false }); } diff --git a/lib/plugins/renderer/index.js b/lib/plugins/renderer/index.js index 3904c29960..356cf0c211 100644 --- a/lib/plugins/renderer/index.js +++ b/lib/plugins/renderer/index.js @@ -12,7 +12,8 @@ module.exports = ctx => { renderer.register('json', 'json', require('./json'), true); - renderer.register('swig', 'html', require('./swig'), true); + const nunjucks = require('hexo-renderer-nunjucks'); + nunjucks.register(ctx); const yaml = require('./yaml'); diff --git a/lib/plugins/renderer/swig.js b/lib/plugins/renderer/swig.js deleted file mode 100644 index d272df7a0c..0000000000 --- a/lib/plugins/renderer/swig.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict'; - -const swig = require('swig-templates'); -const extras = require('swig-extras'); -const forTag = require('swig-templates/lib/tags/for'); - -extras.useTag(swig, 'markdown'); -extras.useTag(swig, 'switch'); -extras.useTag(swig, 'case'); - -extras.useFilter(swig, 'batch'); -extras.useFilter(swig, 'groupby'); -extras.useFilter(swig, 'markdown'); -extras.useFilter(swig, 'nl2br'); -extras.useFilter(swig, 'pluck'); -extras.useFilter(swig, 'split'); -extras.useFilter(swig, 'trim'); -extras.useFilter(swig, 'truncate'); - -swig.setDefaults({ - cache: false, - autoescape: false -}); - -// Hack: Override for tag of Swig -swig.setTag('for', forTag.parse, (...args) => { - const compile = forTag.compile(...args).split('\n'); - - compile.splice(3, 0, ' if (!Array.isArray(__l) && typeof __l.toArray === "function") { __l = __l.toArray(); }'); - - return compile.join('\n'); -}, forTag.ends, true); - -function swigRenderer(data, locals) { - return swig.render(data.text, { - locals, - filename: data.path - }); -} - -swigRenderer.compile = (data, locals) => swig.compile(data.text, { - filename: data.path -}); - -module.exports = swigRenderer; diff --git a/package.json b/package.json index 67904bf6fc..1da4217f10 100644 --- a/package.json +++ b/package.json @@ -48,18 +48,16 @@ "hexo-fs": "^2.0.0", "hexo-i18n": "^1.0.0", "hexo-log": "^1.0.0", + "hexo-renderer-nunjucks": "^2.0.0", "hexo-util": "^1.9.0", "js-yaml": "^3.12.0", "lodash": "^4.17.11", "micromatch": "^4.0.2", "moment": "^2.22.2", "moment-timezone": "^0.5.21", - "nunjucks": "^3.1.3", "pretty-hrtime": "^1.0.3", "resolve": "^1.8.1", "strip-ansi": "^6.0.0", - "swig-extras": "0.0.1", - "swig-templates": "^2.0.3", "text-table": "^0.2.0", "tildify": "^2.0.0", "titlecase": "^1.1.2", diff --git a/test/scripts/renderers/index.js b/test/scripts/renderers/index.js index 9417ec7c7e..b79059d7f3 100644 --- a/test/scripts/renderers/index.js +++ b/test/scripts/renderers/index.js @@ -3,6 +3,5 @@ describe('Renderers', () => { require('./json'); require('./plain'); - require('./swig'); require('./yaml'); }); From 35fc684fcdd63a6fd603db5de693543dec71d901 Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Wed, 13 Dec 2017 14:05:21 +0100 Subject: [PATCH 2/4] Run swig tests with nunjucks renderer --- test/scripts/renderers/swig.js | 47 ---------------------------------- 1 file changed, 47 deletions(-) delete mode 100644 test/scripts/renderers/swig.js diff --git a/test/scripts/renderers/swig.js b/test/scripts/renderers/swig.js deleted file mode 100644 index 53727bb163..0000000000 --- a/test/scripts/renderers/swig.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict'; - -describe('swig', () => { - const r = require('../../../lib/plugins/renderer/swig'); - - it('normal', () => { - const body = [ - 'Hello {{ name }}!' - ].join('\n'); - - r({text: body}, { - name: 'world' - }).should.eql('Hello world!'); - }); - - it('override "for" tag', () => { - const body = [ - '{% for x in arr %}', - '{{ x }}', - '{% endfor %}' - ].join(''); - - const data = { - arr: { - toArray() { - return [1, 2, 3]; - } - } - }; - - r({text: body}, data).should.eql('123'); - }); - - it('compile', () => { - const body = [ - 'Hello {{ name }}!' - ].join('\n'); - - const render = r.compile({ - text: body - }); - - render({ - name: 'world' - }).should.eql('Hello world!'); - }); -}); From 3d4f4ee1bc09ebf8cc391c903b8b1efc35d8ee46 Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Fri, 17 Jan 2020 13:19:02 +0100 Subject: [PATCH 3/4] Remove Swig rendering condition --- lib/hexo/post.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 1d62074eca..5759689ae8 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -243,8 +243,6 @@ class Post { return Promise.reject(new Error('No input file or string!')).asCallback(callback); } - const isSwig = ext === 'swig'; - // disable Nunjucks when the renderer spcify that. const disableNunjucks = ext && ctx.render.renderer.get(ext) && !!ctx.render.renderer.get(ext).disableNunjucks; @@ -258,12 +256,7 @@ class Post { }).then(() => { data.content = cacheObj.escapeContent(data.content); - if (isSwig) { - // Render with Nunjucks if this is a swig file - return tag.render(data.content, data); - } - - // Escape all Swig tags + // Escape all Nunjucks/Swig tags if (!disableNunjucks) { data.content = cacheObj.escapeAllSwigTags(data.content); } From 2cdecbfdec9ec3c23226b88445de74c08aff44ae Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Fri, 17 Jan 2020 13:19:26 +0100 Subject: [PATCH 4/4] Replace Swig tests with nunjucks --- test/scripts/console/generate.js | 4 +- test/scripts/extend/renderer.js | 8 ++-- test/scripts/helpers/partial.js | 16 ++++---- test/scripts/hexo/hexo.js | 22 +++++----- test/scripts/hexo/post.js | 10 ++--- test/scripts/hexo/render.js | 16 ++++---- test/scripts/processors/asset.js | 22 +++++----- test/scripts/theme/theme.js | 30 +++++++------- test/scripts/theme/view.js | 54 ++++++++++++------------- test/scripts/theme_processors/source.js | 1 + test/scripts/theme_processors/view.js | 20 ++++----- 11 files changed, 103 insertions(+), 100 deletions(-) diff --git a/test/scripts/console/generate.js b/test/scripts/console/generate.js index 13d934164d..dda6986ed3 100644 --- a/test/scripts/console/generate.js +++ b/test/scripts/console/generate.js @@ -183,14 +183,14 @@ describe('generate', () => { // Add some source files writeFile(join(hexo.theme_dir, 'source', 'a.txt'), 'a'), writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'b'), - writeFile(join(hexo.theme_dir, 'source', 'c.swig'), 'c') + writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'c') ]); await generate(); // Update source file await Promise.all([ writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'bb'), - writeFile(join(hexo.theme_dir, 'source', 'c.swig'), 'cc') + writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'cc') ]); // Generate again diff --git a/test/scripts/extend/renderer.js b/test/scripts/extend/renderer.js index 6be084536f..be09798410 100644 --- a/test/scripts/extend/renderer.js +++ b/test/scripts/extend/renderer.js @@ -90,11 +90,11 @@ describe('Renderer', () => { r.isRenderableSync('yaml').should.be.false; - r.register('swig', 'html', () => {}, true); + r.register('njk', 'html', () => {}, true); - r.isRenderableSync('swig').should.be.true; - r.isRenderableSync('.swig').should.be.true; - r.isRenderableSync('layout.swig').should.be.true; + r.isRenderableSync('njk').should.be.true; + r.isRenderableSync('.njk').should.be.true; + r.isRenderableSync('layout.njk').should.be.true; r.isRenderableSync('foo.html').should.be.false; }); diff --git a/test/scripts/helpers/partial.js b/test/scripts/helpers/partial.js index 9700ffc5c9..129ae0f355 100644 --- a/test/scripts/helpers/partial.js +++ b/test/scripts/helpers/partial.js @@ -9,7 +9,7 @@ describe('partial', () => { const hexo = new Hexo(pathFn.join(__dirname, 'partial_test'), {silent: true}); const themeDir = pathFn.join(hexo.base_dir, 'themes', 'test'); const viewDir = pathFn.join(themeDir, 'layout') + pathFn.sep; - const viewName = 'article.swig'; + const viewName = 'article.njk'; const ctx = { site: hexo.locals, @@ -32,7 +32,7 @@ describe('partial', () => { fs.writeFile(hexo.config_path, 'theme: test') ]); await hexo.init(); - hexo.theme.setView('widget/tag.swig', 'tag widget'); + hexo.theme.setView('widget/tag.njk', 'tag widget'); }); after(() => fs.rmdir(hexo.base_dir)); @@ -52,28 +52,28 @@ describe('partial', () => { }); it('locals', () => { - hexo.theme.setView('test.swig', '{{ foo }}'); + hexo.theme.setView('test.njk', '{{ foo }}'); partial('test', {foo: 'bar'}).should.eql('bar'); }); it('cache', () => { - hexo.theme.setView('test.swig', '{{ foo }}'); + hexo.theme.setView('test.njk', '{{ foo }}'); partial('test', {foo: 'bar'}, {cache: true}).should.eql('bar'); partial('test', {}, {cache: true}).should.eql('bar'); }); it('only', () => { - hexo.theme.setView('test.swig', '{{ foo }}{{ bar }}'); + hexo.theme.setView('test.njk', '{{ foo }}{{ bar }}'); partial('test', {bar: 'bar'}, {only: true}).should.eql('bar'); }); it('a partial in another partial', () => { - hexo.theme.setView('partial/a.swig', '{{ partial("b") }}'); - hexo.theme.setView('partial/b.swig', '{{ partial("c") }}'); - hexo.theme.setView('partial/c.swig', 'c'); + hexo.theme.setView('partial/a.njk', '{{ partial("b") }}'); + hexo.theme.setView('partial/b.njk', '{{ partial("c") }}'); + hexo.theme.setView('partial/c.njk', 'c'); partial('partial/a').should.eql('c'); }); diff --git a/test/scripts/hexo/hexo.js b/test/scripts/hexo/hexo.js index 69a193c035..e001c7039b 100644 --- a/test/scripts/hexo/hexo.js +++ b/test/scripts/hexo/hexo.js @@ -351,7 +351,7 @@ describe('Hexo', () => { }); it('_generate() - layout', () => { - hexo.theme.setView('test.swig', [ + hexo.theme.setView('test.njk', [ '{{ config.title }}', '{{ page.foo }}', '{{ layout }}', @@ -378,7 +378,7 @@ describe('Hexo', () => { }); it('_generate() - layout array', () => { - hexo.theme.setView('baz.swig', 'baz'); + hexo.theme.setView('baz.njk', 'baz'); hexo.extend.generator.register('test', () => ({ path: 'test', @@ -418,7 +418,7 @@ describe('Hexo', () => { it('_generate() - after_route_render filter', () => { const hook = spy(result => result.replace('foo', 'bar')); hexo.extend.filter.register('after_route_render', hook); - hexo.theme.setView('test.swig', 'foo'); + hexo.theme.setView('test.njk', 'foo'); hexo.extend.generator.register('test', () => ({ path: 'test', layout: 'test' @@ -442,7 +442,7 @@ describe('Hexo', () => { }); it('_generate() - validate locals', () => { - hexo.theme.setView('test.swig', [ + hexo.theme.setView('test.njk', [ '{{ path }}', '{{ url }}', '{{ view_dir }}' @@ -464,7 +464,7 @@ describe('Hexo', () => { const path = 'bár'; hexo.config.url = 'http://fôo.com'; - hexo.theme.setView('test.swig', '{{ url }}'); + hexo.theme.setView('test.njk', '{{ url }}'); hexo.extend.generator.register('test', () => ({ path, @@ -488,7 +488,7 @@ describe('Hexo', () => { it('_generate() - reset cache for new route', () => { let count = 0; - hexo.theme.setView('test.swig', '{{ page.count() }}'); + hexo.theme.setView('test.njk', '{{ page.count() }}'); hexo.extend.generator.register('test', () => ({ path: 'test', @@ -508,7 +508,7 @@ describe('Hexo', () => { it('_generate() - cache disabled and use new route', () => { let count = 0; - hexo.theme.setView('test.swig', '{{ page.count() }}'); + hexo.theme.setView('test.njk', '{{ page.count() }}'); hexo.extend.generator.register('test', () => ({ path: 'test', @@ -526,7 +526,7 @@ describe('Hexo', () => { }); it('_generate() - cache disabled & update template', () => { - hexo.theme.setView('test.swig', '0'); + hexo.theme.setView('test.njk', '0'); hexo.extend.generator.register('test', () => ({ path: 'test', @@ -535,12 +535,12 @@ describe('Hexo', () => { return hexo._generate({cache: false}) .then(() => checkStream(route.get('test'), '0')) - .then(() => hexo.theme.setView('test.swig', '1')) + .then(() => hexo.theme.setView('test.njk', '1')) .then(() => checkStream(route.get('test'), '1')); }); it('_generate() - cache enabled & update template', () => { - hexo.theme.setView('test.swig', '0'); + hexo.theme.setView('test.njk', '0'); hexo.extend.generator.register('test', () => ({ path: 'test', @@ -549,7 +549,7 @@ describe('Hexo', () => { return hexo._generate({cache: true}) .then(() => checkStream(route.get('test'), '0')) - .then(() => hexo.theme.setView('test.swig', '1')) + .then(() => hexo.theme.setView('test.njk', '1')) .then(() => checkStream(route.get('test'), '0')); // should return cached result }); diff --git a/test/scripts/hexo/post.js b/test/scripts/hexo/post.js index 6e42af176b..ec049d45c8 100644 --- a/test/scripts/hexo/post.js +++ b/test/scripts/hexo/post.js @@ -608,7 +608,7 @@ describe('Post', () => { }); }); - it('render() - skip render phase if it\'s swig file', () => { + it('render() - skip render phase if it\'s nunjucks file', () => { const content = [ '{% quote Hello World %}', 'quote content', @@ -617,7 +617,7 @@ describe('Post', () => { return post.render(null, { content, - engine: 'swig' + engine: 'njk' }).then(data => { data.content.trim().should.eql([ '

quote content

\n', @@ -626,7 +626,7 @@ describe('Post', () => { }); }); - it('render() - escaping swig blocks with similar names', () => { + it('render() - escaping nunjucks blocks with similar names', () => { const code = 'alert("Hello world")'; const highlighted = highlight(code); @@ -651,7 +651,7 @@ describe('Post', () => { }); }); - it('render() - recover escaped swig blocks which is html escaped', () => { + it('render() - recover escaped nunjucks blocks which is html escaped', () => { const content = '`{% raw %}{{ test }}{% endraw %}`'; return post.render(null, { @@ -662,7 +662,7 @@ describe('Post', () => { }); }); - it('render() - recover escaped swig blocks which is html escaped before post_render', () => { + it('render() - recover escaped nunjucks blocks which is html escaped before post_render', () => { const content = '`{% raw %}{{ test }}{% endraw %}`'; const filter = spy(); diff --git a/test/scripts/hexo/render.js b/test/scripts/hexo/render.js index b37b63599c..38a6d804a5 100644 --- a/test/scripts/hexo/render.js +++ b/test/scripts/hexo/render.js @@ -38,7 +38,8 @@ describe('Render', () => { hexo.render.isRenderable('test.html').should.be.true; // swig - hexo.render.isRenderable('test.swig').should.be.true; + hexo.render.isRenderable('test.swig').should.be.false; + hexo.render.isRenderable('test.njk').should.be.true; // yaml hexo.render.isRenderable('test.yml').should.be.true; @@ -53,7 +54,8 @@ describe('Render', () => { hexo.render.isRenderableSync('test.html').should.be.true; // swig - hexo.render.isRenderableSync('test.swig').should.be.true; + hexo.render.isRenderableSync('test.swig').should.be.false; + hexo.render.isRenderableSync('test.njk').should.be.true; // yaml hexo.render.isRenderableSync('test.yml').should.be.true; @@ -68,7 +70,7 @@ describe('Render', () => { hexo.render.getOutput('test.html').should.eql('html'); // swig - hexo.render.getOutput('test.swig').should.eql('html'); + hexo.render.getOutput('test.njk').should.eql('html'); // yaml hexo.render.getOutput('test.yml').should.eql('json'); @@ -100,7 +102,7 @@ describe('Render', () => { '{{ title }}', '{{ content }}' ].join('\n'), - engine: 'swig' + engine: 'njk' }, { title: 'Hello world', content: 'foobar' @@ -132,7 +134,7 @@ describe('Render', () => { it('render() - after_render filter', () => { const data = { text: ' 123456 ', - engine: 'swig' + engine: 'njk' }; const filter = spy((result, obj) => { @@ -213,7 +215,7 @@ describe('Render', () => { '{{ title }}', '{{ content }}' ].join('\n'), - engine: 'swig' + engine: 'njk' }, { title: 'Hello world', content: 'foobar' @@ -250,7 +252,7 @@ describe('Render', () => { it('renderSync() - after_render filter', () => { const data = { text: ' 123456 ', - engine: 'swig' + engine: 'njk' }; const filter = spy(result => result.trim()); diff --git a/test/scripts/processors/asset.js b/test/scripts/processors/asset.js index 91215388e4..78256560b9 100644 --- a/test/scripts/processors/asset.js +++ b/test/scripts/processors/asset.js @@ -194,7 +194,7 @@ describe('asset', () => { ].join('\n'); const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); @@ -225,7 +225,7 @@ describe('asset', () => { ].join('\n'); const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'update', renderable: true }); @@ -247,7 +247,7 @@ describe('asset', () => { it('page - type: delete', async () => { const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'delete', renderable: true }); @@ -262,7 +262,7 @@ describe('asset', () => { it('page - use the status of the source file if date not set', async () => { const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); @@ -283,7 +283,7 @@ describe('asset', () => { it('page - use the date for updated if use_date_for_updated is set', async () => { const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); @@ -312,7 +312,7 @@ describe('asset', () => { ].join('\n'); const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); @@ -336,7 +336,7 @@ describe('asset', () => { ].join('\n'); const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); @@ -360,7 +360,7 @@ describe('asset', () => { ].join('\n'); const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); @@ -429,7 +429,7 @@ describe('asset', () => { ].join('\n'); const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); @@ -455,7 +455,7 @@ describe('asset', () => { ].join('\n'); const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); @@ -503,7 +503,7 @@ describe('asset', () => { ].join('\n'); const file = newFile({ - path: 'hello.swig', + path: 'hello.njk', type: 'create', renderable: true }); diff --git a/test/scripts/theme/theme.js b/test/scripts/theme/theme.js index 5b6d07ba3d..5ac61bffd1 100644 --- a/test/scripts/theme/theme.js +++ b/test/scripts/theme/theme.js @@ -19,41 +19,41 @@ describe('Theme', () => { after(() => rmdir(hexo.base_dir)); it('getView()', () => { - hexo.theme.setView('test.swig', ''); + hexo.theme.setView('test.njk', ''); // With extension name - hexo.theme.getView('test.swig').should.have.property('path', 'test.swig'); + hexo.theme.getView('test.njk').should.have.property('path', 'test.njk'); // Without extension name - hexo.theme.getView('test').should.have.property('path', 'test.swig'); + hexo.theme.getView('test').should.have.property('path', 'test.njk'); // not exist - should.not.exist(hexo.theme.getView('abc.swig')); + should.not.exist(hexo.theme.getView('abc.njk')); - hexo.theme.removeView('test.swig'); + hexo.theme.removeView('test.njk'); }); it('getView() - escape backslashes', () => { - hexo.theme.setView('foo/bar.swig', ''); + hexo.theme.setView('foo/bar.njk', ''); - hexo.theme.getView('foo\\bar.swig').should.have.property('path', 'foo/bar.swig'); + hexo.theme.getView('foo\\bar.njk').should.have.property('path', 'foo/bar.njk'); - hexo.theme.removeView('foo/bar.swig'); + hexo.theme.removeView('foo/bar.njk'); }); it('setView()', () => { - hexo.theme.setView('test.swig', ''); + hexo.theme.setView('test.njk', ''); - const view = hexo.theme.getView('test.swig'); - view.path.should.eql('test.swig'); + const view = hexo.theme.getView('test.njk'); + view.path.should.eql('test.njk'); - hexo.theme.removeView('test.swig'); + hexo.theme.removeView('test.njk'); }); it('removeView()', () => { - hexo.theme.setView('test.swig', ''); - hexo.theme.removeView('test.swig'); + hexo.theme.setView('test.njk', ''); + hexo.theme.removeView('test.njk'); - should.not.exist(hexo.theme.getView('test.swig')); + should.not.exist(hexo.theme.getView('test.njk')); }); }); diff --git a/test/scripts/theme/view.js b/test/scripts/theme/view.js index a22a341f78..ac652bad2c 100644 --- a/test/scripts/theme/view.js +++ b/test/scripts/theme/view.js @@ -9,7 +9,7 @@ describe('View', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(join(__dirname, 'theme_test')); const themeDir = join(hexo.base_dir, 'themes', 'test'); - const { compile } = Object.assign({}, hexo.extend.renderer.store.swig); + const { compile } = Object.assign({}, hexo.extend.renderer.store.njk); hexo.env.init = true; @@ -24,7 +24,7 @@ describe('View', () => { ]); await hexo.init(); // Setup layout - hexo.theme.setView('layout.swig', [ + hexo.theme.setView('layout.njk', [ 'pre', '{{ body }}', 'post' @@ -33,7 +33,7 @@ describe('View', () => { beforeEach(() => { // Restore compile function - hexo.extend.renderer.store.swig.compile = compile; + hexo.extend.renderer.store.njk.compile = compile; }); after(() => rmdir(hexo.base_dir)); @@ -42,10 +42,10 @@ describe('View', () => { const data = { _content: '' }; - const view = newView('index.swig', data); + const view = newView('index.njk', data); - view.path.should.eql('index.swig'); - view.source.should.eql(join(themeDir, 'layout', 'index.swig')); + view.path.should.eql('index.njk'); + view.source.should.eql(join(themeDir, 'layout', 'index.njk')); view.data.should.eql(data); }); @@ -56,7 +56,7 @@ describe('View', () => { 'content' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); view.data.should.eql({ layout: false, @@ -66,7 +66,7 @@ describe('View', () => { it('precompile view if possible', async () => { const body = 'Hello {{ name }}'; - const view = newView('index.swig', body); + const view = newView('index.njk', body); view._compiledSync({ name: 'Hexo' @@ -80,10 +80,10 @@ describe('View', () => { it('generate precompiled function even if renderer does not provide compile function', async () => { // Remove compile function - delete hexo.extend.renderer.store.swig.compile; + delete hexo.extend.renderer.store.njk.compile; const body = 'Hello {{ name }}'; - const view = newView('index.swig', body); + const view = newView('index.njk', body); view._compiledSync({ name: 'Hexo' @@ -100,7 +100,7 @@ describe('View', () => { '{{ test }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); const content = await view.render({ test: 'foo' @@ -117,7 +117,7 @@ describe('View', () => { '{{ test }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); const content = await view.render({ foo: 'foo', @@ -131,7 +131,7 @@ describe('View', () => { '{{ date() }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); const content = await view.render({ config: hexo.config, @@ -142,7 +142,7 @@ describe('View', () => { it('render() - layout', async () => { const body = 'content'; - const view = newView('index.swig', body); + const view = newView('index.njk', body); const content = await view.render({ layout: 'layout' @@ -152,7 +152,7 @@ describe('View', () => { it('render() - layout not found', async () => { const body = 'content'; - const view = newView('index.swig', body); + const view = newView('index.njk', body); const content = await view.render({ layout: 'wtf' @@ -165,7 +165,7 @@ describe('View', () => { '{{ test }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); view.render({ test: 'foo' @@ -183,7 +183,7 @@ describe('View', () => { '{{ test }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); view.render((err, content) => { should.not.exist(err); @@ -197,7 +197,7 @@ describe('View', () => { '{{ test }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); const filter = fake.returns('bar'); @@ -217,7 +217,7 @@ describe('View', () => { '{{ test }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); view.renderSync({test: 'foo'}).should.eql('foo'); }); @@ -230,7 +230,7 @@ describe('View', () => { '{{ test }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); view.renderSync({ foo: 'foo', @@ -243,7 +243,7 @@ describe('View', () => { '{{ date() }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); view.renderSync({ config: hexo.config, @@ -253,7 +253,7 @@ describe('View', () => { it('renderSync() - layout', () => { const body = 'content'; - const view = newView('index.swig', body); + const view = newView('index.njk', body); view.renderSync({ layout: 'layout' @@ -262,7 +262,7 @@ describe('View', () => { it('renderSync() - layout not found', () => { const body = 'content'; - const view = newView('index.swig', body); + const view = newView('index.njk', body); view.renderSync({ layout: 'wtf' @@ -274,7 +274,7 @@ describe('View', () => { '{{ test }}' ].join('\n'); - const view = newView('index.swig', body); + const view = newView('index.njk', body); const filter = fake.returns('bar'); @@ -285,13 +285,13 @@ describe('View', () => { }); it('_resolveLayout()', () => { - const view = newView('partials/header.swig', 'header'); + const view = newView('partials/header.njk', 'header'); // Relative path - view._resolveLayout('../layout').should.have.property('path', 'layout.swig'); + view._resolveLayout('../layout').should.have.property('path', 'layout.njk'); // Absolute path - view._resolveLayout('layout').should.have.property('path', 'layout.swig'); + view._resolveLayout('layout').should.have.property('path', 'layout.njk'); // Can't be itself should.not.exist(view._resolveLayout('header')); diff --git a/test/scripts/theme_processors/source.js b/test/scripts/theme_processors/source.js index 0608029365..4166abe1a1 100644 --- a/test/scripts/theme_processors/source.js +++ b/test/scripts/theme_processors/source.js @@ -41,6 +41,7 @@ describe('source', () => { pattern.match('source/foo.jpg~').should.be.false; pattern.match('source/foo.jpg%').should.be.false; pattern.match('layout/foo.swig').should.be.false; + pattern.match('layout/foo.njk').should.be.false; pattern.match('package.json').should.be.false; pattern.match('node_modules/test/test.js').should.be.false; pattern.match('source/node_modules/test/test.js').should.be.false; diff --git a/test/scripts/theme_processors/view.js b/test/scripts/theme_processors/view.js index 790913fe0a..f6171e5e33 100644 --- a/test/scripts/theme_processors/view.js +++ b/test/scripts/theme_processors/view.js @@ -36,9 +36,9 @@ describe('view', () => { it('pattern', () => { const { pattern } = processor; - pattern.match('layout/index.swig').path.should.eql('index.swig'); - should.not.exist(pattern.match('index.swig')); - should.not.exist(pattern.match('view/index.swig')); + pattern.match('layout/index.njk').path.should.eql('index.njk'); + should.not.exist(pattern.match('index.njk')); + should.not.exist(pattern.match('view/index.njk')); }); it('type: create', async () => { @@ -49,31 +49,31 @@ describe('view', () => { ].join('\n'); const file = newFile({ - path: 'index.swig', + path: 'index.njk', type: 'create' }); await writeFile(file.source, body); await process(file); - const view = hexo.theme.getView('index.swig'); + const view = hexo.theme.getView('index.njk'); - view.path.should.eql('index.swig'); - view.source.should.eql(join(themeDir, 'layout', 'index.swig')); + view.path.should.eql('index.njk'); + view.source.should.eql(join(themeDir, 'layout', 'index.njk')); view.data.should.eql({ foo: 'bar', _content: 'test' }); - hexo.theme.removeView('index.swig'); + hexo.theme.removeView('index.njk'); unlink(file.source); }); it('type: delete', async () => { const file = newFile({ - path: 'index.swig', + path: 'index.njk', type: 'delete' }); await process(file); - should.not.exist(hexo.theme.getView('index.swig')); + should.not.exist(hexo.theme.getView('index.njk')); }); });