Skip to content

Commit b7d8037

Browse files
committed
fix(#58): fix word wrap on definition lists
1 parent fe36984 commit b7d8037

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-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: 34 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,28 @@ 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
233+
foo bar foo bar foo bar foo bar.
234+
""",
235+
)
236+
237+
DEF_LIST_WITH_NESTED_WRAP_EXPECTED = dedent(
238+
"""\
239+
term
240+
241+
: Definition starts with a paragraph, followed by an unordered list:
242+
243+
- Foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
244+
foo bar foo bar foo bar foo bar.
245+
""",
246+
)
247+
224248

225249
@pytest.mark.parametrize(
226250
("text", "expected", "align_lists", "wrap"),
@@ -255,3 +279,13 @@ def test_wrap(text: str, expected: str, align_lists: bool, wrap: int):
255279
)
256280
print_text(output, expected)
257281
assert output.lstrip() == expected.lstrip()
282+
283+
284+
def test_definition_list_wrap_with_gfm():
285+
output = mdformat.text(
286+
DEF_LIST_WITH_NESTED_WRAP,
287+
options={"wrap": 80},
288+
extensions={"mkdocs", "gfm"},
289+
)
290+
print_text(output, DEF_LIST_WITH_NESTED_WRAP_EXPECTED)
291+
assert output == DEF_LIST_WITH_NESTED_WRAP_EXPECTED

0 commit comments

Comments
 (0)