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

#1036 Add skip parameter to pagination plugin #2466

Open
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Pagination {

this.size = data.pagination.size;
this.alias = data.pagination.alias;

// TODO do we need the full data set for serverless?
this.fullDataSet = this._get(this.data, this._getDataKey());
// this returns an array
Expand All @@ -96,7 +97,20 @@ class Pagination {
data.pagination.serverlessFilter(this.fullDataSet, serverlessPaginationKey),
];
} else {
this.chunkedItems = this.pagedItems;
// this returns an array and skips no elements by default
this.target = this._resolveItems();

// Serverless Shortcut when key is not found in data set (probably running local build and expected a :path param in data)
// Only collections are relevant for templates that don’t have a permalink.build, they don’t have a templateContent and aren’t written to disk
if (
data.pagination.serverless &&
!data.pagination.addAllPagesToCollections
) {
// use the first page only
this.chunkedItems = [this.pagedItems[0]];
} else {
this.chunkedItems = this.pagedItems;
}
}
}

Expand Down Expand Up @@ -183,6 +197,15 @@ class Pagination {
result = result.filter((value) => !this.isFiltered(value));
}

if (this.data.pagination.skip) {
if (typeof this.data.pagination.skip !== "number") {
throw new Error(
`Missing pagination skip in front matter data${this.inputPathForErrorMessages}`
);
}
result = result.slice(this.data.pagination.skip);
}

return result;
}

Expand Down
38 changes: 38 additions & 0 deletions test/PaginationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,41 @@ 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 with skip parameter, issue #1036", async (t) => {
let eleventyConfig = new TemplateConfig();
let tmpl = getNewTemplate(
"./test/stubs/pagination-skip.md",
"./test/stubs/",
"./dist",
null,
null,
eleventyConfig
);

let data = await tmpl.getData();
let paging = new Pagination(tmpl, data, tmpl.config);
paging.setTemplate(tmpl);

t.truthy(data.pagination);
t.is(paging.getPageCount(), 2);
t.is(data.pagination.size, 2);
t.deepEqual(paging.target, ["second", "third", "fourth", "fifth"]);
});

test("Pagination with skip parameter required to be number, issue #1036", async (t) => {
let eleventyConfig = new TemplateConfig();
let tmpl = getNewTemplate(
"./test/stubs/pagination-skip-string.md",
"./test/stubs/",
"./dist",
null,
null,
eleventyConfig
);

let data = await tmpl.getData();
t.throws(() => {
new Pagination(tmpl, data, tmpl.config);
});
});
13 changes: 13 additions & 0 deletions test/stubs/pagination-skip-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
venues:
- first
- second
- third
- fourth
- fifth
pagination:
data: venues
size: 2
skip: one
addAllPagesToCollections: true
---
13 changes: 13 additions & 0 deletions test/stubs/pagination-skip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
venues:
- first
- second
- third
- fourth
- fifth
pagination:
data: venues
size: 2
skip: 1
addAllPagesToCollections: true
---