Skip to content

Commit 9da42ff

Browse files
KyleKingclaude
andauthored
fix(#63): ensure 4-space indentation for nested lists in definition bodies (#64)
Co-authored-by: Claude <[email protected]>
1 parent aab64ec commit 9da42ff

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

mdformat_mkdocs/_normalize_list.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,14 @@ def normalize_list(
469469
str: formatted text
470470
471471
"""
472-
if node.level > 1:
473-
# Note: this function is called recursively,
474-
# so only process the top-level item
472+
# Calculate the expected level for a top-level list in the current context
473+
# Each definition body adds 2 to the level and 4 to indent_width
474+
defbody_count = context.env.get("indent_width", 0) // 4
475+
expected_top_level = defbody_count * 2
476+
477+
# Only process top-level lists (not nested within other lists)
478+
# This function is called recursively, so we skip nested lists to avoid double-processing
479+
if node.level > expected_top_level + 1:
475480
return _strip_filler(text)
476481

477482
# Retrieve user-options

tests/format/test_wrap.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,13 @@ def gcd(a, b):
245245
: Definition starts with a paragraph, followed by an unordered list:
246246
247247
- 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
251248
foo bar foo bar foo bar foo bar.
252249
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.
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.
255255
""",
256256
)
257257

@@ -299,3 +299,43 @@ def test_definition_list_wrap_with_gfm():
299299
)
300300
print_text(output, DEF_LIST_WITH_NESTED_WRAP_EXPECTED)
301301
assert output == DEF_LIST_WITH_NESTED_WRAP_EXPECTED
302+
303+
304+
def test_definition_list_nested_indentation():
305+
"""Test that nested lists in definition bodies use 4-space increments.
306+
307+
This is a regression test for issue #63.
308+
"""
309+
input_text = dedent(
310+
"""\
311+
term
312+
313+
: Definition with a list:
314+
315+
- First item
316+
- Nested item
317+
- Deep nested item
318+
""",
319+
)
320+
expected = dedent(
321+
"""\
322+
term
323+
324+
: Definition with a list:
325+
326+
- First item
327+
- Nested item
328+
- Deep nested item
329+
""",
330+
)
331+
output = mdformat.text(input_text, extensions={"mkdocs"})
332+
print_text(output, expected)
333+
assert output == expected
334+
335+
# Verify the indentation levels are multiples of 4
336+
lines = output.split('\n')
337+
for line in lines:
338+
if line.strip().startswith('-'):
339+
spaces = len(line) - len(line.lstrip())
340+
# Spaces before '-' should be 4, 8, or 12 (multiples of 4)
341+
assert spaces in [4, 8, 12], f"Expected 4/8/12 spaces, got {spaces} in: {repr(line)}"

0 commit comments

Comments
 (0)