From b748acf2a04b756ba14e4258159e7f292fc5c4c0 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Thu, 16 Dec 2021 00:49:12 +0100 Subject: [PATCH] Fixed emoji alias not parsed in links (#16221) * Do not skip links. * Restrict text in links to emojis. Co-authored-by: Lunny Xiao Co-authored-by: zeripath --- modules/markup/html.go | 17 ++++++++--------- modules/markup/markdown/markdown_test.go | 10 +++++++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index c47ecc165f..827be1a9af 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -322,7 +322,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output node = node.FirstChild } - visitNode(ctx, procs, node, true) + visitNode(ctx, procs, procs, node) newNodes := make([]*html.Node, 0, 5) @@ -354,7 +354,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output return nil } -func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText bool) { +func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node) { // Add user-content- to IDs if they don't already have them for idx, attr := range node.Attr { if attr.Key == "id" && !(strings.HasPrefix(attr.Val, "user-content-") || blackfridayExtRegex.MatchString(attr.Val)) { @@ -362,16 +362,14 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText } if attr.Key == "class" && attr.Val == "emoji" { - visitText = false + textProcs = nil } } - // We ignore code, pre and already generated links. + // We ignore code and pre. switch node.Type { case html.TextNode: - if visitText { - textNode(ctx, procs, node) - } + textNode(ctx, textProcs, node) case html.ElementNode: if node.Data == "img" { for i, attr := range node.Attr { @@ -390,7 +388,8 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText node.Attr[i] = attr } } else if node.Data == "a" { - visitText = false + // Restrict text in links to emojis + textProcs = emojiProcessors } else if node.Data == "code" || node.Data == "pre" { return } else if node.Data == "i" { @@ -416,7 +415,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText } } for n := node.FirstChild; n != nil; n = n.NextSibling { - visitNode(ctx, procs, n, visitText) + visitNode(ctx, procs, textProcs, n) } } // ignore everything else diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 936e4a39fd..083484813a 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -391,5 +391,13 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) { res, err := RenderRawString(&markup.RenderContext{}, testcase) assert.NoError(t, err) assert.Equal(t, expected, res) - +} + +func TestRenderEmojiInLinks_Issue12331(t *testing.T) { + testcase := `[Link with emoji :moon: in text](https://gitea.io)` + expected := `

Link with emoji 🌔 in text

+` + res, err := RenderString(&markup.RenderContext{}, testcase) + assert.NoError(t, err) + assert.Equal(t, expected, res) }