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
10 changes: 8 additions & 2 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ def implementation(self, el, text, parent_tags):
markup_suffix = markup_prefix
if '_noformat' in parent_tags:
return text
# Keep boundary line breaks outside the emphasis delimiters. chomp()
# strips newlines and would also leave a backslash break inside them.
leading, text, trailing = re.match(
r'^((?:[ \t]*\\?\n)*)(.*?)((?:[ \t]*\\?\n[ \t]*)*)$',
text, re.DOTALL,
).groups()
prefix, suffix, text = chomp(text)
if not text:
return ''
return '%s%s%s%s%s' % (prefix, markup_prefix, text, markup_suffix, suffix)
return leading + trailing
return '%s%s%s%s%s%s%s' % (leading, prefix, markup_prefix, text, markup_suffix, suffix, trailing)
return implementation


Expand Down
11 changes: 11 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ def test_hn_chained():
assert md('X<h1>First</h1>') == 'X\n\nFirst\n=====\n\n'


def test_line_breaks_at_emphasis_boundaries():
assert md('<strong><u>Title<br><br></u></strong>Text') == '**Title** \n \nText'
assert md('<b>foo<br/></b>bar') == '**foo** \nbar'
assert md('<b>foo<br/></b>bar', newline_style=BACKSLASH) == '**foo**\\\nbar'
assert md('a<em><br/>foo</em>b') == 'a \n*foo*b'
assert md('a<em><br/>foo</em>b', newline_style=BACKSLASH) == 'a\\\n*foo*b'
assert md('a<b><br></b>b') == 'a \nb'
assert md('a<b><br></b>b', newline_style=BACKSLASH) == 'a\\\nb'
assert md('<b><em>foo<br/></em></b>bar') == '***foo*** \nbar'


def test_hn_nested_tag_heading_style():
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX_CLOSED) == '\n\n# A P C #\n\n'
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX) == '\n\n# A P C\n\n'
Expand Down