Python port of markdown-it providing CommonMark-compliant markdown parsing with configurable syntax and pluggable architecture
—
Parser configuration system with built-in presets and rule management for customizing parsing behavior. Allows fine-grained control over which markdown features are enabled and how they are processed.
Configure the parser with presets and custom options.
def configure(self, presets: str | dict, options_update: dict = None) -> MarkdownIt:
"""
Batch load of all options and component settings.
Parameters:
- presets: preset name or configuration dictionary
- options_update: additional options to merge into preset
Returns:
- MarkdownIt: self for method chaining
"""
def set(self, options: dict) -> None:
"""
Set parser options directly.
Parameters:
- options: options dictionary following OptionsType structure
"""Pre-configured parser settings for common use cases.
# Available preset names (str)
PRESETS = [
"commonmark", # CommonMark specification compliant
"default", # Default configuration with common features
"zero", # Minimal configuration for manual setup
"gfm-like", # GitHub Flavored Markdown-like features
"js-default" # Alias for default preset
]Usage Example:
from markdown_it import MarkdownIt
# CommonMark compliant (strict spec adherence)
md_cm = MarkdownIt('commonmark')
# Default preset with additional features
md_default = MarkdownIt('default')
# Zero preset for manual configuration
md_zero = MarkdownIt('zero').enable(['emphasis', 'link'])
# GFM-like with tables and strikethrough
md_gfm = MarkdownIt('gfm-like')
# Custom options with preset
md_custom = MarkdownIt('commonmark', {
'breaks': True, # Convert \n to <br>
'html': True, # Allow HTML tags
'linkify': True # Auto-convert URLs to links
})Enable, disable, and manage parsing rules dynamically.
def enable(self, names: str | list[str], ignoreInvalid: bool = False) -> MarkdownIt:
"""
Enable list of rules.
Parameters:
- names: rule name or list of rule names to enable
- ignoreInvalid: ignore errors when rule not found
Returns:
- MarkdownIt: self for method chaining
"""
def disable(self, names: str | list[str], ignoreInvalid: bool = False) -> MarkdownIt:
"""
Disable list of rules.
Parameters:
- names: rule name or list of rule names to disable
- ignoreInvalid: ignore errors when rule not found
Returns:
- MarkdownIt: self for method chaining
"""
def get_all_rules(self) -> dict[str, list[str]]:
"""
Get names of all available rules by parser chain.
Returns:
- dict: mapping of chain names to rule lists
"""
def get_active_rules(self) -> dict[str, list[str]]:
"""
Get names of all currently active rules by parser chain.
Returns:
- dict: mapping of chain names to active rule lists
"""Usage Example:
from markdown_it import MarkdownIt
md = MarkdownIt('zero')
# Enable specific rules
md.enable(['emphasis', 'strikethrough', 'table'])
# Disable rules
md.disable(['html_inline', 'html_block'])
# Chain operations
md = (MarkdownIt('commonmark')
.enable(['table', 'strikethrough'])
.disable(['html_inline']))
# Check rule status
print("All rules:", md.get_all_rules())
print("Active rules:", md.get_active_rules())Temporarily modify rules within a context.
def reset_rules(self) -> Generator[None, None, None]:
"""
Context manager that resets enabled rules on exit.
Yields:
- None: context for temporary rule modifications
"""Usage Example:
from markdown_it import MarkdownIt
md = MarkdownIt('commonmark')
# Temporarily disable rules
with md.reset_rules():
md.disable(['emphasis', 'strong'])
# Process with limited rules
html = md.render("**bold** and *italic*")
print(html) # No emphasis processing
# Rules are restored after context exit
html = md.render("**bold** and *italic*")
print(html) # Full emphasis processing restoredLoad and configure plugins to extend parsing capabilities.
def use(self, plugin: callable, *params, **options) -> MarkdownIt:
"""
Load plugin with parameters.
Parameters:
- plugin: plugin function that modifies parser
- params: positional arguments for plugin
- options: keyword arguments for plugin
Returns:
- MarkdownIt: self for method chaining
"""Usage Example:
from markdown_it import MarkdownIt
def custom_plugin(md, option1, option2=None):
"""Custom plugin that modifies parser behavior."""
# Plugin implementation
pass
md = (MarkdownIt('commonmark')
.use(custom_plugin, 'param1', option2='value'))
# Chain multiple plugins
md.use(plugin1).use(plugin2, arg1, arg2)Complete options structure for fine-grained control.
class OptionsType(TypedDict):
maxNesting: int # Recursion limit (default: 20-100)
html: bool # Enable HTML tags in source
linkify: bool # Auto-convert URLs to links
typographer: bool # Enable smart quotes and replacements
quotes: str # Quote replacement characters
xhtmlOut: bool # Use XHTML self-closing tags
breaks: bool # Convert \n to <br>
langPrefix: str # CSS prefix for code blocks
highlight: callable | None # Syntax highlighter functionDetails of built-in preset configurations:
CommonMark Preset:
{
"options": {
"maxNesting": 20,
"html": True,
"linkify": False,
"typographer": False,
"xhtmlOut": True,
"breaks": False
},
"components": {
"core": {"rules": ["normalize", "block", "inline", "text_join"]},
"block": {"rules": ["blockquote", "code", "fence", "heading", "hr",
"html_block", "lheading", "list", "reference", "paragraph"]},
"inline": {"rules": ["autolink", "backticks", "emphasis", "entity",
"escape", "html_inline", "image", "link", "newline", "text"]}
}
}Default Preset:
{
"options": {
"maxNesting": 100,
"html": False,
"linkify": False,
"typographer": False,
"xhtmlOut": False,
"breaks": False
},
"components": {} # Enables all available rules
}Zero Preset:
{
"options": {
"maxNesting": 20,
"html": False,
"linkify": False,
"typographer": False,
"xhtmlOut": False,
"breaks": False
},
"components": {
"core": {"rules": ["normalize", "block", "inline", "text_join"]},
"block": {"rules": ["paragraph"]},
"inline": {"rules": ["text"]}
}
}GFM-like Preset:
# Extends CommonMark with:
# - Linkification
# - Tables
# - Strikethrough
# - HTML enabledUsage Example:
from markdown_it import MarkdownIt
# Custom options with specific features
md = MarkdownIt('zero', {
'html': True,
'breaks': True,
'typographer': True,
'quotes': '«»‹›', # French quotes
'highlight': custom_highlighter
})
# Enable only needed rules
md.enable(['emphasis', 'strong', 'link', 'autolink'])
html = md.render("""
"Smart quotes" and auto-links: https://example.com
**Bold** and *italic* text.
""")Install with Tessl CLI
npx tessl i tessl/pypi-markdown-it-py