llm_markdown_parser library

Markdown for text that arrives in pieces, such as AI chat replies, without any UI: incremental parsing, healing of unfinished syntax, plain text, <think> splitting and server-sent events.

Pure Dart, so it runs on a server, in a CLI and in isolates. The llm_markdown package builds its Flutter widget on top of this.

final parser = IncrementalMarkdownParser();
var text = '';
await for (final chunk in chunks) {
  text += chunk;
  parser.update(text, complete: false);
  // parser.frozenBlocks are done; parser.openNodes is the open block
}
parser.update(text, complete: true);

Classes

AlertSyntax
CitationSyntax
DetailsBlockSyntax
FrozenBlock
A finished block: its text no longer changes, so it is parsed once.
HighlightSyntax
IncrementalMarkdownParser
Parses Markdown that grows at the end, such as a streamed AI reply.
InlineMathSyntax
MarkdownSyntax
Which Markdown extensions are recognized, beyond GitHub-flavored Markdown.
MathBlockSyntax
SafeHtmlSyntax
SchemeAutolinkSyntax
ServerSentEvent
One event of a server-sent events (SSE) stream.
SpoilerSyntax
ThinkingSplit
The thinking part and the answer of a reply.

Constants

blockMathTag → const String
citationTag → const String
detailsTag → const String
inlineMathTag → const String
spoilerTag → const String

Properties

detailsCloser → RegExp
final
detailsOpener → RegExp
final

Functions

findBlockBoundaries(String text, int start, {required bool complete, bool dollarMath = true}) → List<int>
healIncompleteMarkdown(String source, {bool dollarMath = true}) → String
Returns source changed so that its unfinished end renders cleanly.
isBlank(String text) → bool
markdownToPlainText(String markdown) → String
Returns markdown as plain text, for example to copy a reply into an app that doesn't understand Markdown.
mathOpenerFor({required bool dollar}) → RegExp
newMarkdownDocument([MarkdownSyntax syntax = const MarkdownSyntax()]) → Document
A package:markdown document with GitHub-flavored Markdown and the extensions turned on in syntax, as StreamingMarkdown parses it.
serverSentEvents(Stream<List<int>> bytes) → Stream<ServerSentEvent>
Splits the raw bytes of an HTTP response body into server-sent events.
splitThinking(String text, {required bool complete}) → ThinkingSplit
Splits the <think>...</think> part that models such as DeepSeek R1 and Qwen put at the start of a reply off the answer.
texToUnicode(String tex) → String
A readable plain-text version of simple TeX, for when no math renderer is plugged in: \alpha becomes α, x^2 x² and \frac{a}{b} a/b.

Exceptions / Errors

ModelStreamError
Error from a model API, sent inside its event stream.