Skip to content

Commit

Permalink
feat: add an option to number first page. (#114)
Browse files Browse the repository at this point in the history
* feat: add an option to number first page.

* test case

* update README
  • Loading branch information
uiolee authored May 17, 2024
1 parent bf9f3e2 commit 449d59c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
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}/`);
}
});
});

0 comments on commit 449d59c

Please sign in to comment.