Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ def _indent_for_blockquote(match):
return '\n' + text + '\n\n'

def convert_br(self, el, text, parent_tags):
if 'pre' in parent_tags and self.should_convert_tag('pre'):
return '\n' + text

if '_inline' in parent_tags:
return text + ' ' if text else ' '

Expand Down
23 changes: 23 additions & 0 deletions tests/test_pre_breaks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from markdownify import markdownify


@pytest.mark.parametrize('style', ['SPACES', 'BACKSLASH'])
@pytest.mark.parametrize('content', [
'print(1)<br>print(2)',
'<code>print(1)<br>print(2)</code>',
'<span>print(1)<br>print(2)</span>',
])
def test_preformatted_breaks_are_plain_newlines(style, content):
assert markdownify('<pre>' + content + '</pre>', newline_style=style) == (
'```\nprint(1)\nprint(2)\n```'
)


def test_mixed_break_forms_keep_nested_text_in_pre():
assert markdownify('<pre>a<br>b<br />c</pre>') == '```\na\nb\nc\n```'


def test_stripped_pre_keeps_normal_break_behavior():
assert markdownify('<pre>a<br>b</pre>', strip=['pre']) == 'a \nb'