From f06ae6b05541d34914fdc5ff2a65194cf6a4978d Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 31 Dec 2024 14:24:48 +0100 Subject: [PATCH] refactor: dynamic tag lists --- src/pages/tags/[tag].astro | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/pages/tags/[tag].astro b/src/pages/tags/[tag].astro index b5b744c..5a23c0e 100644 --- a/src/pages/tags/[tag].astro +++ b/src/pages/tags/[tag].astro @@ -6,28 +6,26 @@ export async function getStaticPaths() { const posts = Object.values( import.meta.glob("../posts/*.md", { eager: true }), ); - return [ - { params: { tag: "astro" }, props: { posts } }, - { params: { tag: "successes" }, props: { posts } }, - { params: { tag: "community" }, props: { posts } }, - { params: { tag: "blogging" }, props: { posts } }, - { params: { tag: "setbacks" }, props: { posts } }, - { params: { tag: "learning in public" }, props: { posts } }, + const tags = [ + ...new Set(posts.flatMap((post: any) => post.frontmatter.tags)), ]; + return tags.map((tag: any) => { + const filteredPosts = posts.filter((post: any) => + post.frontmatter.tags?.includes(tag), + ); + return { params: { tag }, props: { posts: filteredPosts } }; + }); } const { tag } = Astro.params; const { posts } = Astro.props; -const filteredPosts = posts.filter((post: any) => - post.frontmatter.tags?.includes(tag), -); ---

Posts tagged with {tag}