cmarkgfm: GitHub Flavored Markdown (GFM) Parser
cmarkgfm provides minimalist Python bindings to GitHub's fork of cmark, the C reference implementation of CommonMark. It allows parsing and rendering CommonMark and GitHub Flavored Markdown (GFM) to HTML, offering fast and accurate conversions. The library is actively maintained with frequent releases, currently at version 2025.10.22, and aims to stay up-to-date with Python versions and the underlying `cmark-gfm` C library.
Warnings
- breaking Python Version Support Changes: Version `2025.10.20` dropped support for Python 3.6, 3.7, and 3.8. Users on these versions must upgrade Python or pin `cmarkgfm` to an older version.
- breaking Code Block HTML Output Changed: In version `0.6.0`, the HTML output for code blocks changed to align with `github.com`'s output. This may break existing CSS rules if they depended on the previous structure.
- gotcha Unsafe HTML/Link Rendering Default: Since version `0.5.0`, the default rendering behavior is *safe*, meaning potentially unsafe HTML (like raw HTML, JavaScript, unsafe links) is not rendered. Earlier versions defaulted to unsafe.
- gotcha Python Version Requirement: The library requires Python 3.9 or newer. Ensure your Python environment meets this requirement.
Install
-
pip install cmarkgfm
Imports
- markdown_to_html
from cmarkgfm import markdown_to_html
- github_flavored_markdown_to_html
from cmarkgfm import github_flavored_markdown_to_html
- Options
from cmarkgfm.cmark import Options
Quickstart
import cmarkgfm
from cmarkgfm.cmark import Options as cmarkgfmOptions
markdown_text = "# Hello CommonMark\n\nThis is **bold** text."
html_commonmark = cmarkgfm.markdown_to_html(markdown_text)
print("CommonMark HTML:\n", html_commonmark)
gfm_text = "This is GitHub Flavored Markdown with a ~strikethrough~ and a [link](https://github.com)."
html_gfm = cmarkgfm.github_flavored_markdown_to_html(gfm_text)
print("\nGFM HTML:\n", html_gfm)
# Example with options: smart quotes and GitHub pre-lang
options = (
cmarkgfmOptions.CMARK_OPT_SMART |
cmarkgfmOptions.CMARK_OPT_GITHUB_PRE_LANG
)
markdown_with_options = "“Smart quotes” and ```python\nprint('hello')\n```"
html_with_options = cmarkgfm.markdown_to_html(markdown_with_options, options)
print("\nHTML with Options:\n", html_with_options)