Skip to content

Commit

Permalink
Fix pagination and eleventy computed for .njk engine (#3120)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat authored Jun 10, 2024
2 parents ecd0579 + 301d2c0 commit 1176b4a
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/Util/ProxyWrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ function wrapObject(target, fallback) {
// Nunjucks needs this
ownKeys(target) {
// unique
let keys = new Set([
...Reflect.ownKeys(target),
...Reflect.ownKeys(fallback),
]);
let keys = new Set([...Reflect.ownKeys(target), ...Reflect.ownKeys(fallback)]);
// console.log( "handler:ownKeys", keys );
return Array.from(keys);
},
Expand All @@ -38,18 +35,16 @@ function wrapObject(target, fallback) {

return fallback[prop];
},
// set(target, prop, value) {
// console.log( "handler:set", prop, value );
// return Reflect.set(target, prop, value);
// }
set(target, prop, value) {
// console.log( "handler:set", prop, value );
return Reflect.set(target, prop, value);
},
});
}

function ProxyWrap(target, fallback) {
if (!isPlainObject(target) || !isPlainObject(fallback)) {
throw new Error(
"ProxyWrap expects objects for both the target and fallback"
);
throw new Error("ProxyWrap expects objects for both the target and fallback");
}
let wrapped = wrapObject(target, fallback);
return wrapped;
Expand Down
33 changes: 33 additions & 0 deletions test/PaginationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const TemplateData = require("../src/TemplateData");
const Pagination = require("../src/Plugins/Pagination");
const TemplateConfig = require("../src/TemplateConfig");
const FileSystemSearch = require("../src/FileSystemSearch");
const Eleventy = require("../src/Eleventy");

const getNewTemplate = require("./_getNewTemplateForTests");
const getRenderedTmpls = require("./_getRenderedTemplates");
Expand Down Expand Up @@ -828,3 +829,35 @@ test("Pagination and eleventyComputed permalink, issue #1555 and #1865", async (
t.is(templates[1].data.page.url, "/venues/second/");
t.is(templates[2].data.page.url, "/venues/third/");
});

test("Pagination and eleventyComputed data, issues #2512, #2837, #3013", async (t) => {
let templateLangs = ["liquid", "html", "md", "hbs", "mustache", "njk"];
let apostrophe = {
liquid: "'",
html: "'",
md: "'",
hbs: "'",
mustache: "'",
njk: "'",
};
for (let lang of templateLangs) {
let msg = `lang: ${lang}`;
let le = lang === "md" ? "\n" : "";

let elev = new Eleventy(`./test/stubs-3013/${lang}/`, `./test/stubs-3013/${lang}/_site`, {
source: "cli",
runMode: "build",
});
await elev.init();
let written = await elev.toJSON();

t.is(written[0].url, "/paul-mescal/", msg);
t.is(written[0].content, `<title>The Effervescent adventures of Paul Mescal</title>${le}`, msg);
t.is(written[1].url, "/populace-and-power/", msg);
t.is(
written[1].content,
`<title>Populace and Power: A user${apostrophe[lang]}s guide</title>${le}`,
msg
);
}
});
12 changes: 12 additions & 0 deletions test/stubs-3013/hbs/_data/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 1,
"name": "The Effervescent adventures of Paul Mescal",
"shortname": "paul-mescal"
},
{
"id": 2,
"name": "Populace and Power: A user's guide",
"shortname": "populace-and-power"
}
]
4 changes: 4 additions & 0 deletions test/stubs-3013/hbs/_includes/base.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Books
---
<title>{{ title }}</title>
11 changes: 11 additions & 0 deletions test/stubs-3013/hbs/book.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
layout: base
pagination:
data: books
size: 1
alias: book
permalink: /{{ book.shortname }}/
eleventyComputed:
title: "{{ book.name }}"
---
{{ title }}
12 changes: 12 additions & 0 deletions test/stubs-3013/html/_data/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 1,
"name": "The Effervescent adventures of Paul Mescal",
"shortname": "paul-mescal"
},
{
"id": 2,
"name": "Populace and Power: A user's guide",
"shortname": "populace-and-power"
}
]
4 changes: 4 additions & 0 deletions test/stubs-3013/html/_includes/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Books
---
<title>{{ title }}</title>
11 changes: 11 additions & 0 deletions test/stubs-3013/html/book.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
layout: base
pagination:
data: books
size: 1
alias: book
permalink: /{{ book.shortname }}/
eleventyComputed:
title: "{{ book.name }}"
---
{{ title }}
12 changes: 12 additions & 0 deletions test/stubs-3013/liquid/_data/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 1,
"name": "The Effervescent adventures of Paul Mescal",
"shortname": "paul-mescal"
},
{
"id": 2,
"name": "Populace and Power: A user's guide",
"shortname": "populace-and-power"
}
]
4 changes: 4 additions & 0 deletions test/stubs-3013/liquid/_includes/base.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Books
---
<title>{{ title }}</title>
11 changes: 11 additions & 0 deletions test/stubs-3013/liquid/book.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
layout: base
pagination:
data: books
size: 1
alias: book
permalink: /{{ book.shortname }}/
eleventyComputed:
title: "{{ book.name }}"
---
{{ title }}
12 changes: 12 additions & 0 deletions test/stubs-3013/md/_data/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 1,
"name": "The Effervescent adventures of Paul Mescal",
"shortname": "paul-mescal"
},
{
"id": 2,
"name": "Populace and Power: A user's guide",
"shortname": "populace-and-power"
}
]
5 changes: 5 additions & 0 deletions test/stubs-3013/md/_includes/base.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Books
---

