Skip to content

🐛 Keep the hard line break after a literal backslash - #482

Open
imsurya26 wants to merge 1 commit into
executablebooks:masterfrom
imsurya26:fix/backslash-space-hardbreak
Open

imsurya26 wants to merge 1 commit into
executablebooks:masterfrom
imsurya26:fix/backslash-space-hardbreak

Conversation

@imsurya26

Copy link
Copy Markdown

A literal backslash before two trailing spaces produces a soft break instead of a hard line break:

>>> MarkdownIt("commonmark").render("a\  \nb")
'<p>a\ \nb</p>\n'          # markdown-it 14.3.3: '<p>a\<br />\nb</p>\n'
>>> MarkdownIt("commonmark").render("a\ \nb")
'<p>a\ \nb</p>\n'          # markdown-it 14.3.3: '<p>a\\nb</p>\n'

A backslash before a space is literal (CommonMark §6.1), so the two trailing spaces should still form a hard break (§6.7). The escape rule handled backslash-space in its generic branch, emitting text_special with content "\ " and consuming the space. The newline rule then only saw one trailing space in state.pending.

This ports the upstream fix from markdown-it (markdown-it/markdown-it@56c2404e6d, #1185). A separate branch for ch1 == " " emits the backslash as text_special (markup \, info escape) and leaves the space unconsumed, as in upstream escape.mjs.

The same change was proposed in #417, which its author closed during a cleanup of their older PRs without a review. This PR picks it up again.

Tests:

  • new test_backslash_before_space in tests/test_port/test_misc.py (hard break, mid-line, single-space soft break). 2 of the 3 cases fail without the change
  • pytest tests: 1193 passed
  • I compared the rendering with markdown-it 14.3.3 for all strings up to length 5 over a, \, space, newline, tab, *, `, [, plus hand-written cases (commonmark and default presets). Every backslash/space/newline case now matches
  • ruff check/format clean, mypy clean

A backslash before a space is a literal backslash (CommonMark 6.1), but the escape rule consumed the space with it. For 'a\  \nb' the newline rule then saw only one trailing space and emitted a softbreak instead of a hard break. Port the backslash-space branch from upstream markdown-it (56c2404e6d, #1185): emit the backslash and leave the space in the text.

This branch has not been deployed

No deployments
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