# typstream

> Render Typst streamed from an LLM, live. typstream heals, compiles and renders Typst while a language model is still writing it: every token, a document that compiles, in the browser or on the server.

Every page below is Markdown. Append `.md` to any docs URL (or send `Accept: text/markdown`) for the
Markdown version; https://typstream.vercel.app/llms-full.txt has all of them in one file. Source code: https://github.com/ikramhasan/typstream.

Key facts: Typst is pinned to 0.15.1. Offsets in every JS API are UTF-16 indices. Most React apps need only
the `typstream` package (`import { Typstream } from "typstream"`); servers use `@typstream/node`.

- [Introduction](https://typstream.vercel.app/docs.md): What typstream is, the problem it solves, and which package to start with.

## Getting started

- [Installation](https://typstream.vercel.app/docs/getting-started/installation.md): Which typstream package to install for your app, what your bundler and browsers need, and what the engine downloads and when.
- [React](https://typstream.vercel.app/docs/getting-started/react.md): Render streamed Typst in a Vite and React app with the typstream package's Typstream component, from a fake stream to a real one.
- [Next.js and the AI SDK](https://typstream.vercel.app/docs/getting-started/nextjs-ai-sdk.md): A Next.js App Router chat whose answers are Typst documents, streamed with the Vercel AI SDK's streamText and useChat and rendered with the Typstream component, with @typstream/ai reading each answer.
- [Without a framework](https://typstream.vercel.app/docs/getting-started/vanilla.md): Render streamed Typst in plain TypeScript with @typstream/dom's mountTypstream, or drive the engine and the renderers yourself.
- [Node.js (server)](https://typstream.vercel.app/docs/getting-started/node.md): Validate, render and repair LLM-written Typst on the server with @typstream/node, including a fix loop that sends Typst's errors back to the model.

## Concepts

- [How it works](https://typstream.vercel.app/docs/concepts/how-it-works.md): Why streaming Typst is harder than streaming Markdown, and the pipeline typstream runs every token through, from the model's text to pixels.
- [Healing](https://typstream.vercel.app/docs/concepts/healing.md): How @typstream/heal turns any prefix of a model's Typst output into a source that parses, which text it never touches, and the lint rules that fix Markdown habits.
- [Streaming and final mode](https://typstream.vercel.app/docs/concepts/streaming.md): The lifecycle of a typstream document, from the first token to the finished answer, its statuses, what final mode changes, and how regenerating and editing work.
- [Render targets](https://typstream.vercel.app/docs/concepts/render-targets.md): The two ways typstream shows a document, semantic HTML or pages, how each one renders and updates, and which to pick.
- [The engine](https://typstream.vercel.app/docs/concepts/engine.md): How @typstream/engine compiles streamed Typst in a Web Worker, when it compiles, how the watchdog keeps runaway documents from freezing the page, and its limits.
- [Fixers and fallback](https://typstream.vercel.app/docs/concepts/fallback.md): What the engine does when a healed document still doesn't compile: diagnostic fixers that repair errors only the compiler finds, and a fallback ladder that always leaves something to show.
- [Plugins](https://typstream.vercel.app/docs/concepts/plugins.md): The plugin model that extends every typstream package at once, how plugins are ordered and scoped, and the plugins typstream ships.

## Guides

- [Prompting the model](https://typstream.vercel.app/docs/guides/prompting.md): How to get Typst that compiles from an LLM, with @typstream/edu's buildSystemPrompt or a short hand-written prompt.
- [Streaming edits](https://typstream.vercel.app/docs/guides/streaming-edits.md): Let a model change part of a finished document. Its edits quote the text they replace, stream into place while the rest stays, and end with Keep or Undo, with @typstream/ai's edit session.
- [Worksheets, quizzes and answer keys](https://typstream.vercel.app/docs/guides/edu-documents.md): Use @typstream/edu to render teachers' documents with numbered questions, hidden answers, a teacher's copy and an answer key, in English, Arabic or Bengali.
- [Typst packages](https://typstream.vercel.app/docs/guides/typst-packages.md): Let documents import Typst Universe packages such as CeTZ from an allowlist, add your own, and mount local packages.
- [Images](https://typstream.vercel.app/docs/guides/images.md): Show the images a document names, from your own files or the web, and what a document shows for an image it can't have.
- [Fonts and languages](https://typstream.vercel.app/docs/guides/fonts-and-languages.md): The fonts the engine ships, how Arabic, Bengali and Devanagari fonts load on demand, any Google Fonts family by name, right-to-left text in both targets, and how to add fonts such as CJK.
- [Preloading the engine](https://typstream.vercel.app/docs/guides/preloading.md): Download and start the Typst engine before the first document appears, so it renders at once instead of waiting for the wasm and fonts.
- [Exports: PDF, DOCX, HTML and source](https://typstream.vercel.app/docs/guides/exports.md): Export a rendered document as a PDF, an editable Word file, a standalone HTML file or its Typst source, in the browser or on the server, and handle export errors.
- [Theming and styling](https://typstream.vercel.app/docs/guides/theming.md): Light and dark themes, the CSS variables and classes typstream exposes, extra stylesheets, and the word fade-in and caret while streaming.
- [Telemetry and events](https://typstream.vercel.app/docs/guides/telemetry.md): What typstream reports about each compile and render, where to listen for it, and how to send it to your own analytics without collecting documents.
- [Writing a plugin](https://typstream.vercel.app/docs/guides/writing-a-plugin.md): Package your own Typst functions, styles, prompt rules and compile fixes as a typstream plugin, and load it in React, the DOM renderer or the engine.
- [Writing an island](https://typstream.vercel.app/docs/guides/islands.md): Mount an interactive component over a drawing a Typst function writes, a render island, with the Typst drawing as its fallback for print, PDF and renderers without it.
- [Validating and repairing on the server](https://typstream.vercel.app/docs/guides/server-fix-loop.md): Check an LLM-written Typst document before you save it, ask the model to fix what doesn't compile, and render the result to PDF with @typstream/node.
- [Self-hosting the engine and assets](https://typstream.vercel.app/docs/guides/self-hosting.md): What the engine worker downloads, how to serve it fast from your own origin or a CDN, how to write a custom worker, and how to share and dispose of engines.
- [Running the engine on a server](https://typstream.vercel.app/docs/guides/remote-engine.md): Serve typstream's engine protocol over WebSocket with @typstream/node, so browsers render live previews without downloading the 6 MB compiler, and connect the browser client to it.
- [Security](https://typstream.vercel.app/docs/guides/security.md): What a hostile document can and can't do to a page that renders it, the defences in each layer, and how to serve typstream under a strict Content-Security-Policy.

## Packages

- [typstream](https://typstream.vercel.app/docs/reference/typstream.md): The convenience package for React apps, with the Typstream component from @typstream/react already set up with html-compat, the math font and the vendored Typst packages.
- [@typstream/react](https://typstream.vercel.app/docs/reference/react.md): React bindings for typstream, with the Typstream component, TypstreamProvider for app-wide settings and the useTypstream() hook.
- [@typstream/ai](https://typstream.vercel.app/docs/reference/ai.md): typstream with the Vercel AI SDK. An assistant message is a Typst document (textOf, isStreaming, textStream), and an answer that edits one applies its edits as they stream (createEditSession, editPrompt).
- [@typstream/dom](https://typstream.vercel.app/docs/reference/dom.md): Framework-free renderers for typstream's output (HTML and SVG pages), mountTypstream to show an engine document in an element, and standalone HTML exports.
- [@typstream/engine](https://typstream.vercel.app/docs/reference/engine.md): Compiles streamed Typst in a Web Worker, with a main-thread client that heals, schedules and supervises compiles, and the worker-side server you can host yourself.
- [@typstream/heal](https://typstream.vercel.app/docs/reference/heal.md): The self-healing parser that turns any prefix of streamed Typst into a source that parses, with lint fixes for Markdown habits.
- [@typstream/node](https://typstream.vercel.app/docs/reference/node.md): Typst on the server, to validate and render documents, run the LLM fix loop, and serve the engine protocol over WebSocket.

## @typstream/edu

- [@typstream/edu](https://typstream.vercel.app/docs/reference/edu.md): The Typst vocabulary for teachers' documents (templates, questions, answers and keys, callouts, rubrics, drawings, chemistry) as a typstream plugin with a prompt kit.
- [Edu vocabulary](https://typstream.vercel.app/docs/reference/edu/vocabulary.md): Every function and template in @typstream/edu (@typstream/edu:0.1.0), with its signature, parameters and a live example.
- [@typstream/html-compat](https://typstream.vercel.app/docs/reference/html-compat.md): A Typst prelude and stylesheet that keep grids, alignment, shapes, placement and drawings in Typst's HTML export.
- [@typstream/universe](https://typstream.vercel.app/docs/reference/universe.md): The Typst Universe packages typstream allows by default (CeTZ, fletcher, cetz-plot, physica, unify, chemformula, alchemist), vendored with integrity hashes as a plugin.
- [@typstream/google-fonts](https://typstream.vercel.app/docs/reference/google-fonts.md): Any family of the Google Fonts library in typstream documents, by name, as a plugin; a font catalogue the engine loads families from when a document names them, in both render targets.
- [@typstream/mindmap](https://typstream.vercel.app/docs/reference/mindmap.md): Mind maps written as a list, drawn in plain Typst for print and PDF, and interactive in the viewer (pan, zoom, open and close branches); a plugin with its prompt kit and the first render island.
- [@typstream/mermaid](https://typstream.vercel.app/docs/reference/mermaid.md): Mermaid diagrams in typstream documents, drawn by pele in the engine's worker, the same on pages, in the HTML target, the PDF and a DOCX export; a plugin with its prompt kit.
- [@typstream/engine-wasm](https://typstream.vercel.app/docs/reference/engine-wasm.md): Typst 0.15.1 compiled to WebAssembly, the low-level compiler behind typstream, with HTML, changed-page SVG, PDF and UTF-16 diagnostics.
- [@typstream/docx](https://typstream.vercel.app/docs/reference/docx.md): The DOCX writer behind exportDocx() — OOXML from the laid-out pages, with Typst's fonts embedded.
- [@typstream/core](https://typstream.vercel.app/docs/reference/core.md): The contracts every typstream package shares, with text edits and position mapping, diagnostics, the plugin contract, telemetry events and the engine protocol.

## Resources

- [Compatibility](https://typstream.vercel.app/docs/resources/compatibility.md): The Typst version typstream compiles with, the browsers, frameworks and Node.js versions it runs on, module formats, bundle sizes, fonts and Typst packages.
- [Troubleshooting](https://typstream.vercel.app/docs/resources/troubleshooting.md): Common problems when rendering streamed Typst with typstream, what causes them, and how to fix them.
