Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions mdformat_mkdocs/_normalize_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,19 @@ 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)

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}"
Expand All @@ -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"))
Expand Down
44 changes: 44 additions & 0 deletions tests/format/test_wrap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from textwrap import dedent

import mdformat
import pytest

Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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