Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination: pass metadata to before callback #1209

Merged
merged 1 commit into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Plugins/Pagination.js
Expand Up @@ -120,7 +120,7 @@ class Pagination {
typeof this.data.pagination.before === "function"
) {
// we don’t need to make a copy of this because we already .filter() above
result = this.data.pagination.before(result);
result = this.data.pagination.before(result, this.data);
}

if (this.data.pagination.reverse === true) {
Expand Down
12 changes: 12 additions & 0 deletions test/PaginationTest.js
Expand Up @@ -606,6 +606,18 @@ test("Pagination `before` Callback", async t => {
t.deepEqual(templates[0].data.myalias, "item6");
});

test("Pagination `before` Callback with metadata", async t => {
let tmpl = new Template(
"./test/stubs/paged/paged-before-metadata.njk",
"./test/stubs/",
"./dist"
);

let data = await tmpl.getData();
let templates = await tmpl.getTemplates(data);
t.deepEqual(templates[0].data.pagination.items, ["item3"]);
});

test("Pagination `before` Callback with a Filter", async t => {
let tmpl = new Template(
"./test/stubs/paged/paged-before-filter.njk",
Expand Down
14 changes: 14 additions & 0 deletions test/stubs/paged/paged-before-metadata.njk
@@ -0,0 +1,14 @@
---js
{
keyword: "item3",
pagination: {
data: "items",
size: 1,
before: function(data, metadata) {
return data.filter(el => el === metadata.keyword);
}
},
items: ["item1", "item2", "item3", "item4", "item5", "item6"]
}
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>