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

Commit

Permalink
fix: sort uncorrect when writing date in 2-digits (fix #56) (#57)
Browse files Browse the repository at this point in the history
* fix: sort uncorrect when writing date in 2-digits

* docs: update sorter section
  • Loading branch information
billyyyyy3320 committed Jan 12, 2020
1 parent 402dc3e commit c3acf01
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
15 changes: 8 additions & 7 deletions docs/pagination/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ We strongly recommend that you read the [Getting Started](../guide/getting-start

Sorter for matched pages, the default sorter is as follows:

```typescript
function sorter(prev: VuePressPage, next: VuePressPage){
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
},
```js
sorter: (prev, next) => {
const dayjs = require('dayjs');
const prevTime = dayjs(prev.frontmatter.date);
const nextTime = dayjs(next.frontmatter.date);
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.
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. `dayjs` accepts this two different result, whereas `new Date` won't work fine in some browser (e.g. Safari).
:::

## prevText
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"dependencies": {
"@vssue/api-github-v3": "^1.1.2",
"@vssue/vuepress-plugin-vssue": "^1.2.0",
"dayjs": "^1.8.19",
"vuejs-paginate": "^2.1.0",
"vuepress-plugin-disqus-comment": "^0.2.3",
"vuepress-plugin-mailchimp": "^1.4.1",
Expand Down
9 changes: 3 additions & 6 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,9 @@ export function resolvePaginationConfig(
* Some browsers (e.g. Safari) don't support this format.
*/
sorter: (prev: VuePressPage, next: VuePressPage) => {
const prevTime = new Date(
prev.frontmatter.date.replace(/\-/g, '/')
).getTime();
const nextTime = new Date(
next.frontmatter.date.replace(/\-/g, '/')
).getTime();
const dayjs = require('dayjs'); // eslint-disable-line
const prevTime = dayjs(prev.frontmatter.date);
const nextTime = dayjs(next.frontmatter.date);
return prevTime - nextTime > 0 ? -1 : 1;
},
},
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3066,6 +3066,11 @@ dateformat@^3.0.0:
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==

dayjs@^1.8.19:
version "1.8.19"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.8.19.tgz#5117dc390d8f8e586d53891dbff3fa308f51abfe"
integrity sha512-7kqOoj3oQSmqbvtvGFLU5iYqies+SqUiEGNT0UtUPPxcPYgY1BrkXR0Cq2R9HYSimBXN+xHkEN4Hi399W+Ovlg==

de-indent@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/de-indent/-/de-indent-1.0.2.tgz#b2038e846dc33baa5796128d0804b455b8c1e21d"
Expand Down

0 comments on commit c3acf01

Please sign in to comment.