barrettruth.com/src/pages/[category]/[slug].astro
2025-11-09 14:59:59 -05:00

49 lines
1.3 KiB
Text

---
import { getCollection } from "astro:content";
import PostLayout from "../../layouts/PostLayout.astro";
import * as collections from "../../content/config";
import type { PostCollection, AnyCollectionEntry } from "../../types";
export async function getStaticPaths() {
const categories = Object.keys(collections.collections).filter(
(c) => c !== "git" && c !== "gists",
) as PostCollection[];
const entries: Array<{
params: { category: string; slug: string };
props: { post: AnyCollectionEntry };
}> = [];
for (const category of categories) {
const docs = await getCollection(category);
for (const doc of docs) {
entries.push({
params: { category, slug: doc.slug },
props: { post: doc },
});
}
}
return entries;
}
interface Props {
post: AnyCollectionEntry;
}
const { post } = Astro.props;
const category = Astro.params.category;
const { Content } = await post.render();
const pageTitle = `${category}/${post.data.title ?? post.slug}`;
if (post.data?.redirect) {
return Astro.redirect(post.data.redirect, 301);
}
---
<PostLayout frontmatter={post.data} post={post}>
<Fragment slot="head">
<title>{pageTitle}</title>
<script src="/scripts/index.js" is:inline></script>
</Fragment>
<Content />
</PostLayout>