react中渲染markdown

使用场景

  • 使用markdown编辑器编辑的内容
  • 展示GPT中输出的内容(GPT输出格式是:markdown)
  • 一些说明文档

其他说明

  • 代码高亮(指定编程语言的时候)
  • 代码复制功能

使用方法

  • 下载相应的包(例子中使用版本说明)
// 安装的包说明
react-syntax-highlighter:15.5.0
react-markdown:8.0.7
// 按照命令
yarn add react-syntax-highlighter react-markdown
  • REACT代码示例
import React from 'react';
import ReactMarkdown from 'react-markdown';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { coyWithoutShadows, vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';

const them = {
    dark: vscDarkPlus,
    light: coyWithoutShadows
};
interface IMarkdownRendererProps {
    content: string;
    darkMode?: boolean;
}
const MarkdownRenderer: React.FC<IMarkdownRendererProps> = ({ content, darkMode }) => {
    const copyCode = function (value) {
        navigator.clipboard.writeText(value);
    };
    const renderers = {
        code: ({ node, inline, className, children, ...props }) => {
            const languageMatch = /language-(\w+)/.exec(className || '');
            const value = String(children).replace(/\n$/, '');
            const lineNum = value.split('\n').length;
            const toHtml = (codeContent, language?) => {
                return (
                    <div style={{ position: 'relative' }}>
                        <div style={{ display: 'flex', position: 'absolute', top: 0, right: 0, padding: lineNum > 1 ? '10px' : '0 10px', color: '#999', zIndex: 1 }}>
                            <div style={{ padding: '0 5px' }}>{language}</div>
                            <div onClick={() => copyCode(value)} style={{ cursor: 'pointer', color: '#690' }}>
                                复制代码
                            </div>
                        </div>
                        {codeContent}
                    </div>
                );
            };
            if (languageMatch) {
                const language = languageMatch[1];
                return toHtml(
                    <SyntaxHighlighter showLineNumbers={true} style={darkMode ? them.dark : them.light} language={language} PreTag='div' {...props}>
                        {value}
                    </SyntaxHighlighter>,
                    language
                );
            } else {
                return toHtml(
                    <code className={className} {...props}>
                        {children}
                    </code>
                );
            }
        }
    };

    return <ReactMarkdown components={renderers} children={content} />;
};

export default MarkdownRenderer;
  • 使用例子
const textContent = '# 标题\n```html\n<div>test</div>\n<div>test</div>\n```';
ReactDOM.render(<MarkdownRenderer content={textContent} />, document.getElementById('root'));

注意事项

  • 版本不同导致错误
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光速度的进程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值