feat(theme): allow customizing last updated date time format options (#2332)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/2566/head
Mateu Llull 2 years ago committed by GitHub
parent e95015f598
commit 24abc7c6bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -292,21 +292,41 @@ export interface EditLink {
} }
``` ```
## lastUpdatedText ## lastUpdated
- Type: `string` - Type: `LastUpdatedOptions`
- Default: `Last updated`
The prefix text showing right before the last updated time. Allows customization for the last updated text and date format.
```ts ```ts
export default { export default {
themeConfig: { themeConfig: {
lastUpdatedText: 'Updated Date' lastUpdated: {
text: 'Updated at',
formatOptions: {
dateStyle: 'full',
timeStyle: 'medium'
}
}
} }
} }
``` ```
```ts
export interface LastUpdatedOptions {
/**
* @default 'Last updated'
*/
text?: string
/**
* @default
* { dateStyle: 'short', timeStyle: 'short' }
*/
formatOptions?: Intl.DateTimeFormatOptions
}
```
## algolia ## algolia
- Type: `AlgoliaSearch` - Type: `AlgoliaSearch`

@ -20,3 +20,4 @@ lastUpdated: false
--- ---
``` ```
Also refer [Default Theme: Last Updated](./default-theme-last-updated#last-updated) for more details. Any truthy value at theme-level will also enable the feature unless explicitly disabled at site or page level.

@ -2,7 +2,7 @@
import { ref, computed, watchEffect, onMounted } from 'vue' import { ref, computed, watchEffect, onMounted } from 'vue'
import { useData } from '../composables/data' import { useData } from '../composables/data'
const { theme, page, lang } = useData() const { theme, page } = useData()
const date = computed(() => new Date(page.value.lastUpdated!)) const date = computed(() => new Date(page.value.lastUpdated!))
const isoDatetime = computed(() => date.value.toISOString()) const isoDatetime = computed(() => date.value.toISOString())
@ -12,14 +12,20 @@ const datetime = ref('')
// potential differences in timezones of the server and clients // potential differences in timezones of the server and clients
onMounted(() => { onMounted(() => {
watchEffect(() => { watchEffect(() => {
datetime.value = date.value.toLocaleString(lang.value) datetime.value = new Intl.DateTimeFormat(
undefined,
theme.value.lastUpdated?.formatOptions ?? {
dateStyle: 'short',
timeStyle: 'short'
}
).format(date.value)
}) })
}) })
</script> </script>
<template> <template>
<p class="VPLastUpdated"> <p class="VPLastUpdated">
{{ theme.lastUpdatedText || 'Last updated' }}: {{ theme.lastUpdated?.text || theme.lastUpdatedText || 'Last updated' }}:
<time :datetime="isoDatetime">{{ datetime }}</time> <time :datetime="isoDatetime">{{ datetime }}</time>
</p> </p>
</template> </template>

@ -105,7 +105,8 @@ export async function resolveConfig(
logger, logger,
tempDir: resolve(root, '.temp'), tempDir: resolve(root, '.temp'),
markdown: userConfig.markdown, markdown: userConfig.markdown,
lastUpdated: userConfig.lastUpdated, lastUpdated:
userConfig.lastUpdated ?? !!userConfig.themeConfig?.lastUpdated,
vue: userConfig.vue, vue: userConfig.vue,
vite: userConfig.vite, vite: userConfig.vite,
shouldPreload: userConfig.shouldPreload, shouldPreload: userConfig.shouldPreload,

@ -25,8 +25,7 @@ export namespace DefaultTheme {
outline?: Outline | Outline['level'] | false outline?: Outline | Outline['level'] | false
/** /**
* @deprecated * @deprecated Use `outline.label` instead.
* Use `outline.label` instead.
* *
* @default 'On this page' * @default 'On this page'
*/ */
@ -58,12 +57,16 @@ export namespace DefaultTheme {
editLink?: EditLink editLink?: EditLink
/** /**
* @deprecated Use `lastUpdated.text` instead.
*
* Set custom last updated text. * Set custom last updated text.
* *
* @default 'Last updated' * @default 'Last updated'
*/ */
lastUpdatedText?: string lastUpdatedText?: string
lastUpdated?: LastUpdatedOptions
/** /**
* Set custom prev/next labels. * Set custom prev/next labels.
*/ */
@ -349,4 +352,24 @@ export namespace DefaultTheme {
code: string code: string
placement: string placement: string
} }
// last updated --------------------------------------------------------------
export interface LastUpdatedOptions {
/**
* Set custom last updated text.
*
* @default 'Last updated'
*/
text?: string
/**
* Set options for last updated time formatting.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options
*
* @default
* { dateStyle: 'short', timeStyle: 'short' }
*/
formatOptions?: Intl.DateTimeFormatOptions
}
} }

Loading…
Cancel
Save