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
P
C ', heading_style=ATX_CLOSED) == '\n\n# A P C #\n\n' assert md('P
C ', heading_style=ATX) == '\n\n# A P C\n\n'