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('
alpha beta gamma delta', '
alpha beta gamma delta', '
| alpha beta gamma delta |
alpha beta gamma delta']: + html = '
alpha beta gamma delta