This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
import type { Metadata } from "next";
|
||||
import Link from "next/link";
|
||||
import Image from "next/image";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getPostBySlug, getFeaturedImageUrl, getRelatedPosts } from "@/lib/wp-api";
|
||||
import { formatDateShamsi } from "@/lib/format-date";
|
||||
import { ChevronRight } from "lucide-react";
|
||||
import type { CollapseBlockPayload, BlockquoteBlockPayload, TipBlockPayload } from "@/lib/blog-blocks";
|
||||
import { parseContentHeadings, getReadingTimeMinutes, formatAuthorName } from "@/lib/blog-utils";
|
||||
import { BlogToc } from "@/components/blog-toc";
|
||||
import { BlogPostMeta } from "@/components/blog-post-meta";
|
||||
import { BlogCollapseSection } from "@/components/blog-collapse-section";
|
||||
import { BlogBlockquote } from "@/components/blog-blockquote";
|
||||
import { BlogTip } from "@/components/blog-tip";
|
||||
import { BlogPullquote } from "@/components/blog-pullquote";
|
||||
import { ScrollToTop } from "@/components/scroll-to-top";
|
||||
import { BlogPostCard } from "@/components/blog-post-card";
|
||||
|
||||
interface BlogPostPageProps {
|
||||
params: Promise<{ slug: string }>;
|
||||
}
|
||||
|
||||
export async function generateMetadata({
|
||||
params,
|
||||
}: BlogPostPageProps): Promise<Metadata> {
|
||||
const { slug } = await params;
|
||||
const post = await getPostBySlug(slug);
|
||||
if (!post) return { title: "پست یافت نشد" };
|
||||
const title = post.title?.rendered ?? "پست";
|
||||
const excerpt = post.excerpt?.rendered?.replace(/<[^>]*>/g, "").trim().slice(0, 160) ?? "";
|
||||
return {
|
||||
title,
|
||||
description: excerpt,
|
||||
};
|
||||
}
|
||||
|
||||
export default async function BlogPostPage({ params }: BlogPostPageProps) {
|
||||
const { slug } = await params;
|
||||
let post;
|
||||
try {
|
||||
post = await getPostBySlug(slug);
|
||||
} catch {
|
||||
notFound();
|
||||
}
|
||||
if (!post) notFound();
|
||||
|
||||
let relatedPosts: Awaited<ReturnType<typeof getRelatedPosts>> = [];
|
||||
try {
|
||||
relatedPosts = await getRelatedPosts(post.id, post.categories ?? [], 3);
|
||||
} catch {
|
||||
// show no related section on API error
|
||||
}
|
||||
|
||||
const date = post.date ? formatDateShamsi(post.date) : null;
|
||||
const modifiedDate = post.modified
|
||||
? formatDateShamsi(post.modified)
|
||||
: null;
|
||||
const authorName = formatAuthorName(post._embedded?.author?.[0]?.name);
|
||||
const thumbnailUrl = getFeaturedImageUrl(post, "full");
|
||||
const media = post._embedded?.["wp:featuredmedia"]?.[0];
|
||||
const altText = media?.alt_text || post.title?.rendered?.replace(/<[^>]*>/g, "") || "";
|
||||
|
||||
const { toc, segments } = parseContentHeadings(post.content.rendered);
|
||||
const readingTimeMinutes = getReadingTimeMinutes(post.content.rendered);
|
||||
const postTitle = post.title?.rendered?.replace(/<[^>]*>/g, "") ?? "";
|
||||
const postPath = `/blog/${slug}`;
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* تصویر شاخص تمامعرض بالای صفحه */}
|
||||
{thumbnailUrl && (
|
||||
<div className="pt-13 md:pt-24">
|
||||
<div className="relative container mx-auto max-w-5xl md:rounded-xl overflow-hidden shadow-sm w-full aspect-[5/2] min-h-[10rem] md:min-h-[10rem] bg-muted">
|
||||
<Image
|
||||
src={thumbnailUrl}
|
||||
alt={altText}
|
||||
fill
|
||||
className="object-cover"
|
||||
quality={100}
|
||||
priority
|
||||
sizes="(max-width: 768px) 100vw, 1024px"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<section className="bg-background pt-8 pb-20 md:pt-12 md:pb-28">
|
||||
<div className="mx-auto max-w-6xl px-6">
|
||||
{/* Single grid so sticky TOC stays for whole page scroll */}
|
||||
<div className="lg:grid lg:grid-cols-[16rem_1fr] lg:gap-10">
|
||||
<BlogToc items={toc} className="mb-6 lg:mb-0" />
|
||||
|
||||
<div className="min-w-0 space-y-0">
|
||||
<div className="pb-8 md:pb-12">
|
||||
<nav className="mb-6" aria-label="مسیر">
|
||||
<Link
|
||||
href="/blog"
|
||||
className="text-muted-foreground hover:text-foreground text-sm inline-flex items-center gap-1 transition-colors"
|
||||
>
|
||||
<ChevronRight className="size-4" aria-hidden />
|
||||
بلاگ
|
||||
</Link>
|
||||
</nav>
|
||||
<h1
|
||||
className="text-2xl font-bold md:text-3xl leading-tight tracking-normal"
|
||||
dangerouslySetInnerHTML={{ __html: post.title.rendered }}
|
||||
/>
|
||||
<BlogPostMeta
|
||||
url={postPath}
|
||||
title={postTitle}
|
||||
readingTimeMinutes={readingTimeMinutes}
|
||||
date={date}
|
||||
dateTime={post.date}
|
||||
modifiedDate={modifiedDate}
|
||||
authorName={authorName}
|
||||
className="mt-4"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<article className="blog-content text-justify space-y-6 text-foreground [&_h2]:text-2xl [&_h2]:font-bold [&_h2]:mt-8 [&_h2]:mb-4 [&_h3]:text-xl [&_h3]:font-bold [&_h3]:mt-6 [&_h3]:mb-3 [&_p]:leading-relaxed [&_p]:text-muted-foreground [&_a]:text-primary [&_a]:no-underline hover:[&_a]:underline [&_ul]:list-disc [&_ul]:pr-6 [&_ol]:list-decimal [&_ol]:pr-6 [&_li]:my-1 [&_h2]:scroll-mt-24 [&_h3]:scroll-mt-24 [&_h4]:scroll-mt-24">
|
||||
{segments.map((seg, i) =>
|
||||
seg.type === "html" ? (
|
||||
<div
|
||||
key={i}
|
||||
dangerouslySetInnerHTML={{ __html: seg.content }}
|
||||
/>
|
||||
) : seg.blockType === "collapse" ? (
|
||||
<BlogCollapseSection
|
||||
key={i}
|
||||
section={seg.payload as CollapseBlockPayload}
|
||||
idPrefix={`blog-${slug}-${i}`}
|
||||
/>
|
||||
) : seg.blockType === "blockquote" ? (
|
||||
<BlogBlockquote key={i} payload={seg.payload as BlockquoteBlockPayload} />
|
||||
) : seg.blockType === "tip" ? (
|
||||
<BlogTip key={i} payload={seg.payload as TipBlockPayload} />
|
||||
) : (
|
||||
null
|
||||
)
|
||||
)}
|
||||
</article>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{relatedPosts.length > 0 && (
|
||||
<section className="bg-background border-t pb-16 md:pb-20">
|
||||
<div className="mx-auto max-w-6xl px-6 pt-10 md:pt-14">
|
||||
<h2 className="text-xl md:text-2xl text-center mb-8 md:mb-10">
|
||||
مقالات مرتبط
|
||||
</h2>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6 min-w-0">
|
||||
{relatedPosts.map((p) => (
|
||||
<BlogPostCard key={p.id} post={p} variant="related" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
|
||||
<ScrollToTop />
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user