Markdown Viewer
Markdown rendering with tables, task lists, alerts, highlighted code, and sanitized HTML.
Installation
pnpm dlx shadcn@latest add @sui/markdown-viewerInstall with the shadcn CLI or use the shared @workspace/ui package. Follow the installation guide to configure the registry, load styles, and choose import aliases.
Usage
import { MarkdownViewer } from "@workspace/ui/viewers/markdown-viewer";
<MarkdownViewer content="# Release notes\n\nA **Markdown** document." />;Supported formatting
Supports headings, links, tables, task lists, strikethrough, line breaks, and fenced code. Fenced code, including blocks without a language, and indented code use Code Viewer. Blocks without a language render as plain text and retain copying and line breaks. Inline code remains inline. GitHub-style [!NOTE], [!TIP], [!IMPORTANT], [!WARNING], and [!CAUTION] blocks have semantic visual treatments.
Heading anchors are scoped to each viewer instance. A Contents, Table of Contents, or TOC heading can generate a table of contents. Math syntax is parsed, but this component does not load a dedicated mathematical typesetting engine.
HTML and editable content
Embedded HTML is parsed and sanitized before rendering. Common safe formatting, including details and summary, is retained; scripts and event-handler attributes are removed. The example lets you edit Markdown containing a disclosure and a script that is not executed. Unlike Html Viewer, this component does not execute HTML scripts.
Images
Markdown images render as Image with preview on, and all images in one document form a single gallery. They keep their natural size up to the column width. labels.image translates the preview. Override components.img to render them differently.
Streaming
Set status="streaming" while content is still arriving, such as an AI reply. An unfinished last code fence is closed for rendering, so the rest of the reply is not swallowed into it, and that block shows CodeViewer's writing state. Set status="complete" or omit it once the content is final.
<MarkdownViewer content={reply} status={done ? "complete" : "streaming"} />Extending
remarkPlugins run after the built-in ones (GFM, heading ids, table of contents, breaks, math and GitHub alerts). rehypePlugins run after raw HTML is parsed and before sanitizing, which always runs last, so a plugin cannot reintroduce scripts or arbitrary classes. To change how an element looks, pass components, which are merged over the built-in renderers.
<MarkdownViewer
content={content}
remarkPlugins={[remarkEmoji]}
components={{ a: (props) => <Link {...props} /> }}
/>Theme and localization
The content follows the surrounding theme in both directions when appearance changes. An explicit theme="dark" creates a local dark boundary; removing it resumes inheritance. Fenced code displays its language once; use CodeViewer directly when a separate file title is needed. Use className for container layout. labels.empty customizes the empty state, while note, tip, important, warning, and caution translate alert headings. labels.code accepts CodeViewer labels for embedded code blocks, including copy, loading, and failure feedback. Markdown content itself is provided by your application and is not translated automatically.
API reference
| Prop | Type | Default / behavior |
|---|---|---|
content | string | Required Markdown source. |
theme | "light" | "dark" | Follows surrounding theme. |
className | string | Container layout classes. |
labels | Partial<MarkdownViewerLabels> | Empty and alert labels; code and image translate embedded CodeViewer and Image feedback. Defaults to English. |
status | "streaming" | "complete" | "complete". |
remarkPlugins, rehypePlugins | Plugin lists | Added to the built-in plugins; sanitizing runs last. |
components | Components | Merged over the built-in element renderers. |
The module exports MarkdownViewerProps, MarkdownViewerLabels, and the label presets markdownViewerLabels and markdownViewerLabelsZhCN; pass markdownViewerLabelsZhCN to labels for Chinese. Rendering uses react-markdown.