GitHub-flavored Markdown to HTML Converter
gh-md-to-html is a feature-rich Python module and command-line interface for converting GitHub-flavored Markdown to HTML. It leverages GitHub's online API by default for conversion but also offers robust offline capabilities. The library extends basic Markdown conversion with features like GitHub-flavored CSS, formula support, advanced image caching and optimization, host-ready file placement, PDF conversion, emoji shortcode support, and Table of Contents generation. The current version is 1.21.3, with PyPI releases occurring on a feature-driven, irregular cadence, complemented by frequent internal 'test-releases' on GitHub.
Common errors
-
FileNotFoundError: [Errno 2] No such file or directory: 'wkhtmltopdf'
cause The `wkhtmltopdf` executable, required for PDF conversion, is not found in your system's PATH.fixInstall `wkhtmltopdf` on your system and add its installation directory (specifically the directory containing the `wkhtmltopdf` executable) to your system's PATH environment variable. Verify installation by running `wkhtmltopdf --version` in your terminal. -
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))cause The default GitHub Markdown REST API is unreachable due to network issues, rate limiting, or the service being down. This prevents online Markdown conversion.fixCheck your internet connection. If the issue persists, consider using the offline conversion feature by installing `gh-md-to-html[offline_conversion]` and specifying `core_converter='offline'` in your Python code or CLI command. -
Images not displayed in the generated HTML, despite correct paths in Markdown.
cause Images referenced from local disk have paths containing whitespaces, which `gh-md-to-html` or the underlying HTML rendering engine might not handle correctly, leading to broken links.fixRename the image files and their containing directories to remove any whitespaces. Ensure all image paths in your Markdown are updated accordingly. -
JavaScript or CSS embedded in Markdown is stripped from the final HTML output.
cause When using the default (GitHub API) core converter, `gh-md-to-html` applies GitHub's security filters, which strip `<script>` and `<style>` tags to prevent XSS vulnerabilities.fixIf you need to preserve custom JavaScript or CSS, use the `offline+` core converter option. Install with `pip install gh-md-to-html[offline_conversion]` and then specify `core_converter='offline+'`. **Caution:** Only use `offline+` for trusted Markdown sources, as it bypasses security filters.
Warnings
- gotcha By default, `gh-md-to-html` uses GitHub's online Markdown REST API for conversion, which requires an internet connection and strips potentially harmful content like `<script>` and `<style>` tags for security. If you need offline conversion or want to retain such content, use the `offline` or `offline+` core converter options.
- gotcha PDF conversion requires the external tool `wkhtmltopdf` to be installed on your system and accessible via the system PATH, in addition to the Python `pdfkit` library (installed with `gh-md-to-html[pdf]`). Without `wkhtmltopdf`, PDF output will fail silently or with an error related to its absence.
- gotcha When embedding images from local disk (i.e., not via a URL) into your Markdown, ensure that the file paths to these images do not contain whitespaces. Whitespaces in local image paths can lead to broken image links in the generated HTML.
- gotcha Markdown syntax for formulas can be sensitive to whitespace or specific characters when using certain core converters (like `pandoc`) which have strict rules. Formulas that work in some Markdown editors might not render correctly.
Install
-
pip install gh-md-to-html -
pip install gh-md-to-html[offline_conversion] -
pip install gh-md-to-html[pdf]
Imports
- main_workflow
from gh_md_to_html import main_workflow
Quickstart
import os
from gh_md_to_html import main_workflow
markdown_content = """
# Hello, gh-md-to-html!
This is **GitHub-flavored Markdown** with $\LaTeX$ formulas.
- Feature 1
- Feature 2
```python
print('Code blocks are awesome!')
```
"""
# Convert markdown string to HTML and save to a file
output_html_file = 'output.html'
main_workflow(
md_origin=markdown_content,
origin_type='string',
output_name=output_html_file,
destination_directory='.'
)
print(f"Converted markdown saved to {output_html_file}")
# Example of printing directly to console (without saving to file)
# html_output_string = main_workflow(
# md_origin=markdown_content,
# origin_type='string',
# output_name='print'
# )
# print(html_output_string)