README.md

August 20, 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, Wenmode parses CommonMark-style Markdown and renders HTML.

Documentation: https://wenmode.lepture.com

Use Wenmode to:

  • 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': '.'},
            ],
        }
    ],
}

Set positions=True to include source ranges for editor integration, 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 renderer when you need reStructuredText or AsciiDoc output:

from wenmode import AsciiDocRenderer, Wenmode

wen = Wenmode(renderer=AsciiDocRenderer())

text = '# Hello'
expected = '= Hello\n'

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

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 to define 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. It disables raw HTML passthrough and URL sanitization to match the other HTML renderers. Mistune, Python-Markdown, markdown-it-py, and markdown2 enable table support. Marko uses its broader GFM helper. commonmark.py is a CommonMark-only baseline because it does not support pipe tables.

wenmode-all uses the github preset plus Wenmode's built-in plugins, including front matter, math, definition lists, abbreviations, spoilers, ruby text, heading IDs, GitHub alerts, and additional inline formatting. The benchmark corpora use few of these extra rules. This target measures rule dispatch overhead, not equivalent syntax coverage.

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.14.0
mistune3.3.4
mistletoe1.6.0
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
docs138,514wenmode-core21.05ms6.911.00x
docs138,514wenmode-all24.74ms5.810.85x
docs138,514mistune25.26ms5.540.83x
docs138,514mistletoe53.46ms2.600.39x
docs138,514python-markdown78.48ms1.840.27x
docs138,514markdown-it-py42.04ms3.440.50x
docs138,514markdown2181.37ms0.780.12x
docs138,514marko173.29ms0.870.12x
docs138,514commonmark.py115.60ms1.390.18x
rust-book1,226,057wenmode-core168.42ms7.541.00x
rust-book1,226,057wenmode-all187.55ms6.720.90x
rust-book1,226,057mistune224.58ms5.600.75x
rust-book1,226,057mistletoe468.09ms2.630.36x
rust-book1,226,057python-markdown588.85ms2.100.29x
rust-book1,226,057markdown-it-py337.24ms3.710.50x
rust-book1,226,057markdown24.117s0.300.04x
rust-book1,226,057marko1.092s1.120.15x
rust-book1,226,057commonmark.py9.831s0.130.02x
progit502,090wenmode-core29.05ms17.301.00x
progit502,090wenmode-all38.04ms14.700.76x
progit502,090mistune46.85ms11.870.62x
progit502,090mistletoe147.81ms3.530.20x
progit502,090python-markdown139.58ms3.730.21x
progit502,090markdown-it-py74.58ms7.730.39x
progit502,090markdown21.452s0.350.02x
progit502,090marko331.42ms1.540.09x
progit502,090commonmark.py338.07ms1.610.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 to render HTML chunks before the complete document is 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)

Pass the returned iterator to a streaming response in Django, Flask, FastAPI, or another framework. The streaming preset keeps tables, strikethrough, direct links, and direct images enabled. It excludes reference-style links, footnotes, and other deferred document-wide transforms.

Learn more