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: let data key be a function #1711

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 11 additions & 8 deletions src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const lodashSet = require("lodash/set");
const EleventyBaseError = require("../EleventyBaseError");
const { DeepCopy } = require("../Util/Merge");
const { ProxyWrap } = require("../Util/ProxyWrap");
const getPaginationDataKey = require("../Util/GetPaginationDataKey");

class PaginationConfigError extends EleventyBaseError {}
class PaginationError extends EleventyBaseError {}
Expand Down Expand Up @@ -32,13 +33,13 @@ class Pagination {
return Pagination.hasPagination(this.data);
}

circularReferenceCheck(data) {
if (data.eleventyExcludeFromCollections) {
circularReferenceCheck() {
if (this.data.eleventyExcludeFromCollections) {
return;
}

let key = data.pagination.data;
let tags = data.tags || [];
let key = getPaginationDataKey(this.data);
let tags = this.data.tags || [];
for (let tag of tags) {
if (`collections.${tag}` === key) {
throw new PaginationError(
Expand All @@ -65,7 +66,7 @@ class Pagination {
} else if (!("size" in data.pagination)) {
throw new Error("Missing pagination size in front matter data.");
}
this.circularReferenceCheck(data);
this.circularReferenceCheck();

this.size = data.pagination.size;
this.alias = data.pagination.alias;
Expand Down Expand Up @@ -141,16 +142,18 @@ class Pagination {

_has(target, key) {
let notFoundValue = "__NOT_FOUND_ERROR__";
let data = lodashGet(target, key, notFoundValue);
let paginationDataKey = getPaginationDataKey(this.data);
let data = lodashGet(target, paginationDataKey, notFoundValue);
return data !== notFoundValue;
}

_get(target, key) {
let notFoundValue = "__NOT_FOUND_ERROR__";
let data = lodashGet(target, key, notFoundValue);
let paginationDataKey = getPaginationDataKey(this.data);
let data = lodashGet(target, paginationDataKey, notFoundValue);
if (data === notFoundValue) {
throw new Error(
`Could not find pagination data, went looking for: ${key}`
`Could not find pagination data, went looking for: ${paginationDataKey}`
);
}
return data;
Expand Down
9 changes: 4 additions & 5 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const EleventyErrorUtil = require("./EleventyErrorUtil");
const UsingCircularTemplateContentReferenceError = require("./Errors/UsingCircularTemplateContentReferenceError");
const debug = require("debug")("Eleventy:TemplateMap");
const debugDev = require("debug")("Dev:Eleventy:TemplateMap");
const getPaginationDataKey = require("./Util/GetPaginationDataKey");

const EleventyBaseError = require("./EleventyBaseError");

Expand Down Expand Up @@ -81,16 +82,14 @@ class TemplateMap {
*/
isPaginationOverAllCollections(entry) {
if (entry.data.pagination && entry.data.pagination.data) {
return (
entry.data.pagination.data === "collections" ||
entry.data.pagination.data === "collections.all"
);
const key = getPaginationDataKey(entry.data);
return key === "collections" || key === "collections.all";
}
}

getPaginationTagTarget(entry) {
if (entry.data.pagination && entry.data.pagination.data) {
return this.getTagTarget(entry.data.pagination.data);
return this.getTagTarget(getPaginationDataKey(entry.data));
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Util/GetPaginationDataKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const lodashIsFunction = require("lodash/isFunction");

module.exports = function (data) {
return lodashIsFunction(data.pagination.data)
? data.pagination.data(data)
: data.pagination.data;
};
13 changes: 13 additions & 0 deletions test/GetPaginationDataKeyTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const test = require("ava");
const getPaginationDataKey = require("../src/Util/GetPaginationDataKey");

test("getPaginationDataKey when key is string", (t) => {
t.is(getPaginationDataKey({pagination: {data: "foo"}}), "foo");
});

test("getPaginationDataKey when key is function", (t) => {
t.is(
getPaginationDataKey({foo: "bar", pagination: {data: (data) => data.foo}}),
"bar"
);
});
14 changes: 14 additions & 0 deletions test/PaginationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,20 @@ test("Pagination `before` Callback with a Filter", async (t) => {
t.deepEqual(templates[0].data.myalias, "item2");
});

test("Pagination when `data` is a function", async (t) => {
let tmpl = getNewTemplate(
"./test/stubs/paged/paged-data-key-func.njk",
"./test/stubs/",
"./dist"
);

let data = await tmpl.getData();
let templates = await tmpl.getTemplates(data);
t.is(templates.length, 2);
t.deepEqual(templates[0].data.pagination.items, ["foo"]);
t.deepEqual(templates[1].data.pagination.items, ["woo"]);
});

test("Pagination `before` Callback with `reverse: true` (test order of operations)", async (t) => {
let tmpl = getNewTemplate(
"./test/stubs/paged/paged-before-and-reverse.njk",
Expand Down
11 changes: 11 additions & 0 deletions test/stubs/paged/paged-data-key-func.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---js
{
dataCanBeFoundIn: "items",
pagination: {
data: (data) => data.dataCanBeFoundIn,
size: 1
},
items: ["foo", "woo"]
}
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>