--- import { getCollection } from "astro:content"; import BaseLayout from "@/layouts/BaseLayout.astro"; const allPosts = await getCollection("posts", ({ data }) => !data.draft); const sorted = allPosts.sort( (a, b) => b.data.date.getTime() - a.data.date.getTime() ); const categories = [ ...new Set(sorted.map((p) => p.data.category).filter(Boolean)), ].sort(); const fmtDate = (d: Date) => d.toLocaleDateString("en-US", { year: "numeric", month: "short", day: "numeric", }); const readTime = (body?: string) => { const words = body?.split(/\s+/).length ?? 0; return `~${Math.max(1, Math.round(words / 220))} mins`; }; const excerpt = (body?: string, len = 140) => { if (!body) return ""; const plain = body .replace(/^---[\s\S]*?---/, "") .replace(/[#*_`>\[\]()!|~\-]/g, "") .replace(/\n+/g, " ") .trim(); return plain.length > len ? plain.slice(0, len).trimEnd() + " …" : plain; }; --- All {categories.map((cat) => ( {cat} ))} {sorted.map((post) => ( {post.data.image ? ( ) : ( {post.data.title.charAt(0)} )} {post.data.category} {fmtDate(post.data.date)} {readTime(post.body)} {post.data.title} {post.data.description ? ( {post.data.description} ) : ( {excerpt(post.body)} )} ))}
{post.data.description}
{excerpt(post.body)}