Sphinx Markdown Include Extension
sphinx-mdinclude is a simple Sphinx extension that enables including Markdown documents from within reStructuredText. It provides the `.. mdinclude::` directive, and automatically converts the content of Markdown documents to reStructuredText format. It is a fork of `m2r` and `m2r2`, focused solely on providing a Sphinx extension. The current version is 0.6.2, and it is actively maintained as part of the Omnilib Project, with regular releases addressing compatibility and features.
Warnings
- gotcha Header levels must be consistent across reStructuredText and included Markdown files. sphinx-mdinclude converts Markdown headings to reStructuredText headings using a fixed scheme (H1: `=`, H2: `-`, H3: `^`, H4: `~`, H5: `"`, H6: `#`). If your parent `.rst` file establishes a different hierarchy before an `.. mdinclude::` directive, the combined document might render incorrectly or produce warnings.
- gotcha Markdown syntax is generally not processed when nested inside reStructuredText directives. Content within directives (e.g., `.. note::`, `.. code-block::`) should typically be written directly in reStructuredText.
- gotcha Table column alignment in Markdown files included via `.. mdinclude::` is not supported, as this is a limitation inherited from reStructuredText's table capabilities.
- gotcha Relative paths for images or links within included Markdown files may not resolve correctly in the final Sphinx output if the Markdown file is located outside the main Sphinx source directory (e.g., including `README.md` from the project root).
- breaking sphinx-mdinclude is a fork of `m2r` and `m2r2` with a focused scope. Version 0.4.0 notably removed the command-line interface (CLI) and mermaid extension support, shifting focus entirely to being a Sphinx extension. Users migrating from `m2r` or `m2r2` should expect these features to be absent.
- breaking Dependency versions for `Sphinx`, `docutils`, and `mistune` have seen breaking changes and pinning adjustments across `sphinx-mdinclude` versions. For example, `mistune` support shifted from `<1.0` to `2.x`, then to `v3` (v0.6.0). Python 3.7 support was dropped in v0.5.4.
Install
-
pip install sphinx-mdinclude
Imports
- sphinx_mdinclude
# In conf.py extensions = [ # other extensions, 'sphinx_mdinclude' ]
Quickstart
# conf.py
# Add 'sphinx_mdinclude' to your extensions list
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx_mdinclude'
]
# index.rst (or any .rst file)
My Project Documentation
========================
.. mdinclude:: ../README.md
:start-line: 1
:end-line: 10
This section continues with reStructuredText.
.. mdinclude:: introduction.md