Skip to content

Commit 13f4cd6

Browse files
authored
fix(#58): fix word wrap on definition lists (#62)
1 parent 1d8edf8 commit 13f4cd6

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

mdformat_mkdocs/_normalize_list.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,17 +436,19 @@ def parse_text(*, text: str, inc_numbers: bool, use_sem_break: bool) -> ParsedTe
436436
# Outputs string result
437437

438438

439+
def _strip_filler(text: str) -> str:
440+
"""Remove filler characters inserted during wrapping."""
441+
return text.replace(f"{FILLER_CHAR} ", "").replace(FILLER_CHAR, "")
442+
443+
439444
def _join(*, new_lines: list[tuple[str, str]]) -> str:
440445
"""Join ParsedText into a single string representation."""
441446
new_indents, new_contents = unzip(new_lines)
442447

443448
new_indents_iter = new_indents
444449

445450
# Remove filler characters added by inline formatting for 'wrap'
446-
new_contents_iter = (
447-
content.replace(f"{FILLER_CHAR} ", "").replace(FILLER_CHAR, "").rstrip()
448-
for content in new_contents
449-
)
451+
new_contents_iter = (_strip_filler(content).rstrip() for content in new_contents)
450452

451453
return "".join(
452454
f"{new_indent}{new_content}{EOL}"
@@ -470,7 +472,7 @@ def normalize_list(
470472
if node.level > 1:
471473
# Note: this function is called recursively,
472474
# so only process the top-level item
473-
return text
475+
return _strip_filler(text)
474476

475477
# Retrieve user-options
476478
inc_numbers = bool(get_conf(context.options, "number"))

tests/format/test_wrap.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from textwrap import dedent
2+
13
import mdformat
24
import pytest
35

@@ -221,6 +223,38 @@ def gcd(a, b):
221223
///
222224
"""
223225

226+
DEF_LIST_WITH_NESTED_WRAP = dedent(
227+
"""\
228+
term
229+
230+
: Definition starts with a paragraph, followed by an unordered list:
231+
232+
- Foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar.
233+
234+
- (3) bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar.
235+
236+
- foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
237+
(split) foo bar foo bar foo bar foo bar.
238+
""",
239+
)
240+
241+
DEF_LIST_WITH_NESTED_WRAP_EXPECTED = dedent(
242+
"""\
243+
term
244+
245+
: Definition starts with a paragraph, followed by an unordered list:
246+
247+
- Foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
248+
foo bar foo bar foo bar foo bar.
249+
250+
- (3) bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
251+
foo bar foo bar foo bar foo bar.
252+
253+
- foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo
254+
bar (split) foo bar foo bar foo bar foo bar.
255+
""",
256+
)
257+
224258

225259
@pytest.mark.parametrize(
226260
("text", "expected", "align_lists", "wrap"),
@@ -255,3 +289,13 @@ def test_wrap(text: str, expected: str, align_lists: bool, wrap: int):
255289
)
256290
print_text(output, expected)
257291
assert output.lstrip() == expected.lstrip()
292+
293+
294+
def test_definition_list_wrap_with_gfm():
295+
output = mdformat.text(
296+
DEF_LIST_WITH_NESTED_WRAP,
297+
options={"wrap": 80},
298+
extensions={"mkdocs", "gfm"},
299+
)
300+
print_text(output, DEF_LIST_WITH_NESTED_WRAP_EXPECTED)
301+
assert output == DEF_LIST_WITH_NESTED_WRAP_EXPECTED

0 commit comments

Comments
 (0)