fix: keep backslash-space hard line break (CommonMark 6.7)#1185
fix: keep backslash-space hard line break (CommonMark 6.7)#1185puzrin merged 1 commit intomarkdown-it:mastermarkdown-it/markdown-it:masterfrom spokodev:fix/backslash-space-hard-breakspokodev/markdown-it:fix/backslash-space-hard-breakCopy head branch name to clipboard
Conversation
A backslash followed by a space was baking that space into the escape token, so the newline rule saw only one trailing space and emitted a soft break instead of the mandated hard line break. Per CommonMark 0.31.2 section 6.1, a backslash before a space is a literal backslash and the space is not consumed. Leaving the space in the text stream lets section 6.7 two-space hard-break detection fire. Input `a\ \nb` (backslash + two trailing spaces) now renders `<p>a\<br>\nb</p>` instead of `<p>a\ \nb</p>`.
|
I can not reproduce the problem. Result is exactly as you desired, without "fix" Please add details what is wrong to reopen |
|
Thanks for checking. The demo link in your reply uses two backslashes ( The failing case is a single backslash followed by two trailing spaces. On current md({ linkify: true, typographer: true }).render('a\\ \ns')
// the JS string is: a, one backslash, two spaces, newline, sActual: <p>a\
s</p>Expected (CommonMark 0.31.2 reference renderer): <p>a\<br>
s</p>The two trailing spaces collapse to a soft break and a stray space leaks in after the backslash, instead of the hard line break. One-click repro with the single-backslash input: https://markdown-it.github.io/#md3=%7B%22source%22%3A%22a%5C%5C%20%20%5Cns%22%2C%22defaults%22%3A%7B%22html%22%3Afalse%2C%22xhtmlOut%22%3Afalse%2C%22breaks%22%3Afalse%2C%22langPrefix%22%3A%22language-%22%2C%22linkify%22%3Atrue%2C%22typographer%22%3Atrue%2C%22_highlight%22%3Atrue%2C%22_strict%22%3Afalse%2C%22_view%22%3A%22src%22%7D%7D Root cause is unchanged from the PR: in |
|
Got it. Thank you. |
Problem
A backslash followed by two trailing spaces before a newline should emit a hard line break, but markdown-it downgrades it to a soft break and leaves a stray space.
Repro:
Expected (matches the CommonMark 0.31.2 reference renderer):
Actual on current
master:The backslash is kept literal (correct), but the hard line break is gone and a space leaks into the output.
Why
Per CommonMark 0.31.2:
In
lib/rules_inline/escape.mjs, when\is not followed by a newline, the rule builds atext_specialtoken whose content is the backslash plus the following character, and advances past that character. When the following character is a space, that space gets absorbed into the escape token.lib/rules_inline/newline.mjsthen inspectsstate.pendingfor trailing spaces and finds only one, so it takes the soft-break branch.Fix
Special-case a space after the backslash: push a
text_specialtoken holding just the literal backslash and leavestate.pospointing at the space, so the space stays in the text stream and two-space hard-break detection still fires.Controls verified unchanged:
a \nb(hard break),a\\\nb(backslash before newline, hard break),x\ y(mid-line literal backslash + space),a\ \nb(single trailing space stays a soft break).Tests
Added a fixture to
test/fixtures/markdown-it/commonmark_extras.txtassertinga\ \nbrenders the hard line break. It fails onmaster(actual<p>a\ \nb</p>) and passes with this change. The full suite is green, including all 652 CommonMark spec examples.