fix: no 'uncategorized' category in categories page

This commit is contained in:
grassblock 2025-05-29 20:56:40 +08:00
parent 6e89919cd4
commit b103c045e2
3 changed files with 12 additions and 7 deletions

View file

@ -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;
</p>
)}
</ul>
</Layout>
</Layout>