import { EyeOutlined } from '@ant-design/icons'; import { Image, Typography } from 'antd'; import { unescape } from 'lodash'; import { TokensList, marked } from 'marked'; import React, { Fragment, useCallback } from 'react'; import HighlightCode from '../highlight-code'; import './index.less'; const { Text, Link, Paragraph } = Typography; interface MarkdownViewerProps { content: string; height?: string; theme?: 'light' | 'dark'; } const MarkdownViewer: React.FC = ({ content, height = 'auto', theme = 'light' }) => { const renderer = new marked.Renderer(); const tokens = marked.lexer(content); const reDefineTypes = [ 'code', 'link', 'hr', 'heading', 'paragraph', 'codespan', 'strong', 'text', 'image', 'em', 'list', 'list_item', 'br', 'html', 'escape' ]; const renderItem = useCallback((token: any, render: any) => { if (!reDefineTypes.includes(token.type)) { console.log('token======66==', token.type, token); return ( ); } let htmlstr: any = null; let child: any = null; if (token.tokens?.length) { child = render?.(token.tokens as TokensList, render); } const text = child ? child : unescape(token.text); if (token.type === 'escape') { htmlstr = text; } if (token.type === 'html') { htmlstr =
; } if (token.type === 'list') { htmlstr = token.order ? (
    {render?.(token.items, render)}
) : ( ); } if (token.type === 'list_item') { htmlstr =
  • {text}
  • ; } if (token.type === 'br') { htmlstr =
    ; } if (token.type === 'em') { htmlstr = {text}; } if (token.type === 'image') { htmlstr = ( {token.text} }} /> ); } if (token.type === 'text') { htmlstr = text; } if (token.type === 'codespan') { htmlstr = {text}; } if (token.type === 'strong') { htmlstr = {text}; } if (token.type === 'heading') { htmlstr = {text}; } if (token.type === 'paragraph') { htmlstr = {text}; } if (token.type === 'code') { htmlstr = ( ); } if (token.type === 'link') { htmlstr = ( {text} ); } if (token.type === 'hr') { htmlstr =
    ; } return htmlstr; }, []); const renderTokens = (tokens: TokensList): any => { return tokens?.map((token: any, index: number) => { return {renderItem(token, renderTokens)}; }); }; return (
    {renderTokens(tokens)}
    ); }; export default React.memo(MarkdownViewer);