Skip to content

Commit

Permalink
feat: display tags on blog page
Browse files Browse the repository at this point in the history
  • Loading branch information
milan-codes committed Nov 2, 2024
1 parent c39f863 commit bb28c3a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/Navbar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ArrowUpRight from "./ArrowUpRight.astro";
<a class="py-2 hover:underline" href="/"> Home </a>
</li>
<li>
<a class="py-2 hover:underline" href="/blog"> Posts </a>
<a class="py-2 hover:underline" href="/blog"> Blog </a>
</li>
<li>
<a
Expand Down
13 changes: 7 additions & 6 deletions src/components/Tags.astro
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
---
interface Props {
tags: string[];
}
const { tags } = Astro.props;
---

<div>
<h1 class="my-8 mb-2 tracking-wider text-gray-900 dark:text-gray-100">
Tags
</h1>
<ul class="flex flex-wrap">
<ul class="mb-6 flex flex-wrap">
{
tags.map((tag: unknown) => (
<li class="mr-3 text-sm font-medium text-gray-400 hover:underline dark:text-gray-600">
<a href={`/tags/${tag}`}>{`#${tag}`}</a>
<li class="mr-3 text-sm font-medium text-gray-600 hover:underline dark:text-gray-400">
<a href={`/tags/${tag}`}>{tag}</a>
</li>
))
}
Expand Down
4 changes: 4 additions & 0 deletions src/pages/blog/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import { getCollection } from "astro:content";
import BlogPostCard from "../../components/BlogPostCard.astro";
import Navbar from "../../components/Navbar.astro";
import BaseLayout from "../../layouts/BaseLayout.astro";
import Tags from "../../components/Tags.astro";
const posts = await getCollection("blog", ({ data }) => data.draft !== true);
const orderedPosts = posts.sort((a, b) => {
const aDate = new Date(a.data.pubDate);
const bDate = new Date(b.data.pubDate);
return bDate.valueOf() - aDate.valueOf();
});
const tags = [...new Set(posts.map((post) => post.data.tags).flat())];
---

<BaseLayout title="Milán Herke - Blog">
<Navbar />
<h1 class="mb-1 mt-4 lowercase text-gray-900 dark:text-gray-100">Posts</h1>
<Tags tags={tags} />
{orderedPosts.map((post) => <BlogPostCard post={post} />)}
</BaseLayout>
4 changes: 2 additions & 2 deletions src/pages/tags/[tag].astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const allPosts = await getCollection(

<BaseLayout title="Milán Herke - Blog">
<Navbar />
<h1 class="my-8 mb-2 tracking-wider text-gray-900 dark:text-gray-100">
Posts with {tag} tag
<h1 class="mb-6 mt-4 lowercase text-gray-900 dark:text-gray-100">
Posts tagged with {tag}
</h1>
{allPosts.map((post) => <BlogPostCard post={post} />)}
</BaseLayout>

0 comments on commit bb28c3a

Please sign in to comment.