Skip to content

🐛 FIX: Keep a literal backslash before a space in link destinations - #468

Open
abhishekcpp007 wants to merge 1 commit into
executablebooks:masterfrom
abhishekcpp007:fix/backslash-space-link-destination
Open

abhishekcpp007 wants to merge 1 commit into
executablebooks:masterfrom
abhishekcpp007:fix/backslash-space-link-destination

Conversation

@abhishekcpp007

Copy link
Copy Markdown

In an unbracketed link destination, \ followed by a space hits break, so the backslash is dropped and the whole link fails to parse:

MarkdownIt().render("[a](/url\\ )")
# main: <p>[a](/url\ )</p>
# this: <p><a href="/url%5C">a</a></p>

Per CommonMark 6.1, a backslash before a non-punctuation character is literal, and the space just ends the destination. cmark and markdown-it 15 give the second output. This is the same one-line change as markdown-it/markdown-it#1188 (pos++; continue), which landed after the 14.1.0 port.

Added a fixture next to the existing CommonMark#493 case, covering an inline link, one with a title, and a reference definition.

@@ -56,7 +56,10 @@ def parseLinkDestination(string: str, pos: int, maximum: int) -> _Result:

if code == 0x5C and pos + 1 < maximum:
if charCodeAt(string, pos + 1) == 0x20:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles a space, but a tab after the backslash still goes through the pos += 2 path, so the tab and the title end up in the URL:

>>> MarkdownIt("commonmark").render('[a](/url\\\t"t")\n')
'<p><a href="/url%5C%09%22t%22">a</a></p>\n'

Without the backslash, [a](/url\t"t") gives href="/url" title="t", because the loop above already stops at any ASCII control character (tab included). Same for a reference definition [a]: /url\<TAB>"t".

Checking for every character that ends the destination would cover both:

Suggested change
if charCodeAt(string, pos + 1) == 0x20:
next_code = charCodeAt(string, pos + 1)
if next_code is not None and (next_code <= 0x20 or next_code == 0x7F):

(with the comment adjusted). With that, [a](/url\\\t"t") gives href="/url%5C" title="t", and the space cases in your fixture are unchanged.

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.

2 participants