diff --git a/app/tags/[tag]/page.tsx b/app/tags/[tag]/page.tsx new file mode 100644 index 0000000..d35a080 --- /dev/null +++ b/app/tags/[tag]/page.tsx @@ -0,0 +1,86 @@ +import { posts } from "#site/content"; +import { PostItem } from "@/components/post-item"; +import { Tag } from "@/components/tag"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { getAllTags, getPostsByTagSlug, sortTagsByCount } from "@/lib/utils"; +import { slug } from "github-slugger"; +import { Metadata } from "next"; +import { isAsyncFunction } from "util/types"; + +interface TagPageProps { + params: { + tag: string; + }; +} + +export async function generateMetadata({ + params, +}: TagPageProps): Promise { + const { tag } = params; + return { + title: tag, + description: `Posts on the topic of ${tag}`, + }; +} + +export const generateStaticParams = () => { + const tags = getAllTags(posts); + const paths = Object.keys(tags).map((tag) => ({ tag: slug(tag) })); + return paths; +}; + +export default function TagPage({ params }: TagPageProps) { + const { tag } = params; + const title = tag.split("-").join(" "); + + const displayPosts = getPostsByTagSlug(posts, tag); + const tags = getAllTags(posts); + const sortedTags = sortTagsByCount(tags); + + return ( +
+
+
+

+ {title} +

+
+
+
+
+
+ {displayPosts?.length > 0 ? ( +
    + {displayPosts.map((post) => { + const { slug, date, title, description, tags } = post; + return ( +
  • + +
  • + ); + })} +
+ ) : ( +

Ikke noe innhold her

+ )} +
+ + + Tags + + + {sortedTags?.map((t) => ( + + ))} + + +
+
+ ); +} diff --git a/app/tags/page.tsx b/app/tags/page.tsx new file mode 100644 index 0000000..f4c0df6 --- /dev/null +++ b/app/tags/page.tsx @@ -0,0 +1,30 @@ +import { posts } from "#site/content"; +import { Tag } from "@/components/tag"; +import { getAllTags, sortTagsByCount } from "@/lib/utils"; +import { Metadata } from "next"; + +export const metadata: Metadata = { + title: "Tags", + description: "Topic I've written about", +}; + +export default async function TagsPage() { + const tags = getAllTags(posts); + const sortedTags = sortTagsByCount(tags); + + return ( +
+
+
+

Tags

+
+
+
+
+ {sortedTags?.map((tag) => ( + + ))} +
+
+ ); +} diff --git a/components/post-item.tsx b/components/post-item.tsx index 89da281..399ea46 100644 --- a/components/post-item.tsx +++ b/components/post-item.tsx @@ -23,7 +23,7 @@ export function PostItem({

- {title} + {title}

diff --git a/lib/utils.ts b/lib/utils.ts index f5edbe2..fd4044c 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,5 +1,6 @@ import { Post } from "#site/content"; import { type ClassValue, clsx } from "clsx"; +import { slug } from "github-slugger"; import { twMerge } from "tailwind-merge"; export function cn(...inputs: ClassValue[]) { @@ -37,3 +38,11 @@ export function getAllTags(posts: Array) { export function sortTagsByCount(tags: Record) { return Object.keys(tags).sort((a, b) => tags[b] - tags[a]); } + +export function getPostsByTagSlug(posts: Array, tag: string) { + return posts.filter((post) => { + if (!post.tags) return false; + const slugifiedTags = post.tags.map((tag) => slug(tag)); + return slugifiedTags.includes(tag); + }); +}