# Streaming Markdown

Markdown from a model, rendered as real elements — `<p>`, `<ol>`, `<table>`, `<blockquote>`, and Kernel's [Code Block](/components/code-block/) for fences — while it is still arriving. Finished blocks never re-render, and half-written syntax never flashes on screen as raw asterisks and pipes.

## Playground

Type Markdown into `content`, and toggle `streaming` to see how unfinished syntax is read.

## Props

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `content` | `string` | Required | The Markdown so far. Append to it as chunks arrive. |
| `streaming` | `boolean` | `false` | Text is still arriving: unfinished constructs (an open fence, an unclosed `**`, half a link or table row) render as their finished form would, instead of as raw syntax. |
| `animate` | `boolean` | `true` | Fade newly arrived words in while streaming. Off under reduced motion whatever this says. Default `true`. |
| `components` | `StreamingMarkdownComponents` | — | — |
| `className` | `ClassNameValue<StreamingMarkdownState>` | — | A class string, or a function of the component's state that returns one. |
| `labels` | `Partial<StreamingMarkdownLabels>` | — | Override built-in strings (e.g. for translation); unset keys keep their English default. |

### Web Components

| Tag | Attributes | Events |
| --- | --- | --- |
| `<kernel-streaming-markdown>` | `content`, `streaming`, `no-animate`, `label-opens-in-new-tab` | — |

## Stable streaming

The top level is split into blocks, and each block remembers the exact source slice it came from. Blocks are keyed by index and memoised on that slice, so a new chunk re-renders only the last block: earlier paragraphs, tables and code blocks keep their DOM nodes, their layout and the reader's text selection. Inside the live block, each word is its own span, also keyed by index, so words already on screen stay put and only the new ones mount — which is what the optional fade-in (`animate`) keys off. The fade is a short opacity and blur settle on the motion tokens, and it is off under reduced motion.

While `streaming` is true, unfinished constructs render as their finished form would:

- an unterminated fence is an open code block, following its last line;
- `**bold` with no closer yet is bold so far, and a marker with nothing after it yet is hidden;
- `[text](https://exa` shows *text* and hides the half-written destination;
- a table renders as soon as its header row arrives, and a half row is a row;
- a last line that is only the start of a marker (`-`, `#`, `>`) is held back until it means something.

In the elements package, assign `el.content` as chunks arrive. Unchanged blocks are left alone and the last one is patched in place, element by element and text node by text node — React's index-keyed reconciliation in miniature.

## What it parses

A compact, dependency-free subset of CommonMark plus GitHub's tables: paragraphs, ATX headings, emphasis, strong, strikethrough, inline code, fenced code, nested ordered and unordered lists, blockquotes, links, autolinks (`<https://…>` and bare URLs) and horizontal rules. Deliberately left out: raw HTML (always text), indented code blocks, setext headings, reference-style links, footnotes and task-list checkboxes. `parseMarkdown` is exported from both packages if you want the tree itself.

## Safety

- The parser produces data, never an HTML string, and the renderer builds elements from it — so model text can only ever become text. A `<script>` or `<img onerror>` in the content renders as those characters.
- Links keep only `http:`, `https:`, `mailto:` and relative destinations, with control characters stripped before the scheme is read. An unsafe link renders as its text. External links open in a new tab with `rel="noopener noreferrer"`.
- Images render as a link to the image and are never loaded. An image URL written by a model is a known way to leak a conversation to a third-party server the moment it renders.

## Overriding elements

`components` replaces any element by tag — each override gets the props the default would have had, so spread them to keep Kernel's styling. `codeBlock` replaces the fence renderer, which is where a highlighter plugs in. Pass a stable object: a new one every render re-renders every block.

## Accessibility

- It is deliberately not a live region. A container that announces every appended token reads a stream back word by word, or re-reads the whole answer. Announcing belongs to the transcript: [Message Scroller](/components/message-scroller/) is `role="log"` with polite announcements, and this component sets `aria-busy` while `streaming` so assistive tech can hold its updates and read the finished text once.
- Headings, lists and tables are the real elements, so screen-reader rotors and table navigation work.
- A table wider than the message scrolls inside a focusable wrapper, so it is reachable by keyboard.
- Links that open a new tab say so, in a visually hidden suffix you can translate through `labels`.
