Markdown Editor
A Markdown editor on CodeMirror 6 with a Typora-like live preview: the document is the Markdown text, so loading and saving keep every byte. Marks show only while the caret is inside their span; headings, bullets and quotes never show theirs; tables render as tables off the caret; frontmatter stays as written. Slash and [[ menus, a one-row toolbar with an overflow menu, a ref handle for host menus and outlines, English and Spanish labels, and agent actions (getMarkdown, insertText, replaceSelection, insertWikilink, toggleFormat, setHeading, toggleTask; setMarkdown needs approval).
Example
Markdown. Formatting marks stay hidden until the caret is inside formatted text. Type / for blocks. Press Escape, then Tab, to leave the editor.
188 characters of plain Markdown
Installation
@codemirror/commands@6.11.1@codemirror/lang-markdown@6.5.2@codemirror/language@6.13.1@codemirror/state@6.7.6@codemirror/view@6.43.15@lezer/common@1.5.5@lezer/highlight@1.2.5@lezer/markdown@1.8.1lucide-reactUsage
Pass a ref for a host's Insert or Link menu and an outline: MarkdownEditorHandle runs the toolbar's commands, inserts links and wikilinks, opens the wikilink suggestions (openWikilinkSuggestions, also from a slash command's run) and moves the caret, without importing CodeMirror. The [[ that openWikilinkSuggestions types is provisional: getValue() and onChange never include it, so an autosave that reads getValue() while the list is open saves the note as it was; dismissing leaves it untouched, choosing a note is one undo step, and isComposing() says a choice is pending. frontmatter="readonly" keeps the YAML block byte for byte and leaves the body editable, also when the closing fence ends the file.
API reference
MarkdownEditor
| Prop | Type | Default |
|---|---|---|
labelrequiredAccessible name of the editor and its text box. | string | – |
value | string | – |
defaultValue | string | – |
onChangeDebounced by `changeDelay`; flushed on blur and unmount. Never called for `value` itself. | (markdown: string) => void | – |
changeDelay | number | – |
onDirtyChangeWhether the text differs from the last `value` (or `defaultValue`), counting edits `onChange` hasn't sent yet. | (dirty: boolean) => void | – |
onConflictRuns when a new `value` arrives while edits are still waiting for `onChange`. Return the text to keep, or nothing to take `remote`. Without it, the local text goes out through `onChange` first, then `remote` applies. | (conflict: MarkdownConflict) => string | undefined | void | – |
readOnly | boolean | – |
frontmatter`"readonly"` protects a leading YAML block: typing, deleting or pasting into it, or across its fences, leaves it as written, so the Markdown round-trips byte for byte. After a closing fence that ends the note, the body is still editable: what goes in there starts on a new line. A new `value` still replaces it. `"editable"` by default. | FrontmatterEditing | – |
initialCaretWhere the caret starts: on the line after a leading YAML block (default), or `"start"`. Read at mount. | InitialCaret | – |
placeholder | string | – |
resolveWikilink | WikilinkResolver | – |
suggestWikilinks | SuggestWikilinks | – |
onWikilinkClickCtrl/Cmd+click on a wikilink or an embed, or Enter with the caret inside one. `event` is the click. | (target: string, event: Event | undefined) => void | – |
onOpenWikilinkLike `onWikilinkClick`, with the target only. | (slug: string) => void | – |
embedPreviewShows `![[target]]` off the caret as a preview: `renderEmbed`, or a link-like placeholder. Read at mount. | boolean | – |
renderEmbedWhat an embed preview shows. Keep it stable (module-level or `useCallback`). | (target: string, alias: string | undefined) => ReactNode | – |
slashCommands | SlashCommand[] | – |
codeLanguagesLanguages for fenced code, loaded when a block first uses one; read once, at mount. | CodeLanguages | – |
cspNonceNonce for the style element CodeMirror injects, for pages with a strict Content Security Policy. | string | – |
toolbar | boolean | – |
labels | Partial<MarkdownEditorLabels> | – |
className | string | – |
refGets a `MarkdownEditorHandle` once the editor has mounted, for a host's menus and an outline. | Ref<MarkdownEditorHandle | null> | – |
MarkdownEditorMenus
| Prop | Type | Default |
|---|---|---|
adapterrequired | TextEditorAdapter | undefined | – |
controllerOpens the wikilink suggestions for the editor's handle. | TriggerMenuController | – |
viewrequired | EditorView | undefined | – |
slashCommandsrequired | SlashCommand[] | – |
suggestWikilinks | SuggestWikilinks | – |
onOpenChangerequired | (open: boolean) => void | – |
runCommandRuns a slash command's `run`, so the editor knows what it calls from there. | (run: () => void) => void | runNow |
emptyLabelrequired | ReactNode | – |
MarkdownEditorPortals
| Prop | Type | Default |
|---|---|---|
storerequired | PortalStore | – |
labelsrequired | MarkdownEditorLabels | – |
optionsrequired | ViewOptions | – |
MarkdownEditorToolbar
| Prop | Type | Default |
|---|---|---|
labelsrequired | MarkdownEditorLabels | – |
onFormatrequired | (format: ToolbarFormat) => void | – |
onHeadingrequired | (level: number) => void | – |
headingLevelrequired | () => number | – |
restoreFocusrequired | () => HTMLElement | null | – |
Other exports
markdownEditorLabelsEnmarkdownEditorLabelsEsmarkdownSlashCommandscreateAdapterBridgemenuAwareKeymapmenuOpenopenProvisionallysetMenuOpenblockPreviewBLOCK_KINDSFORMATSinsertBlockinsertAtCaretinsertAtSelectioninsertLinkinsertWikilinklineSeparatorExtensionlineSeparatorOfminimalChangereplaceDocumenttoDocTexttoggleFormatwrapSelectioncreateEditorHandlerunCommandtoEditorPostoOffsetdecorateTreehiddenlineslotdeleteMarkerdispatchLineChangesheadingLevelindentListoutdentListsetHeadingWIKILINK_QUERYlivePreviewpreviewConfigtoggleTaskAtmarkdownDocumentmarkdownEditorcreateEditorStatefromValuereconfiguretoggleTaskOnLinewikilinkAtblockMarkscaretOutOfPrefixfrontmatterEndlineStructurelistDepthactiveProvisionalcommittedDoccommittedSelectioncommittedTextisProvisionalprovisionalTriggersrevertProvisionalsettleProvisionaltypeProvisionaluseMarkdownSyncmarkdownEditorThemeBulletWidgetCodeLanguageWidgetEmbedWidgetLabelWidgetPortalStoreTableWidgetTaskWidgetuseEditorHandleuseMarkdownEditorViewuseRequestMeasureuseResizeObservertype FrontmatterEditingtype InitialCarettype InsertTextOptionstype MarkdownBlockKindtype MarkdownConflicttype MarkdownEditorCommandtype MarkdownEditorHandletype MarkdownEditorLabelstype MarkdownEditorPropstype MarkdownSelectiontype SlashCommandtype SuggestWikilinkstype MarkdownFormattype HandleMenustype Spantype TreeContexttype SlashLabelstype WikilinkSuggestiontype PreviewConfigtype MarkdownEditorActionstype MarkdownEditorOptionstype CodeLanguagestype EditorHookstype EditorSettingstype LineStructuretype ProvisionalTriggertype ToolbarFormattype PortalEntryAccessibility
- Named by label; the toolbar is one tab stop with arrow keys, and what doesn't fit moves into More.
- Escape then Tab leaves the editor. Shortcuts: Mod+B/I/E, Mod+Alt+0..6 for headings.
- A heading, quote or list command leaves the caret after its marker, so typing goes into the text.
- The slash menu says so when nothing matches, and stays open until Escape or a space.
Agent actions
Agents read and set the Markdown text through the editor's own part.
Runs agent.snapshot() from @ui-sdk/react/agent on the live preview above.