Skip to content

Commit

Permalink
Tests for #2250
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 14, 2022
1 parent 0807bf2 commit a0b6928
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"environmentVariables": {},
"failFast": true,
"files": [
"./test/*.js"
"./test/*.js",
"./test/_issues/**/*test.js"
],
"ignoredByWatcher": [
"./test/stubs*/**/*",
Expand Down
10 changes: 7 additions & 3 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Plugins/RenderPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down
37 changes: 37 additions & 0 deletions test/_issues/2250/2250-test.js
Original file line number Diff line number Diff line change
@@ -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/");
});
1 change: 1 addition & 0 deletions test/_issues/2250/handlebars.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ getUrl }}
3 changes: 3 additions & 0 deletions test/_issues/2250/javascript.11ty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return this.getUrl();
};
1 change: 1 addition & 0 deletions test/_issues/2250/liquid.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ "test" | getUrl }}
1 change: 1 addition & 0 deletions test/_issues/2250/nunjucks.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ "test" | getUrl }}

0 comments on commit a0b6928

Please sign in to comment.