"use client";
import Image from "next/image";
import Link from "next/link";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { useEffect, useMemo, useRef, useState } from "react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism";
import { Badge, Card, Typography } from "poyraz-ui/atoms";
import type { BlogDetail } from "@/data/blog-detail";
type BlogDetailContentProps = {
post: BlogDetail;
};
function MermaidBlock({ chart }: { chart: string }) {
const [svg, setSvg] = useState("");
const [error, setError] = useState("");
const idRef = useRef(`mermaid-${Math.random().toString(36).slice(2)}`);
useEffect(() => {
let mounted = true;
const renderChart = async () => {
try {
const mermaid = (await import("mermaid")).default;
mermaid.initialize({ startOnLoad: false, theme: "neutral", securityLevel: "loose" });
const { svg: rendered } = await mermaid.render(idRef.current, chart);
if (mounted) {
setSvg(rendered);
setError("");
}
} catch {
if (mounted) {
setError("Mermaid diyagramı oluşturulamadı.");
}
}
};
void renderChart();
return () => {
mounted = false;
};
}, [chart]);
if (error) {
return (
{error}
);
}
if (!svg) {
return (
Diyagram hazırlanıyor...
);
}
return (
);
}
export function BlogDetailContent({ post }: BlogDetailContentProps) {
const scrollerRef = useRef(null);
const [progress, setProgress] = useState(0);
const progressWidth = useMemo(() => `${Math.min(100, Math.max(0, progress))}%`, [progress]);
const handleScroll = () => {
const element = scrollerRef.current;
if (!element) return;
const scrollable = element.scrollHeight - element.clientHeight;
if (scrollable <= 0) {
setProgress(100);
return;
}
setProgress((element.scrollTop / scrollable) * 100);
};
return (
<>
{"<- Blog'a dön"}
{post.category}
{post.title}
{post.author} - {post.date} - {post.readTime}
{post.excerpt}
(
{children}
),
h3: ({ children }) => (
{children}
),
p: ({ children }) => (
{children}
),
li: ({ children }) => (
{children}
),
blockquote: ({ children }) => (
{children}
),
table: ({ children }) => (
),
thead: ({ children }) => {children},
tr: ({ children }) => {children}
,
th: ({ children }) => (
{children} |
),
td: ({ children }) => {children} | ,
code: ({ className, children }) => {
const match = /language-(\w+)/.exec(className ?? "");
const codeText = String(children).replace(/\n$/, "");
const language = match?.[1] ?? "";
if (!match) {
return {children};
}
if (language === "mermaid") {
return ;
}
return (
{codeText}
);
},
}}
>
{post.markdown}
>
);
}