Skip to content

Commit

Permalink
fix: ensure new line at end of the text
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Mar 22, 2023
1 parent d69cc51 commit a75c0bf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/mistune/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def parse(self, s: str, state: Optional[BlockState]=None):
# normalize line separator
s = s.replace('\r\n', '\n')
s = s.replace('\r', '\n')

# ensure new line at end
if s[-1] != '\n':
if not s.endswith('\n'):
s += '\n'

state.process(s)
Expand Down
2 changes: 1 addition & 1 deletion src/mistune/renderers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def block_quote(self, text: str) -> str:

def block_html(self, html: str) -> str:
if self._escape:
return '<p>' + escape_text(html) + '</p>\n'
return '<p>' + escape_text(html.strip()) + '</p>\n'
return html + '\n'

def block_error(self, text: str) -> str:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def test_harmful_links(self):
expected = '<p><a href="#harmful-link">h</a></p>'
self.assertEqual(result.strip(), expected)

def test_ref_link(self):
result = mistune.html('[link][h]\n\n[h]: /foo')
expected = '<p><a href="/foo">link</a></p>'
self.assertEqual(result.strip(), expected)

def test_allow_harmful_protocols(self):
renderer = mistune.HTMLRenderer(allow_harmful_protocols=True)
md = mistune.Markdown(renderer)
Expand Down

0 comments on commit a75c0bf

Please sign in to comment.