mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 00:57:53 +02:00
refactor(v2): format last updated date using Intl (#4352)
This commit is contained in:
parent
4f419801da
commit
6be0bd41b0
6 changed files with 38 additions and 13 deletions
|
@ -352,6 +352,7 @@ describe('simple site', () => {
|
||||||
editUrl: 'https://github.com/customUrl/docs/lorem.md',
|
editUrl: 'https://github.com/customUrl/docs/lorem.md',
|
||||||
description: 'Lorem ipsum.',
|
description: 'Lorem ipsum.',
|
||||||
lastUpdatedAt: 1539502055,
|
lastUpdatedAt: 1539502055,
|
||||||
|
formattedLastUpdatedAt: '10/14/2018',
|
||||||
lastUpdatedBy: 'Author',
|
lastUpdatedBy: 'Author',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -14,6 +14,7 @@ import {
|
||||||
normalizeUrl,
|
normalizeUrl,
|
||||||
parseMarkdownString,
|
parseMarkdownString,
|
||||||
posixPath,
|
posixPath,
|
||||||
|
getDateTimeFormat,
|
||||||
} from '@docusaurus/utils';
|
} from '@docusaurus/utils';
|
||||||
import {LoadContext} from '@docusaurus/types';
|
import {LoadContext} from '@docusaurus/types';
|
||||||
|
|
||||||
|
@ -113,7 +114,7 @@ export function processDocMetadata({
|
||||||
}): DocMetadataBase {
|
}): DocMetadataBase {
|
||||||
const {source, content, lastUpdate, docsDirPath, filePath} = docFile;
|
const {source, content, lastUpdate, docsDirPath, filePath} = docFile;
|
||||||
const {homePageId} = options;
|
const {homePageId} = options;
|
||||||
const {siteDir} = context;
|
const {siteDir, i18n} = context;
|
||||||
|
|
||||||
// ex: api/myDoc -> api
|
// ex: api/myDoc -> api
|
||||||
// ex: myDoc -> .
|
// ex: myDoc -> .
|
||||||
|
@ -210,6 +211,11 @@ export function processDocMetadata({
|
||||||
version: versionMetadata.versionName,
|
version: versionMetadata.versionName,
|
||||||
lastUpdatedBy: lastUpdate.lastUpdatedBy,
|
lastUpdatedBy: lastUpdate.lastUpdatedBy,
|
||||||
lastUpdatedAt: lastUpdate.lastUpdatedAt,
|
lastUpdatedAt: lastUpdate.lastUpdatedAt,
|
||||||
|
formattedLastUpdatedAt: lastUpdate.lastUpdatedAt
|
||||||
|
? getDateTimeFormat(i18n.currentLocale)(i18n.currentLocale).format(
|
||||||
|
lastUpdate.lastUpdatedAt * 1000,
|
||||||
|
)
|
||||||
|
: undefined,
|
||||||
sidebar_label,
|
sidebar_label,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,6 +69,7 @@ declare module '@theme/DocItem' {
|
||||||
readonly permalink?: string;
|
readonly permalink?: string;
|
||||||
readonly editUrl?: string;
|
readonly editUrl?: string;
|
||||||
readonly lastUpdatedAt?: number;
|
readonly lastUpdatedAt?: number;
|
||||||
|
readonly formattedLastUpdatedAt?: string;
|
||||||
readonly lastUpdatedBy?: string;
|
readonly lastUpdatedBy?: string;
|
||||||
readonly version?: string;
|
readonly version?: string;
|
||||||
readonly previous?: {readonly permalink: string; readonly title: string};
|
readonly previous?: {readonly permalink: string; readonly title: string};
|
||||||
|
|
|
@ -128,6 +128,7 @@ export type OrderMetadata = {
|
||||||
|
|
||||||
export type LastUpdateData = {
|
export type LastUpdateData = {
|
||||||
lastUpdatedAt?: number;
|
lastUpdatedAt?: number;
|
||||||
|
formattedLastUpdatedAt?: string;
|
||||||
lastUpdatedBy?: string;
|
lastUpdatedBy?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,14 @@ function DocItem(props: Props): JSX.Element {
|
||||||
hide_table_of_contents: hideTableOfContents,
|
hide_table_of_contents: hideTableOfContents,
|
||||||
},
|
},
|
||||||
} = DocContent;
|
} = DocContent;
|
||||||
const {description, title, editUrl, lastUpdatedAt, lastUpdatedBy} = metadata;
|
const {
|
||||||
|
description,
|
||||||
|
title,
|
||||||
|
editUrl,
|
||||||
|
lastUpdatedAt,
|
||||||
|
formattedLastUpdatedAt,
|
||||||
|
lastUpdatedBy,
|
||||||
|
} = metadata;
|
||||||
|
|
||||||
const {pluginId} = useActivePlugin({failfast: true});
|
const {pluginId} = useActivePlugin({failfast: true});
|
||||||
const versions = useVersions(pluginId);
|
const versions = useVersions(pluginId);
|
||||||
|
@ -81,6 +88,7 @@ function DocItem(props: Props): JSX.Element {
|
||||||
{(lastUpdatedAt || lastUpdatedBy) && (
|
{(lastUpdatedAt || lastUpdatedBy) && (
|
||||||
<LastUpdated
|
<LastUpdated
|
||||||
lastUpdatedAt={lastUpdatedAt}
|
lastUpdatedAt={lastUpdatedAt}
|
||||||
|
formattedLastUpdatedAt={formattedLastUpdatedAt}
|
||||||
lastUpdatedBy={lastUpdatedBy}
|
lastUpdatedBy={lastUpdatedBy}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -9,21 +9,23 @@ import React from 'react';
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
import Translate from '@docusaurus/Translate';
|
import Translate from '@docusaurus/Translate';
|
||||||
|
|
||||||
function LastUpdatedAtDate({lastUpdatedAt}: {lastUpdatedAt: number}) {
|
function LastUpdatedAtDate({
|
||||||
|
lastUpdatedAt,
|
||||||
|
formattedLastUpdatedAt,
|
||||||
|
}: {
|
||||||
|
lastUpdatedAt: number;
|
||||||
|
formattedLastUpdatedAt: string;
|
||||||
|
}) {
|
||||||
return (
|
return (
|
||||||
<Translate
|
<Translate
|
||||||
id="theme.lastUpdated.atDate"
|
id="theme.lastUpdated.atDate"
|
||||||
description="The words used to describe on which date a page has been last updated"
|
description="The words used to describe on which date a page has been last updated"
|
||||||
values={{
|
values={{
|
||||||
// TODO localize this date
|
|
||||||
// If it's the only place we need this, we'd rather keep it simple
|
|
||||||
// Day.js may be a good lightweight option?
|
|
||||||
// https://www.skypack.dev/blog/2021/02/the-best-javascript-date-libraries/
|
|
||||||
date: (
|
date: (
|
||||||
<time
|
<time
|
||||||
dateTime={new Date(lastUpdatedAt * 1000).toISOString()}
|
dateTime={new Date(lastUpdatedAt * 1000).toISOString()}
|
||||||
className={styles.lastUpdatedDate}>
|
className={styles.lastUpdatedDate}>
|
||||||
{new Date(lastUpdatedAt * 1000).toLocaleDateString()}
|
{formattedLastUpdatedAt}
|
||||||
</time>
|
</time>
|
||||||
),
|
),
|
||||||
}}>
|
}}>
|
||||||
|
@ -47,9 +49,11 @@ function LastUpdatedByUser({lastUpdatedBy}: {lastUpdatedBy: string}) {
|
||||||
|
|
||||||
export default function LastUpdated({
|
export default function LastUpdated({
|
||||||
lastUpdatedAt,
|
lastUpdatedAt,
|
||||||
|
formattedLastUpdatedAt,
|
||||||
lastUpdatedBy,
|
lastUpdatedBy,
|
||||||
}: {
|
}: {
|
||||||
lastUpdatedAt: number | undefined;
|
lastUpdatedAt: number | undefined;
|
||||||
|
formattedLastUpdatedAt: string | undefined;
|
||||||
lastUpdatedBy: string | undefined;
|
lastUpdatedBy: string | undefined;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
|
@ -60,8 +64,12 @@ export default function LastUpdated({
|
||||||
id="theme.lastUpdated.lastUpdatedAtBy"
|
id="theme.lastUpdated.lastUpdatedAtBy"
|
||||||
description="The sentence used to display when a page has been last updated, and by who"
|
description="The sentence used to display when a page has been last updated, and by who"
|
||||||
values={{
|
values={{
|
||||||
atDate: lastUpdatedAt ? (
|
atDate:
|
||||||
<LastUpdatedAtDate lastUpdatedAt={lastUpdatedAt} />
|
lastUpdatedAt && formattedLastUpdatedAt ? (
|
||||||
|
<LastUpdatedAtDate
|
||||||
|
lastUpdatedAt={lastUpdatedAt}
|
||||||
|
formattedLastUpdatedAt={formattedLastUpdatedAt}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
''
|
''
|
||||||
),
|
),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue