MarkdownView Markdown
GitHub-flavored Markdown renderer with explicit sanitization and light/dark theme modes.
Usage
Use content as the single source string. marked is configured with GFM and soft line breaks enabled.
Loading demo...
Best Practices
- Keep
sanitizeenabled for user-generated, remote, or plugin-provided content. - Prefer short, already-normalized Markdown strings; do not pass partially escaped HTML unless you intentionally rely on Markdown HTML support.
- Set
themeexplicitly inside previews, cards, or split light/dark comparisons where the surrounding page theme is not representative. - Use
TxMarkdownViewfor display-only content. Pair it with an editor or textarea outside the component when users need to edit Markdown.
API Reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
content | string | required | Markdown source rendered into the .markdown-body container. |
sanitize | boolean | true | Sanitizes generated HTML with DOMPurify. While DOMPurify is loading, sanitized output is withheld. |
theme | 'auto' | 'light' | 'dark' | 'auto' | auto resolves from document data-theme / theme classes and falls back to light. |
Slots
No slots. The rendered HTML is fully derived from content.
Events
No component events.
Rich Markdown
Tables, task lists, blockquotes, inline code, and fenced code blocks are rendered into the internal .markdown-body container.
Loading demo...
Theme Preview
Set theme explicitly when the surrounding surface is known. Leave it as auto when the page owns the theme state.
Loading demo...
Overview
- Markdown is parsed with
markedusinggfm: trueandbreaks: true. - With
sanitize=true, raw generated HTML is never rendered before DOMPurify resolves. - With
sanitize=false,markedoutput is written directly withv-html; only use this for trusted Markdown. theme="auto"checkshtml/bodydata-themeandlight/darkclasses, then observeshtmlclass anddata-themechanges.- The public wrapper receives
class="light"orclass="dark"and a matchingdata-themeattribute. - The bundled GitHub-Markdown stylesheet is a global import, so every rule in it is scoped to
:where(.tx-markdown-view, .tx-stream-md)..markdown-bodyis a very generic class name — before the scope, importing this component restyled any host page that used it for its own prose.:where()adds no specificity, so the scope confines the sheet without changing how its rules compete with each other.
Technologies
- Security note:
sanitize=truewaits for DOMPurify and renders an empty body if the sanitizer import fails. Do not disable sanitization for plugin, remote, or user-authored Markdown unless it has already been trusted and normalized upstream. - Theme note:
theme="auto"reads bothhtmlandbodytheme markers initially, but the mutation observer watches onlyhtmlclass anddata-themechanges. Prefer explicitthemefor embedded previews that do not follow the document root. - Verified coverage:
markdown-view.test.tschecks sanitizer withholding, sanitized rendering, raw rendering whensanitize=false, explicit light/dark themes, auto-theme resolution from document markers, observer updates, and light fallback. - Component source:
packages/tuffex/packages/components/src/markdown-view/src/TxMarkdownView.vue. - Types:
packages/tuffex/packages/components/src/markdown-view/src/types.tsexportsMarkdownViewProps. - Styles:
packages/tuffex/packages/components/src/markdown-view/src/github-markdown.cssis imported by the component stylesheet, andTxStreamMarkdownimports the same file. Every rule in it is scoped to:where(.tx-markdown-view, .tx-stream-md)— keep the scope if you re-vendor it. - Export alias:
packages/tuffex/packages/components/src/markdown-view/index.tsexportsMarkdownView,TxMarkdownView,MarkdownViewProps, andTxMarkdownViewInstance. - Coverage:
packages/tuffex/packages/components/src/markdown-view/__tests__/markdown-view.test.tsverifies sanitization and theme behavior.
查看源码
packages/tuffex/packages/components/src/markdown-view/index.ts