Skip to content

Commit b748acf

Browse files
KN4CK3Rlunnyzeripath
authored
Fixed emoji alias not parsed in links (#16221)
* Do not skip links. * Restrict text in links to emojis. Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent f58e687 commit b748acf

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

modules/markup/html.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
322322
node = node.FirstChild
323323
}
324324

325-
visitNode(ctx, procs, node, true)
325+
visitNode(ctx, procs, procs, node)
326326

327327
newNodes := make([]*html.Node, 0, 5)
328328

@@ -354,24 +354,22 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
354354
return nil
355355
}
356356

357-
func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText bool) {
357+
func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node) {
358358
// Add user-content- to IDs if they don't already have them
359359
for idx, attr := range node.Attr {
360360
if attr.Key == "id" && !(strings.HasPrefix(attr.Val, "user-content-") || blackfridayExtRegex.MatchString(attr.Val)) {
361361
node.Attr[idx].Val = "user-content-" + attr.Val
362362
}
363363

364364
if attr.Key == "class" && attr.Val == "emoji" {
365-
visitText = false
365+
textProcs = nil
366366
}
367367
}
368368

369-
// We ignore code, pre and already generated links.
369+
// We ignore code and pre.
370370
switch node.Type {
371371
case html.TextNode:
372-
if visitText {
373-
textNode(ctx, procs, node)
374-
}
372+
textNode(ctx, textProcs, node)
375373
case html.ElementNode:
376374
if node.Data == "img" {
377375
for i, attr := range node.Attr {
@@ -390,7 +388,8 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText
390388
node.Attr[i] = attr
391389
}
392390
} else if node.Data == "a" {
393-
visitText = false
391+
// Restrict text in links to emojis
392+
textProcs = emojiProcessors
394393
} else if node.Data == "code" || node.Data == "pre" {
395394
return
396395
} else if node.Data == "i" {
@@ -416,7 +415,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText
416415
}
417416
}
418417
for n := node.FirstChild; n != nil; n = n.NextSibling {
419-
visitNode(ctx, procs, n, visitText)
418+
visitNode(ctx, procs, textProcs, n)
420419
}
421420
}
422421
// ignore everything else

modules/markup/markdown/markdown_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,5 +391,13 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) {
391391
res, err := RenderRawString(&markup.RenderContext{}, testcase)
392392
assert.NoError(t, err)
393393
assert.Equal(t, expected, res)
394+
}
394395

396+
func TestRenderEmojiInLinks_Issue12331(t *testing.T) {
397+
testcase := `[Link with emoji :moon: in text](https://gitea.io)`
398+
expected := `<p><a href="https://gitea.io" rel="nofollow">Link with emoji <span class="emoji" aria-label="waxing gibbous moon">🌔</span> in text</a></p>
399+
`
400+
res, err := RenderString(&markup.RenderContext{}, testcase)
401+
assert.NoError(t, err)
402+
assert.Equal(t, expected, res)
395403
}

0 commit comments

Comments
 (0)