From d8e06a90f06116134e9b2b661ad404210cc052cd Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 11 Oct 2021 20:12:06 +0800 Subject: [PATCH] Open markdown image links in new window (#17287) --- modules/markup/markdown/goldmark.go | 1 + modules/markup/markdown/markdown_test.go | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/markup/markdown/goldmark.go b/modules/markup/markdown/goldmark.go index f1c259f8242..43023220a56 100644 --- a/modules/markup/markdown/goldmark.go +++ b/modules/markup/markdown/goldmark.go @@ -114,6 +114,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa wrap := ast.NewLink() wrap.Destination = link wrap.Title = v.Title + wrap.SetAttributeString("target", []byte("_blank")) // Duplicate the current image node image := ast.NewImage(ast.NewLink()) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index f22c3895310..936e4a39fdc 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" . "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" @@ -94,10 +95,11 @@ func TestRender_Images(t *testing.T) { title := "Train" href := "https://gitea.io" result := util.URLJoin(AppSubURL, url) + // hint: With Markdown v2.5.2, there is a new syntax: [link](URL){:target="_blank"} , but we do not support it now test( "!["+title+"]("+url+")", - `

`+title+`

`) + `

`+title+`

`) test( "[["+title+"|"+url+"]]", @@ -109,7 +111,7 @@ func TestRender_Images(t *testing.T) { url = "/../../.images/src/02/train.jpg" test( "!["+title+"]("+url+")", - `

`+title+`

`) + `

`+title+`

`) test( "[["+title+"|"+url+"]]", @@ -373,6 +375,7 @@ func TestMarkdownRenderRaw(t *testing.T) { } for _, testcase := range testcases { + log.Info("Test markdown render error with fuzzy data: %x, the following errors can be recovered", testcase) _, err := RenderRawString(&markup.RenderContext{}, string(testcase)) assert.NoError(t, err) } @@ -382,8 +385,8 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) { testcase := `![image1](/image1) ![image2](/image2) ` - expected := `

image1
-image2

+ expected := `

image1
+image2

` res, err := RenderRawString(&markup.RenderContext{}, testcase) assert.NoError(t, err)