|
1 | 1 | // Copyright 2014 The Gogs Authors. All rights reserved.
|
| 2 | +// Copyright 2018 The Gitea Authors. All rights reserved. |
2 | 3 | // Use of this source code is governed by a MIT-style
|
3 | 4 | // license that can be found in the LICENSE file.
|
4 | 5 |
|
@@ -38,7 +39,16 @@ func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []
|
38 | 39 | link = []byte(mLink)
|
39 | 40 | }
|
40 | 41 |
|
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 | + } |
42 | 52 | }
|
43 | 53 |
|
44 | 54 | // List renders markdown bullet or digit lists to HTML
|
@@ -90,36 +100,21 @@ func (r *Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
90 | 100 | r.Renderer.ListItem(out, text, flags)
|
91 | 101 | }
|
92 | 102 |
|
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 |
| - |
100 | 103 | // Image defines how images should be processed to produce corresponding HTML elements.
|
101 | 104 | func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
|
102 | 105 | prefix := r.URLPrefix
|
103 | 106 | if r.IsUncyclo {
|
104 | 107 | prefix = util.URLJoin(prefix, "wiki", "raw")
|
105 | 108 | }
|
106 | 109 | 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) |
121 | 115 | }
|
122 | 116 |
|
| 117 | + // Put a link around it pointing to itself by default |
123 | 118 | out.WriteString(`<a href="`)
|
124 | 119 | out.Write(link)
|
125 | 120 | out.WriteString(`">`)
|
|
0 commit comments