Markdown output for Sphinx based documentation

12,727

Solution 1

I didn't find anything which could take reStructuredText files and convert them to Markdown except for Pandoc, so I wrote a custom writer for Docutils (the reference implementation of reStructuredText and what Sphinx is built upon). The code is available on GitHub.

Note that it is only an initial implementation: it handles any reStructuredText document without error (tested against the standard.txt test document from the Docutils source repository) but many of the reStructuredText constructs (e.g. substitutions, raw directives etc.) are not supported and so not included in the Markdown output. I hope to add support for links, code blocks, images and tables: any help towards this is more than welcome - just go ahead and fork the code.

It seems that to add another writer/output format to Sphinx you need to add a "builder" using an extension.

Solution 2

Update Nov'18: sphinx-markdown-builder is now available - thanks to @Jam Risser :

Installation

pip3 install sphinx-markdown-builder

Dependencies

Python 3

Usage

Load extension in configuration.

conf.py

extensions = [
    'sphinx_markdown_builder'
]

If using recommonmark, make sure you explicitly ignore the build files as they will conflict with the system.

conf.py

exclude_patterns = [
    'build/*'
]

Build markdown files with Makefile

make markdown

Build markdown files with sphinx-build command

cd docs
sphinx-build -M markdown ./ build

References

ps. Outdated original answer (since sphinx-markdown-builder is now available): Created feature request for direct Markdown output support on Sphinx project site: https://github.com/sphinx-doc/sphinx/issues/4219 Thanks everyone who upvoted that github request - that made the difference!

Solution 3

If you'd like to use pandoc why won't you simply change the Makefile Sphinx generates when you run sphinx-quickstart.py for the first time to convert the reStructuredText to Markdown?
It's the easiest solution, although Chris's solution should work as well if you incorporate it into the Makefile.

Share:
12,727
Pedro Romano
Author by

Pedro Romano

I am a dedicated user and supporter of Free and Open Source Software and a proud Pythonista. In the wise words of Valentine Michael Smith: I am only an egg.

Updated on July 01, 2022

Comments

  • Pedro Romano
    Pedro Romano almost 2 years

    I found myself with a use case, where in addition to generating HTML and PDF from my Sphinx based documentation sources, I would also like to generate a Markdown version of the reStructuredText source files.

    My preliminary research didn't find any core or extension support for this in Sphinx. Other than manually using pandoc or creating a new Sphinx extension for the task, is there be a simpler/more integrated solution for this?