From 7c5962485c712a6693a7a9247faeb5acd03ba731 Mon Sep 17 00:00:00 2001 From: gbanyan Date: Mon, 17 Nov 2025 18:40:27 +0800 Subject: [PATCH] Link tags to tag pages and add tag overview --- app/blog/[slug]/page.tsx | 8 +++--- app/tags/[tag]/page.tsx | 49 +++++++++++++++++++++++++++++++++++ components/post-list-item.tsx | 7 ++--- components/right-sidebar.tsx | 7 ++--- 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 app/tags/[tag]/page.tsx diff --git a/app/blog/[slug]/page.tsx b/app/blog/[slug]/page.tsx index beb1d9c..d815752 100644 --- a/app/blog/[slug]/page.tsx +++ b/app/blog/[slug]/page.tsx @@ -1,3 +1,4 @@ +import Link from 'next/link'; import { notFound } from 'next/navigation'; import type { Metadata } from 'next'; import { allPosts } from 'contentlayer/generated'; @@ -55,12 +56,13 @@ export default function BlogPostPage({ params }: Props) { {post.tags && (
{post.tags.map((t) => ( - #{t} - + ))}
)} diff --git a/app/tags/[tag]/page.tsx b/app/tags/[tag]/page.tsx new file mode 100644 index 0000000..cccbc5b --- /dev/null +++ b/app/tags/[tag]/page.tsx @@ -0,0 +1,49 @@ +import type { Metadata } from 'next'; +import { allPosts } from 'contentlayer/generated'; +import { PostListItem } from '@/components/post-list-item'; + +export function generateStaticParams() { + const tags = new Set(); + for (const post of allPosts) { + if (!post.tags) continue; + for (const tag of post.tags) { + tags.add(tag); + } + } + return Array.from(tags).map((tag) => ({ + tag + })); +} + +interface Props { + params: { tag: string }; +} + +export function generateMetadata({ params }: Props): Metadata { + const tag = params.tag; + return { + title: `標籤:${tag}` + }; +} + +export default function TagPage({ params }: Props) { + const tag = params.tag; + + const posts = allPosts.filter( + (post) => post.tags && post.tags.includes(tag) + ); + + return ( +
+

+ 標籤:{tag} +

+
    + {posts.map((post) => ( + + ))} +
+
+ ); +} + diff --git a/components/post-list-item.tsx b/components/post-list-item.tsx index e3e7826..937804f 100644 --- a/components/post-list-item.tsx +++ b/components/post-list-item.tsx @@ -43,12 +43,13 @@ export function PostListItem({ post }: Props) { {post.tags && post.tags.length > 0 && (
{post.tags.slice(0, 4).map((t) => ( - #{t} - + ))}
)} diff --git a/components/right-sidebar.tsx b/components/right-sidebar.tsx index a2648af..71a1f4d 100644 --- a/components/right-sidebar.tsx +++ b/components/right-sidebar.tsx @@ -47,12 +47,13 @@ export function RightSidebar() { else if (count >= 3) sizeClass = 'text-xs font-medium'; return ( - {tag} - + ); })}