Streaming markdown parser and renderer with support for CommonMark, GFM extensions (tables, strikethrough, autolinks, task lists), and djot features (fenced divs, inline attributes).
Provides static terminal rendering via TSS, streaming terminal rendering, and HTML5 output.
| Module | Description |
|---|---|
| markdown.buffer | Streaming buffer for chunk boundary handling in inline parsing. |
| markdown.events | Callback-based event emitter for the markdown parser. |
| markdown.from_html | HTML → markdown content extraction. |
| markdown.inline | Inline element parser using a two-phase opener stack approach. |
| markdown.parser | Core streaming parser that coordinates block detection, inline parsing, and callback-based event emission. |
| markdown.renderer | Renderer registry and factory with three implementations: static (full document rendering for pager display), streaming (live cursor-based rendering for LLM output), and html (semantic HTML5). |
| markdown.state | Block state machine with pattern-based detection for CommonMark block structures: headings, code fences, thematic breaks, lists, tables, blockquotes, divs, link references, and footnotes. |
| markdown.tightness | Event filter that resolves CommonMark list tightness exactly. |
| Name | Signature |
|---|---|
| stream | stream(options) -> parser |
| render | render(input, options) -> result, err |
| render_html | render_html(input, options) -> html, err |
| from_html | from_html(html) -> markdown |
| parse | parse(input, options) -> events |
| drop_controls | drop_controls(line) -> line |
stream(
options) ->parser
Create a streaming parser instance for incremental parsing
Returns a parser instance with feed(chunk), finish(), reset(), and
set_event_callback(fn) methods. Options:
on_event: callback function(event) invoked for each parser eventinline: parse inline elements (default true)streaming_inline: emit inline events incrementally (default true)List tightness cannot be known when a list starts, so tight on a list's
block_start is provisional. The parser emits a list_loose event for the
innermost open list the moment a blank line proves it loose. Consumers that
can afford to buffer should wrap on_event in markdown.tightness to get
the resolved value on block_start instead.
render(
input,options) ->result,err
Render markdown to styled terminal output
Parses and renders markdown through the named renderer. Options:
renderer: renderer name ("static" default)width: content width (default 80)rss: custom RSS table to merge with defaultsindent: global indentation (default 0)hide_link_urls: hide URLs in rendered output (default false)supports_ts: terminal supports OSC 66 text sizing (default true)borders: draw the frames of tables, code blocks and divs on their
cells' edges (default term.supports_borders(), false until a probe ran)diagrams: draw a mermaid fence as its picture (default true)graphics: draw a picture's strokes as pixels under its text (default
term.supports_graphics(), false until a probe ran)cell_w, cell_h: terminal cell size in pixels; caps image display
width at natural size and refines image row estimates. Without them
the renderer guesses a 1:2 cell and sends the image's rows with itrender_html(
input,options) ->html,err
Render markdown to semantic HTML5
Parses and renders markdown through the HTML renderer. options goes to
the renderer whole. Options:
diagrams: draw a mermaid fence as an inline SVG in a
<figure class="diagram"> (default true); a source that gives no
picture is a code block either waydiagram_cols: the page's width in cells (default 80); a diagram
wider than that is laid out compact, and drawn even when still wider,
in the direction its source givesThe page may come from a writer the site does not trust. So the renderer
checks a destination before it writes it to href or src. A destination
with no scheme passes: a relative path, a #fragment, //host/path. So
does one with a scheme from this table:
| Scheme | Use |
|---|---|
http, https | a web page |
mailto, tel | an address, a phone number |
wiki, media | a WIKIDB entry and its media, which the caller rewrites |
Any other scheme is refused: javascript:, vbscript:, data:, file:
and the rest. A refused link is its text, with no <a>. A refused image
is its alt text, with no <img>. An allowed destination goes out as
written.
The scheme is read as a browser reads it. Leading spaces and control bytes
are dropped, tabs and newlines inside it are dropped, and case is ignored.
So host:8080/path has the scheme host, and is refused: write
//host:8080/path.
An image's alt text is the plain text of its content, escaped once.
Emphasis or code inside it writes no tag, and a line break in it is a
space:  gives <img src="x.png" alt="a & b">.
An element writes its attributes in a fixed order: an image src, alt,
title, class; a video or an audio src, aria-label, title,
class; a link href, title, class; a heading id, class.
An image of a video or an audio file becomes a <video> or an <audio>
element. The extension of the destination gives the kind, in any case, with
a ?query and a #fragment cut off:
| Kind | Extensions |
|---|---|
| video | mp4, m4v, webm, ogv, mov |
| audio | mp3, wav, ogg, oga, opus, m4a, flac, aac |
The class video or audio forces the kind, for a URL with no extension:
{.video}. The first of the two words in
the class wins. Any other image stays an <img>, and a link stays a link.
The element carries controls and preload="metadata". Its src is the
destination as written, so clip.mp4#t=10,20 still cuts the clip. The alt
text is the element's name: it goes to aria-label, and to the text of a
fallback link to src inside the element. An empty alt gives no
aria-label, and the destination as the link's text. Inside a link the
fallback is the bare text, since a second <a> would nest. title and
class pass through, the class whole:
<video controls preload="metadata" src="videos/demo.mp4" aria-label="Demo run" class="small"><a href="videos/demo.mp4">Demo run</a></video>
The element stays inside its paragraph, as an image does. The page's stylesheet sizes it. The terminal renderers print such an image as any other image.
from_html(
html) ->markdown
Extract a markdown-friendly rendering from raw HTML
Picks the article root from messy real-world HTML, strips noise tags,
converts structural tags to markdown, and decodes entities. Output is
intended for a markdown renderer or pager — see markdown.from_html
submodule for decode_entities if needed separately.
parse(
input,options) ->events
Parse markdown and collect events into an array
Parses input and returns an array of event tables. Options:
on_event: custom callback (default: collect into array)inline: parse inline elements (default true)streaming_inline: emit inline events incrementally (default true)resolve_tightness: resolve list looseness onto each list's block_start
instead of reporting it through list_loose events (default false)An attribute block after a link, an image or an autolink adds its keys to
the link's attrs: {.cls #id key=val} gives class, id and key.
The block never sets href, autolink or wikilink, which the parser
reads from the link itself. It sets title only when the link has none:
[x](a "t"){title=u} keeps t.
drop_controls(
line) ->line
Drop from one line what a terminal would act on
The parser runs this on every line it reads. It is public for text that reaches a terminal without the parser, such as the pager's raw mode.
An escape sequence goes whole: a CSI to its final byte, an OSC to BEL or ST, a DCS, SOS, PM or APC to ST, any other ESC with the byte after it. The C0 bytes but the tab go, DEL goes, and the C1 controls in UTF-8 go.
Pass one line at a time. A sequence with no end takes the rest of the string, and a newline is a C0 byte, so it goes too.