diff --git a/packages/@vuepress/shared-utils/package.json b/packages/@vuepress/shared-utils/package.json index d92543d4d8..15992bffa5 100644 --- a/packages/@vuepress/shared-utils/package.json +++ b/packages/@vuepress/shared-utils/package.json @@ -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", diff --git a/packages/@vuepress/shared-utils/src/getPermalink.ts b/packages/@vuepress/shared-utils/src/getPermalink.ts index 7207fff873..bcc9cc14f7 100644 --- a/packages/@vuepress/shared-utils/src/getPermalink.ts +++ b/packages/@vuepress/shared-utils/src/getPermalink.ts @@ -1,3 +1,4 @@ +import dayjs from 'dayjs' import ensureEndingSlash from './ensureEndingSlash' import ensureLeadingSlash from './ensureLeadingSlash' @@ -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 @@ -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