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(content-docs): format last update date as "Jun 19, 2020" #7673

Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ describe('simple site', () => {
unrelated_front_matter: "won't be part of metadata",
},
lastUpdatedAt: 1539502055,
formattedLastUpdatedAt: '10/14/2018',
formattedLastUpdatedAt: 'October 14, 2018',
lastUpdatedBy: 'Author',
tags: [],
});
Expand Down Expand Up @@ -518,7 +518,7 @@ describe('simple site', () => {
title: 'Custom Last Update',
},
lastUpdatedAt: new Date('1/1/2000').getTime() / 1000,
formattedLastUpdatedAt: '1/1/2000',
formattedLastUpdatedAt: 'January 1, 2000',
lastUpdatedBy: 'Custom Author',
sidebarPosition: undefined,
tags: [],
Expand Down Expand Up @@ -557,7 +557,7 @@ describe('simple site', () => {
title: 'Last Update Author Only',
},
lastUpdatedAt: 1539502055,
formattedLastUpdatedAt: '10/14/2018',
formattedLastUpdatedAt: 'October 14, 2018',
lastUpdatedBy: 'Custom Author',
sidebarPosition: undefined,
tags: [],
Expand Down Expand Up @@ -596,7 +596,7 @@ describe('simple site', () => {
title: 'Last Update Date Only',
},
lastUpdatedAt: new Date('1/1/2000').getTime() / 1000,
formattedLastUpdatedAt: '1/1/2000',
formattedLastUpdatedAt: 'January 1, 2000',
lastUpdatedBy: 'Author',
sidebarPosition: undefined,
tags: [],
Expand Down
23 changes: 20 additions & 3 deletions packages/docusaurus-plugin-content-docs/src/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,21 @@ async function doProcessDocMetadata({

const draft = isDraftForEnvironment({env, frontMatter});

const formatDate = (locale: string, date: Date, calendar: string): string => {
try {
return new Intl.DateTimeFormat(locale, {
day: 'numeric',
month: 'long',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At any rate, I'd prefer this to be short, like Jun 19, 2020. MDN and the TypeScript website both use this style.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

year: 'numeric',
timeZone: 'UTC',
calendar,
}).format(date);
} catch (err) {
logger.error`Can't format docs lastUpdatedAt date "${String(date)}"`;
throw err;
}
};

// Assign all of object properties during instantiation (if possible) for
// NodeJS optimization.
// Adding properties to object after instantiation will cause hidden
Expand All @@ -290,9 +305,11 @@ async function doProcessDocMetadata({
lastUpdatedBy: lastUpdate.lastUpdatedBy,
lastUpdatedAt: lastUpdate.lastUpdatedAt,
formattedLastUpdatedAt: lastUpdate.lastUpdatedAt
? new Intl.DateTimeFormat(i18n.currentLocale, {
calendar: i18n.localeConfigs[i18n.currentLocale]!.calendar,
}).format(lastUpdate.lastUpdatedAt * 1000)
? formatDate(
i18n.currentLocale,
new Date(lastUpdate.lastUpdatedAt * 1000),
i18n.localeConfigs[i18n.currentLocale]!.calendar,
)
: undefined,
sidebarPosition,
frontMatter,
Expand Down