From 9aa6ea6c9848bd8d612484da6ef85566f4668560 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Mon, 21 Jun 2021 16:49:00 +0200 Subject: [PATCH 1/2] Do not skip links. --- modules/markup/html.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index edf860da4510d..b09c6800e4fd5 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -380,8 +380,6 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText attr.Val = util.URLJoin(prefix, attr.Val) } } - } else if node.Data == "a" { - visitText = false } else if node.Data == "code" || node.Data == "pre" { return } else if node.Data == "i" { From 177bf45af663b306111ec80d8452497a9da80d6d Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Mon, 21 Jun 2021 23:49:00 +0200 Subject: [PATCH 2/2] Restrict text in links to emojis. --- modules/markup/html.go | 17 +++++++++-------- modules/markup/markdown/markdown_test.go | 8 ++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index b09c6800e4fd5..e66ae3ac06fd3 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -310,7 +310,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output } for _, node := range nodes { - visitNode(ctx, procs, node, true) + visitNode(ctx, procs, procs, node) } newNodes := make([]*html.Node, 0, len(nodes)) @@ -346,7 +346,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)) { @@ -354,16 +354,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 _, attr := range node.Attr { @@ -380,6 +378,9 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText attr.Val = util.URLJoin(prefix, attr.Val) } } + } else if node.Data == "a" { + // Restrict text in links to emojis + textProcs = emojiProcessors } else if node.Data == "code" || node.Data == "pre" { return } else if node.Data == "i" { @@ -405,7 +406,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 76c6d28d07b7d..891fe44e0c75d 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -387,5 +387,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) }