# react-md-editor **Repository Path**: CNNNNNNNN/react-md-editor ## Basic Information - **Project Name**: react-md-editor - **Description**: 轻量级具有预览功能的 Markdown 编辑器,基于Textarea 封装,不依赖第三方编辑器,使用 React.js 和 TypeScript 实现。 - **Primary Language**: TypeScript - **License**: MIT - **Default Branch**: master - **Homepage**: https://uiw.gitee.io/react-md-editor/ - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 14 - **Created**: 2021-06-07 - **Last Updated**: 2021-06-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README

react-md-editor logo

Build & Deploy Issues Forks Stars Open in unpkg Release npm version npm bundle size

A simple markdown editor with preview, implemented with React.js and TypeScript. This React Component aims to provide a simple Markdown editor with syntax highlighting support. This is based on `textarea` encapsulation, so it does not depend on any modern code editors such as Acs, CodeMirror, Monaco etc. ### Features - 📑 Indent line or selected text by pressing tab key, with customizable indentation. - ♻️ Based on `textarea` encapsulation, does not depend on any modern code editors. - 🚧 Does not depend on the [`uiw`](https://github.com/uiwjs/uiw) component library. - 🚘 Automatic list on new lines. - 😻 GitHub flavored markdown support. - 💡 Support [next.js](https://github.com/uiwjs/react-md-editor/issues/52#issuecomment-848969341), [Use examples](#support-nextjs) in [next.js](https://nextjs.org/). ### Quick Start ```bash npm i @uiw/react-md-editor ``` ### Using [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/markdown-editor-for-react-izdd6?fontsize=14&hidenavigation=1&theme=dark) [![Open in Github gh-pages](https://img.shields.io/badge/Open%20In-Github%20gh--pages-blue?logo=github)](https://uiwjs.github.io/react-md-editor/) [![Open in Gitee gh-pages](https://img.shields.io/badge/Open%20In-Gitee%20gh--pages-blue?logo=web)](https://uiw.gitee.io/react-md-editor/) ```jsx import React from "react"; import ReactDOM from "react-dom"; import MDEditor from '@uiw/react-md-editor'; export default function App() { const [value, setValue] = React.useState("**Hello world!!!**"); return (
); } ``` ### Custom Toolbars [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/react-md-editor-custom-toolbars-m2n10?fontsize=14&hidenavigation=1&theme=dark) ```tsx import React from "react"; import ReactDOM from "react-dom"; import MDEditor, { commands, ICommand, TextState, TextAreaTextApi } from '@uiw/react-md-editor'; const title3: ICommand = { name: 'title3', keyCommand: 'title3', buttonProps: { 'aria-label': 'Insert title3' }, icon: ( ), execute: (state: TextState, api: TextAreaTextApi) => { let modifyText = `### ${state.selectedText}\n`; if (!state.selectedText) { modifyText = `### `; } api.replaceSelection(modifyText); }, }; export default function App() { const [value, setValue] = React.useState("**Hello world!!!**"); return (
), children: ({ close, execute, getState, textApi }) => { return (
My Custom Toolbar
); }, execute: (state: TextState, api: TextAreaTextApi) => { console.log('>>>>>>update>>>>>', state) }, buttonProps: { 'aria-label': 'Insert title'} }), ]} />
); } ``` ### Preview Markdown [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/react-md-editor-preview-markdown-vrucl?fontsize=14&hidenavigation=1&theme=dark) ```jsx import React from "react"; import ReactDOM from "react-dom"; import MDEditor from '@uiw/react-md-editor'; export default function App() { return (
); } ``` ### Support Custom KaTeX Preview KaTeX is a fast, easy-to-use JavaScript library for TeX math rendering on the web, We perform math rendering through [`KaTeX`](https://github.com/KaTeX/KaTeX). The following example is preview in [CodeSandbox](https://codesandbox.io/s/markdown-editor-katex-for-react-7v3vl). [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/headless-frog-em8yg?fontsize=14&hidenavigation=1&theme=dark) > ⚠️ Upgrade v2 to v3 [d025430](https://github.com/uiwjs/react-md-editor/blob/d02543050c9abd8f7c72ae02b6421ac2e6ae421a/website/ExmapleKaTeX.tsx#L1-L59) ```bash npm install katex ``` ```jsx import React from "react"; import ReactDOM from "react-dom"; import MDEditor from '@uiw/react-md-editor'; import katex from 'katex'; import 'katex/dist/katex.css'; const mdKaTeX = `This is to display the \`\$\$\c = \\pm\\sqrt{a^2 + b^2}\$\$\` in one line \`\`\`KaTeX c = \\pm\\sqrt{a^2 + b^2} \`\`\` `; export default function App() { return ( { const txt = children[0] || ''; if (inline) { if (typeof txt === 'string' && /^\$\$(.*)\$\$/.test(txt)) { const html = katex.renderToString(txt.replace(/^\$\$(.*)\$\$/, '$1'), { throwOnError: false, }); return ; } return {txt}; } if ( typeof txt === 'string' && typeof className === 'string' && /^language-katex/.test(className.toLocaleLowerCase()) ) { const html = katex.renderToString(txt, { throwOnError: false, }); return ; } return {txt}; }, }, }} /> ); } ``` ### Markdown text to Imgage [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/react-md-editor-text-to-images-ijqmx?fontsize=14&hidenavigation=1&theme=dark) ```jsx import React from "react"; import MDEditor, { commands, ICommand, TextState, TextAreaTextApi } from "@uiw/react-md-editor"; import domToImage from "dom-to-image"; const textToImage: ICommand = { name: "Text To Image", keyCommand: "text2image", buttonProps: { "aria-label": "Insert title3" }, icon: ( ), execute: (state: TextState, api: TextAreaTextApi) => { const dom = document.getElementsByClassName("w-md-editor")[0]; if (dom) { domToImage.toJpeg(dom, {}).then((dataUrl) => { const link = document.createElement("a"); link.download = "image.jpg"; link.href = dataUrl; link.click(); }); } } }; export default function App() { return (
); } ``` ### Support Custom Mermaid Preview Using [mermaid](https://github.com/mermaid-js/mermaid) to generation of diagram and flowchart from text in a similar manner as markdown [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/markdown-editor-mermaid-for-react-uvtsx?fontsize=14&hidenavigation=1&theme=dark) ```bash npm install mermaid ``` ```jsx import React from "react"; import ReactDOM from "react-dom"; import MDEditor from "@uiw/react-md-editor"; import mermaid from "mermaid"; const mdMermaid = `The following are some examples of the diagrams, charts and graphs that can be made using Mermaid and the Markdown-inspired text specific to it. \`\`\`mermaid graph TD A[Hard] -->|Text| B(Round) B --> C{Decision} C -->|One| D[Result 1] C -->|Two| E[Result 2] \`\`\` \`\`\`mermaid sequenceDiagram Alice->>John: Hello John, how are you? loop Healthcheck John->>John: Fight against hypochondria end Note right of John: Rational thoughts! John-->>Alice: Great! John->>Bob: How about you? Bob-->>John: Jolly good! \`\`\` `; export default function App() { return ( { const txt = children[0] || ''; if ( typeof txt === 'string' && typeof className === 'string' && /^language-mermaid/.test(className.toLocaleLowerCase()) ) { const Elm = document.createElement("div"); Elm.id = "demo"; const svg = mermaid.render("demo", txt); return } return {txt}; }, }, }} /> ); } ReactDOM.render(, document.getElementById("container")); ``` ### Support Nextjs Use examples in [nextjs](https://nextjs.org/). [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/nextjs-example-react-md-editor-qjhn7?fontsize=14&hidenavigation=1&theme=dark) ```bash npm install next-remove-imports ``` ```js // next.config.js const removeImports = require('next-remove-imports')(); module.exports = removeImports({}); ``` ```jsx // pages/index.js import Head from 'next/head' import MDEditor from '@uiw/react-md-editor'; import '@uiw/react-md-editor/dist/markdown-editor.css' import '@uiw/react-markdown-preview/dist/markdown.css'; export default function Home() { return (
Create Next App
) } ``` ### Props - `value: string`: The Markdown value. - `onChange?: (value: string)`: Event handler for the `onChange` event. - `commands?: ICommand[]`: An array of [`ICommand`](https://github.com/uiwjs/react-md-editor/blob/d02543050c9abd8f7c72ae02b6421ac2e6ae421a/src/commands/index.ts#L39-L57), which, each one, contain a [`commands`](https://github.com/uiwjs/react-md-editor/blob/d02543050c9abd8f7c72ae02b6421ac2e6ae421a/src/commands/index.ts#L155-L180) property. If no commands are specified, the default will be used. Commands are explained in more details below. - `extraCommands?: ICommand[]`: Displayed on the right side of the toolbar. - `autoFocus?: true`: Can be used to make `Markdown Editor` focus itself on initialization. - `previewOptions?: ReactMarkdown.ReactMarkdownProps`: This is reset [@uiw/react-markdown-preview](https://github.com/uiwjs/react-markdown-preview/tree/e6e8462d9a5c64a7045e25adcb4928095d74ca37#options-props) settings. - `textareaProps?: TextareaHTMLAttributes`: Set the `textarea` related props. - `height?: number=200`: The height of the editor. - `visiableDragbar?: boolean=true`: Show drag and drop tool. Set the height of the editor. - `highlightEnable?: boolean=true`: Disable editing area code highlighting. The value is `false`, which increases the editing speed. - `fullscreen?: boolean=false`: Show markdown preview. - `preview?: 'live' | 'edit' | 'preview'`: Default value `live`, Show markdown preview. - `maxHeight?: number=1200`: Maximum drag height. The `visiableDragbar=true` value is valid. - `minHeights?: number=100`: Minimum drag height. The `visiableDragbar=true` value is valid. - `tabSize?: number=2`: The number of characters to insert when pressing tab key. Default `2` spaces. - `hideToolbar?: boolean=false`: Option to hide the tool bar. - `enableScroll?: boolean=true`: Whether to enable scrolling. ### Development ```bash npm run watch # Listen create type and .tsx files. npm run css:watch # listen to the component compile and output the .css file npm run start # Preview code example. ``` ### Related - [@uiw/react-md-editor](https://github.com/uiwjs/react-md-editor): A simple markdown editor with preview, implemented with React.js and TypeScript. - [@uiw/react-codemirror](https://github.com/uiwjs/react-codemirror): CodeMirror component for React. @codemirror - [@uiw/react-monacoeditor](https://github.com/jaywcjlove/react-monacoeditor): Monaco Editor component for React. - [@uiw/react-markdown-editor](https://github.com/uiwjs/react-markdown-editor): A markdown editor with preview, implemented with React.js and TypeScript. - [@uiw/react-markdown-preview](https://github.com/uiwjs/react-markdown-preview): React component preview markdown text in web browser. ### License Licensed under the MIT License.