Skip to content

Commit 30ef44b

Browse files
committed
refactor: move link token construction
1 parent 3066209 commit 30ef44b

2 files changed

Lines changed: 27 additions & 28 deletions

File tree

src/mistune/_inline/links.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def parse_link(inline: "InlineParser", m: Match[str], state: InlineState) -> Opt
7979
if pos2:
8080
if text is None:
8181
text = state.src[text_start:text_end]
82-
token = inline._parse_link_token(is_image, text, attrs, state)
82+
token = build_link_token(inline, is_image, text, attrs, state)
8383
state.append_token(token)
8484
return pos2
8585
if scan_end > body_end_pos:
@@ -113,7 +113,7 @@ def parse_link(inline: "InlineParser", m: Match[str], state: InlineState) -> Opt
113113
if text is None:
114114
text = state.src[text_start:text_end]
115115
attrs = {"url": env["url"], "title": env.get("title")}
116-
token = inline._parse_link_token(is_image, text, attrs, state)
116+
token = build_link_token(inline, is_image, text, attrs, state)
117117
token["ref"] = key
118118
token["label"] = label
119119
state.append_token(token)
@@ -122,6 +122,31 @@ def parse_link(inline: "InlineParser", m: Match[str], state: InlineState) -> Opt
122122
return None
123123

124124

125+
def build_link_token(
126+
inline: "InlineParser",
127+
is_image: bool,
128+
text: str,
129+
attrs: Optional[Dict[str, object]],
130+
state: InlineState,
131+
) -> Dict[str, object]:
132+
new_state = state.copy()
133+
new_state.src = text
134+
if is_image:
135+
new_state.in_image = True
136+
new_state.image_depth += 1
137+
return {
138+
"type": "image",
139+
"children": inline.render(new_state),
140+
"attrs": attrs,
141+
}
142+
new_state.in_link = True
143+
return {
144+
"type": "link",
145+
"children": inline.render(new_state),
146+
"attrs": attrs,
147+
}
148+
149+
125150
def mark_no_link_before(state: InlineState, end_pos: int) -> None:
126151
if end_pos > state.no_link_before:
127152
state.no_link_before = end_pos

src/mistune/inline_parser.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,32 +126,6 @@ def parse_escape(self, m: Match[str], state: InlineState) -> int:
126126
def parse_link(self, m: Match[str], state: InlineState) -> Optional[int]:
127127
return parse_inline_link(self, m, state)
128128

129-
def _parse_link_token(
130-
self,
131-
is_image: bool,
132-
text: str,
133-
attrs: Optional[Dict[str, Any]],
134-
state: InlineState,
135-
) -> Dict[str, Any]:
136-
new_state = state.copy()
137-
new_state.src = text
138-
if is_image:
139-
new_state.in_image = True
140-
new_state.image_depth += 1
141-
token = {
142-
"type": "image",
143-
"children": self.render(new_state),
144-
"attrs": attrs,
145-
}
146-
else:
147-
new_state.in_link = True
148-
token = {
149-
"type": "link",
150-
"children": self.render(new_state),
151-
"attrs": attrs,
152-
}
153-
return token
154-
155129
def parse_auto_link(self, m: Match[str], state: InlineState) -> int:
156130
text = m.group(0)
157131
pos = m.end()

0 commit comments

Comments
 (0)