|
| 1 | +--- |
| 2 | +title: Custom renderers |
| 3 | +description: Register custom renderers for arbitrary code fence languages. |
| 4 | +type: reference |
| 5 | +summary: Map code fence languages to custom React components for rendering charts, diagrams, and more. |
| 6 | +prerequisites: |
| 7 | + - /docs/configuration |
| 8 | +related: |
| 9 | + - /docs/plugins/mermaid |
| 10 | + - /docs/code-blocks |
| 11 | + - /docs/plugins |
| 12 | +--- |
| 13 | + |
| 14 | +The `renderers` field on `PluginConfig` lets you register custom React components for arbitrary code fence languages. Use this to render Vega-Lite charts, D2 diagrams, PlantUML, or any other visualization — without forking Streamdown. |
| 15 | + |
| 16 | +Custom renderers take priority over default code blocks. If a custom renderer matches a language, it renders instead of the default `CodeBlock`. You can even override mermaid by registering a renderer for the `"mermaid"` language. |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Pass an array of `{ language, component }` objects to `plugins.renderers`: |
| 21 | + |
| 22 | +```tsx title="chat.tsx" lineNumbers |
| 23 | +import { Streamdown } from "streamdown"; |
| 24 | +import type { CustomRendererProps } from "streamdown"; |
| 25 | + |
| 26 | +const VegaLiteChart = ({ code, language, isIncomplete }: CustomRendererProps) => { |
| 27 | + if (isIncomplete) { |
| 28 | + return <div className="animate-pulse h-48 bg-muted rounded-lg" />; |
| 29 | + } |
| 30 | + |
| 31 | + const spec = JSON.parse(code); |
| 32 | + return <MyVegaRenderer spec={spec} />; |
| 33 | +}; |
| 34 | + |
| 35 | +export default function Chat() { |
| 36 | + return ( |
| 37 | + <Streamdown |
| 38 | + plugins={{ |
| 39 | + renderers: [ |
| 40 | + { language: "vega-lite", component: VegaLiteChart }, |
| 41 | + ], |
| 42 | + }} |
| 43 | + > |
| 44 | + {markdown} |
| 45 | + </Streamdown> |
| 46 | + ); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Multiple languages |
| 51 | + |
| 52 | +The `language` field accepts a string or an array of strings: |
| 53 | + |
| 54 | +```tsx |
| 55 | +const renderers = [ |
| 56 | + { language: ["vega", "vega-lite"], component: VegaLiteChart }, |
| 57 | + { language: "d2", component: D2Diagram }, |
| 58 | + { language: "plantuml", component: PlantUMLDiagram }, |
| 59 | +]; |
| 60 | + |
| 61 | +<Streamdown plugins={{ renderers }}>{markdown}</Streamdown> |
| 62 | +``` |
| 63 | + |
| 64 | +## Custom renderer props |
| 65 | + |
| 66 | +Every custom renderer receives these props: |
| 67 | + |
| 68 | +| Prop | Type | Description | |
| 69 | +|------|------|-------------| |
| 70 | +| `code` | `string` | The raw text content inside the code fence | |
| 71 | +| `language` | `string` | The language identifier from the code fence | |
| 72 | +| `isIncomplete` | `boolean` | `true` while the code fence is still being streamed | |
| 73 | + |
| 74 | +## Reusing built-in components |
| 75 | + |
| 76 | +Streamdown exports its internal code block components so your custom renderers can reuse them for consistent styling: |
| 77 | + |
| 78 | +```tsx title="vega-renderer.tsx" lineNumbers |
| 79 | +import { |
| 80 | + CodeBlockContainer, |
| 81 | + CodeBlockHeader, |
| 82 | + CodeBlockCopyButton, |
| 83 | + CodeBlockDownloadButton, |
| 84 | +} from "streamdown"; |
| 85 | +import type { CustomRendererProps } from "streamdown"; |
| 86 | + |
| 87 | +const VegaLiteChart = ({ code, language, isIncomplete }: CustomRendererProps) => { |
| 88 | + if (isIncomplete) { |
| 89 | + return ( |
| 90 | + <CodeBlockContainer language={language} isIncomplete> |
| 91 | + <CodeBlockHeader language={language} /> |
| 92 | + <div className="animate-pulse h-48 bg-muted rounded-lg" /> |
| 93 | + </CodeBlockContainer> |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <CodeBlockContainer language={language}> |
| 99 | + <CodeBlockHeader language={language} /> |
| 100 | + <MyVegaRenderer spec={JSON.parse(code)} /> |
| 101 | + </CodeBlockContainer> |
| 102 | + ); |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | +### Exported components |
| 107 | + |
| 108 | +| Component | Description | |
| 109 | +|-----------|-------------| |
| 110 | +| `CodeBlock` | Full code block with syntax highlighting and controls | |
| 111 | +| `CodeBlockContainer` | Outer wrapper with border and styling | |
| 112 | +| `CodeBlockHeader` | Language label header | |
| 113 | +| `CodeBlockCopyButton` | Copy-to-clipboard button | |
| 114 | +| `CodeBlockDownloadButton` | Download button | |
| 115 | +| `CodeBlockSkeleton` | Loading skeleton placeholder | |
| 116 | + |
| 117 | +## Streaming considerations |
| 118 | + |
| 119 | +During streaming, `isIncomplete` is `true` while the code fence is still being written. Use this to show a loading state and avoid parsing incomplete content: |
| 120 | + |
| 121 | +```tsx |
| 122 | +const VegaLiteChart = ({ code, isIncomplete }: CustomRendererProps) => { |
| 123 | + if (isIncomplete) { |
| 124 | + return ( |
| 125 | + <div className="animate-pulse h-48 bg-muted rounded-lg flex items-center justify-center"> |
| 126 | + <span className="text-muted-foreground">Loading chart...</span> |
| 127 | + </div> |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + return <MyVegaRenderer spec={JSON.parse(code)} />; |
| 132 | +}; |
| 133 | +``` |
| 134 | + |
| 135 | +## Combining with other plugins |
| 136 | + |
| 137 | +Custom renderers work alongside all other plugins. The rendering priority is: |
| 138 | + |
| 139 | +1. Custom renderers (checked first) |
| 140 | +2. Mermaid plugin (if configured) |
| 141 | +3. Default code block with syntax highlighting |
| 142 | + |
| 143 | +```tsx |
| 144 | +import { mermaid } from "@streamdown/mermaid"; |
| 145 | +import { code } from "@streamdown/code"; |
| 146 | + |
| 147 | +<Streamdown |
| 148 | + plugins={{ |
| 149 | + code, |
| 150 | + mermaid, |
| 151 | + renderers: [ |
| 152 | + { language: "vega-lite", component: VegaLiteChart }, |
| 153 | + ], |
| 154 | + }} |
| 155 | +> |
| 156 | + {markdown} |
| 157 | +</Streamdown> |
| 158 | +``` |
| 159 | + |
| 160 | +## Type reference |
| 161 | + |
| 162 | +```tsx |
| 163 | +interface CustomRendererProps { |
| 164 | + code: string; |
| 165 | + language: string; |
| 166 | + isIncomplete: boolean; |
| 167 | +} |
| 168 | + |
| 169 | +interface CustomRenderer { |
| 170 | + language: string | string[]; |
| 171 | + component: React.ComponentType<CustomRendererProps>; |
| 172 | +} |
| 173 | + |
| 174 | +interface PluginConfig { |
| 175 | + // ...existing fields |
| 176 | + renderers?: CustomRenderer[]; |
| 177 | +} |
| 178 | +``` |
0 commit comments