# Svelte Markdown - Complete Reference > A powerful, customizable markdown and HTML renderer for Svelte 5 — built for rendering streaming AI agent output from Claude Code, ChatGPT, and agentic workflows. Built on Marked and HTMLParser2 with 24 markdown renderers, 83 HTML tag renderers, LRU token caching, allow/deny filtering, and XSS-safe defaults. ## Package Information - Package: `@humanspeak/svelte-markdown` - License: MIT - Requires: Svelte 5 - Parser: Marked - HTML Parser: HTMLParser2 - Heading IDs: github-slugger - Maintained by: Humanspeak, Inc. - Homepage: https://markdown.svelte.page - Repository: https://github.com/humanspeak/svelte-markdown - Current version: see https://www.npmjs.com/package/@humanspeak/svelte-markdown ## Installation ```bash npm install @humanspeak/svelte-markdown # or pnpm add @humanspeak/svelte-markdown # or yarn add @humanspeak/svelte-markdown ``` ## SvelteMarkdown Component Props | Prop | Type | Default | Description | |------|------|---------|-------------| | source | string \| Token[] | [] | Markdown string or pre-parsed tokens to render | | streaming | boolean | false | Enable LLM streaming mode (incremental updates via `writeChunk()`) | | renderers | Partial | {} | Custom renderer components to override defaults | | options | Partial | {} | Marked parser configuration options | | isInline | boolean | false | Parse as inline markdown (no block-level elements) | | parsed | (tokens: Token[] \| TokensList) => void | () => {} | Callback invoked with parsed tokens | | extensions | MarkedExtension[] | [] | Array of marked extensions (KaTeX, Mermaid, alerts, footnotes) | | sanitizeUrl | SanitizeUrlFn | defaultSanitizeUrl | URL sanitizer applied before render — protocol allowlist by default | | sanitizeAttributes | SanitizeAttributesFn | defaultSanitizeAttributes | Attribute sanitizer applied before render — strips `on*` and `srcdoc` by default | ## Imperative Streaming Methods When `streaming={true}`, the component exposes two methods via `bind:this`: | Method | Signature | Purpose | |--------|-----------|---------| | writeChunk | `(chunk: StreamingChunk) => void` | Append a string chunk OR overwrite at an offset (`{ value, offset }`) | | resetStream | `(nextSource?: string) => void` | Clear the streaming buffer; optionally seed with a new initial source | ```typescript type StreamingChunk = string | StreamingOffsetChunk type StreamingOffsetChunk = { value: string; offset: number } ``` ## Configuration Options | Option | Type | Default | Description | |--------|------|---------|-------------| | headerIds | boolean | true | Generate id attributes on headings (via github-slugger) | | headerPrefix | string | '' | Prefix for generated heading IDs | | gfm | boolean | true | Enable GitHub Flavored Markdown | | breaks | boolean | false | Convert single newlines to br | ## Basic Usage ```svelte ``` ## Streaming AI Agent Output The library is purpose-built to render LLM streaming output that mixes markdown and HTML. Partial nested HTML structures (e.g. an unclosed `
` that later receives its `
`) reconcile correctly as content arrives, and built-in sanitization runs per token so a `javascript:` URL or `on*=` handler emitted mid-stream is blocked before the DOM ever sees it. ### Imperative API (recommended) ```svelte ``` ### Websocket-style offset chunks Out-of-order delivery is supported via overwrite-at-position semantics: ```typescript markdown?.writeChunk({ value: ' world', offset: 5 }) markdown?.writeChunk({ value: 'Hello', offset: 0 }) ``` The first successful write locks the stream into one input mode (append or offset) until `resetStream()` is called. ### With the Anthropic SDK (Claude) ```svelte ``` ### With the OpenAI SDK (ChatGPT) ```svelte ``` ### Reactive source prop (alternative) ```svelte ``` ## XSS-Safe Defaults The library applies built-in sanitization in the Parser before tokens reach any renderer or snippet, so custom renderers cannot bypass it. ### Defaults applied automatically - **URL protocol allowlist** (`defaultSanitizeUrl`) — `href`, `src`, `action`, `formaction`, `cite`, `data`, `poster` and markdown link/image URLs are restricted to `http:`, `https:`, `mailto:`, `tel:`, and relative URLs. `javascript:`, `vbscript:`, `data:`, and `blob:` are blocked (including mixed-case and leading-whitespace variants). - **Event handler stripping** (`defaultSanitizeAttributes`) — every `on*` attribute (`onclick`, `onerror`, `onload`, etc.) is removed. - **`srcdoc` stripping** — prevents iframe HTML injection. - **No ``) rather than executing or applying them. - **Secure HTML parsing** — HTMLParser2's streaming parser, not `innerHTML`. ### Custom sanitization ```svelte ``` ### Disable sanitization (for trusted content only) ```svelte ``` ### Known gaps (not handled by defaults) - Inline `style="..."` attributes are NOT sanitized. Modern browsers don't execute JavaScript via CSS, but visual hijacking and exfiltration via background-image URLs are possible. - `