cmarkgfm - Python bindings to GitHub's cmark
Minimalist Python bindings to GitHub's fork of cmark.
This package is published on PyPI as cmarkgfm and can be installed with pip or pipenv:
pip install --user cmarkgfm pipenv install cmarkgfm
Wheels are provided for macOS, Linux, and Windows for Python 2.7*, 3.5, 3.6, 3.7, 3.8 and 3.9.
* Python 2.7 is not supported on Windows. A PR to build wheels would be welcome.
High-level usage is really straightforward. To render normal CommonMark markdown:
import cmarkgfm html = cmarkgfm.markdown_to_html(markdown_text)
To render GitHub-flavored markdown:
import cmarkgfm html = cmarkgfm.github_flavored_markdown_to_html(markdown_text)
github_flavored_markdown_to_html prevents some raw HTML tags (including
script) from being rendered, it does not block unsafe URLs in links.
Therefore it is recommend to call the rendering method with the SAFE option turned on. The safe option does not render raw HTML or potentially dangerous URLs. (Raw HTML is replaced by a placeholder comment; potentially dangerous URLs are replaced by empty strings.) Dangerous URLs are those that begin with
data: (except for
image/webp mime types) To do this, use:
import cmarkgfm from cmarkgfm.cmark import Options as cmarkgfmOptions html = cmarkgfm.markdown_to_html(markdown_text, options=cmarkgfmOptions.CMARK_OPT_SAFE) # or html = cmarkgfm.github_flavored_markdown_to_html(markdown_text, options=cmarkgfmOptions.CMARK_OPT_SAFE)
If you trust the markdown text to not include any unsafe tags and links, then you may skip this.
In version 0.4.0 and earlier, the default behavior is unsafe, as described above. In later versions, the default behavior is safe, and to render potentially unsafe HTML pass the
Both rendering methods
options argument that can be used to activate options of cmark.
import cmarkgfm from cmarkgfm.cmark import Options as cmarkgfmOptions html = cmarkgfm.markdown_to_html(markdown_text, options=cmarkgfmOptions.CMARK_OPT_SAFE | cmarkgfmOptions.CMARK_OPT_SMART)
The options are:
|CMARK_OPT_SAFE (≤0.4.0)||Prevents rendering unsafe HTML and links.|
|CMARK_OPT_UNSAFE (>0.4.0)||Allows rendering unsafe HTML and links.|
|CMARK_OPT_SMART||Render curly quotes, en/em-dashes, ellipses|
|CMARK_OPT_NORMALIZE||Consolidate adjacent text nodes.|
|CMARK_OPT_HARDBREAKS||Renders line breaks within paragraphs as
|CMARK_OPT_NOBREAKS||Render soft line breaks as spaces.|
Pull requests are welcome. :)
This project is under the MIT License. It includes components under differing
copyright under the
third_party directory in this source tree.