Files
aragrp/app/blog/[slug]/page.tsx
T
ar.badiei f0c290786e
Build & Push Docker Image / build (push) Has been cancelled
feat: enhance date formatting in changelog
- Added support for Latin digit formatting in release dates to improve readability.
- Updated the ChangelogVersionCard component to ensure proper text direction for dates.
2026-07-12 02:08:11 +03:30

193 lines
7.0 KiB
TypeScript

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, TableBlockPayload } 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 { BlogTable } from "@/components/blog-table";
import { BlogPullquote } from "@/components/blog-pullquote";
import { ScrollToTop } from "@/components/scroll-to-top";
import { BlogPostCard } from "@/components/blog-post-card";
import { type, typeSpacing } from "@/lib/typography";
import { cn } from "@/lib/utils";
interface BlogPostPageProps {
params: Promise<{ slug: string }>;
}
function stripHtml(html: string): string {
return html.replace(/<[^>]*>/g, "").trim();
}
export async function generateMetadata({
params,
}: BlogPostPageProps): Promise<Metadata> {
const { slug } = await params;
let post;
try {
post = await getPostBySlug(slug);
} catch {
return { title: "پست یافت نشد" };
}
if (!post) return { title: "پست یافت نشد" };
const titleRaw = post.title?.rendered ?? "پست";
const title = stripHtml(titleRaw);
const excerpt = stripHtml(post.excerpt?.rendered ?? "").slice(0, 160);
const imageUrl = getFeaturedImageUrl(post, "medium_large");
return {
title: titleRaw,
description: excerpt,
openGraph: {
title,
description: excerpt,
...(imageUrl && { images: [{ url: imageUrl, alt: title }] }),
},
twitter: {
card: "summary_large_image",
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={cn(type.link, "inline-flex items-center gap-1 transition-colors hover:text-foreground")}
>
<ChevronRight className="size-4" aria-hidden />
دانشنامه
</Link>
</nav>
<h1
className={type.h1}
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">
{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 === "pullquote" ? (
<BlogPullquote key={i} payload={seg.payload as BlockquoteBlockPayload} />
) : seg.blockType === "tip" ? (
<BlogTip key={i} payload={seg.payload as TipBlockPayload} />
) : seg.blockType === "table" ? (
<BlogTable key={i} payload={seg.payload as TableBlockPayload} />
) : (
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={cn(type.h3, "mb-8 text-center 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 />
</>
);
}