import { notFound } from 'next/navigation'; import Link from 'next/link'; import { loadThoughts } from '@/utils/mdxLoader'; import { formatReadingTime } from '@/utils/readingTime'; interface ThoughtPageProps { params: { slug: string; }; } import fs from 'fs'; export async function generateStaticParams() { const thoughtsDirectory = process.cwd() + '/src/content/thoughts'; try { const filenames = fs.readdirSync(thoughtsDirectory); return filenames.map((filename: string) => ({ slug: filename.replace(/\.mdx$/, ''), })); } catch { return []; } } export async function generateMetadata({ params }: ThoughtPageProps) { const thought = await getThoughtBySlug(params.slug); if (!thought) { return { title: 'Thought Not Found', }; } return { title: `${thought.title} | Mainasara Tsowa`, description: thought.excerpt, openGraph: { title: `${thought.title} | Mainasara Tsowa`, description: thought.excerpt, url: `https://mainasara.dev/thoughts/${params.slug}`, }, twitter: { title: `${thought.title} | Mainasara Tsowa`, description: thought.excerpt, }, }; } async function getThoughtBySlug(slug: string) { const thoughts = await loadThoughts(); return thoughts.find(thought => thought.id === slug.replace(/[^a-zA-Z0-9]/g, '-')); } export default async function ThoughtPage({ params }: ThoughtPageProps) { const thought = await getThoughtBySlug(params.slug); if (!thought) { notFound(); } return (
🌸

Thoughts

Reflections on technology and life

{thought.title}

{thought.category} {formatReadingTime(thought.readTime)} read {new Date(thought.createdAt).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}
{thought.content}
); }