Skip to content

Commit 1e059fe

Browse files
committed
Pagination: let data key be a function
So that pagination data key can be determined dynamically, based on a callback function that gets passed all template data. So you can do something like: ---js { pagination: { data: (data) => { return "collections." + data.foo; }, ...
1 parent e3729ec commit 1e059fe

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

src/Plugins/Pagination.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const lodashChunk = require("lodash/chunk");
22
const lodashGet = require("lodash/get");
33
const lodashSet = require("lodash/set");
44
const EleventyBaseError = require("../EleventyBaseError");
5+
const getPaginationDataKey = require("../Util/GetPaginationDataKey");
56

67
class PaginationConfigError extends EleventyBaseError {}
78
class PaginationError extends EleventyBaseError {}
@@ -30,13 +31,13 @@ class Pagination {
3031
return Pagination.hasPagination(this.data);
3132
}
3233

33-
circularReferenceCheck(data) {
34-
if (data.eleventyExcludeFromCollections) {
34+
circularReferenceCheck() {
35+
if (this.data.eleventyExcludeFromCollections) {
3536
return;
3637
}
3738

38-
let key = data.pagination.data;
39-
let tags = data.tags || [];
39+
let key = getPaginationDataKey(this.data);
40+
let tags = this.data.tags || [];
4041
for (let tag of tags) {
4142
if (`collections.${tag}` === key) {
4243
throw new PaginationError(
@@ -63,7 +64,7 @@ class Pagination {
6364
} else if (!("size" in data.pagination)) {
6465
throw new Error("Missing pagination size in front matter data.");
6566
}
66-
this.circularReferenceCheck(data);
67+
this.circularReferenceCheck();
6768

6869
this.size = data.pagination.size;
6970
this.alias = data.pagination.alias;
@@ -76,10 +77,6 @@ class Pagination {
7677
this.template = tmpl;
7778
}
7879

79-
_getDataKey() {
80-
return this.data.pagination.data;
81-
}
82-
8380
doResolveToObjectValues() {
8481
if ("resolve" in this.data.pagination) {
8582
return this.data.pagination.resolve === "values";
@@ -102,7 +99,7 @@ class Pagination {
10299

103100
_resolveItems() {
104101
let notFoundValue = "__NOT_FOUND_ERROR__";
105-
let key = this._getDataKey();
102+
let key = getPaginationDataKey(this.data);
106103
let fullDataSet = lodashGet(this.data, key, notFoundValue);
107104
if (fullDataSet === notFoundValue) {
108105
throw new Error(
@@ -195,7 +192,7 @@ class Pagination {
195192

196193
let override = {
197194
pagination: {
198-
data: this.data.pagination.data,
195+
data: getPaginationDataKey(this.data),
199196
size: this.data.pagination.size,
200197
alias: this.alias,
201198

src/TemplateMap.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const EleventyErrorUtil = require("./EleventyErrorUtil");
55
const UsingCircularTemplateContentReferenceError = require("./Errors/UsingCircularTemplateContentReferenceError");
66
const debug = require("debug")("Eleventy:TemplateMap");
77
const debugDev = require("debug")("Dev:Eleventy:TemplateMap");
8+
const getPaginationDataKey = require("./Util/GetPaginationDataKey");
89

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

@@ -71,16 +72,14 @@ class TemplateMap {
7172
*/
7273
isPaginationOverAllCollections(entry) {
7374
if (entry.data.pagination && entry.data.pagination.data) {
74-
return (
75-
entry.data.pagination.data === "collections" ||
76-
entry.data.pagination.data === "collections.all"
77-
);
75+
const key = getPaginationDataKey(entry.data);
76+
return key === "collections" || key === "collections.all";
7877
}
7978
}
8079

8180
getPaginationTagTarget(entry) {
8281
if (entry.data.pagination && entry.data.pagination.data) {
83-
return this.getTagTarget(entry.data.pagination.data);
82+
return this.getTagTarget(getPaginationDataKey(entry.data));
8483
}
8584
}
8685

src/Util/GetPaginationDataKey.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const lodashIsFunction = require("lodash/isFunction");
2+
3+
module.exports = function (data) {
4+
return lodashIsFunction(data.pagination.data)
5+
? data.pagination.data(data)
6+
: data.pagination.data;
7+
};

test/GetPaginationDataKeyTest.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const test = require("ava");
2+
const getPaginationDataKey = require("../src/Util/GetPaginationDataKey");
3+
4+
test("getPaginationDataKey when key is string", (t) => {
5+
t.is(getPaginationDataKey({pagination: {data: "foo"}}), "foo");
6+
});
7+
8+
test("getPaginationDataKey when key is function", (t) => {
9+
t.is(
10+
getPaginationDataKey({foo: "bar", pagination: {data: (data) => data.foo}}),
11+
"bar"
12+
);
13+
});

0 commit comments

Comments
 (0)