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): fix date parse logic for permalinks #2181

Merged
merged 1 commit into from
Aug 23, 2020
Merged
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
34 changes: 33 additions & 1 deletion packages/@vuepress/shared-utils/src/getPermalink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ interface PermalinkOption {
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) {
billyyyyy3320 marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -33,7 +65,7 @@ export = function getPermalink ({
}
slug = encodeURI(slug)

const d = new Date(date)
const d = addTzOffset(toUtcTime(date))
const year = d.getFullYear()
const iMonth = d.getMonth() + 1
const iDay = d.getDate()
Expand Down