import { cn } from '@/lib/utils'; import { useEffect, useState } from 'react'; import { Marked } from 'marked'; import hljs from 'highlight.js'; import { markedHighlight } from 'marked-highlight'; const markedAndHighlight = new Marked( markedHighlight({ emptyLangClass: 'hljs', langPrefix: 'hljs language-', highlight(code, lang, info) { const language = hljs.getLanguage(lang) ? lang : 'plaintext'; return hljs.highlight(code, { language }).value; }, }), ); export const md2html = async (md: string) => { const html = markedAndHighlight.parse(md); return html; }; export const clearMeta = (markdown?: string) => { if (!markdown) return ''; // Remove YAML front matter if present const yamlRegex = /^---\n[\s\S]*?\n---\n/; return markdown.replace(yamlRegex, ''); }; type Props = { children?: React.ReactNode; className?: string; style?: React.CSSProperties; content?: string; // Optional content prop for markdown text [key: string]: any; // Allow any additional props }; export const MarkdownPreview = (props: Props) => { return (
{props.children ? {props.children} : }
); }; export const WrapperText = (props: { children?: React.ReactNode; html?: string }) => { if (props.html) { return
; } return
{props.children}
; }; export const MarkdownPreviewWrapper = (props: Props) => { const [html, setHtml] = useState(''); useEffect(() => { init(); }, [props.content]); const init = async () => { if (props.content) { const htmlContent = await md2html(props.content); setHtml(htmlContent); } else { setHtml(''); } }; return ; };