import type { Metadata } from "next"; import Link from "next/link"; import { notFound } from "next/navigation"; import Markdown from "@/components/Markdown"; import { Badge } from "@/components/ui/badge"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { PublicApiError, getPublicPost } from "@/lib/public-api"; import { apiBaseUrl, siteUrl, toAbsoluteUrl } from "@/lib/site"; import { formatJalali } from "@/lib/utils"; type Params = Promise<{ slug: string }>; function cleanText(value?: string | null) { if (!value) return ""; return value.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim(); } async function loadPost(slug: string) { try { return await getPublicPost(slug); } catch (error) { if (error instanceof PublicApiError && error.status === 404) { notFound(); } throw error; } } export async function generateMetadata({ params, }: { params: Params; }): Promise { const { slug } = await params; const post = await loadPost(slug); const description = cleanText(post.excerpt || post.content).slice(0, 160); const image = toAbsoluteUrl( post.absolute_featured_image_url || post.featured_image, apiBaseUrl, ) ?? `${siteUrl}/favicon.ico`; return { title: post.title, description, alternates: { canonical: `/blog/${post.slug}` }, openGraph: { title: post.title, description, url: `${siteUrl}/blog/${post.slug}`, siteName: "انجمن علمی کامپیوتر شرق گیلان", type: "article", images: [image], locale: "fa_IR", publishedTime: post.published_at || post.created_at, modifiedTime: post.updated_at, }, twitter: { card: "summary_large_image", title: post.title, description, images: [image], }, }; } export default async function BlogDetailPage({ params, }: { params: Params; }) { const { slug } = await params; const post = await loadPost(slug); const description = cleanText(post.excerpt || post.content).slice(0, 160); const image = toAbsoluteUrl( post.absolute_featured_image_url || post.featured_image, apiBaseUrl, ) ?? `${siteUrl}/favicon.ico`; const structuredData = { "@context": "https://schema.org", "@type": "BlogPosting", headline: post.title, description, image: [image], datePublished: post.published_at || post.created_at, dateModified: post.updated_at, url: `${siteUrl}/blog/${post.slug}`, author: { "@type": "Person", name: [post.author.first_name, post.author.last_name].filter(Boolean).join(" ") || post.author.username, }, publisher: { "@type": "Organization", name: "انجمن علمی کامپیوتر شرق گیلان", logo: { "@type": "ImageObject", url: `${siteUrl}/favicon.ico`, }, }, }; return (