markdown: Fix some bugs, add tests.