mirror of
https://github.com/KazooTTT/kazoottt-blog-v2.git
synced 2025-06-20 01:11:20 +08:00
feat(content): 支持内容集合中可选的日期字段
This commit is contained in:
@ -34,7 +34,10 @@ const post = defineCollection({
|
|||||||
draft: z.boolean().default(false),
|
draft: z.boolean().default(false),
|
||||||
banner: z.string().optional(),
|
banner: z.string().optional(),
|
||||||
tags: z.array(z.string()).default([]).transform(removeDupsAndLowerCase),
|
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
|
date_modified: z
|
||||||
.union([z.string(), z.number(), z.date()])
|
.union([z.string(), z.number(), z.date()])
|
||||||
.optional()
|
.optional()
|
||||||
@ -52,7 +55,10 @@ const note = defineCollection({
|
|||||||
loader: glob({ base: "./src/content/note", pattern: "**/*.{md,mdx}" }),
|
loader: glob({ base: "./src/content/note", pattern: "**/*.{md,mdx}" }),
|
||||||
schema: baseSchema.extend({
|
schema: baseSchema.extend({
|
||||||
description: z.string().optional().nullable(),
|
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
|
date_modified: z
|
||||||
.union([z.string(), z.number(), z.date()])
|
.union([z.string(), z.number(), z.date()])
|
||||||
.optional()
|
.optional()
|
||||||
|
@ -19,25 +19,12 @@ export function collectionDateSort(
|
|||||||
a: CollectionEntry<"post" | "note">,
|
a: CollectionEntry<"post" | "note">,
|
||||||
b: CollectionEntry<"post" | "note">,
|
b: CollectionEntry<"post" | "note">,
|
||||||
) {
|
) {
|
||||||
return b.data.date.getTime() - a.data.date.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;
|
||||||
const datePriorityForNote = ["date", "date_modified", "data_created"];
|
if (entry.data.date_modified) return entry.data.date_modified;
|
||||||
|
return new Date();
|
||||||
export function collectionModifiedDateSort(
|
};
|
||||||
a: CollectionEntry<"post" | "note">,
|
|
||||||
b: CollectionEntry<"post" | "note">,
|
return getDate(b).getTime() - getDate(a).getTime();
|
||||||
) {
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user