CommonMark Python Parser

0.9.1 · deprecated · verified Thu Apr 09

CommonMark-py is a pure Python port of jgm's commonmark.js, a Markdown parser and renderer for the CommonMark specification, using only native modules. It provides functionality to parse Markdown into an Abstract Syntax Tree (AST) and render it to HTML. While it aimed to keep up with CommonMark spec updates, the project is now officially deprecated, with `markdown-it-py` recommended as an alternative for new projects. [7]

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both the simple `commonmark.commonmark()` function for direct conversion and the more advanced `Parser` and `HtmlRenderer` classes for parsing to an AST and then rendering to HTML. [5, 7]

import commonmark
from commonmark import Parser, HtmlRenderer

markdown_text = "Hello *World*! This is **CommonMark**."

# Simple conversion
html_output_simple = commonmark.commonmark(markdown_text)
print(f"Simple HTML:\n{html_output_simple}")

# Advanced usage with AST manipulation
parser = Parser()
ast = parser.parse(markdown_text)

renderer = HtmlRenderer()
html_output_advanced = renderer.render(ast)
print(f"\nAdvanced HTML:\n{html_output_advanced}")

# Example of AST inspection (prints a tree structure)
# commonmark.dumpAST(ast)

view raw JSON →