From 0fc8c6cb80e4446b6805afe909476311f161b876 Mon Sep 17 00:00:00 2001 From: KazooTTT Date: Tue, 4 Mar 2025 14:08:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(content):=20=E6=94=AF=E6=8C=81=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E9=9B=86=E5=90=88=E4=B8=AD=E5=8F=AF=E9=80=89=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/content.config.ts | 10 ++++++++-- src/utils/date.ts | 29 ++++++++--------------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/content.config.ts b/src/content.config.ts index ac16dee..04a51d1 100644 --- a/src/content.config.ts +++ b/src/content.config.ts @@ -34,7 +34,10 @@ const post = defineCollection({ draft: z.boolean().default(false), banner: z.string().optional(), tags: z.array(z.string()).default([]).transform(removeDupsAndLowerCase), - date: z.union([z.string(), z.number(), z.date()]).transform(processDate), + date: z + .union([z.string(), z.number(), z.date()]) + .optional() + .transform((val) => (val ? processDate(val) : undefined)), date_modified: z .union([z.string(), z.number(), z.date()]) .optional() @@ -52,7 +55,10 @@ const note = defineCollection({ loader: glob({ base: "./src/content/note", pattern: "**/*.{md,mdx}" }), schema: baseSchema.extend({ description: z.string().optional().nullable(), - date: z.union([z.string(), z.number(), z.date()]).transform(processDate), + date: z + .union([z.string(), z.number(), z.date()]) + .optional() + .transform((val) => (val ? processDate(val) : undefined)), date_modified: z .union([z.string(), z.number(), z.date()]) .optional() diff --git a/src/utils/date.ts b/src/utils/date.ts index b7daec6..0961abf 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -19,25 +19,12 @@ export function collectionDateSort( a: CollectionEntry<"post" | "note">, b: CollectionEntry<"post" | "note">, ) { - return b.data.date.getTime() - a.data.date.getTime(); -} - -const datePriorityForNote = ["date", "date_modified", "data_created"]; - -export function collectionModifiedDateSort( - a: CollectionEntry<"post" | "note">, - b: CollectionEntry<"post" | "note">, -) { - let dateA: Date = new Date(), - dateB: Date = new Date(); - datePriorityForNote.forEach((key) => { - if (a.data[key as keyof typeof a.data]) { - dateA = a.data[key as keyof typeof a.data] as Date; - } - if (b.data[key as keyof typeof b.data]) { - dateB = b.data[key as keyof typeof b.data] as Date; - } - }); - - return dateB.getTime() - dateA.getTime(); + const getDate = (entry: CollectionEntry<"post" | "note">): Date => { + if (entry.data.date) return entry.data.date; + if (entry.data.data_created) return entry.data.data_created; + if (entry.data.date_modified) return entry.data.date_modified; + return new Date(); + }; + + return getDate(b).getTime() - getDate(a).getTime(); }