fix: filter out draft posts in pagination and tag collection

This commit is contained in:
fiatcode 2026-01-22 18:46:14 +07:00
parent 6b831e7100
commit 9862f3fa24
4 changed files with 8 additions and 5 deletions

View file

@ -13,8 +13,9 @@ export async function getStaticPaths({
paginate: PaginateFunction; paginate: PaginateFunction;
}) { }) {
const posts = await getCollection("blog"); const posts = await getCollection("blog");
posts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); const filteredPosts = posts.filter((post) => !post.data.draft);
return paginate(posts, { pageSize: 5 }); filteredPosts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
return paginate(filteredPosts, { pageSize: 10 });
} }
interface Props { interface Props {

View file

@ -10,8 +10,9 @@ import BlogPost from "@/components/BlogPost.astro";
export async function getStaticPaths() { export async function getStaticPaths() {
const posts = await getCollection("blog"); const posts = await getCollection("blog");
const filteredPosts = posts.filter((post) => !post.data.draft);
const paths = await Promise.all( const paths = await Promise.all(
posts.map(async (post) => { filteredPosts.map(async (post) => {
const id = post.id; const id = post.id;
const content = await render(post); const content = await render(post);
return { return {

View file

@ -10,7 +10,7 @@ export async function getStaticPaths() {
const tags = new Set<string>(); const tags = new Set<string>();
posts.forEach((post) => { posts.forEach((post) => {
if (post.data.tags) { if (post.data.tags && !post.data.draft) {
post.data.tags.forEach((tag: string) => tags.add(tag)); post.data.tags.forEach((tag: string) => tags.add(tag));
} }
}); });

View file

@ -5,8 +5,9 @@ import PageTitle from "@/components/PageTitle.astro";
import Tag from "@/components/Tag.astro"; import Tag from "@/components/Tag.astro";
const posts = await getCollection("blog"); const posts = await getCollection("blog");
const filteredPosts = posts.filter((post) => !post.data.draft);
const tagsMap = new Map<string, number>(); const tagsMap = new Map<string, number>();
posts.forEach((post) => { filteredPosts.forEach((post) => {
post.data.tags?.forEach((tag) => { post.data.tags?.forEach((tag) => {
const currentCount = tagsMap.get(tag) || 0; const currentCount = tagsMap.get(tag) || 0;
tagsMap.set(tag, currentCount + 1); tagsMap.set(tag, currentCount + 1);