Python parser for the CommonMark Markdown spec
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Simple one-function interface for common Markdown conversion tasks. The commonmark() function provides the most convenient way to convert Markdown text to various output formats.
Converts CommonMark Markdown text to HTML, JSON, AST representation, or reStructuredText in a single function call.
def commonmark(text, format="html"):
"""
Render CommonMark into HTML, JSON, AST, or reStructuredText.
Args:
text (str): CommonMark Markdown text to parse
format (str): Output format - 'html' (default), 'json', 'ast', or 'rst'
Returns:
str: Rendered output in specified format
Raises:
ValueError: If format is not one of the supported formats
"""import commonmark
markdown = """
# Hello World
This is a **bold** text and this is *italic*.
- Item 1
- Item 2
"""
html = commonmark.commonmark(markdown)
print(html)
# Output: <h1>Hello World</h1>\n<p>This is a <strong>bold</strong> text...</p>\n<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>\nimport commonmark
markdown = "# Hello\n*World*"
json_ast = commonmark.commonmark(markdown, format="json")
print(json_ast) # JSON representation of the ASTimport commonmark
markdown = "# Hello\n*World*"
ast_output = commonmark.commonmark(markdown, format="ast")
print(ast_output) # Human-readable AST structureimport commonmark
markdown = """
# Title
Some **bold** text.
"""
rst = commonmark.commonmark(markdown, format="rst")
print(rst)
# Output: reStructuredText formatted contentThe function validates the format parameter and raises ValueError for unsupported formats:
import commonmark
try:
result = commonmark.commonmark("# Hello", format="invalid")
except ValueError as e:
print(f"Error: {e}") # Error: format must be 'html', 'json' or 'ast'Install with Tessl CLI
npx tessl i tessl/pypi-commonmark