miyuchina / mistletoe

A fast, extensible and spec-compliant Markdown parser in pure Python.
MIT License
791 stars 110 forks source link

MarkdownRenderer should emit extra newline after list #211

Closed ahelwer closed 6 months ago

ahelwer commented 6 months ago

Using latest version 1.2.1. Here's a simple round-trip python script:

from mistletoe import Document
from mistletoe.markdown_renderer import MarkdownRenderer

d = None
with open('test.md', 'r') as f:
    d = Document(f)

with open('test.md', 'w') as f:
    with MarkdownRenderer() as r:
        f.write(r.render(d))

If I run this script on this markdown file:

- Item1
- Item2
- Item3

Some text

it will output:

- Item1
- Item2
- Item3
Some text

if I run it again on this output, it will produce:

- Item1
- Item2
- Item3
  Some text

So the text has become part of the final list item, changing the document layout. The same behavior is also exhibited with ordered lists.

ahelwer commented 6 months ago

There are actually quite a few places where newlines are deleted, changing the formatting (sticking paragraphs together and such).

pbodnar commented 6 months ago

@ahelwer, works for me. I guess you are experiencing #64 rather than a problem with MarkdownRenderer. Can you please confirm?

ahelwer commented 6 months ago

I don't think so, I'm on Linux and running file -k test.md outputs test.md: ASCII text so the line endings are just \n.

pbodnar commented 6 months ago

I'm sorry, looking at it again and I am actually able to reproduce. And it looks like you are experiencing #56 then. :)

I.e. change your code to the following and the MarkdownRenderer will work correctly (because the renderer will manage to add its BlankLine and other tokens to the parsing process):

from mistletoe import Document
from mistletoe.markdown_renderer import MarkdownRenderer

with MarkdownRenderer() as r:
    with open('test.md', 'r') as f:
        d = Document(f)
    with open('test.md', 'w') as f:
        f.write(r.render(d))

So I'm closing this as answered. Feel free to reopen by commenting on this issue.