File size: 1,190 Bytes
2accaeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import React, { useEffect, useState, useMemo } from 'react';
import { marked } from 'marked';
interface MarkdownRendererProps {
markdownText: string;
className?: string;
}
export const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({ markdownText, className = '' }) => {
const [sanitizedHtml, setSanitizedHtml] = useState('');
useEffect(() => {
if (markdownText) {
// Basic sanitization options for marked
marked.setOptions({
gfm: true, // Enable GitHub Flavored Markdown
breaks: false, // Convert GFM line breaks to <br> -- CHANGED TO FALSE
pedantic: false,
// Consider adding a sanitizer like DOMPurify here if content can be malicious
// For now, assuming content from Gemini is generally safe for this context
});
const rawHtml = marked.parse(markdownText) as string;
setSanitizedHtml(rawHtml);
} else {
setSanitizedHtml('');
}
}, [markdownText]);
const combinedClassName = `markdown-content whitespace-pre-wrap break-words ${className}`;
return (
<div
className={combinedClassName}
dangerouslySetInnerHTML={{ __html: sanitizedHtml }}
/>
);
};
|