@@ -125,6 +125,9 @@ def parse_link(self, m: Match[str], state: InlineState) -> Optional[int]:
125125 if not is_image and state .in_link :
126126 state .append_token ({"type" : "text" , "raw" : marker })
127127 return pos
128+ if not is_image and pos <= state .no_link_before :
129+ state .append_token ({"type" : "text" , "raw" : marker })
130+ return pos
128131
129132 text = None
130133 label , end_pos = parse_link_label (state .src , pos )
@@ -144,11 +147,13 @@ def parse_link(self, m: Match[str], state: InlineState) -> Optional[int]:
144147 text = label
145148
146149 assert text is not None
150+ body_end_pos = end_pos
147151
148- if end_pos >= len (state .src ) and label is None :
152+ has_nested_link = not is_image and self ._contains_nested_link (text , state )
153+ if has_nested_link :
149154 return None
150-
151- if not is_image and self . _contains_nested_link ( text , state ):
155+ if end_pos >= len ( state . src ) and label is None :
156+ _mark_no_link_before ( state , body_end_pos )
152157 return None
153158
154159 rules = ["codespan" , "prec_auto_link" , "prec_inline_html" ]
@@ -175,10 +180,12 @@ def parse_link(self, m: Match[str], state: InlineState) -> Optional[int]:
175180 label = label2
176181
177182 if label is None :
183+ _mark_no_link_before (state , body_end_pos )
178184 return None
179185
180186 ref_links = state .env .get ("ref_links" )
181187 if not ref_links :
188+ _mark_no_link_before (state , body_end_pos )
182189 return None
183190
184191 key = unikey (label )
@@ -190,6 +197,7 @@ def parse_link(self, m: Match[str], state: InlineState) -> Optional[int]:
190197 token ["label" ] = label
191198 state .append_token (token )
192199 return end_pos
200+ _mark_no_link_before (state , body_end_pos )
193201 return None
194202
195203 def _contains_nested_link (self , text : str , state : InlineState ) -> bool :
@@ -805,6 +813,11 @@ def _parse_link_text(state: InlineState, pos: int) -> Tuple[Optional[str], int]:
805813 return state .src [pos :close_pos ], close_pos + 1
806814
807815
816+ def _mark_no_link_before (state : InlineState , end_pos : int ) -> None :
817+ if end_pos > state .no_link_before :
818+ state .no_link_before = end_pos
819+
820+
808821def _find_closing_bracket (state : InlineState , pos : int ) -> Optional [int ]:
809822 cache = state .link_brackets .get (id (state .src ))
810823 if cache is not None and cache [0 ] is state .src :
0 commit comments