diff --git a/src/Plugins/Pagination.js b/src/Plugins/Pagination.js index 627ba9698..c071d1b73 100755 --- a/src/Plugins/Pagination.js +++ b/src/Plugins/Pagination.js @@ -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 @@ -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; + } } } @@ -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; } diff --git a/test/PaginationTest.js b/test/PaginationTest.js index c5b29750b..86b3051f6 100644 --- a/test/PaginationTest.js +++ b/test/PaginationTest.js @@ -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); + }); +}); 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 +---