Closes #607
This fixes #569 . `markdown_to_html` is heavily refactored, header-related things is handled in a second pass.