Skip to content

Commit d8e06a9

Browse files
authoredOct 11, 2021
Open markdown image links in new window (#17287)
1 parent 758c8c8 commit d8e06a9

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed
 

‎modules/markup/markdown/goldmark.go

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
114114
wrap := ast.NewLink()
115115
wrap.Destination = link
116116
wrap.Title = v.Title
117+
wrap.SetAttributeString("target", []byte("_blank"))
117118

118119
// Duplicate the current image node
119120
image := ast.NewImage(ast.NewLink())

‎modules/markup/markdown/markdown_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11+
"code.gitea.io/gitea/modules/log"
1112
"code.gitea.io/gitea/modules/markup"
1213
. "code.gitea.io/gitea/modules/markup/markdown"
1314
"code.gitea.io/gitea/modules/setting"
@@ -94,10 +95,11 @@ func TestRender_Images(t *testing.T) {
9495
title := "Train"
9596
href := "https://gitea.io"
9697
result := util.URLJoin(AppSubURL, url)
98+
// hint: With Markdown v2.5.2, there is a new syntax: [link](URL){:target="_blank"} , but we do not support it now
9799

98100
test(
99101
"!["+title+"]("+url+")",
100-
`<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
102+
`<p><a href="`+result+`" target="_blank" rel="nofollow noopener"><img src="`+result+`" alt="`+title+`"/></a></p>`)
101103

102104
test(
103105
"[["+title+"|"+url+"]]",
@@ -109,7 +111,7 @@ func TestRender_Images(t *testing.T) {
109111
url = "/../../.images/src/02/train.jpg"
110112
test(
111113
"!["+title+"]("+url+")",
112-
`<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
114+
`<p><a href="`+result+`" target="_blank" rel="nofollow noopener"><img src="`+result+`" alt="`+title+`"/></a></p>`)
113115

114116
test(
115117
"[["+title+"|"+url+"]]",
@@ -373,6 +375,7 @@ func TestMarkdownRenderRaw(t *testing.T) {
373375
}
374376

375377
for _, testcase := range testcases {
378+
log.Info("Test markdown render error with fuzzy data: %x, the following errors can be recovered", testcase)
376379
_, err := RenderRawString(&markup.RenderContext{}, string(testcase))
377380
assert.NoError(t, err)
378381
}
@@ -382,8 +385,8 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) {
382385
testcase := `![image1](/image1)
383386
![image2](/image2)
384387
`
385-
expected := `<p><a href="/image1" rel="nofollow"><img src="/image1" alt="image1"></a><br>
386-
<a href="/image2" rel="nofollow"><img src="/image2" alt="image2"></a></p>
388+
expected := `<p><a href="/image1" target="_blank" rel="nofollow noopener"><img src="/image1" alt="image1"></a><br>
389+
<a href="/image2" target="_blank" rel="nofollow noopener"><img src="/image2" alt="image2"></a></p>
387390
`
388391
res, err := RenderRawString(&markup.RenderContext{}, testcase)
389392
assert.NoError(t, err)

0 commit comments

Comments
 (0)