Skip to content

Commit b686bd0

Browse files
LER0evertechknowlogick
authored andcommitted
Fix markdown image with link (#4675)
* Fix markdown image with link * Add gitea copyright notice * add a test for markdown image with link * remove svg related variables
1 parent 1037065 commit b686bd0

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

modules/markup/markdown/markdown.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -38,7 +39,16 @@ func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []
3839
link = []byte(mLink)
3940
}
4041

41-
r.Renderer.Link(out, link, title, content)
42+
if len(content) > 10 && string(content[0:9]) == "<a href=\"" && bytes.Contains(content[9:], []byte("<img")) {
43+
// Image with link case: markdown `[![]()]()`
44+
// If the content is an image, then we change the original href around it
45+
// which points to itself to a new address "link"
46+
rightQuote := bytes.Index(content[9:], []byte("\""))
47+
content = bytes.Replace(content, content[9:9+rightQuote], link, 1)
48+
out.Write(content)
49+
} else {
50+
r.Renderer.Link(out, link, title, content)
51+
}
4252
}
4353

4454
// List renders markdown bullet or digit lists to HTML
@@ -90,36 +100,21 @@ func (r *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
90100
r.Renderer.ListItem(out, text, flags)
91101
}
92102

93-
// Note: this section is for purpose of increase performance and
94-
// reduce memory allocation at runtime since they are constant literals.
95-
var (
96-
svgSuffix = []byte(".svg")
97-
svgSuffixWithMark = []byte(".svg?")
98-
)
99-
100103
// Image defines how images should be processed to produce corresponding HTML elements.
101104
func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
102105
prefix := r.URLPrefix
103106
if r.IsUncyclo {
104107
prefix = util.URLJoin(prefix, "wiki", "raw")
105108
}
106109
prefix = strings.Replace(prefix, "/src/", "/raw/", 1)
107-
if len(link) > 0 {
108-
if markup.IsLink(link) {
109-
// External link with .svg suffix usually means CI status.
110-
// TODO: define a keyword to allow non-svg images render as external link.
111-
if bytes.HasSuffix(link, svgSuffix) || bytes.Contains(link, svgSuffixWithMark) {
112-
r.Renderer.Image(out, link, title, alt)
113-
return
114-
}
115-
} else {
116-
lnk := string(link)
117-
lnk = util.URLJoin(prefix, lnk)
118-
lnk = strings.Replace(lnk, " ", "+", -1)
119-
link = []byte(lnk)
120-
}
110+
if len(link) > 0 && !markup.IsLink(link) {
111+
lnk := string(link)
112+
lnk = util.URLJoin(prefix, lnk)
113+
lnk = strings.Replace(lnk, " ", "+", -1)
114+
link = []byte(lnk)
121115
}
122116

117+
// Put a link around it pointing to itself by default
123118
out.WriteString(`<a href="`)
124119
out.Write(link)
125120
out.WriteString(`">`)

modules/markup/markdown/markdown_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestRender_Images(t *testing.T) {
7373

7474
url := "../../.images/src/02/train.jpg"
7575
title := "Train"
76+
href := "https://gitea.io"
7677
result := util.URLJoin(AppSubURL, url)
7778

7879
test(
@@ -82,6 +83,9 @@ func TestRender_Images(t *testing.T) {
8283
test(
8384
"[["+title+"|"+url+"]]",
8485
`<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" title="`+title+`" alt="`+title+`"/></a></p>`)
86+
test(
87+
"[!["+title+"]("+url+")]("+href+")",
88+
`<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
8589
}
8690

8791
func testAnswers(baseURLContent, baseURLImages string) []string {

0 commit comments

Comments
 (0)