YuhuanStudioYunUIDocs
Content

Markdown Renderer

Full GFM markdown with math, Shiki code, Mermaid diagrams, GitHub callouts and lazy zoomable images.

The full content renderer. Give it a markdown string and it renders GitHub-flavored markdown (tables, task lists, strikethrough), KaTeX math (inline $…$ and block $$…$$), Shiki-highlighted fenced code blocks, ```mermaid diagrams, GitHub-style > [!NOTE] callouts, heading anchors, and lazy, zoomable images — all in one component. Rewrite image/link URLs with urlTransform, and pass onCodeEdit to surface an "Edit" button on code blocks.

Import

tsx1 line
import { MarkdownRenderer } from "@yuhuanowo/yunui/content";

Requires @yuhuanowo/yunui/content.css and katex/dist/katex.min.css to be imported by the host app, plus the content peer dependencies (see Content overview).

Example

The demo below feeds a single markdown string exercising headings, a table, a task list, a fenced code block, a Mermaid diagram, math, and a callout.

MarkdownRenderer

Markdown renderer

Full GFM with emphasis, strikethrough, links and inline code.

Note

Callouts use GitHub's > [!TYPE] blockquote syntax — note, tip, important, warning, caution.

Table

FeatureSupportedNotes
Tables✅GitHub-flavored
Task list✅Interactive checkboxes
Math✅KaTeX inline & block

Task list

  • Render headings, tables and lists
  • Highlight code with Shiki
  • Ship to production

Code

TypeScript3 lines
export function greet(name: string): string {
  return `Hello, ${name}!`;
}

Math

The Gaussian integral is ∫−∞∞e−x2 dx=π\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}, and in block form:
E=mc2E = mc^2

Diagram

Rendering diagram…

Usage

tsx5 linesScroll horizontally
<MarkdownRenderer
content={message.content}
urlTransform={(url) => (url.startsWith("/") ? `${API_HOST}${url}` : url)}
onCodeEdit={({ code, language, filename }) => openInEditor({ code, language, filename })}
/>

Raw HTML in the Markdown input is sanitized after heading IDs are generated and before KaTeX math rendering. Common formatting and supported Markdown features such as GFM tables, task-list checkboxes, callouts, and code highlighting remain available. Unsafe or unsupported raw HTML is cleaned: scripts, iframes, embedded objects, inline event handlers, and unsafe attributes such as inline styles are removed. This is input sanitization for this renderer, not a guarantee that arbitrary content is risk-free.

urlTransform can rewrite image and link URLs, but the rewritten result still passes through React Markdown's safe-protocol filter. Relative URLs and supported protocols such as http:, https:, mailto:, irc:, ircs:, and xmpp: are accepted; unsafe protocols such as javascript: and data: are rejected.

linkOrigin optionally supplies a stable site origin for link classification in both SSR and the browser. Same-origin absolute links are treated as internal; other absolute links are external. Without linkOrigin, absolute HTTP(S) and protocol-relative URLs are external, while relative and fragment links are internal.

tsx1 line
<MarkdownRenderer content={markdown} linkOrigin="https://example.com" />

Props

PropTypeDefaultDescription
classNamestring—
content*string—
linkOriginstring—Stable origin used to recognize same-origin absolute links, in SSR and the browser alike (e.g. "https://example.com"). Without it, absolute HTTP(S) links open externally; relative and fragment links remain internal.
onCodeEdit((payload: CodeBlockEditPayload) => void)—When provided, code blocks show an "Edit" button that calls this with the code payload (e.g. open it in an editor). Hidden if unset.
renderLink((link: { href?: string; children: ReactNode; isExternal: boolean; }) => ReactNode)—Intercept a rendered Markdown link. Return a node to replace the default link, or `undefined` to retain YunUI's normal internal/external link UI. Useful for typed inline citations encoded as fragment links.
urlTransform((url: string) => string)—Rewrite image/link URLs before rendering (e.g. prefix a backend host or proxy). Rewritten URLs still pass the renderer's safe-protocol filter.

On this page