diff --git a/src/pages/posts/[slug].astro b/src/pages/posts/[id].astro similarity index 54% rename from src/pages/posts/[slug].astro rename to src/pages/posts/[id].astro index 4a347c7..c7e4dea 100644 --- a/src/pages/posts/[slug].astro +++ b/src/pages/posts/[id].astro @@ -1,24 +1,29 @@ --- -import { getCollection } from "astro:content"; +import { getCollection, render, type CollectionEntry, type RenderResult } from "astro:content"; import Layout from "../../layouts/Layout.astro"; import BlogPost from "../../components/BlogPost.astro"; -import { render } from "astro:content"; -import type { CollectionEntry } from "astro:content"; - -interface Props { - post: CollectionEntry<"blog">; -} export async function getStaticPaths() { const posts = await getCollection("blog"); - return posts.map((post) => ({ - params: { slug: post.id }, - props: { post }, - })); + const paths = await Promise.all( + posts.map(async (post) => { + const id = post.id; + const content = await render(post); + return { + params: { id }, + props: { post, content }, + }; + }) + ); + return paths; } -const { post } = Astro.props; -const renderedPost = await render(post); +interface Props { + post: CollectionEntry<"blog">; + content: RenderResult; +} + +const { post, content } = Astro.props; --- - + diff --git a/src/pages/tags/[name].astro b/src/pages/tags/[name].astro new file mode 100644 index 0000000..7d72788 --- /dev/null +++ b/src/pages/tags/[name].astro @@ -0,0 +1,59 @@ +--- +import { getCollection, type CollectionEntry } from "astro:content"; +import BlogPostCard from "../../components/BlogPostCard.astro"; +import Layout from "../../layouts/Layout.astro"; + +export async function getStaticPaths() { + const posts = await getCollection("blog"); + const tags = new Set(); + + posts.forEach((post) => { + if (post.data.tags) { + post.data.tags.forEach((tag: string) => tags.add(tag)); + } + }); + + return Array.from(tags).map((name) => { + const filteredPosts = posts.filter( + (post) => post.data.tags && post.data.tags.includes(name), + ); + return { + params: { name }, + props: { filteredPosts }, + }; + }); +} + +interface Props { + filteredPosts: CollectionEntry<"blog">[]; +} + +const { name } = Astro.params; +const { filteredPosts } = Astro.props; +--- + + +

Posts tagged with "{name}"

+ { + filteredPosts.length > 0 ? ( + + ) : ( +

No posts found with the tag "{name}".

+ ) + } +