In my Python application I need to render to HTML a Markdown document generated by third-party software. The document contains long lists that contain non-standard and incompatible formatting (especially, blank lines between the elements of the same bulleted list). Additionally, the software does not comply with the indentation standard, so the lists may look like: ## Enumeration 1. Element 1 2. Element 2 ## List (all elements rely on the same topic) * Element 1 level 1 * **Element 2** Level 2 * Element 3 level 1 * Element 4 level 2 * Element 5 level 3 - Element 6 level 1 - Element 7 level 2 - Element 8 level 3 How would I sanitize this Markdown document in Python and set it up properly for the rendering? It is stored in a single Python string, including all the newlines, etc., and I need to expose the result in the same way. Continue reading...