« Back to Search  |  Reader Mode  |  Switch to Full Retro Site View Links: Stay in Reader  |  Retro Mode: ON  |  Original »
GitHub · Extracted in 1129 ms

🐛 Keep backslash escapes and entities in image alt text (#446) · executablebooks/markdown-it-py@2fcca3a

Source: https://github.com/executablebooks/markdown-it-py/commit/2fcca3a970520693269fc3af5734e3e71b8168eb

Repository navigation

Commit 2fcca3a


Browse files
gyanu2507 authored 🐛 Keep backslash escapes and entities in image alt text (#446 ) `text_join` now also joins the `text_special` tokens held by an image, so the image's `children` carry joined `text` tokens and the alt text keeps backslash escapes and entities (`![a \* b](/u)` gives `alt="a * b"`, not `alt="a b"`). Ported from markdown-it 15.0.0 (974e2d7), with the same one-level scope: a nested image's own children are still not joined, as in markdown-it 15.0.3. Fixes #445
1 parent a852307 commit 2fcca3a

2 files changed

Lines changed: 67 additions & 30 deletions

File tree

‎markdown_it/rules_core/text_join.py‎

Lines changed: 34 additions & 30 deletions
Original file line number Diff line number Diff line change
@@ -19,35 +19,39 @@ def text_join(state: StateCore) -> None:
19 19 if inline_token.type != "inline":
20 20 continue
21 21
22 - # convert text_special to text and join all adjacent text nodes
23 - new_tokens: list[Token] = []
24 22 children = inline_token.children or []
25 - i = 0
26 - while i < len(children):
27 - child_token = children[i]
28 - if child_token.type == "text_special":
29 - child_token.type = "text"
30 - if (
31 - child_token.type == "text"
32 - and new_tokens
33 - and new_tokens[-1].type == "text"
34 - ):
35 - # Collapse a run of adjacent text nodes in a single join, instead
36 - # of pairwise `a + b` concatenation. The pairwise form is O(L*k)
37 - # in the size of the run because each step rebuilds the growing
38 - # prefix; "".join is O(L).
39 - parts = [new_tokens[-1].content, child_token.content]
23 + for child_token in children:
24 + # image `alt` is parsed into its own token tree
25 + if child_token.children:
26 + child_token.children = _join_text_tokens(child_token.children)
27 + inline_token.children = _join_text_tokens(children)
28 +
29 +
30 +def _join_text_tokens(tokens: list[Token]) -> list[Token]:
31 + """Convert `text_special` to `text` and join all adjacent text tokens"""
32 + new_tokens: list[Token] = []
33 + i = 0
34 + while i < len(tokens):
35 + token = tokens[i]
36 + if token.type == "text_special":
37 + token.type = "text"
38 + if token.type == "text" and new_tokens and new_tokens[-1].type == "text":
39 + # Collapse a run of adjacent text nodes in a single join, instead
40 + # of pairwise `a + b` concatenation. The pairwise form is O(L*k)
41 + # in the size of the run because each step rebuilds the growing
42 + # prefix; "".join is O(L).
43 + parts = [new_tokens[-1].content, token.content]
44 + i += 1
45 + while i < len(tokens):
46 + next_token = tokens[i]
47 + if next_token.type == "text_special":
48 + next_token.type = "text"
49 + if next_token.type != "text":
50 + break
51 + parts.append(next_token.content)
40 52 i += 1
41 - while i < len(children):
42 - next_token = children[i]
43 - if next_token.type == "text_special":
44 - next_token.type = "text"
45 - if next_token.type != "text":
46 - break
47 - parts.append(next_token.content)
48 - i += 1
49 - new_tokens[-1].content = "".join(parts)
50 - else:
51 - new_tokens.append(child_token)
52 - i += 1
53 - inline_token.children = new_tokens
53 + new_tokens[-1].content = "".join(parts)
54 + else:
55 + new_tokens.append(token)
56 + i += 1
57 + return new_tokens

‎tests/test_port/fixtures/issue-fixes.md‎

Lines changed: 33 additions & 0 deletions
Original file line number Diff line number Diff line change
@@ -54,3 +54,36 @@ Fix parsing of incorrect numeric character references
54 54 <p><a href="&amp;#X22y;"></a> &amp;#X22y;
55 55 <a href="&amp;#35y;"></a> &amp;#35y;</p>
56 56 .
57 +
58 +#445 Image alt keeps backslash escapes and entities
59 +.
60 +![a \* b](/u)
61 +
62 +![a &amp; b](/u)
63 +
64 +![a &lt; b](/u)
65 +
66 +![a \* *b* &amp; c](/u)
67 +
68 +![C:\Python26](/u)
69 +.
70 +<p><img src="/u" alt="a * b" /></p>
71 +<p><img src="/u" alt="a &amp; b" /></p>
72 +<p><img src="/u" alt="a &lt; b" /></p>
73 +<p><img src="/u" alt="a * b &amp; c" /></p>
74 +<p><img src="/u" alt="C:\Python26" /></p>
75 +.
76 +
77 +#445 Image alt keeps backslash escapes when the image is a link's text
78 +.
79 +[![a \* b](x)](y)
80 +.
81 +<p><a href="y"><img src="x" alt="a * b" /></a></p>
82 +.
83 +
84 +#445 Nested image alt: only one level is joined, as in markdown-it 15.0.3
85 +.
86 +![a ![b \* c](x) d](y)
87 +.
88 +<p><img src="y" alt="a b c d" /></p>
89 +.