Files
blazestar.net/src/lib/blog.ts
2026-05-11 14:41:05 -07:00

27 lines
793 B
TypeScript

import { type CollectionEntry, getCollection } from "astro:content";
function publishedOnly(
p: CollectionEntry<"blog">,
): p is CollectionEntry<"blog"> & { data: { pubDate: Date } } {
return p.data.pubDate !== undefined;
}
export async function getPublishedPosts(
tag?: string,
): Promise<CollectionEntry<"blog">[]> {
const allPosts = (await getCollection("blog", publishedOnly)).sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
);
return tag ? allPosts.filter((p) => p.data.tags?.includes(tag)) : allPosts;
}
export async function getAllTags(): Promise<string[]> {
const posts = await getCollection("blog", publishedOnly);
return Object.keys(
Object.fromEntries(
posts.flatMap((p) => p.data.tags || []).map((t) => [t, 1]),
),
);
}