Skip to content
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

feat: add an option to number first page. #114

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
51 changes: 26 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,50 @@ Pagination utilities for [Hexo] generator plugins.

## Installation

``` bash
```bash
$ npm install hexo-pagination --save
```

## Usage

### 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 |
| 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` |

For example:

``` js
var pagination = require('hexo-pagination');
```js
var pagination = require("hexo-pagination");

pagination('/tags/hexo', [], {
pagination("/tags/hexo", [], {
perPage: 10,
format: 'page/%d/',
layout: ['archive', 'index'],
format: "page/%d/",
layout: ["archive", "index"],
data: {
tag: 'hexo'
}
tag: "hexo",
},
});
```

This function returns an array containing objects with 3 properties: `path`, `layout`, `data`.

Data | Description
--- | ---
`base` | Base URL
`total` | Total pages
`current` | Current page number
`current_url` | Path of the current page (which equals to `path`)
`posts` | The slice of posts for the current page
`prev` | Previous page number
`prev_link` | The path to the previous page
`next` | Next page number
`next_link` | The path to the next page
| Data | Description |
| ------------- | ------------------------------------------------- |
| `base` | Base URL |
| `total` | Total pages |
| `current` | Current page number |
| `current_url` | Path of the current page (which equals to `path`) |
| `posts` | The slice of posts for the current page |
| `prev` | Previous page number |
| `prev_link` | The path to the previous page |
| `next` | Next page number |
| `next_link` | The path to the next page |

## License

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

const { length } = posts;

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

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

const url = i > 1 ? base + format(_format, i) : base;
let url;
if (!explicitPaging) {
url = i > 1 ? base + format(_format, i) : base;
} else {
url = base + format(_format, i);
}
urlCache.set(i, url);

return url;
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,15 @@ describe('pagination', () => {
result[i].data.tag.should.eql('test');
}
});

it('explicitPaging', () => {
const result = pagination('/', posts, {
explicitPaging: true
});

for (let i = 0, len = result.length; i < len; i++) {
const pageNum = i + 1;
result[i].path.should.eql(`/page/${pageNum}/`);
}
});
});
Loading