From b103c045e2d0cf647e516a6422ca848c320cc505 Mon Sep 17 00:00:00 2001 From: grassblock Date: Thu, 29 May 2025 20:56:40 +0800 Subject: [PATCH] fix: no 'uncategorized' category in categories page --- src/content/posts/_schemas.ts | 4 ++-- src/content/posts/minimalism/index.md | 2 ++ src/pages/categories/[...category].astro | 13 ++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/content/posts/_schemas.ts b/src/content/posts/_schemas.ts index 15806c1..2a535ff 100644 --- a/src/content/posts/_schemas.ts +++ b/src/content/posts/_schemas.ts @@ -5,8 +5,8 @@ export const posts = ({ image }) => z.object({ description: z.string(), pubDate: z.coerce.date(), updatedDate: z.coerce.date().optional(), - categories: z.array(z.string()).optional().default(['uncategorized']), - tags: z.array(z.string()).optional().default([]), + categories: z.array(z.string()).default(['uncategorized']), + tags: z.array(z.string()).optional(), cover: image().optional(), author: z.string().optional(), }); \ No newline at end of file diff --git a/src/content/posts/minimalism/index.md b/src/content/posts/minimalism/index.md index 24c9388..a580155 100644 --- a/src/content/posts/minimalism/index.md +++ b/src/content/posts/minimalism/index.md @@ -7,6 +7,8 @@ tags: - 'design' - 'code' - 'minimalism' +categories: + - 'thinking' --- Minimalism isn't just about having less, it's about making room for what matters. diff --git a/src/pages/categories/[...category].astro b/src/pages/categories/[...category].astro index 4270589..e8b4c1a 100644 --- a/src/pages/categories/[...category].astro +++ b/src/pages/categories/[...category].astro @@ -1,14 +1,16 @@ --- import Layout from '../../layouts/Layout.astro'; import {getCollection} from "astro:content"; -import {categoryLabel} from "astro/client/dev-toolbar/apps/audit/rules"; export async function getStaticPaths() { const allPosts = await getCollection('posts'); - console.log(allPosts) - const uniqueCategories = [...new Set(allPosts.map((post: any) => post.data.categories ? post.data.categories : []).flat())]; + // Get all categories, ensuring the default 'uncategorized' is used when categories is undefined + const uniqueCategories = [...new Set(allPosts.map((post: any) => + post.data.categories || ['uncategorized']).flat())]; + return uniqueCategories.map((category) => { - const filteredPosts = allPosts.filter((post: any) => post.data.categories?.includes(category)); + const filteredPosts = allPosts.filter((post: any) => + (post.data.categories || ['uncategorized']).includes(category)); return { params: { category }, props: { posts: filteredPosts }, @@ -30,4 +32,5 @@ const { posts } = Astro.props;

)} - \ No newline at end of file + +