diff --git a/mdformat_mkdocs/_normalize_list.py b/mdformat_mkdocs/_normalize_list.py index b50fa1c..46cdba5 100644 --- a/mdformat_mkdocs/_normalize_list.py +++ b/mdformat_mkdocs/_normalize_list.py @@ -436,6 +436,11 @@ def parse_text(*, text: str, inc_numbers: bool, use_sem_break: bool) -> ParsedTe # Outputs string result +def _strip_filler(text: str) -> str: + """Remove filler characters inserted during wrapping.""" + return text.replace(f"{FILLER_CHAR} ", "").replace(FILLER_CHAR, "") + + def _join(*, new_lines: list[tuple[str, str]]) -> str: """Join ParsedText into a single string representation.""" new_indents, new_contents = unzip(new_lines) @@ -443,10 +448,7 @@ def _join(*, new_lines: list[tuple[str, str]]) -> str: new_indents_iter = new_indents # Remove filler characters added by inline formatting for 'wrap' - new_contents_iter = ( - content.replace(f"{FILLER_CHAR} ", "").replace(FILLER_CHAR, "").rstrip() - for content in new_contents - ) + new_contents_iter = (_strip_filler(content).rstrip() for content in new_contents) return "".join( f"{new_indent}{new_content}{EOL}" @@ -470,7 +472,7 @@ def normalize_list( if node.level > 1: # Note: this function is called recursively, # so only process the top-level item - return text + return _strip_filler(text) # Retrieve user-options inc_numbers = bool(get_conf(context.options, "number")) diff --git a/tests/format/test_wrap.py b/tests/format/test_wrap.py index 61ffe84..04d04cb 100644 --- a/tests/format/test_wrap.py +++ b/tests/format/test_wrap.py @@ -1,3 +1,5 @@ +from textwrap import dedent + import mdformat import pytest @@ -221,6 +223,38 @@ def gcd(a, b): /// """ +DEF_LIST_WITH_NESTED_WRAP = dedent( + """\ + term + + : Definition starts with a paragraph, followed by an unordered list: + + - 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. + + - (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. + + - foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar + (split) foo bar foo bar foo bar foo bar. + """, +) + +DEF_LIST_WITH_NESTED_WRAP_EXPECTED = dedent( + """\ + term + + : Definition starts with a paragraph, followed by an unordered list: + + - 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. + + - (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. + + - foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo + bar (split) foo bar foo bar foo bar foo bar. + """, +) + @pytest.mark.parametrize( ("text", "expected", "align_lists", "wrap"), @@ -255,3 +289,13 @@ def test_wrap(text: str, expected: str, align_lists: bool, wrap: int): ) print_text(output, expected) assert output.lstrip() == expected.lstrip() + + +def test_definition_list_wrap_with_gfm(): + output = mdformat.text( + DEF_LIST_WITH_NESTED_WRAP, + options={"wrap": 80}, + extensions={"mkdocs", "gfm"}, + ) + print_text(output, DEF_LIST_WITH_NESTED_WRAP_EXPECTED) + assert output == DEF_LIST_WITH_NESTED_WRAP_EXPECTED