diff --git a/package.json b/package.json index 0f908df7d..d929fac05 100755 --- a/package.json +++ b/package.json @@ -106,12 +106,12 @@ "hamljs": "^0.6.2", "handlebars": "^4.7.6", "javascript-stringify": "^2.0.1", + "liquid-args": "^0.3.1", "liquidjs": "^6.4.3", "lodash": "^4.17.15", "luxon": "^1.24.1", "markdown-it": "^10.0.0", "minimist": "^1.2.5", - "moo": "^0.5.1", "multimatch": "^4.0.0", "mustache": "^2.3.2", "normalize-path": "^3.0.0", diff --git a/src/Engines/Liquid.js b/src/Engines/Liquid.js index 5a56dccc8..885690752 100644 --- a/src/Engines/Liquid.js +++ b/src/Engines/Liquid.js @@ -1,4 +1,4 @@ -const moo = require("moo"); +const argParser = require("liquid-args"); const LiquidLib = require("liquidjs"); const TemplateEngine = require("./TemplateEngine"); const TemplatePath = require("../TemplatePath"); @@ -13,13 +13,7 @@ class Liquid extends TemplateEngine { this.setLibrary(this.config.libraryOverrides.liquid); this.setLiquidOptions(this.config.liquidOptions); - this.argLexer = moo.compile({ - number: /[0-9]+\.*[0-9]*/, - doubleQuoteString: /"(?:\\["\\]|[^\n"\\])*"/, - singleQuoteString: /'(?:\\['\\]|[^\n'\\])*'/, - keyword: /[a-zA-Z0-9\.\-\_]+/, - "ignore:whitespace": /[, \t]+/ // includes comma separator - }); + this.argLexer = argParser; } setLibrary(lib) { @@ -48,7 +42,7 @@ class Liquid extends TemplateEngine { root: [super.getIncludesDir()], // overrides in compile with inputPath below extname: ".liquid", dynamicPartials: false, - strict_filters: false + strict_filters: false, }; let options = Object.assign(defaults, this.liquidOptions || {}); @@ -97,32 +91,9 @@ class Liquid extends TemplateEngine { } } - static parseArguments(lexer, str, scope) { - let argArray = []; - - if (typeof str === "string") { - // TODO key=value key2=value - // TODO JSON? - lexer.reset(str); - let arg = lexer.next(); - while (arg) { - /*{ - type: 'doubleQuoteString', - value: '"test 2"', - text: '"test 2"', - toString: [Function: tokenToString], - offset: 0, - lineBreaks: 0, - line: 1, - col: 1 }*/ - if (arg.type.indexOf("ignore:") === -1) { - argArray.push(LiquidLib.evalExp(arg.value, scope)); // or evalValue - } - arg = lexer.next(); - } - } - - return argArray; + static parseArguments(lexer, args, scope) { + const parse = (arg) => LiquidLib.evalExp(arg, scope); + return lexer(args, parse); } static _normalizeShortcodeScope(scope) { @@ -135,13 +106,13 @@ class Liquid extends TemplateEngine { addShortcode(shortcodeName, shortcodeFn) { let _t = this; - this.addTag(shortcodeName, function(liquidEngine) { + this.addTag(shortcodeName, function (liquidEngine) { return { - parse: function(tagToken, remainTokens) { + parse: function (tagToken, remainTokens) { this.name = tagToken.name; this.args = tagToken.args; }, - render: function(scope, hash) { + render: function (scope, hash) { let argArray = Liquid.parseArguments(_t.argLexer, this.args, scope); return Promise.resolve( shortcodeFn.call( @@ -149,37 +120,37 @@ class Liquid extends TemplateEngine { ...argArray ) ); - } + }, }; }); } addPairedShortcode(shortcodeName, shortcodeFn) { let _t = this; - this.addTag(shortcodeName, function(liquidEngine) { + this.addTag(shortcodeName, function (liquidEngine) { return { - parse: function(tagToken, remainTokens) { + parse: function (tagToken, remainTokens) { this.name = tagToken.name; this.args = tagToken.args; this.templates = []; var stream = liquidEngine.parser .parseStream(remainTokens) - .on("template", tpl => this.templates.push(tpl)) - .on("tag:end" + shortcodeName, token => stream.stop()) - .on("end", x => { + .on("template", (tpl) => this.templates.push(tpl)) + .on("tag:end" + shortcodeName, (token) => stream.stop()) + .on("end", (x) => { throw new Error(`tag ${tagToken.raw} not closed`); }); stream.start(); }, - render: function(scope, hash) { + render: function (scope, hash) { let argArray = Liquid.parseArguments(_t.argLexer, this.args, scope); return new Promise((resolve, reject) => { liquidEngine.renderer .renderTemplates(this.templates, scope) - .then(function(html) { + .then(function (html) { resolve( shortcodeFn.call( Liquid._normalizeShortcodeScope(scope), @@ -189,7 +160,7 @@ class Liquid extends TemplateEngine { ); }); }); - } + }, }; }); } @@ -205,10 +176,10 @@ class Liquid extends TemplateEngine { } else { options.root = [ super.getIncludesDir(), - TemplatePath.getDirFromFilePath(inputPath) + TemplatePath.getDirFromFilePath(inputPath), ]; } - return async function(data) { + return async function (data) { return engine.render(tmpl, data, options); }; } diff --git a/test/TemplateRenderLiquidTest.js b/test/TemplateRenderLiquidTest.js index b13169ecb..39581d3c9 100644 --- a/test/TemplateRenderLiquidTest.js +++ b/test/TemplateRenderLiquidTest.js @@ -9,33 +9,33 @@ function getNewTemplateRender(name, inputDir) { } async function getPromise(resolveTo) { - return new Promise(function(resolve) { - setTimeout(function() { + return new Promise(function (resolve) { + setTimeout(function () { resolve(resolveTo); }); }); } // Liquid -test("Liquid", t => { +test("Liquid", (t) => { t.is(getNewTemplateRender("liquid").getEngineName(), "liquid"); }); -test("Liquid Render Addition", async t => { +test("Liquid Render Addition", async (t) => { let fn = await new TemplateRender("liquid").getCompiledTemplate( "
{{ number | plus: 1 }}
" ); t.is(await fn({ number: 1 }), "2
"); }); -test("Liquid Render Raw", async t => { +test("Liquid Render Raw", async (t) => { let fn = await new TemplateRender("liquid").getCompiledTemplate( "{% raw %}{{name}}{% endraw %}
" ); t.is(await fn({ name: "tim" }), "{{name}}
"); }); -test("Liquid Render Raw Multiline", async t => { +test("Liquid Render Raw Multiline", async (t) => { let fn = await new TemplateRender("liquid").getCompiledTemplate( `{% raw %} {{name}} @@ -49,14 +49,14 @@ test("Liquid Render Raw Multiline", async t => { ); }); -test("Liquid Render (with Helper)", async t => { +test("Liquid Render (with Helper)", async (t) => { let fn = await getNewTemplateRender("liquid").getCompiledTemplate( "
{{name | capitalize}}
" ); t.is(await fn({ name: "tim" }), "Tim
"); }); -test("Liquid Render Include", async t => { +test("Liquid Render Include", async (t) => { t.is( getNewTemplateRender("liquid", "./test/stubs/").getEngineName(), "liquid" @@ -69,7 +69,7 @@ test("Liquid Render Include", async t => { t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Relative Include", async t => { +test("Liquid Render Relative Include", async (t) => { t.is( getNewTemplateRender("liquid", "./test/stubs/").getEngineName(), "liquid" @@ -82,7 +82,7 @@ test("Liquid Render Relative Include", async t => { t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Relative (current dir) Include", async t => { +test("Liquid Render Relative (current dir) Include", async (t) => { let fn = await getNewTemplateRender( "./test/stubs/relative-liquid/does_not_exist_and_thats_ok.liquid", "./test/stubs/" @@ -90,7 +90,7 @@ test("Liquid Render Relative (current dir) Include", async t => { t.is(await fn(), "TIME IS RELATIVE.
"); }); -test("Liquid Render Relative (parent dir) Include", async t => { +test("Liquid Render Relative (parent dir) Include", async (t) => { let fn = await getNewTemplateRender( "./test/stubs/relative-liquid/dir/does_not_exist_and_thats_ok.liquid", "./test/stubs/" @@ -98,7 +98,7 @@ test("Liquid Render Relative (parent dir) Include", async t => { t.is(await fn(), "TIME IS RELATIVE.
"); }); -test.skip("Liquid Render Relative (relative include should ignore _includes dir) Include", async t => { +test.skip("Liquid Render Relative (relative include should ignore _includes dir) Include", async (t) => { let tr = getNewTemplateRender( "./test/stubs/does_not_exist_and_thats_ok.liquid", "./test/stubs/" @@ -111,7 +111,7 @@ test.skip("Liquid Render Relative (relative include should ignore _includes dir) t.is(await fn(), "This is not in the includes dir.
"); }); -test("Liquid Render Include with Liquid Suffix", async t => { +test("Liquid Render Include with Liquid Suffix", async (t) => { t.is( getNewTemplateRender("liquid", "./test/stubs/").getEngineName(), "liquid" @@ -124,7 +124,7 @@ test("Liquid Render Include with Liquid Suffix", async t => { t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include with HTML Suffix", async t => { +test("Liquid Render Include with HTML Suffix", async (t) => { t.is( getNewTemplateRender("liquid", "./test/stubs/").getEngineName(), "liquid" @@ -137,7 +137,7 @@ test("Liquid Render Include with HTML Suffix", async t => { t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include with HTML Suffix and Data Pass in", async t => { +test("Liquid Render Include with HTML Suffix and Data Pass in", async (t) => { t.is( getNewTemplateRender("liquid", "./test/stubs/").getEngineName(), "liquid" @@ -152,41 +152,41 @@ test("Liquid Render Include with HTML Suffix and Data Pass in", async t => { t.is((await fn()).trim(), "This is an include. myValue"); }); -test("Liquid Custom Filter", async t => { +test("Liquid Custom Filter", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addFilter("prefixWithZach", function(val) { + tr.engine.addFilter("prefixWithZach", function (val) { return "Zach" + val; }); t.is(await tr._testRender("{{ 'test' | prefixWithZach }}", {}), "Zachtest"); }); -test.skip("Liquid Async Filter", async t => { +test.skip("Liquid Async Filter", async (t) => { let tr = new TemplateRender("liquid", "test/stubs"); tr.engine.addFilter({ - myAsyncFilter: function(value) { + myAsyncFilter: function (value) { return new Promise((resolve, reject) => { - setTimeout(function() { + setTimeout(function () { resolve(`HI${value}`); }, 100); }); - } + }, }); let fn = await tr.getCompiledTemplate("{{ 'test' | myAsyncFilter }}"); t.is((await fn()).trim(), "HItest"); }); -test("Liquid Custom Tag prefixWithZach", async t => { +test("Liquid Custom Tag prefixWithZach", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addTag("prefixWithZach", function(liquidEngine) { + tr.engine.addTag("prefixWithZach", function (liquidEngine) { return { - parse: function(tagToken, remainTokens) { + parse: function (tagToken, remainTokens) { this.str = tagToken.args; // name }, - render: function(scope, hash) { + render: function (scope, hash) { var str = liquidEngine.evalValue(this.str, scope); // 'alice' return Promise.resolve("Zach" + str); // 'Alice' - } + }, }; }); @@ -196,17 +196,17 @@ test("Liquid Custom Tag prefixWithZach", async t => { ); }); -test("Liquid Custom Tag postfixWithZach", async t => { +test("Liquid Custom Tag postfixWithZach", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addTag("postfixWithZach", function(liquidEngine) { + tr.engine.addTag("postfixWithZach", function (liquidEngine) { return { - parse: function(tagToken, remainTokens) { + parse: function (tagToken, remainTokens) { this.str = tagToken.args; }, - render: function(scope, hash) { + render: function (scope, hash) { var str = liquidEngine.evalValue(this.str, scope); return Promise.resolve(str + "Zach"); - } + }, }; }); @@ -216,16 +216,16 @@ test("Liquid Custom Tag postfixWithZach", async t => { ); }); -test("Liquid Custom Tag Unquoted String", async t => { +test("Liquid Custom Tag Unquoted String", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addTag("testUnquotedStringTag", function(liquidEngine) { + tr.engine.addTag("testUnquotedStringTag", function (liquidEngine) { return { - parse: function(tagToken, remainTokens) { + parse: function (tagToken, remainTokens) { this.str = tagToken.args; }, - render: function(scope, hash) { + render: function (scope, hash) { return Promise.resolve(this.str + "Zach"); - } + }, }; }); @@ -238,27 +238,27 @@ test("Liquid Custom Tag Unquoted String", async t => { ); }); -test("Liquid addTag errors", async t => { +test("Liquid addTag errors", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); t.throws(() => { tr.engine.addTag("badSecondArgument", {}); }); }); -test("Liquid addTags", async t => { +test("Liquid addTags", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.addCustomTags({ - postfixWithZach: function(liquidEngine) { + postfixWithZach: function (liquidEngine) { return { - parse: function(tagToken, remainTokens) { + parse: function (tagToken, remainTokens) { this.str = tagToken.args; }, - render: function(scope, hash) { + render: function (scope, hash) { var str = liquidEngine.evalValue(this.str, scope); return Promise.resolve(str + "Zach"); - } + }, }; - } + }, }); t.is( @@ -267,11 +267,11 @@ test("Liquid addTags", async t => { ); }); -test("Liquid Shortcode", async t => { +test("Liquid Shortcode", async (t) => { t.plan(3); let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("postfixWithZach", function(str) { + tr.engine.addShortcode("postfixWithZach", function (str) { // Data in context t.is(this.page.url, "/hi/"); t.not(this.name, "test"); @@ -283,23 +283,23 @@ test("Liquid Shortcode", async t => { await tr._testRender("{% postfixWithZach name %}", { name: "test", page: { - url: "/hi/" - } + url: "/hi/", + }, }), "testZach" ); }); -test("Liquid Shortcode returns promise", async t => { +test("Liquid Shortcode returns promise", async (t) => { t.plan(2); let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("postfixWithZach", function(str) { + tr.engine.addShortcode("postfixWithZach", function (str) { // Data in context t.is(this.page.url, "/hi/"); - return new Promise(function(resolve) { - setTimeout(function() { + return new Promise(function (resolve) { + setTimeout(function () { resolve(str + "Zach"); }); }); @@ -309,18 +309,18 @@ test("Liquid Shortcode returns promise", async t => { await tr._testRender("{% postfixWithZach name %}", { name: "test", page: { - url: "/hi/" - } + url: "/hi/", + }, }), "testZach" ); }); -test("Liquid Shortcode returns promise (await inside)", async t => { +test("Liquid Shortcode returns promise (await inside)", async (t) => { t.plan(2); let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("postfixWithZach", async function(str) { + tr.engine.addShortcode("postfixWithZach", async function (str) { // Data in context t.is(this.page.url, "/hi/"); @@ -331,18 +331,18 @@ test("Liquid Shortcode returns promise (await inside)", async t => { await tr._testRender("{% postfixWithZach name %}", { name: "test", page: { - url: "/hi/" - } + url: "/hi/", + }, }), "testZach" ); }); -test("Liquid Shortcode returns promise (no await inside)", async t => { +test("Liquid Shortcode returns promise (no await inside)", async (t) => { t.plan(2); let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("postfixWithZach", async function(str) { + tr.engine.addShortcode("postfixWithZach", async function (str) { // Data in context t.is(this.page.url, "/hi/"); return getPromise(str + "Zach"); @@ -352,17 +352,17 @@ test("Liquid Shortcode returns promise (no await inside)", async t => { await tr._testRender("{% postfixWithZach name %}", { name: "test", page: { - url: "/hi/" - } + url: "/hi/", + }, }), "testZach" ); }); -test("Liquid Shortcode Safe Output", async t => { +test("Liquid Shortcode Safe Output", async (t) => { t.plan(2); let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("postfixWithZach", function(str) { + tr.engine.addShortcode("postfixWithZach", function (str) { // Data in context t.is(this.page.url, "/hi/"); return `${str}`; @@ -372,17 +372,17 @@ test("Liquid Shortcode Safe Output", async t => { await tr._testRender("{% postfixWithZach name %}", { name: "test", page: { - url: "/hi/" - } + url: "/hi/", + }, }), "test" ); }); -test("Liquid Paired Shortcode", async t => { +test("Liquid Paired Shortcode", async (t) => { t.plan(2); let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addPairedShortcode("postfixWithZach", function(content, str) { + tr.engine.addPairedShortcode("postfixWithZach", function (content, str) { // Data in context t.is(this.page.url, "/hi/"); return str + content + "Zach"; @@ -394,22 +394,22 @@ test("Liquid Paired Shortcode", async t => { { name: "test", page: { - url: "/hi/" - } + url: "/hi/", + }, } ), "testContentZach" ); }); -test("Liquid Async Paired Shortcode", async t => { +test("Liquid Async Paired Shortcode", async (t) => { t.plan(2); let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addPairedShortcode("postfixWithZach", function(content, str) { + tr.engine.addPairedShortcode("postfixWithZach", function (content, str) { // Data in context t.is(this.page.url, "/hi/"); - return new Promise(function(resolve) { - setTimeout(function() { + return new Promise(function (resolve) { + setTimeout(function () { resolve(str + content + "Zach"); }); }); @@ -421,15 +421,15 @@ test("Liquid Async Paired Shortcode", async t => { { name: "test", page: { - url: "/hi/" - } + url: "/hi/", + }, } ), "testContentZach" ); }); -test("Liquid Render Include Subfolder", async t => { +test("Liquid Render Include Subfolder", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -437,7 +437,7 @@ test("Liquid Render Include Subfolder", async t => { t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder HTML", async t => { +test("Liquid Render Include Subfolder HTML", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -445,7 +445,7 @@ test("Liquid Render Include Subfolder HTML", async t => { t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder No file extension", async t => { +test("Liquid Render Include Subfolder No file extension", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -456,7 +456,7 @@ test("Liquid Render Include Subfolder No file extension", async t => { // Skipped tests pending https://github.com/harttle/liquidjs/issues/61 // Resolution: we’re going to leave this skipped as LiquidJS will require dynamicPartials // to be on for quoted includes! -test.skip("Liquid Render Include Subfolder Single quotes", async t => { +test.skip("Liquid Render Include Subfolder Single quotes", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -464,7 +464,7 @@ test.skip("Liquid Render Include Subfolder Single quotes", async t => { t.is(await fn(), "This is an include.
"); }); -test.skip("Liquid Render Include Subfolder Double quotes", async t => { +test.skip("Liquid Render Include Subfolder Double quotes", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -472,7 +472,7 @@ test.skip("Liquid Render Include Subfolder Double quotes", async t => { t.is(await fn(), "This is an include.
"); }); -test.skip("Liquid Render Include Subfolder Single quotes HTML", async t => { +test.skip("Liquid Render Include Subfolder Single quotes HTML", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -480,7 +480,7 @@ test.skip("Liquid Render Include Subfolder Single quotes HTML", async t => { t.is(await fn(), "This is an include.
"); }); -test.skip("Liquid Render Include Subfolder Double quotes HTML", async t => { +test.skip("Liquid Render Include Subfolder Double quotes HTML", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -488,7 +488,7 @@ test.skip("Liquid Render Include Subfolder Double quotes HTML", async t => { t.is(await fn(), "This is an include.
"); }); -test.skip("Liquid Render Include Subfolder Single quotes No file extension", async t => { +test.skip("Liquid Render Include Subfolder Single quotes No file extension", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -496,7 +496,7 @@ test.skip("Liquid Render Include Subfolder Single quotes No file extension", asy t.is(await fn(), "This is an include.
"); }); -test.skip("Liquid Render Include Subfolder Double quotes No file extension", async t => { +test.skip("Liquid Render Include Subfolder Double quotes No file extension", async (t) => { let fn = await getNewTemplateRender( "liquid", "./test/stubs/" @@ -505,7 +505,7 @@ test.skip("Liquid Render Include Subfolder Double quotes No file extension", asy }); /* End skipped tests */ -test("Liquid Options Overrides", async t => { +test("Liquid Options Overrides", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -513,7 +513,7 @@ test("Liquid Options Overrides", async t => { t.is(options.dynamicPartials, true); }); -test("Liquid Render Include Subfolder Single quotes no extension dynamicPartials true", async t => { +test("Liquid Render Include Subfolder Single quotes no extension dynamicPartials true", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -523,7 +523,7 @@ test("Liquid Render Include Subfolder Single quotes no extension dynamicPartials t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder Single quotes (relative include current dir) dynamicPartials true", async t => { +test("Liquid Render Include Subfolder Single quotes (relative include current dir) dynamicPartials true", async (t) => { let tr = getNewTemplateRender( "./test/stubs/does_not_exist_and_thats_ok.liquid", "./test/stubs/" @@ -536,7 +536,7 @@ test("Liquid Render Include Subfolder Single quotes (relative include current di t.is(await fn(), "TIME IS RELATIVE.
"); }); -test("Liquid Render Include Subfolder Single quotes (relative include parent dir) dynamicPartials true", async t => { +test("Liquid Render Include Subfolder Single quotes (relative include parent dir) dynamicPartials true", async (t) => { let tr = getNewTemplateRender( "./test/stubs/subfolder/does_not_exist_and_thats_ok.liquid", "./test/stubs/" @@ -549,7 +549,7 @@ test("Liquid Render Include Subfolder Single quotes (relative include parent dir t.is(await fn(), "TIME IS RELATIVE.
"); }); -test("Liquid Render Include Subfolder Double quotes no extension dynamicPartials true", async t => { +test("Liquid Render Include Subfolder Double quotes no extension dynamicPartials true", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -559,7 +559,7 @@ test("Liquid Render Include Subfolder Double quotes no extension dynamicPartials t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder Single quotes dynamicPartials true", async t => { +test("Liquid Render Include Subfolder Single quotes dynamicPartials true", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -569,7 +569,7 @@ test("Liquid Render Include Subfolder Single quotes dynamicPartials true", async t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder Double quotes dynamicPartials true", async t => { +test("Liquid Render Include Subfolder Double quotes dynamicPartials true", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -579,7 +579,7 @@ test("Liquid Render Include Subfolder Double quotes dynamicPartials true", async t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder Single quotes HTML dynamicPartials true", async t => { +test("Liquid Render Include Subfolder Single quotes HTML dynamicPartials true", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -589,7 +589,7 @@ test("Liquid Render Include Subfolder Single quotes HTML dynamicPartials true", t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder Double quotes HTML dynamicPartials true", async t => { +test("Liquid Render Include Subfolder Double quotes HTML dynamicPartials true", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -599,7 +599,7 @@ test("Liquid Render Include Subfolder Double quotes HTML dynamicPartials true", t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder Single quotes HTML dynamicPartials true, data passed in", async t => { +test("Liquid Render Include Subfolder Single quotes HTML dynamicPartials true, data passed in", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -609,7 +609,7 @@ test("Liquid Render Include Subfolder Single quotes HTML dynamicPartials true, d t.is(await fn(), "This is an include.
"); }); -test("Liquid Render Include Subfolder Double quotes HTML dynamicPartials true, data passed in", async t => { +test("Liquid Render Include Subfolder Double quotes HTML dynamicPartials true, data passed in", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ dynamicPartials: true }); @@ -619,7 +619,7 @@ test("Liquid Render Include Subfolder Double quotes HTML dynamicPartials true, d t.is(await fn(), "This is an include.
"); }); -test("Liquid Render: with Library Override", async t => { +test("Liquid Render: with Library Override", async (t) => { let tr = getNewTemplateRender("liquid"); let lib = require("liquidjs")(); @@ -629,9 +629,9 @@ test("Liquid Render: with Library Override", async t => { t.is(await fn({ name: "tim" }), "Tim
"); }); -test("Liquid Paired Shortcode with Tag Inside", async t => { +test("Liquid Paired Shortcode with Tag Inside", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addPairedShortcode("postfixWithZach", function(content, str) { + tr.engine.addPairedShortcode("postfixWithZach", function (content, str) { return str + content + "Zach"; }); @@ -644,9 +644,9 @@ test("Liquid Paired Shortcode with Tag Inside", async t => { ); }); -test("Liquid Nested Paired Shortcode", async t => { +test("Liquid Nested Paired Shortcode", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addPairedShortcode("postfixWithZach", function(content, str) { + tr.engine.addPairedShortcode("postfixWithZach", function (content, str) { return str + content + "Zach"; }); @@ -659,22 +659,104 @@ test("Liquid Nested Paired Shortcode", async t => { ); }); -test("Liquid Shortcode Multiple Args", async t => { +test("Liquid Paired Kwargs Shortcode with Tag Inside", async (t) => { + let tr = getNewTemplateRender("liquid", "./test/stubs/"); + tr.engine.addPairedShortcode("postfixWithZach", function (content, kwargs) { + var { str } = kwargs && kwargs.__keywords === true ? kwargs : {}; + return str + content + "Zach"; + }); + + t.is( + await tr._testRender( + "{% postfixWithZach str=name %}Content{% if tester %}If{% endif %}{% endpostfixWithZach %}", + { name: "test", tester: true } + ), + "testContentIfZach" + ); +}); + +test("Liquid Nested Paired Kwargs Shortcode", async (t) => { + let tr = getNewTemplateRender("liquid", "./test/stubs/"); + tr.engine.addPairedShortcode("postfixWithZach", function (content, kwargs) { + var { str } = kwargs && kwargs.__keywords === true ? kwargs : {}; + return str + content + "Zach"; + }); + + t.is( + await tr._testRender( + "{% postfixWithZach str=name %}Content{% postfixWithZach str=name2 %}Content{% endpostfixWithZach %}{% endpostfixWithZach %}", + { name: "test", name2: "test2" } + ), + "testContenttest2ContentZachZach" + ); +}); + +test("Liquid Shortcode Multiple Args", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("postfixWithZach", function(str, str2) { + tr.engine.addShortcode("postfixWithZach", function (str, str2) { return str + str2 + "Zach"; }); t.is( await tr._testRender("{% postfixWithZach name other %}", { name: "test", - other: "howdy" + other: "howdy", }), "testhowdyZach" ); }); -test.skip("Liquid Include Scope Leak", async t => { +test("Liquid Shortcode Keyword Arg", async (t) => { + let tr = getNewTemplateRender("liquid", "./test/stubs/"); + tr.engine.addShortcode("postfixWithZach", function (str, kwargs) { + let { append } = kwargs && kwargs.__keywords === true ? kwargs : {}; + return str + "Zach" + append; + }); + + t.is( + await tr._testRender("{% postfixWithZach name append=other %}", { + name: "test", + other: "howdy", + }), + "testZachhowdy" + ); +}); + +test("Liquid Shortcode Multiple Keyword Args", async (t) => { + let tr = getNewTemplateRender("liquid", "./test/stubs/"); + tr.engine.addShortcode("postfixWithZach", function (str, kwargs) { + let { prepend, append } = kwargs && kwargs.__keywords ? kwargs : {}; + return prepend + str + "Zach" + append; + }); + + t.is( + await tr._testRender( + "{% postfixWithZach name prepend='string' append=other %}", + { + name: "test", + other: "howdy", + } + ), + "stringtestZachhowdy" + ); +}); + +test("Liquid Shortcode Only Keyword Args", async (t) => { + let tr = getNewTemplateRender("liquid", "./test/stubs/"); + tr.engine.addShortcode("postfixWithZach", function (kwargs) { + let { prepend, append } = kwargs && kwargs.__keywords ? kwargs : {}; + return prepend + "Zach" + append; + }); + + t.is( + await tr._testRender("{% postfixWithZach prepend='string' append=name %}", { + name: "test", + }), + "stringZachtest" + ); +}); + +test.skip("Liquid Include Scope Leak", async (t) => { t.is( getNewTemplateRender("liquid", "./test/stubs/").getEngineName(), "liquid" @@ -688,7 +770,7 @@ test.skip("Liquid Include Scope Leak", async t => { }); // TODO this will change in 1.0 -test("Liquid Missing Filter Issue #183 (no strict_filters)", async t => { +test("Liquid Missing Filter Issue #183 (no strict_filters)", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); try { @@ -699,7 +781,7 @@ test("Liquid Missing Filter Issue #183 (no strict_filters)", async t => { } }); -test("Liquid Missing Filter Issue #183", async t => { +test("Liquid Missing Filter Issue #183", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.setLiquidOptions({ strict_filters: true }); @@ -711,7 +793,7 @@ test("Liquid Missing Filter Issue #183", async t => { } }); -test("Issue 258: Liquid Render Date", async t => { +test("Issue 258: Liquid Render Date", async (t) => { let fn = await getNewTemplateRender("liquid").getCompiledTemplate( "{{ myDate }}
" ); @@ -721,33 +803,33 @@ test("Issue 258: Liquid Render Date", async t => { t.not(dateStr.substr(2, 1), '"'); }); -test("Issue 347: Liquid addTags with space in argument", async t => { +test("Issue 347: Liquid addTags with space in argument", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); tr.engine.addCustomTags({ - issue347CustomTag: function(liquidEngine) { + issue347CustomTag: function (liquidEngine) { return { - parse: function(tagToken, remainTokens) { + parse: function (tagToken, remainTokens) { this.str = tagToken.args; }, - render: function(scope, hash) { + render: function (scope, hash) { var str = liquidEngine.evalValue(this.str, scope); return Promise.resolve(str + "Zach"); - } + }, }; - } + }, }); t.is( await tr._testRender("{% issue347CustomTag 'te st' %}", { - name: "slkdjflksdjf" + name: "slkdjflksdjf", }), "te stZach" ); }); -test("Issue 347: Liquid Shortcode, string argument", async t => { +test("Issue 347: Liquid Shortcode, string argument", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue347", function(str) { + tr.engine.addShortcode("issue347", function (str) { return str + "Zach"; }); @@ -757,23 +839,23 @@ test("Issue 347: Liquid Shortcode, string argument", async t => { ); }); -test("Issue 347: Liquid Shortcode string argument with space, double quotes", async t => { +test("Issue 347: Liquid Shortcode string argument with space, double quotes", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue347b", function(str) { + tr.engine.addShortcode("issue347b", function (str) { return str + "Zach"; }); t.is( await tr._testRender('{% issue347b "test 2" "test 3" %}', { - name: "alkdsjfkslja" + name: "alkdsjfkslja", }), "test 2Zach" ); }); -test("Issue 347: Liquid Shortcode string argument with space, single quotes", async t => { +test("Issue 347: Liquid Shortcode string argument with space, single quotes", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue347", function(str) { + tr.engine.addShortcode("issue347", function (str) { return str + "Zach"; }); @@ -783,65 +865,65 @@ test("Issue 347: Liquid Shortcode string argument with space, single quotes", as ); }); -test("Issue 347: Liquid Shortcode string argument with space, combination of quotes", async t => { +test("Issue 347: Liquid Shortcode string argument with space, combination of quotes", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue347", function(str, str2) { + tr.engine.addShortcode("issue347", function (str, str2) { return str + str2 + "Zach"; }); t.is( await tr._testRender("{% issue347 'test 2' \"test 3\" %}", { - name: "alkdsjfkslja" + name: "alkdsjfkslja", }), "test 2test 3Zach" ); }); -test("Issue 347: Liquid Shortcode multiple arguments, comma separated", async t => { +test("Issue 347: Liquid Shortcode multiple arguments, comma separated", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue347", function(str, str2) { + tr.engine.addShortcode("issue347", function (str, str2) { return str + str2 + "Zach"; }); t.is( await tr._testRender("{% issue347 'test 2', \"test 3\" %}", { - name: "alkdsjfkslja" + name: "alkdsjfkslja", }), "test 2test 3Zach" ); }); -test("Issue 347: Liquid Shortcode multiple arguments, comma separated, one is an integer", async t => { +test("Issue 347: Liquid Shortcode multiple arguments, comma separated, one is an integer", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue347", function(str, str2) { + tr.engine.addShortcode("issue347", function (str, str2) { return str + str2 + "Zach"; }); t.is( await tr._testRender("{% issue347 'test 2', 3 %}", { - name: "alkdsjfkslja" + name: "alkdsjfkslja", }), "test 23Zach" ); }); -test("Issue 347: Liquid Shortcode multiple arguments, comma separated, one is a float", async t => { +test("Issue 347: Liquid Shortcode multiple arguments, comma separated, one is a float", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue347", function(str, str2) { + tr.engine.addShortcode("issue347", function (str, str2) { return str + str2 + "Zach"; }); t.is( await tr._testRender("{% issue347 'test 2', 3.23 %}", { - name: "alkdsjfkslja" + name: "alkdsjfkslja", }), "test 23.23Zach" ); }); -test("Issue 347: Liquid Shortcode boolean argument", async t => { +test("Issue 347: Liquid Shortcode boolean argument", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue347", function(bool) { + tr.engine.addShortcode("issue347", function (bool) { return bool ? "Zach" : "Not Zach"; }); @@ -855,9 +937,9 @@ test("Issue 347: Liquid Shortcode boolean argument", async t => { ); }); -test("Issue 347: Liquid Paired Shortcode with Spaces", async t => { +test("Issue 347: Liquid Paired Shortcode with Spaces", async (t) => { let tr = getNewTemplateRender("liquid", "./test/stubs/"); - tr.engine.addPairedShortcode("postfixWithZach", function( + tr.engine.addPairedShortcode("postfixWithZach", function ( content, str1, num, @@ -875,72 +957,72 @@ test("Issue 347: Liquid Paired Shortcode with Spaces", async t => { ); }); -test("Liquid Render with dash variable Issue #567", async t => { +test("Liquid Render with dash variable Issue #567", async (t) => { let tr = getNewTemplateRender("liquid"); let fn = await tr.getCompiledTemplate("{{ my-global-name }}
"); t.is(await fn({ "my-global-name": "Zach" }), "Zach
"); }); -test("Issue 600: Liquid Shortcode argument page.url", async t => { +test("Issue 600: Liquid Shortcode argument page.url", async (t) => { let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue600", function(str) { + tr.engine.addShortcode("issue600", function (str) { return str + "Zach"; }); t.is( await tr._testRender("{% issue600 page.url %}", { - page: { url: "alkdsjfkslja" } + page: { url: "alkdsjfkslja" }, }), "alkdsjfksljaZach" ); }); -test("Issue 600: Liquid Shortcode argument with dashes", async t => { +test("Issue 600: Liquid Shortcode argument with dashes", async (t) => { let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue600b", function(str) { + tr.engine.addShortcode("issue600b", function (str) { return str + "Zach"; }); t.is( await tr._testRender("{% issue600b page-url %}", { - "page-url": "alkdsjfkslja" + "page-url": "alkdsjfkslja", }), "alkdsjfksljaZach" ); }); -test("Issue 600: Liquid Shortcode argument with underscores", async t => { +test("Issue 600: Liquid Shortcode argument with underscores", async (t) => { let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("issue600c", function(str) { + tr.engine.addShortcode("issue600c", function (str) { return str + "Zach"; }); t.is( await tr._testRender("{% issue600c page_url %}", { - page_url: "alkdsjfkslja" + page_url: "alkdsjfkslja", }), "alkdsjfksljaZach" ); }); -test.skip("Issue 611: Run a function", async t => { +test.skip("Issue 611: Run a function", async (t) => { // This works in Nunjucks let tr = new TemplateRender("liquid", "./test/stubs/"); t.is( await tr._testRender("{{ test() }}", { - test: function() { + test: function () { return "alkdsjfksljaZach"; - } + }, }), "alkdsjfksljaZach" ); }); -test("Liquid Shortcode (with sync function, error throwing)", async t => { +test("Liquid Shortcode (with sync function, error throwing)", async (t) => { let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("postfixWithZach", function(str) { + tr.engine.addShortcode("postfixWithZach", function (str) { throw new Error("Liquid Shortcode (with sync function, error throwing)"); }); @@ -954,9 +1036,9 @@ test("Liquid Shortcode (with sync function, error throwing)", async t => { ); }); -test("Liquid Shortcode (with async function, error throwing)", async t => { +test("Liquid Shortcode (with async function, error throwing)", async (t) => { let tr = new TemplateRender("liquid", "./test/stubs/"); - tr.engine.addShortcode("postfixWithZach", async function(str) { + tr.engine.addShortcode("postfixWithZach", async function (str) { throw new Error("Liquid Shortcode (with async function, error throwing)"); }); @@ -970,7 +1052,7 @@ test("Liquid Shortcode (with async function, error throwing)", async t => { ); }); -test("Liquid Render a false #1069", async t => { +test("Liquid Render a false #1069", async (t) => { let fn = await new TemplateRender("liquid").getCompiledTemplate( "{{ falseValue }}" );