From b90d674f9a8949c3e8b807991e6137f2944f1885 Mon Sep 17 00:00:00 2001 From: Marco Azimonti Date: Thu, 16 Jan 2025 19:36:24 +0900 Subject: [PATCH] added overwriteLatest and verbose options --- README.md | 18 +++++++++++------- lib/pagination.js | 16 ++++++++++++++-- test/index.js | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fb3bac8..93f23b9 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,17 @@ $ npm install hexo-pagination --save ### pagination(base, posts, [options]) -| Option | Description | Default | -| ---------------- | ----------------------------------------------- | ---------------------- | -| `perPage` | Posts displayed per page | `10` | -| `format` | URL format | `page/%d/` | -| `layout` | Layout. This value can be a string or an array. | `['archive', 'index']` | -| `data` | Extra data | `{}` | -| `explicitPaging` | Number the first page. e.g. `page/1/index.html` | `false` | +| Option | Description | Default | +| ---------------- | ------------------------------------------------------- | ---------------------- | +| `perPage` | Posts displayed per page | `10` | +| `format` | URL format | `page/%d/` | +| `layout` | Layout. This value can be a string or an array. | `['archive', 'index']` | +| `data` | Extra data | `{}` | +| `explicitPaging` | Number the first page. e.g. `page/1/index.html` | `false` | +| `overwriteLatest`| Set the latest page name. e.g. `page/latest/index.html` | `false` | +| `verbose` | Display the generated urls | `false` | + +If there is a single page `overwriteLatest` requires `explicitPaging=true`. For example: diff --git a/lib/pagination.js b/lib/pagination.js index efb2c9d..809ba53 100644 --- a/lib/pagination.js +++ b/lib/pagination.js @@ -10,12 +10,14 @@ function pagination(base, posts, options = {}) { const { length } = posts; - const { format: _format, layout, data, perPage, explicitPaging } = Object.assign({ + const { format: _format, layout, data, perPage, explicitPaging, overwriteLatest, verbose } = Object.assign({ format: 'page/%d/', layout: ['archive', 'index'], data: {}, perPage: 10, - explicitPaging: false + explicitPaging: false, + overwriteLatest: false, + verbose: false }, options); const total = perPage ? Math.ceil(length / perPage) : 1; @@ -78,6 +80,16 @@ function pagination(base, posts, options = {}) { }); } + if ((overwriteLatest && result.length > 1) || (overwriteLatest && explicitPaging && result.length === 1)) { + const lastPage = result[result.length - 1]; + lastPage.path = lastPage.path.replace(/\/page\/\d+\/?$/, '/latest/'); + } + + if (verbose) { + result.forEach(page => { + console.log(`Generated route: ${page.path}`); + }); + } return result; } diff --git a/test/index.js b/test/index.js index 76058e5..cd1ef56 100644 --- a/test/index.js +++ b/test/index.js @@ -152,4 +152,29 @@ describe('pagination', () => { result[i].path.should.eql(`/page/${pageNum}/`); } }); + it('overwriteLatest 1', () => { + const result = pagination('/', posts, { + explicitPaging: true, + overwriteLatest: true + }); + + for (let i = 0, len = result.length; i < len - 1; i++) { + const pageNum = i + 1; + result[i].path.should.eql(`/page/${pageNum}/`); + } + result[result.length - 1].path.should.eql('/latest/'); + }); + it('overwriteLatest 2', () => { + const result = pagination('/', [posts[0]], { + explicitPaging: true, + overwriteLatest: true + }); + result[0].path.should.eql('/latest/'); + }); + it('overwriteLatest 3', () => { + const result = pagination('/', [posts[0]], { + overwriteLatest: true + }); + result[0].path.should.eql('/'); + }); });