From 8ac9b865f5bc2fe95dacb0b28f9c778604c59bc4 Mon Sep 17 00:00:00 2001 From: radishzzz Date: Tue, 11 Mar 2025 15:33:39 +0000 Subject: [PATCH] feat: add date formatting component and configuration options - Add DateFormat and PostTime components, supporting multiple date display formats - Add dateFormat option in theme configuration, allowing customization of date display styles - Refactor time display logic in article list and article detail pages - Update configuration type definitions to support new date format options --- src/components/DateFormat.astro | 78 +++++++++++++++++++++++++++++ src/components/PostList.astro | 27 +++++----- src/components/PostTime.astro | 78 +++++++++++++++++++++++++++++ src/config.ts | 10 ++-- src/pages/[lang]/posts/[slug].astro | 2 +- src/pages/posts/[slug].astro | 18 +++---- src/types/index.d.ts | 1 + 7 files changed, 184 insertions(+), 30 deletions(-) create mode 100644 src/components/DateFormat.astro create mode 100644 src/components/PostTime.astro diff --git a/src/components/DateFormat.astro b/src/components/DateFormat.astro new file mode 100644 index 0000000..ae6590d --- /dev/null +++ b/src/components/DateFormat.astro @@ -0,0 +1,78 @@ +--- +import { themeConfig } from '@/config' +import { isPostPage } from '@/utils/path' + +interface Props { + date: Date + updatedDate?: Date + minutes?: number +} + +const { date, updatedDate, minutes } = Astro.props +const format = themeConfig.global.dateFormat +const currentPath = Astro.url.pathname +const isPost = isPostPage(currentPath) +const updatedTimeMarginClass = isPost ? 'ml-1.75' : 'ml-1.5' +const readingTimeMarginClass = isPost ? 'ml-1.75' : 'ml-1.5' + +function formatDate(date: Date, format: 'YYYY-MM-DD' | 'MM-DD-YYYY' | 'DD-MM-YYYY' | 'MONTH DAY YYYY' | 'DAY MONTH YYYY') { + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + month: format === 'MONTH DAY YYYY' || format === 'DAY MONTH YYYY' ? 'short' : '2-digit', + day: format === 'MONTH DAY YYYY' || format === 'DAY MONTH YYYY' ? 'numeric' : '2-digit', + } + + switch (format) { + // ISO format: 2024-03-04 + case 'YYYY-MM-DD': + return date.toISOString().split('T')[0] + + // US date format: 03-04-2024 + case 'MM-DD-YYYY': + return date.toLocaleDateString('en-US', options).replace(/\//g, '-') + + // European date format: 04-03-2024 + case 'DD-MM-YYYY': + return date.toLocaleDateString('en-GB', options).replace(/\//g, '-') + + // US month text format: Mar 4 2024 + case 'MONTH DAY YYYY': + return date.toLocaleDateString('en-US', { + year: 'numeric', + month: 'short', + day: 'numeric', + }).replace(',', '') + + // British month text format: 4 Mar 2024 + case 'DAY MONTH YYYY': + return date.toLocaleDateString('en-GB', { + year: 'numeric', + month: 'short', + day: 'numeric', + }).replace(',', '') + + // Default to ISO format + default: + return date.toISOString().split('T')[0] + } +} +--- + + + + + +{updatedDate && ( + +)} + + +{minutes !== undefined && ( + + {minutes} min + +)} diff --git a/src/components/PostList.astro b/src/components/PostList.astro index 0f0488b..a643282 100644 --- a/src/components/PostList.astro +++ b/src/components/PostList.astro @@ -1,5 +1,6 @@ --- -// Define props received by the component +import PostTime from '@/components/PostTime.astro' + interface Post { data: { title: string @@ -31,9 +32,9 @@ function getPostUrl(post: Post) { ---