Skip to content

fix: preserve indentation and parse options with blank lines in nested mkdocstrings injection blocks#82

Open
bwrob wants to merge 6 commits into
KyleKing:mainfrom
bwrob:fix/mkdocstrings-injection-nested-formatting
Open

fix: preserve indentation and parse options with blank lines in nested mkdocstrings injection blocks#82
bwrob wants to merge 6 commits into
KyleKing:mainfrom
bwrob:fix/mkdocstrings-injection-nested-formatting

Conversation

@bwrob

@bwrob bwrob commented Jun 27, 2026

Copy link
Copy Markdown

Fixing mkdocstring injections with empty lines in options or inside List.

Formatting Comparison

1. Nested Example (Inside List Item)

Input Markdown

- List item:
    ::: my_package._private_module
        options:
          heading_level: 2

          show_source: true

Before

- List item:
    ::: my_package.\_private_module
    options:
    heading_level: 2

    ```
        show_source: true
    ```

After

- List item:
    ::: my_package._private_module
        options:
          heading_level: 2

          show_source: true

2. Plain Example

Input Markdown

::: my_package._private_module
    options:
      heading_level: 2

      show_source: true

Before

::: my_package._private_module
    options:
      heading_level: 2

show_source: true

After

::: my_package._private_module
    options:
      heading_level: 2

      show_source: true

@coderabbitai

coderabbitai Bot commented Jun 27, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

This PR exports INJECTION_PATTERN, updates mkdocstrings injection parsing to use it and trim trailing blank lines, extends list normalization to detect injection blocks and account for their indentation, adds injection-focused fixtures and snapshots, and updates the related test parametrization.

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@mdformat_mkdocs/_normalize_list.py`:
- Around line 458-466: The injection block handling in _normalize_list is still
letting mkdocstrings YAML sequences get normalized as Markdown lists, which
corrupts literal payloads. Update the new_contents construction path to treat
BlockIndent.kind == "code" from _parse_injection_block as literal content, not
list syntax, while keeping the existing indentation protection. Adjust the logic
that builds block_indents/injection_indents so code-style injection blocks
bypass the markdown list normalization path and preserve list-like YAML
unchanged.

In `@tests/format/test_format.py`:
- Line 44: The new injection cases in the format test suite are only covered by
plain equality checks, so add them to the existing syrupy snapshot coverage in
the test file that exercises the fixture corpus. Update the relevant snapshot
test around the format test harness to include the new cases, using the existing
snapshot-style assertions so indentation changes are reviewed as snapshots
instead of only direct comparisons.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: e514e204-6a18-4eb5-85a2-911aacee3ce7

📥 Commits

Reviewing files that changed from the base of the PR and between 26e880a and 659def8.

📒 Files selected for processing (5)
  • mdformat_mkdocs/_normalize_list.py
  • mdformat_mkdocs/mdit_plugins/__init__.py
  • mdformat_mkdocs/mdit_plugins/_mkdocstrings_injection.py
  • tests/format/fixtures/mkdocstrings_injection.md
  • tests/format/test_format.py

Comment thread mdformat_mkdocs/_normalize_list.py
Comment thread tests/format/test_format.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
mdformat_mkdocs/_normalize_list.py (1)

458-487: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Use block_indents for semantic-break trimming too.

Line 480 still feeds code_indents into _parse_semantic_indent. Injection blocks only exist in block_indents, so with use_sem_break=True nested ::: blocks are still treated as normal continuation text and trimmed down to the 2-space semantic indent instead of preserving the 4-space child-block indent.

Suggested fix
     if use_sem_break:
         semantic_indents = map_lookback(
             _parse_semantic_indent,
-            [*zip(lines, code_indents, strict=True)],
-            _parse_semantic_indent(SemanticIndent.INITIAL, (lines[0], code_indents[0])),
+            [*zip(lines, block_indents, strict=True)],
+            _parse_semantic_indent(
+                SemanticIndent.INITIAL, (lines[0], block_indents[0])
+            ),
         )

As per coding guidelines, mdformat_mkdocs/_normalize_list.py must enforce 4-space indentation and handle semantic line breaks with 3-space alignment for numbered lists when semantic-break alignment is enabled.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@mdformat_mkdocs/_normalize_list.py` around lines 458 - 487, The
semantic-break trimming path in _normalize_indents still uses code_indents when
calling _parse_semantic_indent, so injection/child blocks are not preserved
correctly. Update the use_sem_break branch in _normalize_indents to base
semantic indentation on block_indents (or equivalent combined block state)
instead of only code_indents, and keep _trim_semantic_indent aligned with the
4-space list indentation and 3-space numbered-list semantic-break behavior.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@mdformat_mkdocs/_normalize_list.py`:
- Around line 458-487: The semantic-break trimming path in _normalize_indents
still uses code_indents when calling _parse_semantic_indent, so injection/child
blocks are not preserved correctly. Update the use_sem_break branch in
_normalize_indents to base semantic indentation on block_indents (or equivalent
combined block state) instead of only code_indents, and keep
_trim_semantic_indent aligned with the 4-space list indentation and 3-space
numbered-list semantic-break behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: e75b765e-f47e-432c-bf12-f18ea99f5f59

📥 Commits

Reviewing files that changed from the base of the PR and between 659def8 and 6cfa449.

📒 Files selected for processing (6)
  • mdformat_mkdocs/__init__.py
  • mdformat_mkdocs/_normalize_list.py
  • mdformat_mkdocs/mdit_plugins/_material_deflist.py
  • mdformat_mkdocs/mdit_plugins/_python_markdown_attr_list.py
  • tests/format/__snapshots__/test_parsed_result.ambr
  • tests/format/test_parsed_result.py
💤 Files with no reviewable changes (3)
  • mdformat_mkdocs/init.py
  • mdformat_mkdocs/mdit_plugins/_python_markdown_attr_list.py
  • mdformat_mkdocs/mdit_plugins/_material_deflist.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant