Skip to content

added renameLast and verbose options #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ $ 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` |
| `renameLast` | Set the last page name. e.g. `page/last/index.html` | `false` |
| `localizedLast` | Localize the last page name. e.g. `page/最後/index.html` | `last` |
| `verbose` | Display the generated urls | `false` |

If there is a single page `overwriteLatest` requires `explicitPaging=true`.

For example:

Expand Down
16 changes: 13 additions & 3 deletions lib/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ function pagination(base, posts, options = {}) {

const { length } = posts;

const { format: _format, layout, data, perPage, explicitPaging } = Object.assign({
const { format: _format, layout, data, perPage, explicitPaging, renameLast, localizedLast, verbose } = Object.assign({
format: 'page/%d/',
layout: ['archive', 'index'],
data: {},
perPage: 10,
explicitPaging: false
explicitPaging: false,
renameLast: false,
localizedLast: 'last',
verbose: false
}, options);

const total = perPage ? Math.ceil(length / perPage) : 1;
Expand All @@ -26,7 +29,9 @@ function pagination(base, posts, options = {}) {
if (urlCache.has(i)) return urlCache.get(i);

let url;
if (!explicitPaging) {
if (renameLast && explicitPaging && i === total) {
url = base + format(_format, i).replace(i, localizedLast);
} else if (!explicitPaging) {
url = i > 1 ? base + format(_format, i) : base;
} else {
url = base + format(_format, i);
Expand Down Expand Up @@ -78,6 +83,11 @@ function pagination(base, posts, options = {}) {
});
}

if (verbose) {
result.forEach(page => {
console.log(`Generated route: ${page.path}`);
});
}
return result;
}

Expand Down
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,37 @@ describe('pagination', () => {
result[i].path.should.eql(`/page/${pageNum}/`);
}
});
it('renameLast', () => {
let result = pagination('/', posts, {
explicitPaging: true,
renameLast: 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('/page/last/');
result[result.length - 2].data.next_link.should.eql('/page/last/');
result = pagination('/', posts, {
format: 'ページ/%d/',
explicitPaging: true,
renameLast: true,
localizedLast: '最後'
});
for (let i = 0, len = result.length; i < len - 1; i++) {
const pageNum = i + 1;
result[i].path.should.eql(`/ページ/${pageNum}/`);
}
result[result.length - 1].path.should.eql('/ページ/最後/');
result[result.length - 2].data.next_link.should.eql('/ページ/最後/');
result = pagination('/', [posts[0]], {
explicitPaging: true,
renameLast: true
});
result[0].path.should.eql('/page/last/');
result = pagination('/', [posts[0]], {
renameLast: true
});
result[0].path.should.eql('/');
});
});