Skip to content

Commit

Permalink
11ty#1036 Add skip parameter to pagination plugin
Browse files Browse the repository at this point in the history
This PR adds an option to skip the first n items of a pagination.
This allows usecases as described in 11ty#1036, where one might want to show the first n items on onether page and then paginate over the rest.

Signed-off-by: Raphael Höser <[email protected]>
  • Loading branch information
Snapstromegon committed Jun 29, 2022
1 parent fc6fc96 commit caf0313
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class Pagination {

this.size = data.pagination.size;
this.alias = data.pagination.alias;
this.skip = data.pagination.skip || 0;
if (typeof this.skip !== "number") {
throw new Error(
`Missing pagination skip in front matter data${this.inputPathForErrorMessages}`
);
}
// TODO do we need the full data set for serverless?
this.fullDataSet = this._get(this.data, this._getDataKey());

Expand All @@ -106,8 +112,8 @@ class Pagination {
];
}
} else {
// this returns an array
this.target = this._resolveItems();
// this returns an array and skips no elements by default
this.target = this._resolveItems().slice(this.skip);

// 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
Expand Down
38 changes: 38 additions & 0 deletions test/PaginationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,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
---

0 comments on commit caf0313

Please sign in to comment.