@uiw/react-md-editor Documentation

repository·master·Indexed 25 days ago

https://github.com/uiwjs/react-md-editor

A lightweight markdown editor with preview implemented with React.js and TypeScript. It uses textarea encapsulation instead of heavy dependencies like Monaco or CodeMirror. The library includes a comprehensive command system for markdown formatting (bold, italic, lists, tables, headings), a TextAreaCommandOrchestrator for programmatic execution, and support for multiple code preview modes (preview, edit, and live).

Tokens
5.9K
Snippets
15
Records
52
Agent score
84%

What's inside @uiw/react-md-editor

  1. Reference MDEditorProps

    master

    The following props are available on the MDEditor component:

    PropTypeDescription
    valuestringThe Markdown value
    onChange(value?: string, event?: React.ChangeEvent<HTMLTextAreaElement>, state?: ContextStore) => voidEvent handler for the onChange event
    onHeightChange(value?: CSSProperties['height'], oldValue?: CSSProperties['height'], state?: ContextStore) => voidEditor height change listener
    onStatistics(data: Statistics) => voidCallback for editor statistics (length, lineCount)
    autoFocusITextAreaProps['autoFocus']Whether to focus the editor on initialization (defaults to on)
    autoFocusEndbooleanWhether to focus at the end of text on initialization
    heightCSSProperties['height']The height of the editor. Note: Dragbar is invalid when using percentage values
    visibleDragbarbooleanShow drag and drop tool to set editor height
    previewPreviewTypeShow markdown preview (e.g., edit, preview, both)
    fullscreenbooleanFull screen display editor
    overflowbooleanDisable fullscreen setting body styles
    maxHeightnumberMaximum drag height (requires visibleDragbar=true)
    minHeightnumberMinimum drag height (requires visibleDragbar=true)
    previewOptionsOmit<MarkdownPreviewProps, 'source'>Settings for react-markdown preview
    textareaPropsITextAreaPropsProps passed to the underlying textarea
    componentsobjectCustom component overrides
    data-color-mode'light' | 'dark'Theme configuration
    highlightEnablebooleanDisable editing area code highlighting. Setting to false increases editing speed (defaults to true)
    tabSizenumberNumber of characters to insert when pressing tab (default 2)
    defaultTabEnablebooleanIf false, the tab key inserts a tab character; if true, it performs default browser behavior
    commandsICommand[]Custom or existing commands to include
    commandsFilter(command: ICommand, isExtra: boolean) => false | ICommandFilter or modify commands
    extraCommandsICommand[]Additional commands to add
    hideToolbarbooleanHide the toolbar
    enableScrollbooleanWhether to enable scrolling
    toolbarBottombooleanPosition toolbar at the bottom
    directionCSSProperties['direction']Text direction (rtl or ltr)
  2. Insert images using the image command

    master

    The image command allows users to insert image syntax into the markdown editor. It supports both manual insertion and smart detection of existing URLs.

    Behavior

    • With URL selected: If the user has a URL (containing http or www) selected, the command wraps the selection in the markdown image syntax: ![image](selected_url).
    • Without URL selected:
      • If the cursor is at a position where it can wrap text, it uses the ![image](...) pattern.
      • If no text is selected, it inserts a placeholder: ![image](url).

    Keyboard Shortcut

    • ctrlcmd+k (Note: ctrlcmd typically refers to Ctrl on Windows/Linux and Cmd on macOS).
  3. Manipulate text with TextAreaTextApi

    master

    The TextAreaTextApi class provides low-level text manipulation capabilities for commands. It is used internally by the orchestrator but can be used to interact with the editor's content.

    Methods:

    • replaceSelection(text: string): Replaces the current selection with the provided text and returns the updated TextState.
    • setSelectionRange(selection: TextRange): Focuses the textarea and sets the selection to the specified start and end indices.
  4. Use the comment command

    master

    The comment command allows users to wrap selected text in Markdown comment syntax.

    • Syntax: Wraps text with <!-- as a prefix and --> as a suffix.
    • Keyboard Shortcut: ctrlcmd + / (Note: ctrlcmd typically refers to Ctrl on Windows/Linux or Cmd on macOS).
    • UI Behavior: It can be triggered via a toolbar button with the label Insert comment (ctrl + /).
  5. Define a custom ICommand

    master

    When creating custom commands, implement the ICommand interface. A command can be a simple command or a command with children (sub-commands).

    ICommandBase properties:

    • name: The display name of the command.
    • keyCommand: A string identifier for the command.
    • shortcuts: Keyboard shortcuts.
    • icon: A React.ReactElement for the command icon.
    • execute: The function called when the command runs. It receives ExecuteState, TextAreaTextApi, dispatch, ExecuteCommandState, and shortcuts.
  6. Use the link command

    master

    The link command allows users to wrap selected text in Markdown link syntax. It supports keyboard shortcuts and automatic detection of existing URLs.

    Behavior

    • With selected text: Wraps the selection in [selected_text](url).
    • With an existing URL: If the selected text contains http or www, it wraps the selection in [](url) (treating the selection as the URL).
    • With no selection: Inserts the placeholder [title](url) at the cursor position.

    Command Details

    • Name: link
    • Shortcut: ctrlcmd+l (Control/Command + L)
    • Markdown Syntax: [prefix](suffix) where prefix is [ and suffix is ](url)
  7. Group commands using the group function

    master
    The group function allows you to organize multiple commands into a single logical group. It accepts an array of commands and an optional GroupOptions object to configure the group's properties. When commands are passed into group, each command's parent property is automatically set to the group object, enabling hierarchical command structures.
  8. Insert inline code

    master

    You can insert inline code using the code command. This command wraps the selected text with single backticks (). If the selected text contains a newline, the command automatically falls back to the codeBlock` behavior to ensure valid Markdown.

    Keyboard Shortcut: ctrlcmd+j

    // Command details for code
    name: 'code'
    keyCommand: 'code'
    shortcuts: 'ctrlcmd+j'
    prefix: '`'