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

Commit c3acf01

Browse files
fix: sort uncorrect when writing date in 2-digits (fix #56) (#57)
* fix: sort uncorrect when writing date in 2-digits * docs: update sorter section
1 parent 402dc3e commit c3acf01

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

Diff for: docs/pagination/README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ We strongly recommend that you read the [Getting Started](../guide/getting-start
1515

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

18-
```typescript
19-
function sorter(prev: VuePressPage, next: VuePressPage){
20-
const prevTime = new Date(prev.frontmatter.date.replace(/\-/g, '/')).getTime()
21-
const nextTime = new Date(next.frontmatter.date.replace(/\-/g, '/')).getTime()
22-
return prevTime - nextTime > 0 ? -1 : 1
23-
},
18+
```js
19+
sorter: (prev, next) => {
20+
const dayjs = require('dayjs');
21+
const prevTime = dayjs(prev.frontmatter.date);
22+
const nextTime = dayjs(next.frontmatter.date);
23+
return prevTime - nextTime > 0 ? -1 : 1;
24+
}
2425
```
2526
The function will be a parameter of [Array.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
2627

2728
::: warning Note
28-
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.
29+
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).
2930
:::
3031

3132
## prevText

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"dependencies": {
5454
"@vssue/api-github-v3": "^1.1.2",
5555
"@vssue/vuepress-plugin-vssue": "^1.2.0",
56+
"dayjs": "^1.8.19",
5657
"vuejs-paginate": "^2.1.0",
5758
"vuepress-plugin-disqus-comment": "^0.2.3",
5859
"vuepress-plugin-mailchimp": "^1.4.1",

Diff for: src/node/util.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,9 @@ export function resolvePaginationConfig(
115115
* Some browsers (e.g. Safari) don't support this format.
116116
*/
117117
sorter: (prev: VuePressPage, next: VuePressPage) => {
118-
const prevTime = new Date(
119-
prev.frontmatter.date.replace(/\-/g, '/')
120-
).getTime();
121-
const nextTime = new Date(
122-
next.frontmatter.date.replace(/\-/g, '/')
123-
).getTime();
118+
const dayjs = require('dayjs'); // eslint-disable-line
119+
const prevTime = dayjs(prev.frontmatter.date);
120+
const nextTime = dayjs(next.frontmatter.date);
124121
return prevTime - nextTime > 0 ? -1 : 1;
125122
},
126123
},

Diff for: yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3066,6 +3066,11 @@ dateformat@^3.0.0:
30663066
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
30673067
integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==
30683068

3069+
dayjs@^1.8.19:
3070+
version "1.8.19"
3071+
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.8.19.tgz#5117dc390d8f8e586d53891dbff3fa308f51abfe"
3072+
integrity sha512-7kqOoj3oQSmqbvtvGFLU5iYqies+SqUiEGNT0UtUPPxcPYgY1BrkXR0Cq2R9HYSimBXN+xHkEN4Hi399W+Ovlg==
3073+
30693074
de-indent@^1.0.2:
30703075
version "1.0.2"
30713076
resolved "https://registry.yarnpkg.com/de-indent/-/de-indent-1.0.2.tgz#b2038e846dc33baa5796128d0804b455b8c1e21d"

0 commit comments

Comments
 (0)