react-markdown库

2022/9/12

# react-markdown-editor-lite

  1. 编辑: react-markdown-editor-lite (opens new window)
  2. 展示:
npm i react-markdown-editor-lite
npm i markdown-it
npm install react-markdown
npm i remark-gfm
1
2
3
4
import React,{ useState } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import MarkdownIt from 'markdown-it';
import MdEditor from 'react-markdown-editor-lite';
import 'react-markdown-editor-lite/lib/index.css';

import {Prism as SyntaxHighlighter} from 'react-syntax-highlighter'
import {dark} from 'react-syntax-highlighter/dist/esm/styles/prism'

const mdParser = new MarkdownIt(/* Markdown-it options */);

const Md = () => {
    const [ text, setText ] = useState("");
    const handleEditorChange = ({ html, text: md }: any ) => {
      setText(md);
    }
  return (
    <>
        <MdEditor style={{ height: '500px' }} renderHTML={text => mdParser.render(text)} onChange={handleEditorChange} />

        <div dangerouslySetInnerHTML={{__html: mdParser.render(text)}}></div>
        <hr />

        <ReactMarkdown
          children={text}
          remarkPlugins={[remarkGfm]}
          components={{
            code({node, inline, className, children, ...props}) {
              const match = /language-(\w+)/.exec(className || '')
              return !inline && match ? (
                <SyntaxHighlighter
                  children={String(children).replace(/\n$/, '')}
                  style={dark}
                  language={match[1]}
                  PreTag="div"
                  {...props}
                />
              ) : (
                <code className={className} {...props}>
                  {children}
                </code>
              )
            }
          }}
        />
    </>
  );
};

export default Md;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
上次更新: 6/13/2025