Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix($shared-utils): use dayjs for permalinks (#2880) #2881

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/@vuepress/shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"dependencies": {
"chalk": "^2.3.2",
"dayjs": "^1.10.5",
"escape-html": "^1.0.3",
"fs-extra": "^7.0.1",
"globby": "^9.2.0",
Expand Down
44 changes: 7 additions & 37 deletions packages/@vuepress/shared-utils/src/getPermalink.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dayjs from 'dayjs'
import ensureEndingSlash from './ensureEndingSlash'
import ensureLeadingSlash from './ensureLeadingSlash'

Expand All @@ -13,37 +14,6 @@ function removeLeadingSlash (path: string) {
return path.replace(/^\//, '')
}

function toUtcTime (date: string | Date): number {
let year = 1970
let month = 0
let day = 1

if (typeof date === 'string') {
const [
yearStr,
monthStr,
dayStr
] = date.split('-')

year = parseInt(yearStr, 10)
month = parseInt(monthStr, 10) - 1
day = parseInt(dayStr, 10)
} else if (date instanceof Date) {
// If `date` is an instance of Date,
// it's because it was parsed from the frontmatter
// by js-yaml, which always assumes UTC
return date.getTime()
}

return Date.UTC(year, month, day)
}

function addTzOffset (utc: number): Date {
const utcDate = new Date(utc)

return new Date(utc + utcDate.getTimezoneOffset() * 60 * 1000)
}

// e.g.
// filename: docs/_posts/evan you.md
// content: # yyx 990803
Expand All @@ -65,12 +35,12 @@ export = function getPermalink ({
}
slug = encodeURI(slug)

const d = addTzOffset(toUtcTime(date))
const year = d.getFullYear()
const iMonth = d.getMonth() + 1
const iDay = d.getDate()
const minutes = d.getMinutes()
const seconds = d.getSeconds()
const d = dayjs(date)
const year = d.year()
const iMonth = d.month() + 1
const iDay = d.date()
const minutes = d.minute()
const seconds = d.second()
const month = iMonth < 10 ? `0${iMonth}` : iMonth
const day = iDay < 10 ? `0${iDay}` : iDay

Expand Down