<title>{{ title }}</title>
12 changes: 12 additions & 0 deletions test/stubs-3013/md/book.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
layout: base
pagination:
data: books
size: 1
alias: book
permalink: /{{ book.shortname }}/
eleventyComputed:
title: "{{ book.name }}"
---

{{ title }}
12 changes: 12 additions & 0 deletions test/stubs-3013/mustache/_data/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 1,
"name": "The Effervescent adventures of Paul Mescal",
"shortname": "paul-mescal"
},
{
"id": 2,
"name": "Populace and Power: A user's guide",
"shortname": "populace-and-power"
}
]
4 changes: 4 additions & 0 deletions test/stubs-3013/mustache/_includes/base.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Books
---
<title>{{ title }}</title>
11 changes: 11 additions & 0 deletions test/stubs-3013/mustache/book.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
layout: base
pagination:
data: books
size: 1
alias: book
permalink: /{{ book.shortname }}/
eleventyComputed:
title: "{{ book.name }}"
---
{{ title }}
12 changes: 12 additions & 0 deletions test/stubs-3013/njk/_data/books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 1,
"name": "The Effervescent adventures of Paul Mescal",
"shortname": "paul-mescal"
},
{
"id": 2,
"name": "Populace and Power: A user's guide",
"shortname": "populace-and-power"
}
]
4 changes: 4 additions & 0 deletions test/stubs-3013/njk/_includes/base.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Books
---
<title>{{ title }}</title>
11 changes: 11 additions & 0 deletions test/stubs-3013/njk/book.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
layout: base
pagination:
data: books
size: 1
alias: book
permalink: /{{ book.shortname }}/
eleventyComputed:
title: "{{ book.name }}"
---
{{ title }}
20 changes: 20 additions & 0 deletions test/stubs/pagination-eleventycomputed-title.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
venues:
- id: 1
name: first
- id: 2
name: second
- id: 3
name: third
randommetadata: "woopers"
pagination:
data: venues
size: 1
alias: venue
eleventyComputed:
title: "website - {{ venue.name }}"
---

{{ venue.name }}

{{ randommetadata }}

0 comments on commit 1176b4a

Please sign in to comment.