README.md

July 2, 2026 ยท View on GitHub

Wenmode

Build Status PyPI version Code Coverage Maintainability Rating Security Rating

Wenmode is a composable Markdown toolkit for Python by the same author as Mistune. It is a rewrite informed by Mistune's design, with a stronger focus on explicit rule composition, mdast-compatible AST output, extension state, and pluggable rendering.

The top-level Wenmode class combines a parser and a renderer. By default it parses CommonMark-style Markdown and renders HTML.

Documentation: https://wenmode.lepture.com

Use Wenmode when you need one or more of these behaviors:

  • render Markdown to HTML with safe defaults for user-authored content,
  • choose the exact Markdown rules your application accepts,
  • inspect or store an mdast-compatible AST,
  • build a custom Markdown dialect with parser rules and renderer handlers,
  • stream HTML output from Markdown input.

Installation

pip install wenmode

Run the CLI without installing it permanently:

uvx wenmode render --preset=github README.md
uvx wenmode ast --preset=github README.md

After installation, use either the console script or Python module entry point:

wenmode render README.md --preset=github
python -m wenmode ast README.md --positions

Quick start

from wenmode import Wenmode

wen = Wenmode()

text = '''
# Hello

This is **wenmode**.
'''
expected = '''
<h1>Hello</h1>
<p>This is <strong>wenmode</strong>.</p>
'''

html = wen.render(text)
assert html == expected.lstrip()

Use parse() when you need the mdast-compatible syntax tree:

from wenmode import Wenmode

wen = Wenmode()
text = 'A [link](https://example.com).'

tree = wen.parse(text)
ast = tree.to_ast()

assert ast == {
    'type': 'root',
    'children': [
        {
            'type': 'paragraph',
            'children': [
                {'type': 'text', 'value': 'A '},
                {
                    'type': 'link',
                    'children': [{'type': 'text', 'value': 'link'}],
                    'url': 'https://example.com',
                },
                {'type': 'text', 'value': '.'},
            ],
        }
    ],
}

Enable source positions when you need editor ranges, diagnostics, or AST-based tooling:

from wenmode import Wenmode

wen = Wenmode(positions=True)
ast = wen.parse('A **bold**.\n').to_ast()

assert ast['children'][0] == {
    'type': 'paragraph',
    'position': {
        'start': {'line': 1, 'column': 1, 'offset': 0},
        'end': {'line': 2, 'column': 1, 'offset': 12}
    },
    'children': [
        {
            'type': 'text',
            'position': {
                'start': {'line': 1, 'column': 1, 'offset': 0},
                'end': {'line': 1, 'column': 3, 'offset': 2}
            },
            'value': 'A '
        },
        {
            'type': 'strong',
            'position': {
                'start': {'line': 1, 'column': 3, 'offset': 2},
                'end': {'line': 1, 'column': 11, 'offset': 10}
            },
            'children': [
                {
                    'type': 'text',
                    'position': {
                        'start': {'line': 1, 'column': 5, 'offset': 4},
                        'end': {'line': 1, 'column': 9, 'offset': 8}
                    },
                    'value': 'bold'
                }
            ]
        },
        {
            'type': 'text',
            'position': {
                'start': {'line': 1, 'column': 11, 'offset': 10},
                'end': {'line': 1, 'column': 12, 'offset': 11}
            },
            'value': '.'
        }
    ]
}

Pass a different renderer when you want another output format, such as reStructuredText or AsciiDoc:

from wenmode import AsciiDocRenderer, Wenmode

wen = Wenmode(renderer=AsciiDocRenderer())

text = '# Hello'
expected = '''
= Hello
'''

asciidoc = wen.render(text)
assert asciidoc == expected.lstrip()

Rules, presets, and plugins

Most applications start with a preset:

  • commonmark, the default CommonMark-style rule set,
  • github, for GitHub-flavored Markdown features such as tables and task lists,
  • streaming, for incremental HTML output.

Rules are opt-in and composable. Wenmode() uses the commonmark preset by default; pass an explicit rule list when you want a custom Markdown dialect.

from wenmode import Wenmode
from wenmode.rules import AtxHeading, FencedCode, Image, InlineCode, Link

wen = Wenmode([AtxHeading, FencedCode, Link, Image, InlineCode])
text = '''
# h1

hi `code` **strong**
'''
expected = '''
<h1>h1</h1>
<p>hi <code>code</code> **strong**</p>
'''

assert wen.render(text) == expected.lstrip()

Because Emphasis is not enabled above, **strong** stays as text.

