refactor: dynamic tag lists

This commit is contained in:
Ruidy 2024-12-31 14:24:48 +01:00
parent 5c62e71a64
commit f06ae6b055
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

View file

@ -6,28 +6,26 @@ export async function getStaticPaths() {
const posts = Object.values( const posts = Object.values(
import.meta.glob("../posts/*.md", { eager: true }), import.meta.glob("../posts/*.md", { eager: true }),
); );
return [ const tags = [
{ params: { tag: "astro" }, props: { posts } }, ...new Set(posts.flatMap((post: any) => post.frontmatter.tags)),
{ 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 } },
]; ];
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 { tag } = Astro.params;
const { posts } = Astro.props; const { posts } = Astro.props;
const filteredPosts = posts.filter((post: any) =>
post.frontmatter.tags?.includes(tag),
);
--- ---
<BaseLayout pageTitle={`Posts tagged with ${tag}`}> <BaseLayout pageTitle={`Posts tagged with ${tag}`}>
<p>Posts tagged with {tag}</p> <p>Posts tagged with {tag}</p>
<ul> <ul>
{ {
filteredPosts.map((post: any) => ( posts.map((post: any) => (
<BLogPost url={post.url} title={post.frontmatter.title} /> <BLogPost url={post.url} title={post.frontmatter.title} />
)) ))
} }