Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions markdown_it/rules_core/text_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,39 @@ def text_join(state: StateCore) -> None:
if inline_token.type != "inline":
continue

# convert text_special to text and join all adjacent text nodes
new_tokens: list[Token] = []
children = inline_token.children or []
i = 0
while i < len(children):
child_token = children[i]
if child_token.type == "text_special":
child_token.type = "text"
if (
child_token.type == "text"
and new_tokens
and new_tokens[-1].type == "text"
):
# Collapse a run of adjacent text nodes in a single join, instead
# of pairwise `a + b` concatenation. The pairwise form is O(L*k)
# in the size of the run because each step rebuilds the growing
# prefix; "".join is O(L).
parts = [new_tokens[-1].content, child_token.content]
for child_token in children:
# image `alt` is parsed into its own token tree
if child_token.children:
child_token.children = _join_text_tokens(child_token.children)
inline_token.children = _join_text_tokens(children)


def _join_text_tokens(tokens: list[Token]) -> list[Token]:
"""Convert `text_special` to `text` and join all adjacent text tokens"""
new_tokens: list[Token] = []
i = 0
while i < len(tokens):
token = tokens[i]
if token.type == "text_special":
token.type = "text"
if token.type == "text" and new_tokens and new_tokens[-1].type == "text":
# Collapse a run of adjacent text nodes in a single join, instead
# of pairwise `a + b` concatenation. The pairwise form is O(L*k)
# in the size of the run because each step rebuilds the growing
# prefix; "".join is O(L).
parts = [new_tokens[-1].content, token.content]
i += 1
while i < len(tokens):
next_token = tokens[i]
if next_token.type == "text_special":
next_token.type = "text"
if next_token.type != "text":
break
parts.append(next_token.content)
i += 1
while i < len(children):
next_token = children[i]
if next_token.type == "text_special":
next_token.type = "text"
if next_token.type != "text":
break
parts.append(next_token.content)
i += 1
new_tokens[-1].content = "".join(parts)
else:
new_tokens.append(child_token)
i += 1
inline_token.children = new_tokens
new_tokens[-1].content = "".join(parts)
else:
new_tokens.append(token)
i += 1
return new_tokens
33 changes: 33 additions & 0 deletions tests/test_port/fixtures/issue-fixes.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,36 @@ Fix parsing of incorrect numeric character references
<p><a href="&amp;#X22y;"></a> &amp;#X22y;
<a href="&amp;#35y;"></a> &amp;#35y;</p>
.

#445 Image alt keeps backslash escapes and entities
.
![a \* b](/u)

![a &amp; b](/u)

![a &lt; b](/u)

![a \* *b* &amp; c](/u)

![C:\Python26](/u)
.
<p><img src="/u" alt="a * b" /></p>
<p><img src="/u" alt="a &amp; b" /></p>
<p><img src="/u" alt="a &lt; b" /></p>
<p><img src="/u" alt="a * b &amp; c" /></p>
<p><img src="/u" alt="C:\Python26" /></p>
.

#445 Image alt keeps backslash escapes when the image is a link's text
.
[![a \* b](x)](y)
.
<p><a href="y"><img src="x" alt="a * b" /></a></p>
.

#445 Nested image alt: only one level is joined, as in markdown-it 15.0.3
.
![a ![b \* c](x) d](y)
.
<p><img src="y" alt="a b c d" /></p>
.
Loading