MarkdownEditor
WYSIWYG/source/preview Markdown editor with sanitization and toolbar actions.
MarkdownEditor
Basic Usage
MarkdownEditor
Loading demo...
Interaction Contract
modeis controlled when provided; otherwisedefaultModeinitializes internal mode.update:modeandmode-changeemit whenever the user changes modes.sanitizedefaults totrue; 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 througharia-pressed(notrole="tab", since there are no associated tabpanels). theme="auto"watchesdocumentElementclass /data-themeand falls back to light.
API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
modelValue / v-model | string | '' | Markdown source. |
placeholder | string | '' | Placeholder shown in editable modes. |
mode | 'wysiwyg' | 'source' | 'preview' | - | Controlled editor mode. |
defaultMode | 'wysiwyg' | 'source' | 'preview' | 'wysiwyg' | Initial internal mode. |
disabled | boolean | false | Disable toolbar and fields. |
readonly | boolean | false | Prevent edits while allowing reading. |
sanitize | boolean | true | Sanitize rendered HTML with DOMPurify. |
theme | 'auto' | 'light' | 'dark' | 'auto' | Editor theme. |
toolbar | boolean | true | Show toolbar and mode switcher. |
toolbarActions | MarkdownEditorToolbarActionKey[] | built-in set | Ordered toolbar actions. |
minHeight | string | number | 220 | Editor body minimum height. |
maxHeight | string | 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
| Event | Params | Description |
|---|---|---|
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
| Method | Description |
|---|---|
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=truefor user-authored or provider-authored content. - Use controlled
modewhen the host stores the current editor tab in route/query state. - Restrict
toolbarActionsfor narrow editors such as release notes or comments. - Provide
linkPromptinstead 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, andmarkdown-editor.test.ts. - Sanitization is enabled by default through DOMPurify;
sanitize=falseshould remain a trusted-content escape hatch only. linkPromptis 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.tsverifies exports, sanitized WYSIWYG rendering, source-mode input updates, uncontrolled mode emits, therole="group"/aria-pressedmode switcher, and rich-content serialization back to Markdown.
查看源码
packages/tuffex/packages/components/src/markdown-editor/index.ts