Quickstart
Requirements
- A browser with ES2022 and Shadow DOM support.
- A bundler that can load ESM packages. Vite, Rollup, webpack, and esbuild all work.
- Vue 3.3 or newer only if you use the
stream-diffs/vueentry.
Install
pnpm add stream-diffsnpm install stream-diffsyarn add stream-diffsThe renderer is a runtime dependency. Shiki grammars and themes selected by your options are loaded by @pierre/diffs on first mount.
Stream code into a page
import { createCodeStream } from 'stream-diffs'
const output = createCodeStream({
fileName: 'answer.ts',
language: 'typescript',
theme: { dark: 'github-dark', light: 'github-light' },
themeType: 'system',
maxHeight: 520,
lineNumbers: true,
autoScroll: 'near-bottom',
})
await output.mount(document.querySelector<HTMLElement>('#output')!)
output.append('export const ')
output.append('answer = 42\n')
await output.finalize({ view: 'file' })While the stream is active, the surface is intentionally plain text. Highlighting starts at finalization, where one accurate snapshot is available.
Finalize to a diff
Pass the previous source and the diff is computed from the complete text:
await output.finalize({
view: 'diff',
original: previousSource,
diffStyle: 'unified', // or 'split'
diffIndicators: 'bars',
lineDiffType: 'word-alt',
enableLineSelection: true,
})finalize() can only be called once per stream. Call reset() to reuse the controller for another answer.
Render a static diff
When both sides already exist, skip the stream entirely:
import { createDiffSurface } from 'stream-diffs'
const before = `export function total(items) {
return items.reduce((sum, item) => sum + item.price, 0)
}
`
const after = `export function total(items) {
return items.reduce((sum, item) => {
if (!Number.isFinite(item.price))
throw new TypeError('price must be finite')
return sum + item.price
}, 0)
}
`
const surface = createDiffSurface({
kind: 'diff',
oldFile: { name: 'src/cart.ts', contents: before, lang: 'typescript' },
newFile: { name: 'src/cart.ts', contents: after, lang: 'typescript' },
options: {
diffStyle: 'split',
enableLineSelection: true,
},
})
await surface.mount(document.querySelector<HTMLElement>('#review')!)The same controller accepts kind: 'file', kind: 'patch', and kind: 'merge-conflict'. See Diffs and review.
Vue components
pnpm add stream-diffs vue<script setup lang="ts">
import { StreamCode, StreamDiff } from 'stream-diffs/vue'
const before = 'export const answer = 0\n'
const after = 'export const answer = 42\n'
</script>
<template>
<StreamCode
:code="after"
file-name="answer.ts"
language="typescript"
:loading="false"
/>
<StreamDiff
:original="before"
:modified="after"
language="typescript"
diff-style="split"
/>
</template>StreamCode keeps its fallback until the block is complete and visible. That behavior is designed for streamed Markdown. See Vue for props and controller access.
Drop into markstream-vue
pnpm add markstream-vue stream-diffs<script setup lang="ts">
import { MarkdownRender } from 'markstream-vue'
import 'markstream-vue/index.css'
</script>
<template>
<MarkdownRender :content="markdown" :loading="streaming" />
</template>When both optional runtimes are installed, markstream selects stream-diffs. See markstream-vue integration.
Try it without writing code
The playground runs the public controller API on a live page. Edit both sources, switch unified and split, annotate a line, then start an inline edit session on the rendered diff.