Skip to content

Commit 7ed04be

Browse files
committed
Refactor tag page to use getCollection for improved data handling and update post properties for consistency
1 parent 913c07f commit 7ed04be

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/pages/tags/[tag].astro

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
---
22
import BaseLayout from "../../layouts/BaseLayout.astro";
33
import BlogPostListItem from "../../components/BlogPostListItem.astro";
4+
import { getCollection } from "astro:content";
45
56
export async function getStaticPaths() {
6-
const allPosts = Object.values(
7-
import.meta.glob("../../data/blog/*.md", { eager: true }),
8-
);
9-
10-
const uniqueTags = [
11-
...new Set(allPosts.map((post: any) => post.frontmatter.tags).flat()),
12-
];
13-
7+
const allPosts = await getCollection("blog");
8+
9+
// Get all unique tags
10+
const uniqueTags = [...new Set(allPosts.flatMap(post => post.data.tags))];
11+
1412
return uniqueTags.map((tag) => {
15-
const filteredPosts = allPosts.filter((post: any) =>
16-
post.frontmatter.tags.includes(tag),
13+
const filteredPosts = allPosts.filter((post) =>
14+
post.data.tags.includes(tag)
1715
);
16+
1817
return {
1918
params: { tag },
2019
props: { posts: filteredPosts },
@@ -30,8 +29,12 @@ const { posts } = Astro.props;
3029
<p>Posts tagged with {tag}</p>
3130
<ul>
3231
{
33-
posts.map((post: any) => (
34-
<BlogPostListItem url={post.url} title={post.frontmatter.title} />
32+
posts.map((post) => (
33+
<BlogPostListItem
34+
url={`/posts/${post.data.slug}`}
35+
title={post.data.title}
36+
datePublished={post.data.formattedDate}
37+
/>
3538
))
3639
}
3740
</ul>

0 commit comments

Comments
 (0)