|
| 1 | +<!--rehype:ignore:start--> |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://github.com/uiwjs/react-markdown-editor"> |
| 5 | + <img alt="React Markdown Editor logo" src="./website/logo.svg?sanitize=true"> |
| 6 | + </a> |
| 7 | +</p> |
| 8 | + |
| 9 | + |
| 10 | +<!--rehype:ignore:end--> |
| 11 | +<!--dividing--> |
| 12 | + |
| 13 | +<p align="center"> |
| 14 | + <a href="https://github.com/uiwjs/react-markdown-editor/actions"> |
| 15 | + <img alt="Build & Deploy" src="https://github.com/uiwjs/react-markdown-editor/actions/workflows/ci.yml/badge.svg"> |
| 16 | + </a> |
| 17 | + <a href="https://www.npmjs.com/package/@uiw/react-markdown-editor"> |
| 18 | + <img alt="NPM Download" src="https://img.shields.io/npm/dm/@uiw/react-markdown-editor.svg?style=flat"> |
| 19 | + </a> |
| 20 | + <a href="https://www.npmjs.com/package/@uiw/react-markdown-editor"> |
| 21 | + <img alt="npm version" src="https://img.shields.io/npm/v/@uiw/react-markdown-editor.svg"> |
| 22 | + </a> |
| 23 | +</p> |
| 24 | + |
| 25 | +<p align="center"> |
| 26 | + A markdown editor with preview, implemented with React.js and TypeScript. |
| 27 | +</p> |
| 28 | + |
| 29 | +<!--rehype:ignore:start--> |
| 30 | +[](https://uiwjs.github.io/react-markdown-editor/) |
| 31 | + |
| 32 | +<!--rehype:ignore:end--> |
| 33 | + |
| 34 | +> Migrate from @uiw/react-markdown-editor [4.x to 5.x.](https://github.com/uiwjs/react-markdown-editor/releases/tag/v5.0.0) |
| 35 | +
|
| 36 | +## Install |
| 37 | + |
| 38 | +```bash |
| 39 | +npm i @uiw/react-markdown-editor |
| 40 | +``` |
| 41 | + |
| 42 | +<!--rehype:ignore:start--> |
| 43 | + |
| 44 | +## Document |
| 45 | + |
| 46 | +Official document [demo preview](https://uiwjs.github.io/react-markdown-editor/) ([🇨🇳中国镜像网站](http://uiw.gitee.io/react-markdown-editor/)) |
| 47 | + |
| 48 | +<!--rehype:ignore:end--> |
| 49 | + |
| 50 | +## Basic Usage |
| 51 | + |
| 52 | +```jsx mdx:preview |
| 53 | +import React from 'react'; |
| 54 | +import MarkdownEditor from '@uiw/react-markdown-editor'; |
| 55 | + |
| 56 | +const mdStr = `# This is a H1 \n## This is a H2 \n###### This is a H6`; |
| 57 | + |
| 58 | +const Dome = () => { |
| 59 | + return ( |
| 60 | + <MarkdownEditor |
| 61 | + value={mdStr} |
| 62 | + onChange={(value, viewUpdate) => { |
| 63 | + |
| 64 | + }} |
| 65 | + /> |
| 66 | + ) |
| 67 | +}; |
| 68 | + |
| 69 | +export default Dome; |
| 70 | +``` |
| 71 | + |
| 72 | +## Controlled Usage |
| 73 | + |
| 74 | +[](https://codesandbox.io/embed/react-markdown-editor-ybpce?file=/src/App.js) |
| 75 | + |
| 76 | +```jsx mdx:preview |
| 77 | +import React, { useState } from 'react'; |
| 78 | +import MarkdownEditor from '@uiw/react-markdown-editor'; |
| 79 | + |
| 80 | +const mdStr = `# This is a H1 \n## This is a H2 \n###### This is a H6`; |
| 81 | +export default function App() { |
| 82 | + const [markdown, setMarkdown] = useState(mdStr); |
| 83 | + return ( |
| 84 | + <MarkdownEditor |
| 85 | + value={markdown} |
| 86 | + height="200px" |
| 87 | + onChange={(value, viewUpdate) => setMarkdown(value)} |
| 88 | + /> |
| 89 | + ); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Only Markdown Preview |
| 94 | + |
| 95 | +[](https://codesandbox.io/embed/react-markdown-editor-forked-qyp83q?fontsize=14&hidenavigation=1&theme=dark) |
| 96 | + |
| 97 | +This markdown preview sub-component is a direct export [`@uiw/react-markdown-preview`](https://github.com/uiwjs/react-markdown-preview) component, API documentation, please check [`@uiw/react-markdown-preview`](https://github.com/uiwjs/react-markdown-preview). |
| 98 | + |
| 99 | +```jsx mdx:preview |
| 100 | +import React from 'react'; |
| 101 | +import MarkdownEditor from '@uiw/react-markdown-editor'; |
| 102 | + |
| 103 | +const mdStr = `# This is a H1 \n## This is a H2 \n###### This is a H6`; |
| 104 | +function App() { |
| 105 | + return ( |
| 106 | + <MarkdownEditor.Markdown source={mdStr} height="200px" /> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +export default App; |
| 111 | +``` |
| 112 | + |
| 113 | +## Custom Toolbars |
| 114 | + |
| 115 | +[](https://codesandbox.io/embed/react-markdown-editorcustom-toolbars-forked-r9ocu?fontsize=14&hidenavigation=1&theme=dark) |
| 116 | + |
| 117 | +```jsx mdx:preview |
| 118 | +import React from "react"; |
| 119 | +import MarkdownEditor from '@uiw/react-markdown-editor'; |
| 120 | + |
| 121 | +const title2 = { |
| 122 | + name: 'title2', |
| 123 | + keyCommand: 'title2', |
| 124 | + button: { 'aria-label': 'Add title text' }, |
| 125 | + icon: ( |
| 126 | + <svg width="12" height="12" viewBox="0 0 512 512"> |
| 127 | + <path fill="currentColor" d="M496 80V48c0-8.837-7.163-16-16-16H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.621v128H154.379V96H192c8.837 0 16-7.163 16-16V48c0-8.837-7.163-16-16-16H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.275v320H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.621V288H357.62v128H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.275V96H480c8.837 0 16-7.163 16-16z" /> |
| 128 | + </svg> |
| 129 | + ), |
| 130 | + execute: ({ state, view }) => { |
| 131 | + if (!state || !view) return; |
| 132 | + const lineInfo = view.state.doc.lineAt(view.state.selection.main.from); |
| 133 | + let mark = '#'; |
| 134 | + const matchMark = lineInfo.text.match(/^#+/) |
| 135 | + if (matchMark && matchMark[0]) { |
| 136 | + const txt = matchMark[0]; |
| 137 | + if (txt.length < 6) { |
| 138 | + mark = txt + '#'; |
| 139 | + } |
| 140 | + } |
| 141 | + if (mark.length > 6) { |
| 142 | + mark = '#'; |
| 143 | + } |
| 144 | + const title = lineInfo.text.replace(/^#+/, '') |
| 145 | + view.dispatch({ |
| 146 | + changes: { |
| 147 | + from: lineInfo.from, |
| 148 | + to: lineInfo.to, |
| 149 | + insert: `${mark} ${title}` |
| 150 | + }, |
| 151 | + // selection: EditorSelection.range(lineInfo.from + mark.length, lineInfo.to), |
| 152 | + selection: { anchor: lineInfo.from + mark.length }, |
| 153 | + }); |
| 154 | + }, |
| 155 | +}; |
| 156 | + |
| 157 | +const Dome = () => ( |
| 158 | + <MarkdownEditor |
| 159 | + value="Hello Markdown!" |
| 160 | + height="200px" |
| 161 | + toolbars={[ |
| 162 | + 'bold', title2 |
| 163 | + ]} |
| 164 | + /> |
| 165 | +); |
| 166 | + |
| 167 | +export default Dome; |
| 168 | +``` |
| 169 | + |
| 170 | +## Support Nextjs |
| 171 | + |
| 172 | +Use examples in [nextjs](https://nextjs.org/). |
| 173 | + |
| 174 | +[](https://codesandbox.io/embed/nextjs-example-react-markdown-editor-72s9d?fontsize=14&hidenavigation=1&theme=dark) |
| 175 | +[](https://github.com/uiwjs/react-md-editor/issues/52#issuecomment-848969341) |
| 176 | +[](https://github.com/uiwjs/react-md-editor/issues/224#issuecomment-901112079) |
| 177 | + |
| 178 | +```bash |
| 179 | +npm install next-remove-imports |
| 180 | +npm install @uiw/react-markdown-editor |
| 181 | +``` |
| 182 | + |
| 183 | +```js |
| 184 | +// next.config.js |
| 185 | +const removeImports = require('next-remove-imports')(); |
| 186 | +module.exports = removeImports({}); |
| 187 | +``` |
| 188 | + |
| 189 | +```jsx |
| 190 | +import dynamic from 'next/dynamic'; |
| 191 | +import '@uiw/react-markdown-editor/markdown-editor.css'; |
| 192 | +import '@uiw/react-markdown-preview/markdown.css'; |
| 193 | + |
| 194 | +const MarkdownEditor = dynamic( |
| 195 | + () => import("@uiw/react-markdown-editor").then((mod) => mod.default), |
| 196 | + { ssr: false } |
| 197 | +); |
| 198 | + |
| 199 | +function HomePage() { |
| 200 | + return ( |
| 201 | + <div> |
| 202 | + <MarkdownEditor value="Hello Markdown!" /> |
| 203 | + </div> |
| 204 | + ); |
| 205 | +} |
| 206 | + |
| 207 | +export default HomePage; |
| 208 | +``` |
| 209 | + |
| 210 | +## Support dark-mode/night-mode |
| 211 | + |
| 212 | +By default, the [`dark-mode`](https://github.com/jaywcjlove/dark-mode/) is automatically switched according to the system. If you need to switch manually, just set the `data-color-mode="dark"` parameter for html Element. |
| 213 | + |
| 214 | +```html |
| 215 | +<html data-color-mode="dark"> |
| 216 | +``` |
| 217 | + |
| 218 | +```js |
| 219 | +document.documentElement.setAttribute('data-color-mode', 'dark') |
| 220 | +document.documentElement.setAttribute('data-color-mode', 'light') |
| 221 | +``` |
| 222 | + |
| 223 | +Inherit custom color variables by adding `.wmde-markdown-var` selector. |
| 224 | + |
| 225 | +```jsx |
| 226 | +const Demo = () => { |
| 227 | + return ( |
| 228 | + <div> |
| 229 | + <div className="wmde-markdown-var"> </div> |
| 230 | + <MarkdownEditor value="Hello Markdown!" /> |
| 231 | + </div> |
| 232 | + ) |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +## Props |
| 237 | + |
| 238 | +- `value (string)` - the raw markdown that will be converted to html (**required**) |
| 239 | +- `visible?: boolean` - Shows a preview that will be converted to html. |
| 240 | +- `toolbars?: ICommand[] | string[]` - Tool display settings. |
| 241 | +- `toolbarsMode?: ICommand[] | string[]` - Tool display settings. |
| 242 | +- `onChange?:function(editor: IInstance, data: CodeMirror.EditorChange, value: string)` - called when a change is made |
| 243 | +- `onBlur?: function(editor: IInstance, event: Event)` - event occurs when an object loses focus |
| 244 | +- `previewProps` - [react-markdown options](https://github.com/uiwjs/react-markdown-preview/tree/v2.1.0#options-props) |
| 245 | + |
| 246 | +```ts |
| 247 | +import { ReactCodeMirrorProps } from '@uiw/react-codemirror'; |
| 248 | +export interface IMarkdownEditor extends ReactCodeMirrorProps { |
| 249 | + className?: string; |
| 250 | + prefixCls?: string; |
| 251 | + /** The raw markdown that will be converted to html (**required**) */ |
| 252 | + value?: string; |
| 253 | + /** Shows a preview that will be converted to html. */ |
| 254 | + visible?: boolean; |
| 255 | + visibleEditor?: boolean; |
| 256 | + /** Override the default preview component */ |
| 257 | + renderPreview?: (props: MarkdownPreviewProps, initVisible: boolean) => React.ReactNode; |
| 258 | + /** Preview expanded width @default `50%` */ |
| 259 | + previewWidth?: string; |
| 260 | + /** Whether to enable scrolling */ |
| 261 | + enableScroll?: boolean; |
| 262 | + /** Tool display settings. */ |
| 263 | + toolbars?: Commands[]; |
| 264 | + /** The tool on the right shows the settings. */ |
| 265 | + toolbarsMode?: Commands[]; |
| 266 | + /** Tool display filter settings. */ |
| 267 | + toolbarsFilter?: (tool: Commands, idx: number) => boolean; |
| 268 | + /** Toolbar on bottom */ |
| 269 | + toolbarBottom?: boolean; |
| 270 | + /** Option to hide the tool bar. */ |
| 271 | + hideToolbar?: boolean; |
| 272 | + /** [@uiw/react-markdown-preview](https://github.com/uiwjs/react-markdown-preview#options-props) options */ |
| 273 | + previewProps?: MarkdownPreviewProps; |
| 274 | + /** replace the default `extensions` */ |
| 275 | + reExtensions?: ReactCodeMirrorProps['extensions']; |
| 276 | +} |
| 277 | +``` |
| 278 | + |
| 279 | +```ts |
| 280 | +import React from 'react'; |
| 281 | +import { ReactCodeMirrorRef } from '@uiw/react-codemirror'; |
| 282 | +import { MarkdownPreviewProps, MarkdownPreviewRef } from '@uiw/react-markdown-preview'; |
| 283 | +export * from '@uiw/react-markdown-preview'; |
| 284 | +export interface ToolBarProps { |
| 285 | + editor: React.RefObject<ReactCodeMirrorRef>; |
| 286 | + preview: React.RefObject<HTMLDivElement>; |
| 287 | + container: React.RefObject<HTMLDivElement>; |
| 288 | + containerEditor: React.RefObject<HTMLDivElement>; |
| 289 | + editorProps: IMarkdownEditor; |
| 290 | +} |
| 291 | +export interface MarkdownEditorRef { |
| 292 | + editor: React.RefObject<ReactCodeMirrorRef> | null; |
| 293 | + preview?: React.RefObject<MarkdownPreviewRef> | null; |
| 294 | +} |
| 295 | +export declare type Commands = keyof typeof defaultCommands | ICommand; |
| 296 | +export interface IToolBarProps<T = Commands> extends ToolBarProps { |
| 297 | + className?: string; |
| 298 | + editorProps: IMarkdownEditor; |
| 299 | + mode?: boolean; |
| 300 | + prefixCls?: string; |
| 301 | + toolbars?: T[]; |
| 302 | + onClick?: (type: string) => void; |
| 303 | +} |
| 304 | +declare const MarkdownEditor: MarkdownEditorComponent; |
| 305 | +declare type MarkdownEditorComponent = React.FC<React.PropsWithRef<IMarkdownEditor>> & { |
| 306 | + Markdown: typeof MarkdownPreview; |
| 307 | +}; |
| 308 | +export default MarkdownEditor; |
| 309 | +``` |
| 310 | + |
| 311 | +```ts |
| 312 | +import { ReactCodeMirrorRef } from '@uiw/react-codemirror'; |
| 313 | +import { MarkdownPreviewProps, MarkdownPreviewRef } from '@uiw/react-markdown-preview'; |
| 314 | +export declare type ButtonHandle = (command: ICommand, props: IMarkdownEditor, options: ToolBarProps) => JSX.Element; |
| 315 | +export declare type ICommand = { |
| 316 | + icon?: React.ReactElement; |
| 317 | + name?: string; |
| 318 | + keyCommand?: string; |
| 319 | + button?: ButtonHandle | React.ButtonHTMLAttributes<HTMLButtonElement>; |
| 320 | + execute?: (editor: ReactCodeMirrorRef) => void; |
| 321 | +}; |
| 322 | +export declare const defaultCommands: { |
| 323 | + undo: ICommand; |
| 324 | + redo: ICommand; |
| 325 | + bold: ICommand; |
| 326 | + italic: ICommand; |
| 327 | + header: ICommand; |
| 328 | + strike: ICommand; |
| 329 | + underline: ICommand; |
| 330 | + quote: ICommand; |
| 331 | + olist: ICommand; |
| 332 | + ulist: ICommand; |
| 333 | + todo: ICommand; |
| 334 | + link: ICommand; |
| 335 | + image: ICommand; |
| 336 | + code: ICommand; |
| 337 | + codeBlock: ICommand; |
| 338 | + fullscreen: ICommand; |
| 339 | + preview: ICommand; |
| 340 | +}; |
| 341 | +export declare const getCommands: () => ICommand[]; |
| 342 | +export declare const getModeCommands: () => ICommand[]; |
| 343 | +export declare const defaultTheme: import("@codemirror/state").Extension; |
| 344 | +``` |
| 345 | + |
| 346 | +### Development |
| 347 | + |
| 348 | +```bash |
| 349 | +npm run watch # Listen create type and .tsx files. |
| 350 | +npm run start # Preview code example. |
| 351 | + |
| 352 | +npm run doc |
| 353 | +``` |
| 354 | + |
| 355 | +### Related |
| 356 | + |
| 357 | +- [@uiw/react-textarea-code-editor](https://github.com/uiwjs/react-textarea-code-editor): A simple code editor with syntax highlighting. |
| 358 | +- [@uiw/react-codemirror](https://github.com/uiwjs/react-codemirror): CodeMirror component for React. @codemirror |
| 359 | +- [@uiw/react-monacoeditor](https://github.com/jaywcjlove/react-monacoeditor): Monaco Editor component for React. |
| 360 | +- [@uiw/react-md-editor](https://github.com/uiwjs/react-md-editor): A simple markdown editor with preview, implemented with React.js and TypeScript. |
| 361 | +- [@uiw/react-markdown-preview](https://github.com/uiwjs/react-markdown-preview): React component preview markdown text in web browser. |
| 362 | + |
| 363 | +## Contributors |
| 364 | + |
| 365 | +As always, thanks to our amazing contributors! |
| 366 | + |
| 367 | +<a href="https://github.com/uiwjs/react-markdown-editor/graphs/contributors"> |
| 368 | + <img src="https://uiwjs.github.io/react-markdown-editor/CONTRIBUTORS.svg" /> |
| 369 | +</a> |
| 370 | + |
| 371 | +Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors). |
| 372 | + |
| 373 | +## License |
| 374 | + |
| 375 | +Licensed under the MIT License. |
0 commit comments