Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
fix: default sorter doesn't work fine in Safari (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
billyyyyy3320 authored Jan 9, 2020
1 parent 92fae58 commit 3fdc6a3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 7 additions & 3 deletions docs/pagination/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ Sorter for matched pages, the default sorter is as follows:

```typescript
function sorter(prev: VuePressPage, next: VuePressPage){
const prevTime = new Date(prev.frontmatter.date).getTime()
const nextTime = new Date(next.frontmatter.date).getTime()
const prevTime = new Date(prev.frontmatter.date.replace(/\-/g, '/')).getTime()
const nextTime = new Date(next.frontmatter.date.replace(/\-/g, '/')).getTime()
return prevTime - nextTime > 0 ? -1 : 1
},
```
The function will be a parameter of [Array.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).

::: warning Note
You might be intrigued by `replace(/\-/g, '/')`. Because only the dates in frontmatter written in 2-digits will be transformed, other dates written in single-digit, such as `2020-1-1` will be treated as string. Some browsers (e.g. Safari) don't support this format.
:::

## lengthPerPage

- Type: number
Expand Down Expand Up @@ -83,7 +87,7 @@ There are three args to help you customize your title:
- `id` is the id in the [config](../config/#id).
- `scope` is the [key](../config/#keys) while configuring frontmatters or same as `id` while configuring directories.

::: tip TIP
::: warning Note
`${index + 2}`: why `+2`?

Plus 1 since index starts at 0. <br>
Expand Down
14 changes: 12 additions & 2 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,19 @@ export function resolvePaginationConfig(
}
: getFrontmatterClassifierPageFilter(keys),

/**
* You might be intrigued by `replace(/\-/g, '/')`.
* Because only the dates in frontmatter written in 2-digits will be transformed,
* other dates written in single-digit, such as `2020-1-1` will be treated as string.
* Some browsers (e.g. Safari) don't support this format.
*/
sorter: (prev: VuePressPage, next: VuePressPage) => {
const prevTime = new Date(prev.frontmatter.date).getTime();
const nextTime = new Date(next.frontmatter.date).getTime();
const prevTime = new Date(
prev.frontmatter.date.replace(/\-/g, '/')
).getTime();
const nextTime = new Date(
next.frontmatter.date.replace(/\-/g, '/')
).getTime();
return prevTime - nextTime > 0 ? -1 : 1;
},
},
Expand Down

0 comments on commit 3fdc6a3

Please sign in to comment.