import type { Metadata } from "next"; import Link from "next/link"; import { notFound } from "next/navigation"; import { BlogCoverImage } from "@/components/BlogCoverImage"; import { ContentPageShell } from "@/components/ContentPageShell"; import { HtmlContent } from "@/components/HtmlContent"; import { ApiNotFoundError, authorName, formatFaDate, getBlog, getBlogCoverImage, } from "@/lib/api"; type BlogDetailPageProps = { params: Promise<{ slug: string }>; }; function stripLeadingCoverImage(html: string, cover: string | null): string { if (!html) return html; let next = html.replace( /^\s*]*>\s*]*>\s*<\/p>/i, "", ); if (cover) { const escaped = cover.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); next = next.replace( new RegExp( `]*>\\s*]*src=["']${escaped}["'][^>]*>\\s*<\\/p>`, "i", ), "", ); } return next.trim(); } export async function generateMetadata({ params, }: BlogDetailPageProps): Promise { const { slug } = await params; try { const blog = await getBlog(slug); return { title: `${blog.title} | مشکی`, description: blog.abstract ?? undefined, alternates: { canonical: `/blog/${blog.slug}` }, openGraph: { title: blog.title, description: blog.abstract ?? undefined, type: "article", }, }; } catch { return { title: "مقاله | مشکی" }; } } export default async function BlogDetailPage({ params }: BlogDetailPageProps) { const { slug } = await params; let blog; try { blog = await getBlog(slug); } catch (error) { if (error instanceof ApiNotFoundError) notFound(); throw error; } const cover = getBlogCoverImage(blog); const date = formatFaDate(blog.publishedAt); const author = authorName(blog.author); const category = blog.categoryName; const bodyHtml = blog.mainTextHtml ? stripLeadingCoverImage(blog.mainTextHtml, cover) : null; const titleEn = category || "Blog"; return (

{titleEn}

{blog.title}

{blog.abstract ? (

{blog.abstract}

) : null}
{cover ? (
) : null} {(date || author) ? (
{author ? نویسنده: {author} : } {date ? ( ) : null}
) : cover ? (
) : null} {bodyHtml ? (
) : null}
); }