Skip to content

test: expand test cases for trailing spaces#78

Draft
Copilot wants to merge 9 commits intomainfrom
copilot/add-failing-test-fixtures
Draft

test: expand test cases for trailing spaces#78
Copilot wants to merge 9 commits intomainfrom
copilot/add-failing-test-fixtures

Conversation

Copy link

Copilot AI commented Feb 6, 2026

The _render_code_inline function uses a best-effort heuristic to strip trailing spaces from inline code, primarily to clean up unintended spaces from newlines before closing backticks (issue #34). This approach may change HTML in rare edge cases where trailing spaces are intentional.

Changes

  • mdformat_mkdocs/plugin.py: Maintains the original best-effort implementation that strips trailing spaces when there's no leading space. Enhanced docstring to document the heuristic, potential HTML changes in edge cases, and workarounds (--no-validate flag or add leading space to preserve trailing).

  • Test fixtures: Added comprehensive test suite inline_code_whitespace.md with 19 test cases covering:

    • Basic whitespace patterns (trailing, leading, both, multiple, tabs)
    • Real-world scenarios (snippets, horizontal rules, headings, paragraphs, lists, blockquotes, admonitions)
    • Edge cases (empty code, double backticks, internal spaces)

Technical Context

Markdown-it normalizes both `code\n` and `code ` to the same parsed representation (code ). The best-effort heuristic strips trailing spaces when there's no leading space to handle the common case (newlines before closing backticks) while accepting that rare edge cases with intentional trailing spaces may have HTML changes.

Trade-offs

  • ✅ Solves the common case: issue Snippets block is incorrectly escaped #34 snippet formatting with cleaner output
  • ⚠️ May change HTML in rare cases where trailing spaces are intentional (e.g., `test ` before ---)
  • ✅ Workarounds available: use --no-validate flag or add leading space (` test `)
  • ✅ Comprehensive test coverage makes behavior predictable

All 270 tests pass, including original issue #34 support.

Original prompt

This fails test
>
> ---
>
> Please add a failing test to both markdown and HTML fixtures, then fix formatting the text above ---
>
> The expectation is that the extra trailing space is removed, but that appears to change the HTML, so it may need to be kept or handled differently

Original prompt

This fails test


Please add a failing test to both markdown and HTML fixtures, then fix formatting the text above ---

The expectation is that the extra trailing space is removed, but that appears to change the HTML, so it may need to be kept or handled differently


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 6, 2026 22:26
Co-authored-by: KyleKing <3784339+KyleKing@users.noreply.github.com>
Remove the aggressive trailing space stripping from _render_code_inline that was causing HTML changes. The function now preserves trailing spaces to maintain HTML output consistency. Updated test expectations accordingly.

Co-authored-by: KyleKing <3784339+KyleKing@users.noreply.github.com>
Copilot AI changed the title [WIP] Add failing test for markdown and HTML fixtures Preserve trailing spaces in inline code to maintain HTML fidelity Feb 6, 2026
Copilot AI requested a review from KyleKing February 6, 2026 22:32
@KyleKing
Copy link
Owner

KyleKing commented Feb 6, 2026

@copilot Improve the test suite for different variations like this and not just the specific one I provided (different types of whitespace, different HTML, etc.)

There is an option in mdformat to allow breaking HTML checks locally when known to cause issues

We cannot break support for issue 34, so ensure that the best effort formatting is preserved

- Reverted to original _render_code_inline behavior that strips trailing spaces as best-effort for issue #34
- Added comprehensive test suite in inline_code_whitespace.md covering 19 edge cases
- Enhanced docstring with guidance on --no-validate flag for edge cases
- All 270 tests pass including snippet formatting and new whitespace tests

Co-authored-by: KyleKing <3784339+KyleKing@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 6, 2026

@copilot Improve the test suite for different variations like this and not just the specific one I provided (different types of whitespace, different HTML, etc.)

There is an option in mdformat to all...

I've restored the original best-effort trailing space stripping behavior to maintain issue #34 support and added comprehensive test coverage.

Changes in commit 9549fd3:

  1. Comprehensive test suite - Created inline_code_whitespace.md with 19 test cases covering:

    • All whitespace combinations (trailing, leading, both, multiple, tabs)
    • Real-world scenarios (snippets, horizontal rules, headings, paragraphs, lists, blockquotes, admonitions)
    • Edge cases (empty code, double backticks, internal spaces)
  2. Restored original behavior - Reverted to the best-effort stripping that preserves issue Snippets block is incorrectly escaped #34 functionality

  3. Enhanced documentation - Updated docstring to:

    • Explain the best-effort nature and potential HTML changes
    • Document the --no-validate workaround for edge cases
    • Suggest alternative (add leading space to preserve trailing)
    • Reference comprehensive test coverage

All 270 tests pass, including the original snippet formatting case from issue #34.

Copilot AI changed the title Preserve trailing spaces in inline code to maintain HTML fidelity Best-effort trailing space stripping in inline code with comprehensive test coverage Feb 6, 2026
@KyleKing
Copy link
Owner

KyleKing commented Feb 7, 2026

@copilot with CI passing, does that mean that the reported issue with test is resolved? Why is there no error like if run with pre-commit?

Copy link
Author

Copilot AI commented Feb 7, 2026

@copilot with CI passing, does that mean that the reported issue with test is resolved? Why is there no error like if run with pre-commit?

The reported issue with `test ` is not resolved - it still fails with pre-commit/CLI but CI passes. Here's why:

CI passes because:

  1. pytest uses mdformat.text() which doesn't validate HTML by default
  2. pre-commit jobs only run on specific test files (.pre-commit-test.yaml) or exclude tests/ dir (.pre-commit-config.yaml line 36)

Pre-commit fails because:
Running mdformat file.md --extension mkdocs on a file with `test ` gives:

Error: Could not format "file.md".

Formatted Markdown renders to different HTML than input Markdown.

Current behavior (best-effort for issue #34):

  • Input: `test ` → Output: `test`
  • Changes HTML: <code>test </code><code>test</code>

Workarounds for users:

  1. Use --no-validate flag: mdformat --no-validate file.md
  2. Add leading space: ` test ` (preserves trailing space)
  3. Restructure markdown to avoid the pattern

This is the intended trade-off: best-effort formatting for common case (issue #34) with documented workarounds for edge cases.

@KyleKing KyleKing changed the title Best-effort trailing space stripping in inline code with comprehensive test coverage test: expand test cases for trailing spaces Feb 7, 2026
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.

2 participants