From caf0313695bc5b327b327b37940fe51ce4f53aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20H=C3=B6ser?= Date: Wed, 29 Jun 2022 22:16:18 +0200 Subject: [PATCH 1/2] #1036 Add skip parameter to pagination plugin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR adds an option to skip the first n items of a pagination. This allows usecases as described in #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 --- src/Plugins/Pagination.js | 10 ++++++-- test/PaginationTest.js | 38 ++++++++++++++++++++++++++++ test/stubs/pagination-skip-string.md | 13 ++++++++++ test/stubs/pagination-skip.md | 13 ++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 test/stubs/pagination-skip-string.md create mode 100644 test/stubs/pagination-skip.md diff --git a/src/Plugins/Pagination.js b/src/Plugins/Pagination.js index 3cba45ac3..1004e5d63 100755 --- a/src/Plugins/Pagination.js +++ b/src/Plugins/Pagination.js @@ -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()); @@ -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 diff --git a/test/PaginationTest.js b/test/PaginationTest.js index 09dd3b6dd..5fc90ca4f 100644 --- a/test/PaginationTest.js +++ b/test/PaginationTest.js @@ -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); + }); +}); diff --git a/test/stubs/pagination-skip-string.md b/test/stubs/pagination-skip-string.md new file mode 100644 index 000000000..2d83f7b75 --- /dev/null +++ b/test/stubs/pagination-skip-string.md @@ -0,0 +1,13 @@ +--- +venues: + - first + - second + - third + - fourth + - fifth +pagination: + data: venues + size: 2 + skip: one + addAllPagesToCollections: true +--- diff --git a/test/stubs/pagination-skip.md b/test/stubs/pagination-skip.md new file mode 100644 index 000000000..cd0c7510a --- /dev/null +++ b/test/stubs/pagination-skip.md @@ -0,0 +1,13 @@ +--- +venues: + - first + - second + - third + - fourth + - fifth +pagination: + data: venues + size: 2 + skip: 1 + addAllPagesToCollections: true +--- From 418e816899bd56145e31af399dc90d464c819b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20H=C3=B6ser?= Date: Wed, 29 Jun 2022 22:38:37 +0200 Subject: [PATCH 2/2] Move skipping to other item modifications MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphael Höser --- src/Plugins/Pagination.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Plugins/Pagination.js b/src/Plugins/Pagination.js index 1004e5d63..580605cd4 100755 --- a/src/Plugins/Pagination.js +++ b/src/Plugins/Pagination.js @@ -81,12 +81,7 @@ 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()); @@ -113,7 +108,7 @@ class Pagination { } } else { // this returns an array and skips no elements by default - this.target = this._resolveItems().slice(this.skip); + 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 @@ -209,6 +204,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; }