From 0b549b000f5ea17e88fd7cd28ccf633ea7831046 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Wed, 14 Dec 2022 16:08:46 -0600 Subject: [PATCH] A bit more shortcode normalization cleanup for #1522 --- src/Engines/Liquid.js | 49 +++++++++++++++++++++-------------------- src/Engines/Nunjucks.js | 39 +++++++++++++++++--------------- 2 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/Engines/Liquid.js b/src/Engines/Liquid.js index 3e646eba6..787abddc4 100644 --- a/src/Engines/Liquid.js +++ b/src/Engines/Liquid.js @@ -53,6 +53,28 @@ class Liquid extends TemplateEngine { return options; } + static wrapFilter(fn) { + return function (...args) { + if (this.context && "get" in this.context) { + this.page = this.context.get(["page"]); + this.eleventy = this.context.get(["eleventy"]); + } + + return fn.call(this, ...args); + }; + } + + // Shortcodes + static normalizeScope(context) { + let obj = {}; + if (context) { + obj.ctx = context; + obj.page = context.get(["page"]); + obj.eleventy = context.get(["eleventy"]); + } + return obj; + } + addCustomTags(tags) { for (let name in tags) { this.addTag(name, tags[name]); @@ -69,17 +91,6 @@ class Liquid extends TemplateEngine { this.liquidLib.registerFilter(name, Liquid.wrapFilter(filter)); } - static wrapFilter(fn) { - return function (...args) { - if (this.context && "get" in this.context) { - this.page = this.context.get(["page"]); - this.eleventy = this.context.get(["eleventy"]); - } - - return fn.call(this, ...args); - }; - } - addTag(name, tagFn) { let tagObj; if (typeof tagFn === "function") { @@ -139,15 +150,6 @@ class Liquid extends TemplateEngine { return argArray; } - static _normalizeShortcodeScope(ctx) { - let obj = {}; - if (ctx) { - obj.page = ctx.get(["page"]); - obj.eleventy = ctx.get(["eleventy"]); - } - return obj; - } - addShortcode(shortcodeName, shortcodeFn) { let _t = this; this.addTag(shortcodeName, function (liquidEngine) { @@ -156,7 +158,7 @@ class Liquid extends TemplateEngine { this.name = tagToken.name; this.args = tagToken.args; }, - render: function* (ctx, emitter) { + render: function* (ctx) { let rawArgs = Liquid.parseArguments(_t.argLexer, this.args); let argArray = []; let contextScope = ctx.getAll(); @@ -166,10 +168,9 @@ class Liquid extends TemplateEngine { } let ret = yield shortcodeFn.call( - Liquid._normalizeShortcodeScope(ctx), + Liquid.normalizeScope(ctx), ...argArray ); - // emitter.write(ret); return ret; }, }; @@ -210,7 +211,7 @@ class Liquid extends TemplateEngine { ); let ret = yield shortcodeFn.call( - Liquid._normalizeShortcodeScope(ctx), + Liquid.normalizeScope(ctx), html, ...argArray ); diff --git a/src/Engines/Nunjucks.js b/src/Engines/Nunjucks.js index 0a7d458b2..232271273 100755 --- a/src/Engines/Nunjucks.js +++ b/src/Engines/Nunjucks.js @@ -114,6 +114,23 @@ class Nunjucks extends TemplateEngine { }; } + // Shortcodes + static normalizeContext(context) { + let obj = {}; + if (context.ctx) { + obj.ctx = context.ctx; + + if (context.ctx.page) { + obj.page = context.ctx.page; + } + + if (context.ctx.eleventy) { + obj.eleventy = context.ctx.eleventy; + } + } + return obj; + } + addCustomTags(tags) { for (let name in tags) { this.addTag(name, tags[name]); @@ -155,20 +172,6 @@ class Nunjucks extends TemplateEngine { } } - static _normalizeShortcodeContext(context) { - let obj = {}; - if (context.ctx) { - obj.ctx = context.ctx; - if (context.ctx.page) { - obj.page = context.ctx.page; - } - if (context.ctx.eleventy) { - obj.eleventy = context.ctx.eleventy; - } - } - return obj; - } - _getShortcodeFn(shortcodeName, shortcodeFn, isAsync = false) { return function ShortcodeFunction() { this.tags = [shortcodeName]; @@ -203,7 +206,7 @@ class Nunjucks extends TemplateEngine { if (isAsync) { shortcodeFn - .call(Nunjucks._normalizeShortcodeContext(context), ...argArray) + .call(Nunjucks.normalizeContext(context), ...argArray) .then(function (returnValue) { resolve( null, @@ -223,7 +226,7 @@ class Nunjucks extends TemplateEngine { } else { try { let ret = shortcodeFn.call( - Nunjucks._normalizeShortcodeContext(context), + Nunjucks.normalizeContext(context), ...argArray ); return new NunjucksLib.runtime.SafeString("" + ret); @@ -274,7 +277,7 @@ class Nunjucks extends TemplateEngine { if (isAsync) { shortcodeFn .call( - Nunjucks._normalizeShortcodeContext(context), + Nunjucks.normalizeContext(context), bodyContent, ...argArray ) @@ -297,7 +300,7 @@ class Nunjucks extends TemplateEngine { null, new NunjucksLib.runtime.SafeString( shortcodeFn.call( - Nunjucks._normalizeShortcodeContext(context), + Nunjucks.normalizeContext(context), bodyContent, ...argArray )