Skip to content
Eskais UI

Editor

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

188 characters of plain Markdown

Installation

npx shadcn@latest add @eskais/markdown-editor
npm packages@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-react

Usage

Import
import {  MarkdownEditor,  markdownEditorLabelsEn,  markdownEditorLabelsEs,  markdownSlashCommands,} from "@/components/ui/markdown-editor"import {  createAdapterBridge,  menuAwareKeymap,  menuOpen,  openProvisionally,  setMenuOpen,} from "@/components/ui/markdown-editor-adapter"import { blockPreview } from "@/components/ui/markdown-editor-blocks"import {  BLOCK_KINDS,  FORMATS,  insertBlock,  insertAtCaret,  insertAtSelection,  insertLink,  insertWikilink,  lineSeparatorExtension,  lineSeparatorOf,  minimalChange,  replaceDocument,  toDocText,  toggleFormat,  wrapSelection,} from "@/components/ui/markdown-editor-commands"import { createEditorHandle, runCommand, toEditorPos, toOffset } from "@/components/ui/markdown-editor-handle"import { decorateTree, hidden, line, slot } from "@/components/ui/markdown-editor-inline"import { markdownEditorLabelsEn, markdownEditorLabelsEs } from "@/components/ui/markdown-editor-labels"import {  deleteMarker,  dispatchLineChanges,  headingLevel,  indentList,  outdentList,  setHeading,} from "@/components/ui/markdown-editor-lines"import { MarkdownEditorMenus, markdownSlashCommands, WIKILINK_QUERY } from "@/components/ui/markdown-editor-menus"import { MarkdownEditorPortals } from "@/components/ui/markdown-editor-portals"import { livePreview, previewConfig, toggleTaskAt } from "@/components/ui/markdown-editor-preview"import { markdownDocument, markdownEditor } from "@/components/ui/markdown-editor-primitive"import {  createEditorState,  fromValue,  reconfigure,  toggleTaskOnLine,  wikilinkAt,} from "@/components/ui/markdown-editor-state"import { blockMarks, caretOutOfPrefix, frontmatterEnd, lineStructure, listDepth } from "@/components/ui/markdown-editor-structure"import {  activeProvisional,  committedDoc,  committedSelection,  committedText,  isProvisional,  provisionalTriggers,  revertProvisional,  settleProvisional,  typeProvisional,} from "@/components/ui/markdown-editor-provisional"import { useMarkdownSync } from "@/components/ui/markdown-editor-sync"import { markdownEditorTheme } from "@/components/ui/markdown-editor-theme"import { MarkdownEditorToolbar } from "@/components/ui/markdown-editor-toolbar"import {  BulletWidget,  CodeLanguageWidget,  EmbedWidget,  LabelWidget,  PortalStore,  TableWidget,  TaskWidget,} from "@/components/ui/markdown-editor-widgets"import {  useEditorHandle,  useMarkdownEditorView,  useRequestMeasure,  useResizeObserver,} from "@/components/ui/use-markdown-editor"

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

PropTypeDefault
labelrequiredAccessible name of the editor and its text box.string–
valuestring–
defaultValuestring–
onChangeDebounced by `changeDelay`; flushed on blur and unmount. Never called for `value` itself.(markdown: string) => void–
changeDelaynumber–
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–
readOnlyboolean–
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–
placeholderstring–
resolveWikilinkWikilinkResolver–
suggestWikilinksSuggestWikilinks–
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–
slashCommandsSlashCommand[]–
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–
toolbarboolean–
labelsPartial<MarkdownEditorLabels>–
classNamestring–
refGets a `MarkdownEditorHandle` once the editor has mounted, for a host's menus and an outline.Ref<MarkdownEditorHandle | null>–

MarkdownEditorMenus

PropTypeDefault
adapterrequiredTextEditorAdapter | undefined–
controllerOpens the wikilink suggestions for the editor's handle.TriggerMenuController–
viewrequiredEditorView | undefined–
slashCommandsrequiredSlashCommand[]–
suggestWikilinksSuggestWikilinks–
onOpenChangerequired(open: boolean) => void–
runCommandRuns a slash command's `run`, so the editor knows what it calls from there.(run: () => void) => voidrunNow
emptyLabelrequiredReactNode–

MarkdownEditorPortals

PropTypeDefault
storerequiredPortalStore–
labelsrequiredMarkdownEditorLabels–
optionsrequiredViewOptions–

MarkdownEditorToolbar

PropTypeDefault
labelsrequiredMarkdownEditorLabels–
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 PortalEntry

Accessibility

  • 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.