forgeplus-react/src/components/render-html.jsx

20 lines
661 B
React
Raw Normal View History

2020-04-28 17:23:34 +08:00
import React, { useEffect, useRef } from 'react'
import 'katex/dist/katex.min.css';
import { exportMdtoHtml } from 'educoder'
let preRegex = /<pre[^>]*>/g
export default ({ value = '', is_md = true, className, style = {} }) => {
let html = is_md ? exportMdtoHtml(value) : value
html = html.replace(/▁/g, "▁▁▁").replace('<p>[TOC]</p>', '')
const el = useRef()
useEffect(() => {
if (el.current && html) {
if (preRegex.test(html)) {
window.PR.prettyPrint()
}
}
}, [html, el.current])
return (<div ref={el} style={style} className={`${className} markdown-body`} dangerouslySetInnerHTML={{ __html: html }}></div>)
}