import React, { useContext, useEffect } from 'react'; import { rehype } from 'rehype'; import rehypePrism from 'rehype-prism-plus/common'; import { type IProps } from '../../Types'; import { EditorContext } from '../../Context'; function html2Escape(sHtml: string) { return sHtml.replace( /[<&"]/g, (c: string) => (({ '<': '<', '>': '>', '&': '&', '"': '"' }) as Record)[c], ); } export interface MarkdownProps extends IProps, React.HTMLAttributes {} export default function Markdown(props: MarkdownProps) { const { prefixCls } = props; const { markdown = '', highlightEnable, dispatch } = useContext(EditorContext); const preRef = React.createRef(); useEffect(() => { if (preRef.current && dispatch) { dispatch({ textareaPre: preRef.current }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); if (!markdown) { return
;
  }
  let mdStr = `
${html2Escape(
    String.raw`${markdown}`,
  )}\n
`; if (highlightEnable) { try { mdStr = rehype() .data('settings', { fragment: true }) // https://github.com/uiwjs/react-md-editor/issues/593 // @ts-ignore .use(rehypePrism, { ignoreMissing: true }) .processSync(mdStr) .toString(); } catch (error) {} } return React.createElement('div', { className: 'wmde-markdown-color', dangerouslySetInnerHTML: { __html: mdStr || '' }, }); }