Components/MarkdownEditor

MarkdownEditor

WYSIWYG/source/preview Markdown editor with sanitization and toolbar actions.

VerifiedSince 1.0.0

MarkdownEditor

Basic Usage

MarkdownEditor

Loading demo...

Interaction Contract

  • mode is controlled when provided; otherwise defaultMode initializes internal mode.
  • update:mode and mode-change emit whenever the user changes modes.
  • sanitize defaults to true; disable it only for already trusted content.
  • Source mode writes directly to Markdown. WYSIWYG mode serializes edited DOM back to Markdown on input.
  • Toolbar actions are shared across modes, but preview mode disables editing actions.
  • The mode switcher is a toggle-button group: the container is role="group" labeled "Markdown editor mode", and each mode button reflects the active mode through aria-pressed (not role="tab", since there are no associated tabpanels).
  • theme="auto" watches documentElement class / data-theme and falls back to light.

API

Props

PropTypeDefaultDescription
modelValue / v-modelstring''Markdown source.
placeholderstring''Placeholder shown in editable modes.
mode'wysiwyg' | 'source' | 'preview'-Controlled editor mode.
defaultMode'wysiwyg' | 'source' | 'preview''wysiwyg'Initial internal mode.
disabledbooleanfalseDisable toolbar and fields.
readonlybooleanfalsePrevent edits while allowing reading.
sanitizebooleantrueSanitize rendered HTML with DOMPurify.
theme'auto' | 'light' | 'dark''auto'Editor theme.
toolbarbooleantrueShow toolbar and mode switcher.
toolbarActionsMarkdownEditorToolbarActionKey[]built-in setOrdered toolbar actions.
minHeightstring | number220Editor body minimum height.
maxHeightstring | number-Editor body maximum height.
linkPrompt(selectedText: string) => string | Promise<string>-Async URL provider for link action.

Toolbar Actions

heading, bold, italic, strike, quote, code, bulletList, orderedList, link, undo, redo.

Slots

No public slots are exposed; toolbar buttons, editing surfaces, and preview output are internal.

Events

EventParamsDescription
update:modelValue(value: string)v-model update.
change(value: string)Markdown value changed.
update:mode(mode: MarkdownEditorMode)Mode update for controlled use.
mode-change(mode: MarkdownEditorMode)Mode changed.
focus()Editable surface or source field focused.
blur()Editable surface or source field blurred.

Exposed Methods

MethodDescription
focus()Focus the active editable surface.
blur()Blur the active editable surface.
setMode(mode)Change editor mode.
getMode()Return current mode.
getValue()Return current Markdown source.
setValue(value)Set Markdown, emit updates, and refresh rendered HTML.

Best Practices

  • Keep sanitize=true for user-authored or provider-authored content.
  • Use controlled mode when the host stores the current editor tab in route/query state.
  • Restrict toolbarActions for narrow editors such as release notes or comments.
  • Provide linkPrompt instead of letting the component own product-specific URL dialogs.

Review Notes

  • Reviewed against packages/tuffex/packages/components/src/markdown-editor/src/types.ts, TxMarkdownEditor.vue, markdown-serializer.ts, and markdown-editor.test.ts.
  • Sanitization is enabled by default through DOMPurify; sanitize=false should remain a trusted-content escape hatch only.
  • linkPrompt is the extension point for product-specific URL collection, keeping dialogs outside the component contract.

Source

  • Component source: packages/tuffex/packages/components/src/markdown-editor/src/TxMarkdownEditor.vue.
  • Types: packages/tuffex/packages/components/src/markdown-editor/src/types.ts.
  • Serializer: packages/tuffex/packages/components/src/markdown-editor/src/markdown-serializer.ts.
  • Verified coverage: Coverage: packages/tuffex/packages/components/src/markdown-editor/__tests__/markdown-editor.test.ts verifies exports, sanitized WYSIWYG rendering, source-mode input updates, uncontrolled mode emits, the role="group" / aria-pressed mode switcher, and rich-content serialization back to Markdown.
查看源码
packages/tuffex/packages/components/src/markdown-editor/index.ts