Skip to content

Commit 15ade8a

Browse files
committed
Fixes #457
1 parent dc32f35 commit 15ade8a

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

_test/extra.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,10 @@ text" /></p>
787787
//- - - - - - - - -//
788788
<p><img src="https://example.com/img.png" alt="`alt" /></p>
789789
//= = = = = = = = = = = = = = = = = = = = = = = =//
790+
791+
63: Emphasis in link label
792+
//- - - - - - - - -//
793+
[*[a]*](b)
794+
//- - - - - - - - -//
795+
<p><a href="b"><em>[a]</em></a></p>
796+
//= = = = = = = = = = = = = = = = = = = = = = = =//

markdown.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
package goldmark
33

44
import (
5+
"io"
6+
57
"github.com/yuin/goldmark/parser"
68
"github.com/yuin/goldmark/renderer"
79
"github.com/yuin/goldmark/renderer/html"
810
"github.com/yuin/goldmark/text"
911
"github.com/yuin/goldmark/util"
10-
"io"
1112
)
1213

1314
// DefaultParser returns a new Parser that is configured by default values.

parser/link.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
126126
if line[0] == '!' {
127127
if len(line) > 1 && line[1] == '[' {
128128
block.Advance(1)
129-
pc.Set(linkBottom, pc.LastDelimiter())
129+
pushLinkBottom(pc)
130130
return processLinkLabelOpen(block, segment.Start+1, true, pc)
131131
}
132132
return nil
133133
}
134134
if line[0] == '[' {
135-
pc.Set(linkBottom, pc.LastDelimiter())
135+
pushLinkBottom(pc)
136136
return processLinkLabelOpen(block, segment.Start, false, pc)
137137
}
138138

@@ -143,6 +143,7 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
143143
}
144144
last := tlist.(*linkLabelState).Last
145145
if last == nil {
146+
_ = popLinkBottom(pc)
146147
return nil
147148
}
148149
block.Advance(1)
@@ -151,11 +152,13 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
151152
// > A link label can have at most 999 characters inside the square brackets.
152153
if linkLabelStateLength(tlist.(*linkLabelState)) > 998 {
153154
ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment)
155+
_ = popLinkBottom(pc)
154156
return nil
155157
}
156158

157159
if !last.IsImage && s.containsLink(last) { // a link in a link text is not allowed
158160
ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment)
161+
_ = popLinkBottom(pc)
159162
return nil
160163
}
161164

@@ -169,6 +172,7 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
169172
link, hasValue = s.parseReferenceLink(parent, last, block, pc)
170173
if link == nil && hasValue {
171174
ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment)
175+
_ = popLinkBottom(pc)
172176
return nil
173177
}
174178
}
@@ -182,12 +186,14 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N
182186
// > A link label can have at most 999 characters inside the square brackets.
183187
if len(maybeReference) > 999 {
184188
ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment)
189+
_ = popLinkBottom(pc)
185190
return nil
186191
}
187192

188193
ref, ok := pc.Reference(util.ToLinkReference(maybeReference))
189194
if !ok {
190195
ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment)
196+
_ = popLinkBottom(pc)
191197
return nil
192198
}
193199
link = ast.NewLink()
@@ -230,11 +236,7 @@ func processLinkLabelOpen(block text.Reader, pos int, isImage bool, pc Context)
230236
}
231237

232238
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)
238240
ProcessDelimiters(bottom, pc)
239241
for c := last.NextSibling(); c != nil; {
240242
next := c.NextSibling()
@@ -395,6 +397,43 @@ func parseLinkTitle(block text.Reader) ([]byte, bool) {
395397
return nil, false
396398
}
397399

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+
398437
func (s *linkParser) CloseBlock(parent ast.Node, block text.Reader, pc Context) {
399438
pc.Set(linkBottom, nil)
400439
tlist := pc.Get(linkLabelStateKey)

0 commit comments

Comments
 (0)