mdformat-toc icon indicating copy to clipboard operation
mdformat-toc copied to clipboard

Incorrect table of contents for additional title spacing with mdformat 0.7.22

Open KristofBCoding opened this issue 11 months ago • 0 comments

Mdformat trims superfluous whitespace in 0.7.22 when formatting text. Because heading information is loaded before any markdown formatting, this can lead to incorrect toc entries. Consider the following example:


<!-- mdformat-toc start --slug=github --no-anchors --maxlevel=6 --minlevel=1 -->

# foo     bar
.

Which, when running mdformat with this plugin gives:

<!-- mdformat-toc start --slug=github --no-anchors --maxlevel=6 --minlevel=1 -->

- [foo     bar](#foo-----bar)

<!-- mdformat-toc end -->

# foo bar

Spacing in the title was formatted away, which is now wrong in the table of contents because that was generated before formatting.

Running twice would fix this, but this isn't exactly clean, especially when using pre-commit.

Note that you can easily see this go wrong in the existing tests, one of them will fail as soon as you install 0.7.22 of mdformat.

KristofBCoding avatar Mar 13 '25 22:03 KristofBCoding