Use Parser directly when you only need an AST and want to choose rendering separately:

from wenmode import HTMLRenderer, Parser
from wenmode.presets import commonmark

parser = Parser(commonmark)
text = '# Hello'

tree = parser.parse(text)

html = HTMLRenderer().render(tree)

Use the github preset for GitHub-flavored Markdown features such as tables, task lists, strikethrough, extended autolinks, and footnotes:

from wenmode import Wenmode
from wenmode.presets import github

wen = Wenmode(github)

Use built-in plugins for non-standard syntax, document metadata, and rendering behavior such as front matter, math, definition lists, abbreviations, spoilers, ruby text, HTML smart punctuation, and extra inline formatting:

from wenmode import Wenmode
from wenmode.plugins import inline_math

wen = Wenmode(plugins=[inline_math])

assert wen.render('Inline $x + y$.\n') == (
    '<p>Inline <span class="math math-inline">x + y</span>.</p>\n'
)

Benchmark

Wenmode is designed so enabling more rules adds limited dispatch overhead. The benchmark script compares Markdown-to-HTML throughput across Wenmode and the libraries covered by the migration guides:

uv run --locked --group benchmark python scripts/benchmark.py --case all

wenmode-core uses CommonMark-style rules plus pipe tables, with raw HTML passthrough and URL sanitization disabled for parity with the other HTML renderers. Mistune, Python-Markdown, markdown-it-py, and markdown2 enable table support; Marko uses its broader GFM helper; commonmark.py is included as a CommonMark-only baseline because it has no pipe table support.

wenmode-all uses the github preset plus Wenmode's built-in plugins, including front matter, math, definition lists, abbreviations, spoilers, ruby text, and additional inline formatting. These extra rules are mostly unused by the benchmark corpora, so this target measures dispatch overhead rather than a syntax-equivalent comparison.

All benchmark targets are created once before warmup and timed iterations, then reused for every render call. Python-Markdown resets the same reusable Markdown instance before each conversion.

Versions used in these snapshots:

LibraryVersion
wenmode0.8.0
mistune3.3.2
python-markdown3.10.2
markdown-it-py4.2.0
markdown22.5.5
marko2.2.3
commonmark.py0.9.2

Mean time from one local Python 3.12.9 --case all run:

CaseBytesLibraryMeanMB/svs core
docs116,875wenmode-core16.56ms7.541.00x
docs116,875wenmode-all18.33ms6.640.90x
docs116,875mistune22.28ms5.670.74x
docs116,875python-markdown69.72ms1.690.24x
docs116,875markdown-it-py34.57ms3.510.48x
docs116,875markdown2129.98ms0.910.13x
docs116,875marko119.55ms1.010.14x
docs116,875commonmark.py83.65ms1.470.20x
rust-book1,225,464wenmode-core163.27ms7.661.00x
rust-book1,225,464wenmode-all197.30ms7.010.83x
rust-book1,225,464mistune246.29ms5.540.66x
rust-book1,225,464python-markdown662.25ms1.930.25x
rust-book1,225,464markdown-it-py358.07ms3.530.46x
rust-book1,225,464markdown24.296s0.300.04x
rust-book1,225,464marko1.175s1.070.14x
rust-book1,225,464commonmark.py10.026s0.130.02x
progit502,090wenmode-core31.54ms18.041.00x
progit502,090wenmode-all35.96ms15.510.88x
progit502,090mistune42.83ms11.770.74x
progit502,090python-markdown149.84ms3.490.21x
progit502,090markdown-it-py77.83ms7.280.41x
progit502,090markdown21.483s0.350.02x
progit502,090marko356.82ms1.450.09x
progit502,090commonmark.py346.01ms1.480.09x

In this run, wenmode-all remains faster than the other parsers even after loading many extra rules that the benchmark inputs mostly do not use.

Benchmark numbers depend on hardware, Python version, corpus, and parser configuration. See the full methodology in the Benchmarks documentation.

Streaming

Use the streaming preset when you want to render HTML chunks without waiting for the entire document to be parsed and rendered:

from wenmode import Wenmode
from wenmode.presets import streaming

wen = Wenmode(streaming)

text = '''
# Hello

A [link](/url).
'''

for chunk in wen.stream(text):
    send(chunk)

The returned iterator can be passed to streaming responses in frameworks such as Django, Flask, and FastAPI. The streaming preset keeps tables, strikethrough, direct links, and direct images enabled, while reference-style links, footnotes, and other deferred document-wide transforms stay out of the streaming path.

Learn more