@@ -126,13 +126,13 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
126
126
if line [0 ] == '!' {
127
127
if len (line ) > 1 && line [1 ] == '[' {
128
128
block .Advance (1 )
129
- pc . Set ( linkBottom , pc . LastDelimiter () )
129
+ pushLinkBottom ( pc )
130
130
return processLinkLabelOpen (block , segment .Start + 1 , true , pc )
131
131
}
132
132
return nil
133
133
}
134
134
if line [0 ] == '[' {
135
- pc . Set ( linkBottom , pc . LastDelimiter () )
135
+ pushLinkBottom ( pc )
136
136
return processLinkLabelOpen (block , segment .Start , false , pc )
137
137
}
138
138
@@ -143,6 +143,7 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
143
143
}
144
144
last := tlist .(* linkLabelState ).Last
145
145
if last == nil {
146
+ _ = popLinkBottom (pc )
146
147
return nil
147
148
}
148
149
block .Advance (1 )
@@ -151,11 +152,13 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
151
152
// > A link label can have at most 999 characters inside the square brackets.
152
153
if linkLabelStateLength (tlist .(* linkLabelState )) > 998 {
153
154
ast .MergeOrReplaceTextSegment (last .Parent (), last , last .Segment )
155
+ _ = popLinkBottom (pc )
154
156
return nil
155
157
}
156
158
157
159
if ! last .IsImage && s .containsLink (last ) { // a link in a link text is not allowed
158
160
ast .MergeOrReplaceTextSegment (last .Parent (), last , last .Segment )
161
+ _ = popLinkBottom (pc )
159
162
return nil
160
163
}
161
164
@@ -169,6 +172,7 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
169
172
link , hasValue = s .parseReferenceLink (parent , last , block , pc )
170
173
if link == nil && hasValue {
171
174
ast .MergeOrReplaceTextSegment (last .Parent (), last , last .Segment )
175
+ _ = popLinkBottom (pc )
172
176
return nil
173
177
}
174
178
}
@@ -182,12 +186,14 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
182
186
// > A link label can have at most 999 characters inside the square brackets.
183
187
if len (maybeReference ) > 999 {
184
188
ast .MergeOrReplaceTextSegment (last .Parent (), last , last .Segment )
189
+ _ = popLinkBottom (pc )
185
190
return nil
186
191
}
187
192
188
193
ref , ok := pc .Reference (util .ToLinkReference (maybeReference ))
189
194
if ! ok {
190
195
ast .MergeOrReplaceTextSegment (last .Parent (), last , last .Segment )
196
+ _ = popLinkBottom (pc )
191
197
return nil
192
198
}
193
199
link = ast .NewLink ()
@@ -230,11 +236,7 @@ func processLinkLabelOpen(block text.Reader, pos int, isImage bool, pc Context)
230
236
}
231
237
232
238
func (s * linkParser ) processLinkLabel (parent ast.Node , link * ast.Link , last * linkLabelState , pc Context ) {
233
- var bottom ast.Node
234
- if v := pc .Get (linkBottom ); v != nil {
235
- bottom = v .(ast.Node )
236
- }
237
- pc .Set (linkBottom , nil )
239
+ bottom := popLinkBottom (pc )
238
240
ProcessDelimiters (bottom , pc )
239
241
for c := last .NextSibling (); c != nil ; {
240
242
next := c .NextSibling ()
@@ -395,6 +397,43 @@ func parseLinkTitle(block text.Reader) ([]byte, bool) {
395
397
return nil , false
396
398
}
397
399
400
+ func pushLinkBottom (pc Context ) {
401
+ bottoms := pc .Get (linkBottom )
402
+ b := pc .LastDelimiter ()
403
+ if bottoms == nil {
404
+ pc .Set (linkBottom , b )
405
+ return
406
+ }
407
+ if s , ok := bottoms .([]ast.Node ); ok {
408
+ pc .Set (linkBottom , append (s , b ))
409
+ return
410
+ }
411
+ pc .Set (linkBottom , []ast.Node {bottoms .(ast.Node ), b })
412
+ }
413
+
414
+ func popLinkBottom (pc Context ) ast.Node {
415
+ bottoms := pc .Get (linkBottom )
416
+ if bottoms == nil {
417
+ return nil
418
+ }
419
+ if v , ok := bottoms .(ast.Node ); ok {
420
+ pc .Set (linkBottom , nil )
421
+ return v
422
+ }
423
+ s := bottoms .([]ast.Node )
424
+ v := s [len (s )- 1 ]
425
+ n := s [0 : len (s )- 1 ]
426
+ switch len (n ) {
427
+ case 0 :
428
+ pc .Set (linkBottom , nil )
429
+ case 1 :
430
+ pc .Set (linkBottom , n [0 ])
431
+ default :
432
+ pc .Set (linkBottom , s [0 :len (s )- 1 ])
433
+ }
434
+ return v
435
+ }
436
+
398
437
func (s * linkParser ) CloseBlock (parent ast.Node , block text.Reader , pc Context ) {
399
438
pc .Set (linkBottom , nil )
400
439
tlist := pc .Get (linkLabelStateKey )
0 commit comments