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 for LLM output, 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

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

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: