From efc96e8ee7b8ecf7953a4ec602918a7d6365203f Mon Sep 17 00:00:00 2001 From: xiangnuans Date: Tue, 10 Nov 2026 15:08:25 +0800 Subject: [PATCH] Support wrapping inline-only list items --- README.rst | 3 +++ markdownify/__init__.py | 44 ++++++++++++++++++++++++++--------------- tests/test_lists.py | 25 +++++++++++++++++++++++ 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/README.rst b/README.rst index 059a68f..b27cdd0 100644 --- a/README.rst +++ b/README.rst @@ -147,6 +147,9 @@ table_infer_header wrap, wrap_width If ``wrap`` is set to ``True``, all text paragraphs are wrapped at ``wrap_width`` characters. Defaults to ``False`` and ``80``. + List items containing only inline content are also wrapped; list markers + and indentation are added outside this width. Items containing block + elements retain their existing layout. Use with ``newline_style=BACKSLASH`` to keep line breaks in paragraphs. A `wrap_width` value of `None` reflows lines to unlimited line length. diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 28cdaf6..6b9c903 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -631,6 +631,14 @@ def convert_li(self, el, text, parent_tags): if not text: return "\n" + # Reflow inline-only items; block descendants already have their own + # layout and must not be flattened (including nested lists). + if self.options['wrap'] and not any( + should_remove_whitespace_outside(child) + or child.name in ('hr', 'figure', 'figcaption', 'caption') + for child in el.find_all(True)): + text = self._wrap_text(text) + # determine list item bullet character to use parent = el.parent if parent is not None and parent.name == 'ol': @@ -667,24 +675,28 @@ def convert_p(self, el, text, parent_tags): return ' ' + text.strip(' \t\r\n') + ' ' text = text.strip(' \t\r\n') if self.options['wrap']: - # Preserve newlines (and preceding whitespace) resulting - # from
tags. Newlines in the input have already been - # replaced by spaces. - if self.options['wrap_width'] is not None: - lines = text.split('\n') - new_lines = [] - for line in lines: - line = line.lstrip(' \t\r\n') - line_no_trailing = line.rstrip() - trailing = line[len(line_no_trailing):] - line = fill(line, - width=self.options['wrap_width'], - break_long_words=False, - break_on_hyphens=False) - new_lines.append(line + trailing) - text = '\n'.join(new_lines) + text = self._wrap_text(text) return '\n\n%s\n\n' % text if text else '' + def _wrap_text(self, text): + # Preserve newlines (and preceding whitespace) resulting + # from
tags. Newlines in the input have already been + # replaced by spaces. + if self.options['wrap_width'] is not None: + lines = text.split('\n') + new_lines = [] + for line in lines: + line = line.lstrip(' \t\r\n') + line_no_trailing = line.rstrip() + trailing = line[len(line_no_trailing):] + line = fill(line, + width=self.options['wrap_width'], + break_long_words=False, + break_on_hyphens=False) + new_lines.append(line + trailing) + text = '\n'.join(new_lines) + return text + def convert_pre(self, el, text, parent_tags): if not text: return '' diff --git a/tests/test_lists.py b/tests/test_lists.py index e9480ab..5ffff09 100644 --- a/tests/test_lists.py +++ b/tests/test_lists.py @@ -1,4 +1,5 @@ from .utils import md +from markdownify import markdownify nested_uls = """ @@ -87,3 +88,27 @@ def test_bullets(): def test_li_text(): assert md('') == '\n\n* foo [bar](#)\n* foo bar\n* foo **bar** *space*.\n' + + +def test_wrap_inline_list_items(): + text = 'alpha beta gamma delta epsilon' + assert markdownify('', wrap=True, wrap_width=16) == '* alpha beta gamma\n delta epsilon' + assert markdownify('
  1. ' + text + '
', wrap=True, wrap_width=16) == '12. alpha beta gamma\n delta epsilon' + assert markdownify('', wrap=False, wrap_width=16) == '* ' + text + assert markdownify('', wrap=True, wrap_width=None) == '* ' + text + + +def test_wrap_inline_list_formatting_and_breaks(): + assert markdownify('', wrap=True, wrap_width=12, newline_style='backslash') == '* alpha beta\n gamma\\\n delta\n epsilon zeta' + assert markdownify('', wrap=True, wrap_width=16) == '* **alpha** beta\n gamma delta' + assert markdownify('', wrap=True, wrap_width=12) == '* alpha beta\n gamma \n delta\n epsilon zeta' + + +def test_wrap_list_blocks_are_not_reflowed(): + nested = '' + assert markdownify(nested, wrap=True, wrap_width=12) == '* parent\n + alpha beta\n gamma delta' + for block in ['
alpha beta gamma delta
', '
', '
alpha beta gamma delta
', '
alpha beta gamma delta
', '
alpha beta gamma delta
', '
alpha beta gamma delta
']: + html = '' + assert markdownify(html, wrap=True, wrap_width=12) == markdownify(html, wrap=False) + html = '' + assert markdownify(html, wrap=True, wrap_width=12) == '* alpha beta\n gamma delta'