Skip to content

Commit

Permalink
added overwriteLatest and verbose options
Browse files Browse the repository at this point in the history
  • Loading branch information
azimonti committed Jan 16, 2025
1 parent 0804e67 commit b90d674
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
16 changes: 14 additions & 2 deletions lib/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
});
});

0 comments on commit b90d674

Please sign in to comment.