Skip to content

Commit a0b65ed

Browse files
GiteaBotwxiaoguang
andauthored
Do not render truncated links in markdown (#32980) (#32983)
Backport #32980 by wxiaoguang Co-authored-by: wxiaoguang <[email protected]>
1 parent ad1b765 commit a0b65ed

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

modules/markup/html_link.go

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/modules/markup/common"
12+
"code.gitea.io/gitea/modules/util"
1213

1314
"golang.org/x/net/html"
1415
"golang.org/x/net/html/atom"
@@ -171,6 +172,10 @@ func linkProcessor(ctx *RenderContext, node *html.Node) {
171172
}
172173

173174
uri := node.Data[m[0]:m[1]]
175+
remaining := node.Data[m[1]:]
176+
if util.IsLikelySplitLeftPart(remaining) {
177+
return
178+
}
174179
replaceContent(node, m[0], m[1], createLink(ctx, uri, uri, "" /*link*/))
175180
node = node.NextSibling.NextSibling
176181
}

modules/markup/html_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ func TestRender_links(t *testing.T) {
206206
test(
207207
"ftps://gitea.com",
208208
`<p>ftps://gitea.com</p>`)
209+
210+
t.Run("LinkSplit", func(t *testing.T) {
211+
input, _ := util.SplitStringAtByteN("http://10.1.2.3", 12)
212+
assert.Equal(t, "http://10…", input)
213+
test(input, "<p>http://10…</p>")
214+
215+
input, _ = util.SplitStringAtByteN("http://10.1.2.3", 13)
216+
assert.Equal(t, "http://10.…", input)
217+
test(input, "<p>http://10.…</p>")
218+
})
209219
}
210220

211221
func TestRender_email(t *testing.T) {

modules/util/string.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
package util
55

6-
import "unsafe"
6+
import (
7+
"strings"
8+
"unsafe"
9+
)
710

811
func isSnakeCaseUpper(c byte) bool {
912
return 'A' <= c && c <= 'Z'
@@ -95,3 +98,15 @@ func UnsafeBytesToString(b []byte) string {
9598
func UnsafeStringToBytes(s string) []byte {
9699
return unsafe.Slice(unsafe.StringData(s), len(s))
97100
}
101+
102+
// SplitTrimSpace splits the string at given separator and trims leading and trailing space
103+
func SplitTrimSpace(input, sep string) []string {
104+
input = strings.TrimSpace(input)
105+
var stringList []string
106+
for _, s := range strings.Split(input, sep) {
107+
if s = strings.TrimSpace(s); s != "" {
108+
stringList = append(stringList, s)
109+
}
110+
}
111+
return stringList
112+
}

modules/util/string_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ func TestToSnakeCase(t *testing.T) {
4545
assert.Equal(t, expected, ToSnakeCase(input))
4646
}
4747
}
48+
49+
func TestSplitTrimSpace(t *testing.T) {
50+
assert.Equal(t, []string{"a", "b", "c"}, SplitTrimSpace("a\nb\nc", "\n"))
51+
assert.Equal(t, []string{"a", "b"}, SplitTrimSpace("\r\na\n\r\nb\n\n", "\n"))
52+
}

modules/util/truncate.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const (
1414
asciiEllipsis = "..."
1515
)
1616

17+
func IsLikelySplitLeftPart(s string) bool {
18+
return strings.HasSuffix(s, utf8Ellipsis) || strings.HasSuffix(s, asciiEllipsis)
19+
}
20+
1721
// SplitStringAtByteN splits a string at byte n accounting for rune boundaries. (Combining characters are not accounted for.)
1822
func SplitStringAtByteN(input string, n int) (left, right string) {
1923
if len(input) <= n {
@@ -38,19 +42,3 @@ func SplitStringAtByteN(input string, n int) (left, right string) {
3842

3943
return input[:end] + utf8Ellipsis, utf8Ellipsis + input[end:]
4044
}
41-
42-
// SplitTrimSpace splits the string at given separator and trims leading and trailing space
43-
func SplitTrimSpace(input, sep string) []string {
44-
// Trim initial leading & trailing space
45-
input = strings.TrimSpace(input)
46-
// replace CRLF with LF
47-
input = strings.ReplaceAll(input, "\r\n", "\n")
48-
49-
var stringList []string
50-
for _, s := range strings.Split(input, sep) {
51-
// trim leading and trailing space
52-
stringList = append(stringList, strings.TrimSpace(s))
53-
}
54-
55-
return stringList
56-
}

0 commit comments

Comments
 (0)