mirror of
https://github.com/KazooTTT/kazoottt-blog-v2.git
synced 2025-06-16 23:41:20 +08:00
feat(content): 支持内容集合中可选的日期字段
This commit is contained in:
@ -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()
|
||||
|
@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user