import React, { useEffect, useState } from 'react'; import { useParams, Link, useLocation } from 'react-router-dom'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import matter from 'gray-matter'; import { ArrowLeft, Calendar, User, Tag } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Buffer } from 'buffer'; // Explicitly import Buffer const imageModules = import.meta.glob('../assets/*.jpg', { eager: true, import: 'default' }); const BlogPost = () => { const { category, slug } = useParams(); // category will be undefined for /component/:slug const location = useLocation(); const [post, setPost] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const loadPost = async () => { try { let contentModule; let basePath; let currentSlug = slug; // Use slug from useParams // Determine content type based on pathname const pathSegments = location.pathname.split('/').filter(Boolean); let contentType = ''; if (pathSegments[0] === 'blog' && pathSegments.length >= 3) { contentType = 'blog'; // For blog posts, category is the second segment, slug is the third currentSlug = pathSegments[2]; basePath = `../content/blog/`; // blog/:category/:slug } else if (pathSegments[0] === 'component' && pathSegments.length >= 2) { contentType = 'component'; currentSlug = pathSegments[1]; // component/:slug basePath = '../content/component/'; } else if (pathSegments[0] === 'technology' && pathSegments.length >= 2) { contentType = 'tech'; currentSlug = pathSegments[1]; basePath = '../content/tech/'; } else if (pathSegments[0] === 'freezone' && pathSegments.length >= 2) { contentType = 'freezone'; currentSlug = pathSegments[1]; basePath = '../content/freezone/'; } else if (pathSegments[0] === 'home' && pathSegments.length >= 2) { contentType = 'home'; currentSlug = pathSegments[1]; basePath = '../content/home/'; } else { setError('Invalid URL path for content.'); setLoading(false); return; } try { const modules = import.meta.glob('../content/**/*.md', { as: 'raw', eager: true }); const fullPath = `${basePath}${currentSlug}.md`; contentModule = modules[fullPath]; if (!contentModule) { throw new Error(`Markdown file not found at ${fullPath}`); } const { data: frontmatter, content: markdownContent } = matter(contentModule); // Resolve image path const resolvedImage = frontmatter.image ? imageModules[`../assets/${frontmatter.image}`] : null; setPost({ frontmatter: { ...frontmatter, image: resolvedImage }, content: markdownContent, contentType }); // Store contentType and resolved image with post } catch (err) { setError(`Failed to load content: ${err.message}. Please ensure the file exists and is correctly formatted.`); } } catch (err) { setError(err.message); } finally { setLoading(false); } }; loadPost(); }, [location.pathname, slug]); // Depend on pathname and slug if (loading) { return (