From 7b8ceeaec32549b50c83dee77e5c0cfa469d7611 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Thu, 25 Jun 2026 11:42:11 +0200 Subject: [PATCH] fix: preserve text nested inside
by html.parser Python's html.parser can wrap text following
(with a space) inside the
element as child content when the
is preceded by a sibling
without a slash. convert_br received the nested text via its `text` parameter but discarded it, silently dropping characters from the output. Append `text` to the returned line-break marker so that any accidentally nested content is always preserved. Closes #244 --- markdownify/__init__.py | 6 +++--- tests/test_conversions.py | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 148d340..28cdaf6 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -475,12 +475,12 @@ def _indent_for_blockquote(match): def convert_br(self, el, text, parent_tags): if '_inline' in parent_tags: - return ' ' + return text + ' ' if text else ' ' if self.options['newline_style'].lower() == BACKSLASH: - return '\\\n' + return '\\\n' + text else: - return ' \n' + return ' \n' + text def convert_code(self, el, text, parent_tags): if '_noformat' in parent_tags: diff --git a/tests/test_conversions.py b/tests/test_conversions.py index dd99dfb..c95483c 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -81,6 +81,9 @@ def test_br(): assert md('a
b
c', newline_style=BACKSLASH) == 'a\\\nb\\\nc' assert md('

foo
bar

', heading_style=ATX) == '\n\n# foo bar\n\n' assert md('foo
bar', heading_style=ATX) == ' foo bar |' + # html.parser may nest text inside
when mixing
and
; text must not be lost + assert md('a
b
c') == 'a \nb \nc' + assert md('a
b
c', newline_style=BACKSLASH) == 'a\\\nb\\\nc' def test_code():