CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-commonmark

Python parser for the CommonMark Markdown spec

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

high-level.mddocs/

High-Level API

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.

Capabilities

Main Conversion Function

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
    """

Usage Examples

HTML Output (Default)

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>\n

JSON AST Output

import commonmark

markdown = "# Hello\n*World*"
json_ast = commonmark.commonmark(markdown, format="json")
print(json_ast)  # JSON representation of the AST

Pretty-Printed AST

import commonmark

markdown = "# Hello\n*World*"
ast_output = commonmark.commonmark(markdown, format="ast")
print(ast_output)  # Human-readable AST structure

reStructuredText Output

import commonmark

markdown = """
# Title

Some **bold** text.
"""

rst = commonmark.commonmark(markdown, format="rst")
print(rst)
# Output: reStructuredText formatted content

Error Handling

The 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

docs

cli.md

high-level.md

index.md

parsing.md

rendering.md

utilities.md

tile.json