feat: add article page and not found component with markdown rendering
This commit is contained in:
99
src/app/(pages)/article/[slug]/page.tsx
Normal file
99
src/app/(pages)/article/[slug]/page.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
import { getArticleBySlug } from '@/lib/feature/article/article.external';
|
||||
import { ArrowLeftIcon, CalendarIcon, ClockIcon } from 'lucide-react';
|
||||
import Link from 'next/link';
|
||||
import { notFound } from 'next/navigation';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
|
||||
interface ArticlePageProps {
|
||||
params: Promise<{ slug: string }>;
|
||||
}
|
||||
|
||||
function readingTime(text: string): number {
|
||||
const words = text.trim().split(/\s+/).length;
|
||||
return Math.max(1, Math.ceil(words / 200));
|
||||
}
|
||||
|
||||
function formatDate(date: Date): string {
|
||||
return new Intl.DateTimeFormat('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
}).format(date);
|
||||
}
|
||||
|
||||
const ArticlePage = async ({ params }: ArticlePageProps) => {
|
||||
const { slug } = await params;
|
||||
const article = await getArticleBySlug(slug);
|
||||
|
||||
if (!article) notFound();
|
||||
|
||||
const minutes = readingTime(article.content);
|
||||
|
||||
return (
|
||||
<article className='w-full'>
|
||||
{article.coverImageUrl && (
|
||||
<div className='relative h-64 w-full overflow-hidden md:h-96'>
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img
|
||||
src={article.coverImageUrl}
|
||||
alt={article.title}
|
||||
className='h-full w-full object-cover'
|
||||
/>
|
||||
<div className='absolute inset-0 bg-linear-to-t from-background via-background/40 to-transparent' />
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className='container mx-auto max-w-3xl px-4'>
|
||||
<div
|
||||
className={
|
||||
article.coverImageUrl ? '-mt-16 relative z-10' : 'pt-12'
|
||||
}
|
||||
>
|
||||
<Link
|
||||
href='/home'
|
||||
className='mb-6 inline-flex items-center gap-1.5 text-sm text-muted-foreground transition-colors hover:text-foreground'
|
||||
>
|
||||
<ArrowLeftIcon className='size-3.5' />
|
||||
All articles
|
||||
</Link>
|
||||
|
||||
<header className='mb-10'>
|
||||
<h1 className='mb-4 text-3xl font-bold leading-tight tracking-tight md:text-4xl'>
|
||||
{article.title}
|
||||
</h1>
|
||||
|
||||
<p className='mb-5 text-lg leading-relaxed text-muted-foreground'>
|
||||
{article.description}
|
||||
</p>
|
||||
|
||||
<div className='flex flex-wrap items-center gap-4 text-xs text-muted-foreground'>
|
||||
<span className='flex items-center gap-1.5'>
|
||||
<CalendarIcon className='size-3.5 shrink-0' />
|
||||
<time
|
||||
dateTime={article.createdAt.toISOString()}
|
||||
>
|
||||
{formatDate(article.createdAt)}
|
||||
</time>
|
||||
</span>
|
||||
<span className='flex items-center gap-1.5'>
|
||||
<ClockIcon className='size-3.5 shrink-0' />
|
||||
{minutes} min read
|
||||
</span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<hr className='mb-10 border-border' />
|
||||
|
||||
<div className='prose prose-neutral dark:prose-invert max-w-none pb-16'>
|
||||
<ReactMarkdown remarkPlugins={[remarkGfm]}>
|
||||
{article.content}
|
||||
</ReactMarkdown>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
};
|
||||
|
||||
export default ArticlePage;
|
||||
Reference in New Issue
Block a user