From 0807bf22b13a461079044799450848b4d44aad04 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Wed, 14 Dec 2022 15:35:37 -0600 Subject: [PATCH] Tweaks to #2250 --- src/Engines/Handlebars.js | 14 ++------------ src/Engines/JavaScript.js | 28 +++++++++++++--------------- src/Engines/Liquid.js | 17 ++++++++--------- src/Engines/Nunjucks.js | 24 +++++++++++++----------- 4 files changed, 36 insertions(+), 47 deletions(-) diff --git a/src/Engines/Handlebars.js b/src/Engines/Handlebars.js index 86707f6ac..8214bed7b 100644 --- a/src/Engines/Handlebars.js +++ b/src/Engines/Handlebars.js @@ -22,20 +22,10 @@ class Handlebars extends TemplateEngine { this.handlebarsLib.registerHelper(name, callback); } - static wrapHelper(callback) { - return function () { - const newThis = { - ...this, - ctx: this, - // page: this.page - }; - return callback.call(newThis, ...arguments); - }; - } - addHelpers(helpers) { for (let name in helpers) { - this.addHelper(name, Handlebars.wrapHelper(helpers[name])); + // We don’t need to wrap helpers for `page` or `eleventy`, this is provided for free by Handlebars + this.addHelper(name, helpers[name]); } } diff --git a/src/Engines/JavaScript.js b/src/Engines/JavaScript.js index 077e0c65f..ebfe487b0 100644 --- a/src/Engines/JavaScript.js +++ b/src/Engines/JavaScript.js @@ -99,7 +99,7 @@ class JavaScript extends TemplateEngine { return getJavaScriptData(inst, inputPath); } - getJavaScriptFunctions(inst, data) { + getJavaScriptFunctions(inst) { let fns = {}; let configFns = this.config.javascriptFunctions; @@ -109,24 +109,22 @@ class JavaScript extends TemplateEngine { // do nothing } else { // note: wrapping creates a new function - fns[key] = JavaScript.wrapJavaScriptFunction( - inst, - data, - configFns[key] - ); + fns[key] = JavaScript.wrapJavaScriptFunction(inst, configFns[key]); } } return fns; } - static wrapJavaScriptFunction(inst, data, fn) { - return function () { - const newThis = { - ...this, - ctx: data, - page: inst.page, - }; - return fn.call(newThis, ...arguments); + static wrapJavaScriptFunction(inst, fn) { + return function (...args) { + if (inst && inst.page) { + this.page = inst.page; + } + if (inst && inst.eleventy) { + this.eleventy = inst.eleventy; + } + + return fn.call(this, ...args); }; } @@ -145,7 +143,7 @@ class JavaScript extends TemplateEngine { if (!inst.page || inst.page.url) { inst.page = data.page; } - Object.assign(inst, this.getJavaScriptFunctions(inst, data)); + Object.assign(inst, this.getJavaScriptFunctions(inst)); return this.normalize(inst.render.call(inst, data)); }.bind(this); diff --git a/src/Engines/Liquid.js b/src/Engines/Liquid.js index 35fddcc0a..3e646eba6 100644 --- a/src/Engines/Liquid.js +++ b/src/Engines/Liquid.js @@ -69,15 +69,14 @@ class Liquid extends TemplateEngine { this.liquidLib.registerFilter(name, Liquid.wrapFilter(filter)); } - static wrapFilter(filter) { - return function () { - let ctx = this.context.environments; - const newThis = { - ...this, - ctx: ctx, - page: ctx.page, - }; - return filter.call(newThis, ...arguments); + 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); }; } diff --git a/src/Engines/Nunjucks.js b/src/Engines/Nunjucks.js index c3de5a01f..9e4f8930b 100755 --- a/src/Engines/Nunjucks.js +++ b/src/Engines/Nunjucks.js @@ -95,20 +95,22 @@ class Nunjucks extends TemplateEngine { this.addGlobals(this.config.nunjucksGlobals); } - addFilters(helpers, isAsync) { - for (let name in helpers) { - this.njkEnv.addFilter(name, Nunjucks.wrapFilter(helpers[name]), isAsync); + addFilters(filters, isAsync) { + for (let name in filters) { + this.njkEnv.addFilter(name, Nunjucks.wrapFilter(filters[name]), isAsync); } } - static wrapFilter(filter) { - return function () { - const newThis = { - ...this, - // ctx: this.ctx, - page: this.ctx.page, - }; - return filter.call(newThis, ...arguments); + static wrapFilter(fn) { + return function (...args) { + if (this.ctx && this.ctx.page) { + this.page = this.ctx.page; + } + if (this.ctx && this.ctx.eleventy) { + this.eleventy = this.ctx.eleventy; + } + + return fn.call(this, ...args); }; }