2026-10-10 16:42:35 +03:30
|
|
|
"use client";
|
|
|
|
|
|
2026-10-18 10:14:22 +03:30
|
|
|
import { useEffect, useState } from "react";
|
2026-10-10 16:42:35 +03:30
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
export function SafeMarkdown({
|
|
|
|
|
text,
|
|
|
|
|
className,
|
|
|
|
|
lines,
|
|
|
|
|
}: {
|
|
|
|
|
text: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
lines?: 2 | 3;
|
|
|
|
|
}) {
|
2026-10-18 10:14:22 +03:30
|
|
|
const [html, setHtml] = useState("");
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!text) { setHtml(""); return; }
|
|
|
|
|
void (async () => {
|
|
|
|
|
const { marked } = await import("marked");
|
|
|
|
|
const DOMPurify = (await import("dompurify")).default;
|
|
|
|
|
const raw = await marked(text);
|
|
|
|
|
setHtml(DOMPurify.sanitize(raw));
|
|
|
|
|
})();
|
|
|
|
|
}, [text]);
|
2026-10-10 16:42:35 +03:30
|
|
|
|
2026-10-18 10:14:22 +03:30
|
|
|
if (!html) {
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
dir="auto"
|
|
|
|
|
className={cn(
|
|
|
|
|
"break-words text-sm [unicode-bidi:plaintext]",
|
|
|
|
|
lines === 2 && "line-clamp-2",
|
|
|
|
|
lines === 3 && "line-clamp-3",
|
|
|
|
|
!lines && "whitespace-pre-wrap",
|
|
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{text}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-10-10 16:42:35 +03:30
|
|
|
}
|
2026-10-18 10:14:22 +03:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
dir="auto"
|
|
|
|
|
className={cn(
|
|
|
|
|
"prose prose-sm max-w-none break-words [unicode-bidi:plaintext]",
|
|
|
|
|
lines === 2 && "line-clamp-2",
|
|
|
|
|
lines === 3 && "line-clamp-3",
|
|
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
dangerouslySetInnerHTML={{ __html: html }}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2026-10-10 16:42:35 +03:30
|
|
|
}
|