Skip to content

Commit

Permalink
Pagination: let data key be a function
Browse files Browse the repository at this point in the history
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;
        },
    ...
  • Loading branch information
Jaza committed Mar 26, 2021
1 parent e3729ec commit 1e059fe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
19 changes: 8 additions & 11 deletions src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const lodashChunk = require("lodash/chunk");
const lodashGet = require("lodash/get");
const lodashSet = require("lodash/set");
const EleventyBaseError = require("../EleventyBaseError");
const getPaginationDataKey = require("../Util/GetPaginationDataKey");

class PaginationConfigError extends EleventyBaseError {}
class PaginationError extends EleventyBaseError {}
Expand Down Expand Up @@ -30,13 +31,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 @@ -63,7 +64,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 All @@ -76,10 +77,6 @@ class Pagination {
this.template = tmpl;
}

_getDataKey() {
return this.data.pagination.data;
}

doResolveToObjectValues() {
if ("resolve" in this.data.pagination) {
return this.data.pagination.resolve === "values";
Expand All @@ -102,7 +99,7 @@ class Pagination {

_resolveItems() {
let notFoundValue = "__NOT_FOUND_ERROR__";
let key = this._getDataKey();
let key = getPaginationDataKey(this.data);
let fullDataSet = lodashGet(this.data, key, notFoundValue);
if (fullDataSet === notFoundValue) {
throw new Error(
Expand Down Expand Up @@ -195,7 +192,7 @@ class Pagination {

let override = {
pagination: {
data: this.data.pagination.data,
data: getPaginationDataKey(this.data),
size: this.data.pagination.size,
alias: this.alias,

Expand Down
9 changes: 4 additions & 5 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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 @@ -71,16 +72,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"
);
});

0 comments on commit 1e059fe

Please sign in to comment.