feat: 允许tags不存在

This commit is contained in:
KazooTTT
2024-10-16 10:45:32 +08:00
parent a8ded02f4a
commit 5b6bd29c15
4 changed files with 4 additions and 4 deletions

View File

@ -76,7 +76,7 @@ const dateTimeOptions: Intl.DateTimeFormatOptions = {
<path d='M6 9h-.01' /> <path d='M6 9h-.01' />
</svg> </svg>
<div class='space-x-1'> <div class='space-x-1'>
{data.tags.map((tag) => ( {data.tags?.map((tag) => (
<a <a
aria-label={`View more blogs with the tag ${tag}`} aria-label={`View more blogs with the tag ${tag}`}
class="inline-block before:content-['#'] hover:underline hover:underline-offset-4" class="inline-block before:content-['#'] hover:underline hover:underline-offset-4"

View File

@ -19,7 +19,7 @@ const post = defineCollection({
.transform((val) => new Date(val)), .transform((val) => new Date(val)),
coverImage: z.string().optional(), coverImage: z.string().optional(),
draft: z.boolean().default(false), draft: z.boolean().default(false),
tags: z.array(z.string()).default([]).transform(removeDupsAndLowerCase), tags: z.array(z.string()).optional().default([]).transform(removeDupsAndLowerCase),
ogImage: z.string().optional(), ogImage: z.string().optional(),
category: z.string().optional().nullable(), category: z.string().optional().nullable(),
finished: z.boolean().default(false) finished: z.boolean().default(false)

View File

@ -16,7 +16,7 @@ export const getStaticPaths: GetStaticPaths = async ({ paginate }) => {
const uniqueTags = getUniqueTags(allPostsByDate) const uniqueTags = getUniqueTags(allPostsByDate)
return uniqueTags.flatMap((tag) => { return uniqueTags.flatMap((tag) => {
const filterPosts = allPostsByDate.filter((post) => post.data.tags.includes(tag)) const filterPosts = allPostsByDate.filter((post) => post.data.tags?.includes(tag))
return paginate(filterPosts, { return paginate(filterPosts, {
pageSize: 50, pageSize: 50,
params: { tag } params: { tag }

View File

@ -18,7 +18,7 @@ export function sortMDByDate(posts: Array<CollectionEntry<'post'>>) {
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */ /** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
export function getAllTags(posts: Array<CollectionEntry<'post'>>) { export function getAllTags(posts: Array<CollectionEntry<'post'>>) {
return posts.flatMap((post) => [...post.data.tags]) return posts.flatMap((post) => [...(post.data?.tags ?? [])])
} }
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */ /** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */