Skip to content

Repository files navigation

Markdown Editor React

License: MITNPM DownloadNPMnpm bundle sizenpm bundle size

Installation

$ npm install md-editor-react codemirror --save

MDEditor Usage

MDEditor is used for show editor with editing controlls and live preview

import{MDEditor}from'md-editor-react';import'md-editor-react/dist/style.css';import'codemirror/mode/gfm/gfm';import'codemirror/lib/codemirror.css';<MDEditordefaultValue="Hello World"/>

CodeMirror is dependancy so you have to import codemirror css file and gfm mode for syntax highlighitng.

MDEditor Props

propstypedefaultdescription
delaynumber300input delay for parsing markdown and show html preview
fullScreenbooleanfalseenable fullscreen mode
watchModebooleantrueshow html view
classNamestringclass name applied in wrapper
optionsobjectcodemirror options
onChangefunctioneditor value on chagne event handler
menuConfiglistadd a menu items in menubar
defaultValuestringset editor initial value
parserOptionsobjectused marked markdown parser options

MDPreview Usage

MDPreview is used for parse markdown and preview html

import{MDPreview}from'md-editor-react';import'md-editor-react/dist/style.css';<MDPreviewvalue="markdown value"/>

MDPreview Props

propstypedescription
valuestringmarkdown value to parse and displayed
parserOptionsobjectused marked markdown parser options
sanitizerOptionsobjectDOMPurify senitizer options

Highlight code in html preview

for highlight code you can used highlightjs library.

how to use in MDEditor

import{MDEditor}from'md-editor-react';importhljsfrom'highlight.js';import'md-editor-react/dist/style.css';import'highlight.js/styles/github-gist.css';import'codemirror/mode/gfm/gfm';import'codemirror/lib/codemirror.css';<MDEditordefaultValue="Hello World"parserOptions={{highlight: code=>hljs.highlightAuto(code).value,}}/>

how to use in MDPreview

import{MDPreview}from'md-editor-react';importhljsfrom'highlight.js';import'md-editor-react/dist/style.css';import'highlight.js/styles/github-gist.css';<MDPreviewvalue="markdown value"parserOptions={{highlight: code=>hljs.highlightAuto(code).value,}}/>

Add Menu Item in Editor

use menuConfig props in MDEditor component

<MDEditormenuConfig={[{id: 'uniq_id',title: 'Menu Title',icon: <MenuIcon/>,command: editor=>{/* write your code here */},}]}/>
propstypedescription
idstringuniq id
titlestringtitle of menu item
iconstring or componenticon display in menu bar
positionnumbermenu item position starting from 1 (divider count included)
commandfunctiontrigger after menu button clicked. editor passed as argument you can modify editor value
componentcomponenta react component render after menu clicked. component has close() and editor props

add divider in menu bar

<MDEditormenuConfig={[{divider: true}]}/>

Menu item component props

if you want to display model like table, link menu. just import and use MDModel component.

import{MDModal,MDInput,MDButton}from'md-editor-react';classMenuComponetextendsReact.PureComponent{constructor(props){super(props);this.state={text: '',};this.onClickSuccess=this.onClickSuccess.bind(this);this.onChangeInput=this.onChangeInput.bind(this);}onChangeInput(event){const{ name, value }=event.target;this.setState({[name]: value,});}onClickSuccess(){const{ text }=this.state;const{ editor, close }=this.props;if(text){editor.replaceSelection(text);close();}}render(){const{ close }=this.props;const{ text }=this.state;return(<MDModalvisibleclosableheader="Menu Header"onClose={close}><MDModal.BodyclassName="menu-modal"><MDInputvalue={text}name="text"onChange={this.onChangeInput}/></MDModal.Body><MDModal.FooteronSuccess={this.onClickSuccess}onCancel={close}/></MDModal>);}}

License

md-editor-react is MIT licensed.

Releases

Packages

Used by

Contributors

Languages