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

fix: default sorter doesn't work fine in Safari #54

Merged
merged 1 commit into from
Jan 9, 2020
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
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