diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 28cdaf6..b828f61 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -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 diff --git a/tests/test_conversions.py b/tests/test_conversions.py index c95483c..7ae985e 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -181,6 +181,17 @@ def test_hn_chained(): assert md('X

First

') == 'X\n\nFirst\n=====\n\n' +def test_line_breaks_at_emphasis_boundaries(): + assert md('Title

Text') == '**Title** \n \nText' + assert md('foo
bar') == '**foo** \nbar' + assert md('foo
bar', newline_style=BACKSLASH) == '**foo**\\\nbar' + assert md('a
foo
b') == 'a \n*foo*b' + assert md('a
foo
b', newline_style=BACKSLASH) == 'a\\\n*foo*b' + assert md('a
b') == 'a \nb' + assert md('a
b', newline_style=BACKSLASH) == 'a\\\nb' + assert md('foo
bar') == '***foo*** \nbar' + + def test_hn_nested_tag_heading_style(): assert md('

A

P

C

', heading_style=ATX_CLOSED) == '\n\n# A P C #\n\n' assert md('

A

P

C

', heading_style=ATX) == '\n\n# A P C\n\n'