diff --git a/package.json b/package.json index 6ac7cd8ca..46c9091ce 100755 --- a/package.json +++ b/package.json @@ -64,7 +64,8 @@ "environmentVariables": {}, "failFast": true, "files": [ - "./test/*.js" + "./test/*.js", + "./test/_issues/**/*test.js" ], "ignoredByWatcher": [ "./test/stubs*/**/*", diff --git a/src/Engines/Nunjucks.js b/src/Engines/Nunjucks.js index 9e4f8930b..0a7d458b2 100755 --- a/src/Engines/Nunjucks.js +++ b/src/Engines/Nunjucks.js @@ -157,10 +157,14 @@ class Nunjucks extends TemplateEngine { static _normalizeShortcodeContext(context) { let obj = {}; - if (context.ctx && context.ctx.page) { + if (context.ctx) { obj.ctx = context.ctx; - obj.page = context.ctx.page; - obj.eleventy = context.ctx.eleventy; + if (context.ctx.page) { + obj.page = context.ctx.page; + } + if (context.ctx.eleventy) { + obj.eleventy = context.ctx.eleventy; + } } return obj; } diff --git a/src/Plugins/RenderPlugin.js b/src/Plugins/RenderPlugin.js index 678708a0c..62ab6d592 100644 --- a/src/Plugins/RenderPlugin.js +++ b/src/Plugins/RenderPlugin.js @@ -155,8 +155,8 @@ function EleventyPlugin(eleventyConfig, options = {}) { let normalizedContext = {}; if (ctx) { if (opts.accessGlobalData) { - // parent template data cascade, should this be `ctx.getAll()` (per below?) - normalizedContext.data = ctx.environments; + // parent template data cascade + normalizedContext.data = ctx.getAll(); } normalizedContext.page = ctx.get(["page"]); diff --git a/test/_issues/2250/2250-test.js b/test/_issues/2250/2250-test.js new file mode 100644 index 000000000..d78a58988 --- /dev/null +++ b/test/_issues/2250/2250-test.js @@ -0,0 +1,37 @@ +const test = require("ava"); +const Eleventy = require("../../../src/Eleventy"); + +test("Issue #2250, page is available in filters", async (t) => { + let elev = new Eleventy("./test/_issues/2250/", "./test/_issues/2250/_site", { + config: function (eleventyConfig) { + eleventyConfig.addFilter("getUrl", function () { + return this.page.url; + }); + }, + }); + + let results = await elev.toJSON(); + let nunjucks = results.filter((entry) => { + return entry.url.startsWith("/nunjucks/"); + }); + + t.is(nunjucks[0].content.trim(), "/nunjucks/"); + + let liquid = results.filter((entry) => { + return entry.url.startsWith("/liquid/"); + }); + + t.is(liquid[0].content.trim(), "/liquid/"); + + let javascript = results.filter((entry) => { + return entry.url.startsWith("/javascript/"); + }); + + t.is(javascript[0].content.trim(), "/javascript/"); + + let handlebars = results.filter((entry) => { + return entry.url.startsWith("/handlebars/"); + }); + + t.is(handlebars[0].content.trim(), "/handlebars/"); +}); diff --git a/test/_issues/2250/handlebars.hbs b/test/_issues/2250/handlebars.hbs new file mode 100644 index 000000000..ce7cd146b --- /dev/null +++ b/test/_issues/2250/handlebars.hbs @@ -0,0 +1 @@ +{{ getUrl }} \ No newline at end of file diff --git a/test/_issues/2250/javascript.11ty.js b/test/_issues/2250/javascript.11ty.js new file mode 100644 index 000000000..1e58310cc --- /dev/null +++ b/test/_issues/2250/javascript.11ty.js @@ -0,0 +1,3 @@ +module.exports = function () { + return this.getUrl(); +}; diff --git a/test/_issues/2250/liquid.liquid b/test/_issues/2250/liquid.liquid new file mode 100644 index 000000000..a42f70e66 --- /dev/null +++ b/test/_issues/2250/liquid.liquid @@ -0,0 +1 @@ +{{ "test" | getUrl }} \ No newline at end of file diff --git a/test/_issues/2250/nunjucks.njk b/test/_issues/2250/nunjucks.njk new file mode 100644 index 000000000..a42f70e66 --- /dev/null +++ b/test/_issues/2250/nunjucks.njk @@ -0,0 +1 @@ +{{ "test" | getUrl }} \ No newline at end of file