Skip to content

mdformat fails with “renders to different HTML” when inline code span contains trailing whitespace #77

@tr0k

Description

@tr0k

mdformat fails with:

Formatted Markdown renders to different HTML than input Markdown.

when an inline code span contains trailing whitespace inside the backticks.

This makes formatting impossible for otherwise valid Markdown.

This appears to be triggered only when the mdformat-mkdocs plugin is enabled.

Environment

mdformat==1.0.0
mdformat-mkdocs==5.1.4
markdown-it-py==4.0.0
mdit-py-plugins==0.5.0
pre-commit
python 3.13 (also reproducible after clean environment rebuild)
macOS

Minimal reproduction

test.md

- example `test `

Run:

pre-commit run mdformat --files test.md -v

Actual behavior

Error: Could not format
Formatted Markdown renders to different HTML than input Markdown.

Expected behavior

Formatting should succeed (or at least not fail hard).
Trailing whitespace inside inline code spans should not cause HTML instability.

Observations

Works:

`test`
`test test`

Fails:

`test `

Removing only the trailing space fixes it.

This suggests the plugin/parser combination produces different HTML before vs after formatting when trailing whitespace exists inside inline code.

Notes

Reproduces consistently after:

pre-commit clean

So it’s not cache-related.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions