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
11 changes: 8 additions & 3 deletions mdformat_mkdocs/_normalize_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,14 @@ def normalize_list(
str: formatted text

"""
if node.level > 1:
# Note: this function is called recursively,
# so only process the top-level item
# Calculate the expected level for a top-level list in the current context
# Each definition body adds 2 to the level and 4 to indent_width
defbody_count = context.env.get("indent_width", 0) // 4
expected_top_level = defbody_count * 2

# Only process top-level lists (not nested within other lists)
# This function is called recursively, so we skip nested lists to avoid double-processing
if node.level > expected_top_level + 1:
return _strip_filler(text)

# Retrieve user-options
Expand Down
50 changes: 45 additions & 5 deletions tests/format/test_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ def gcd(a, b):
: 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.
- (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.
""",
)

Expand Down Expand Up @@ -299,3 +299,43 @@ def test_definition_list_wrap_with_gfm():
)
print_text(output, DEF_LIST_WITH_NESTED_WRAP_EXPECTED)
assert output == DEF_LIST_WITH_NESTED_WRAP_EXPECTED


def test_definition_list_nested_indentation():
"""Test that nested lists in definition bodies use 4-space increments.

This is a regression test for issue #63.
"""
input_text = dedent(
"""\
term

: Definition with a list:

- First item
- Nested item
- Deep nested item
""",
)
expected = dedent(
"""\
term

: Definition with a list:

- First item
- Nested item
- Deep nested item
""",
)
output = mdformat.text(input_text, extensions={"mkdocs"})
print_text(output, expected)
assert output == expected

# Verify the indentation levels are multiples of 4
lines = output.split('\n')
for line in lines:
if line.strip().startswith('-'):
spaces = len(line) - len(line.lstrip())
# Spaces before '-' should be 4, 8, or 12 (multiples of 4)
assert spaces in [4, 8, 12], f"Expected 4/8/12 spaces, got {spaces} in: {repr(line)}"