1"""Join raw text tokens with the rest of the text
2
3This is set as a separate rule to provide an opportunity for plugins
4to run text replacements after text join, but before escape join.
5
6For example, `\\:)` shouldn't be replaced with an emoji.
7"""
8from __future__ import annotations
9
10from ..token import Token
11from .state_core import StateCore
12
13
14def text_join(state: StateCore) -> None:
15 """Join raw text for escape sequences (`text_special`) tokens with the rest of the text"""
16
17 for inline_token in state.tokens[:]:
18 if inline_token.type != "inline":
19 continue
20
21 # convert text_special to text and join all adjacent text nodes
22 new_tokens: list[Token] = []
23 for child_token in inline_token.children or []:
24 if child_token.type == "text_special":
25 child_token.type = "text"
26 if (
27 child_token.type == "text"
28 and new_tokens
29 and new_tokens[-1].type == "text"
30 ):
31 new_tokens[-1].content += child_token.content
32 else:
33 new_tokens.append(child_token)
34 inline_token.children = new_tokens