From 9862f3fa243f776fd9261b2c90bbc72d47f3c9cf Mon Sep 17 00:00:00 2001 From: fiatcode Date: Thu, 22 Jan 2026 18:46:14 +0700 Subject: [PATCH] fix: filter out draft posts in pagination and tag collection --- src/pages/posts/[...page].astro | 5 +++-- src/pages/posts/[id].astro | 3 ++- src/pages/tags/[name].astro | 2 +- src/pages/tags/index.astro | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/pages/posts/[...page].astro b/src/pages/posts/[...page].astro index 2d15724..8f21c29 100644 --- a/src/pages/posts/[...page].astro +++ b/src/pages/posts/[...page].astro @@ -13,8 +13,9 @@ export async function getStaticPaths({ paginate: PaginateFunction; }) { const posts = await getCollection("blog"); - posts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); - return paginate(posts, { pageSize: 5 }); + const filteredPosts = posts.filter((post) => !post.data.draft); + filteredPosts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); + return paginate(filteredPosts, { pageSize: 10 }); } interface Props { diff --git a/src/pages/posts/[id].astro b/src/pages/posts/[id].astro index b7c08d8..d7c6992 100644 --- a/src/pages/posts/[id].astro +++ b/src/pages/posts/[id].astro @@ -10,8 +10,9 @@ import BlogPost from "@/components/BlogPost.astro"; export async function getStaticPaths() { const posts = await getCollection("blog"); + const filteredPosts = posts.filter((post) => !post.data.draft); const paths = await Promise.all( - posts.map(async (post) => { + filteredPosts.map(async (post) => { const id = post.id; const content = await render(post); return { diff --git a/src/pages/tags/[name].astro b/src/pages/tags/[name].astro index 0c2d7c9..b984c20 100644 --- a/src/pages/tags/[name].astro +++ b/src/pages/tags/[name].astro @@ -10,7 +10,7 @@ export async function getStaticPaths() { const tags = new Set(); posts.forEach((post) => { - if (post.data.tags) { + if (post.data.tags && !post.data.draft) { post.data.tags.forEach((tag: string) => tags.add(tag)); } }); diff --git a/src/pages/tags/index.astro b/src/pages/tags/index.astro index 90bbdfa..f575b11 100644 --- a/src/pages/tags/index.astro +++ b/src/pages/tags/index.astro @@ -5,8 +5,9 @@ import PageTitle from "@/components/PageTitle.astro"; import Tag from "@/components/Tag.astro"; const posts = await getCollection("blog"); +const filteredPosts = posts.filter((post) => !post.data.draft); const tagsMap = new Map(); -posts.forEach((post) => { +filteredPosts.forEach((post) => { post.data.tags?.forEach((tag) => { const currentCount = tagsMap.get(tag) || 0; tagsMap.set(tag, currentCount + 1);