import { notFound } from 'next/navigation'; import Image from 'next/image'; import Link from 'next/link'; import fs from 'fs'; import path from 'path'; import matter from 'gray-matter'; import { remark } from 'remark'; import html from 'remark-html'; import gfm from 'remark-gfm'; interface Props { params: Promise<{ slug: string }>; } async function getPost(slug: string) { const blogDir = path.join(process.cwd(), 'src/content/blog'); const filePath = path.join(blogDir, `${slug}.md`); if (!fs.existsSync(filePath)) { return null; } const fileContent = fs.readFileSync(filePath, 'utf-8'); const { data, content } = matter(fileContent); const processedContent = await remark() .use(gfm) .use(html, { sanitize: false }) .process(content); const contentHtml = processedContent.toString(); // Support both 'category' and 'categories' field names const category = data.category || (Array.isArray(data.categories) ? data.categories[0] : 'ทั่วไป'); // Support both 'image' and 'featuredImage' field names const image = data.image || data.featuredImage || '/images/2021/03/ppr-pipe_000C.jpg'; return { slug, title: data.title || 'ไม่มีชื่อ', excerpt: data.excerpt || '', date: data.date || new Date().toISOString(), author: data.author || 'ดีลพลัสเทค', category, image, content: contentHtml, }; } export async function generateMetadata({ params }: Props) { const { slug } = await params; const decodedSlug = decodeURIComponent(slug); const post = await getPost(decodedSlug); if (!post) return { title: 'ไม่พบบทความ' }; return { title: post.title, description: post.excerpt, }; } export async function generateStaticParams() { const blogDir = path.join(process.cwd(), 'src/content/blog'); const files = fs.readdirSync(blogDir).filter(f => f.endsWith('.md')); return files.map(filename => ({ slug: filename.replace('.md', ''), })); } export default async function BlogPostPage({ params }: Props) { const { slug } = await params; const decodedSlug = decodeURIComponent(slug); const post = await getPost(decodedSlug); if (!post) { notFound(); } return (
{/* Header */}
กลับไปหน้าบทความ {post.category}

{post.title}

{post.author}
{/* Featured Image */}
{post.title}
{/* Content */}
{/* CTA */}

สนใจสินค้าหรือบริการ?

ติดต่อเราเพื่อรับคำปรึกษาและใบเสนอราคาฟรี

ติดต่อเรา
); }