@@ -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
|