markdown

index

Overview

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.

Submodules

ModuleDescription
markdown.bufferStreaming buffer for chunk boundary handling in inline parsing.
markdown.eventsCallback-based event emitter for the markdown parser.
markdown.from_htmlHTML → markdown content extraction.
markdown.inlineInline element parser using a two-phase opener stack approach.
markdown.parserCore streaming parser that coordinates block detection, inline parsing, and callback-based event emission.
markdown.rendererRenderer 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.stateBlock state machine with pattern-based detection for CommonMark block structures: headings, code fences, thematic breaks, lists, tables, blockquotes, divs, link references, and footnotes.
markdown.tightnessEvent filter that resolves CommonMark list tightness exactly.

Functions

NameSignature
streamstream(options) -> parser
renderrender(input, options) -> result, err
render_htmlrender_html(input, options) -> html, err
from_htmlfrom_html(html) -> markdown
parseparse(input, options) -> events
drop_controlsdrop_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:

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:

render_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:

The 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:

SchemeUse
http, httpsa web page
mailto, telan address, a phone number
wiki, mediaa 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: ![**a** & b](x.png) gives <img src="x.png" alt="a &amp; 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:

KindExtensions
videomp4, m4v, webm, ogv, mov
audiomp3, wav, ogg, oga, opus, m4a, flac, aac

The class video or audio forces the kind, for a URL with no extension: ![x](https://example.com/watch){.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:

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.