Download src/components/Markdown.jsx from cheumk/studio: direct link, hf CLI and curl.
- Browser
- Download file 667 Bytes
-
https://huggingface.co/spaces/cheumk/studio/resolve/main/src/components/Markdown.jsx
- Command line
-
hf download hf://spaces/cheumk/studio/src/components/Markdown.jsx
-
curl -L -o Markdown.jsx https://huggingface.co/spaces/cheumk/studio/resolve/main/src/components/Markdown.jsx
667 Bytes
| import { useMemo } from 'react' | |
| import { marked } from 'marked' | |
| import DOMPurify from 'dompurify' | |
| marked.setOptions({ gfm: true, breaks: true }) | |
| // 連結一律開新分頁 | |
| DOMPurify.addHook('afterSanitizeAttributes', (node) => { | |
| if (node.tagName === 'A') { | |
| node.setAttribute('target', '_blank') | |
| node.setAttribute('rel', 'noopener noreferrer') | |
| } | |
| }) | |
| /** 模型輸出 → Markdown → HTML,經 DOMPurify 消毒後才插入,避免 XSS */ | |
| export default function Markdown({ text }) { | |
| const html = useMemo(() => DOMPurify.sanitize(marked.parse(String(text || ''))), [text]) | |
| return <div className="md" dangerouslySetInnerHTML={{ __html: html }} /> | |
| } | |