Extended Python bindings for the Comrak Rust library, a fast CommonMark/GFM parser. Fork of lmmx/comrak.
pip install comrak-ext
- Python 3.9+
Fast Markdown to HTML parser in Rust, shipped for Python via PyO3.
Render Markdown to HTML:
from comrak import ExtensionOptions, markdown_to_html
extension_options = ExtensionOptions()
markdown_to_html("foo :smile:", extension_options)
# '<p>foo :smile:</p>\n'
extension_options.shortcodes = True
markdown_to_html("foo :smile:", extension_options)
# '<p>foo 😄</p>\n'
Render Markdown to CommonMark:
from comrak import RenderOptions, ListStyleType, markdown_to_commonmark
render_options = RenderOptions()
markdown_to_commonmark("- one\n- two\n- three", render_options=render_options)
# '- one\n- two\n- three\n' – default is Dash
render_options.list_style = ListStyleType.Plus
markdown_to_commonmark("- one\n- two\n- three", render_options=render_options)
# '+ one\n+ two\n+ three\n'
Parse Markdown into an abstract syntax tree (AST):
from comrak import ExtensionOptions, Document, Text, Paragraph, parse_document
extension_options = ExtensionOptions(front_matter_delimiter = "---")
md_content = """---
This is a text in FrontMatter
---
Hello, Markdown!
"""
x = parse_document(md_content, extension_options)
assert isinstance(x.node_value, Document)
assert not hasattr(x.node_value, "value")
assert len(x.children) == 2
assert isinstance(x.children[0].node_value, FrontMatter)
assert isinstance(x.children[0].node_value.value, str)
assert x.children[0].node_value.value.strip() == "---\nThis is a text in FrontMatter\n---"
assert isinstance(x.children[1].node_value, Paragraph)
assert len(x.children[1].children) == 1
assert isinstance(x.children[1].children[0].node_value, Text)
assert isinstance(x.children[1].children[0].node_value.value, str)
assert x.children[1].children[0].node_value.value == "Hello, Markdown!"
All options are exposed in a simple manner and can be used with markdown_to_html
, markdown_to_commonmark
, and parse_document
.
Refer to the Comrak docs for all available options.
Tested with small (8 lines) and medium (1200 lines) markdown strings
Maintained by Martin005. Contributions welcome!
- Issues & Discussions: Please open a GitHub issue or discussion for bugs, feature requests, or questions.
- Pull Requests: PRs are welcome!
- Install the dev extra (e.g. with uv:
uv pip install -e .[dev]
) - Run tests (when available) and include updates to docs or examples if relevant.
- If reporting a bug, please include the version and the error message/traceback if available.
- Install the dev extra (e.g. with uv:
Licensed under the 2-Clause BSD License. See LICENSE for all the details.