Skip to content

Commit 1b0d525

Browse files
committed
Fix markdown image with link
1 parent 3ad5399 commit 1b0d525

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

modules/markup/markdown/markdown.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@ func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []
3838
link = []byte(mLink)
3939
}
4040

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

4453
// List renders markdown bullet or digit lists to HTML
@@ -104,22 +113,14 @@ func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byt
104113
prefix = util.URLJoin(prefix, "wiki", "raw")
105114
}
106115
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-
}
116+
if len(link) > 0 && !markup.IsLink(link) {
117+
lnk := string(link)
118+
lnk = util.URLJoin(prefix, lnk)
119+
lnk = strings.Replace(lnk, " ", "+", -1)
120+
link = []byte(lnk)
121121
}
122122

123+
// Put a link around it pointint to ifself by default
123124
out.WriteString(`<a href="`)
124125
out.Write(link)
125126
out.WriteString(`">`)

0 commit comments

Comments
 (0)