Skip to content

Commit 7b97e04

Browse files
guillep2ksapk
authored andcommitted
Convert EOL to UNIX-style to render MD properly (#8925)
* Convert EOL to UNIX-style to render MD properly * Update modules/markup/markdown/markdown.go Co-Authored-By: zeripath <[email protected]> * Fix lint optimization * Check for empty content before conversion * Update modules/util/util.go Co-Authored-By: zeripath <[email protected]> * Improved checks and tests * Add paragraph render test * Improve speed even more, improve tests * Small improvement by @gary-kim * Fix test for DOS * More improvements * Restart CI
1 parent cda8de2 commit 7b97e04

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

modules/markup/markdown/markdown.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte {
157157
exts |= blackfriday.HardLineBreak
158158
}
159159

160-
body = blackfriday.Run(body, blackfriday.WithRenderer(renderer), blackfriday.WithExtensions(exts))
160+
// Need to normalize EOL to UNIX LF to have consistent results in rendering
161+
body = blackfriday.Run(util.NormalizeEOL(body), blackfriday.WithRenderer(renderer), blackfriday.WithExtensions(exts))
161162
return markup.SanitizeBytes(body)
162163
}
163164

modules/markup/markdown/markdown_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,25 @@ func TestTotal_RenderString(t *testing.T) {
294294
assert.Equal(t, testCases[i+1], line)
295295
}
296296
}
297+
298+
func TestRender_RenderParagraphs(t *testing.T) {
299+
test := func(t *testing.T, str string, cnt int) {
300+
unix := []byte(str)
301+
res := string(RenderRaw(unix, "", false))
302+
assert.Equal(t, strings.Count(res, "<p"), cnt)
303+
304+
mac := []byte(strings.ReplaceAll(str, "\n", "\r"))
305+
res = string(RenderRaw(mac, "", false))
306+
assert.Equal(t, strings.Count(res, "<p"), cnt)
307+
308+
dos := []byte(strings.ReplaceAll(str, "\n", "\r\n"))
309+
res = string(RenderRaw(dos, "", false))
310+
assert.Equal(t, strings.Count(res, "<p"), cnt)
311+
}
312+
313+
test(t, "\nOne\nTwo\nThree", 1)
314+
test(t, "\n\nOne\nTwo\nThree", 1)
315+
test(t, "\n\nOne\nTwo\nThree\n\n\n", 1)
316+
test(t, "A\n\nB\nC\n", 2)
317+
test(t, "A\n\n\nB\nC\n", 2)
318+
}

modules/util/util.go

+37
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package util
66

77
import (
8+
"bytes"
89
"strings"
910
)
1011

@@ -63,3 +64,39 @@ func Min(a, b int) int {
6364
func IsEmptyString(s string) bool {
6465
return len(strings.TrimSpace(s)) == 0
6566
}
67+
68+
// NormalizeEOL will convert Windows (CRLF) and Mac (CR) EOLs to UNIX (LF)
69+
func NormalizeEOL(input []byte) []byte {
70+
var right, left, pos int
71+
if right = bytes.IndexByte(input, '\r'); right == -1 {
72+
return input
73+
}
74+
length := len(input)
75+
tmp := make([]byte, length)
76+
77+
// We know that left < length because otherwise right would be -1 from IndexByte.
78+
copy(tmp[pos:pos+right], input[left:left+right])
79+
pos += right
80+
tmp[pos] = '\n'
81+
left += right + 1
82+
pos++
83+
84+
for left < length {
85+
if input[left] == '\n' {
86+
left++
87+
}
88+
89+
right = bytes.IndexByte(input[left:], '\r')
90+
if right == -1 {
91+
copy(tmp[pos:], input[left:])
92+
pos += length - left
93+
break
94+
}
95+
copy(tmp[pos:pos+right], input[left:left+right])
96+
pos += right
97+
tmp[pos] = '\n'
98+
left += right + 1
99+
pos++
100+
}
101+
return tmp[:pos]
102+
}

modules/util/util_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package util
66

77
import (
8+
"strings"
89
"testing"
910

1011
"code.gitea.io/gitea/modules/setting"
@@ -94,3 +95,61 @@ func TestIsEmptyString(t *testing.T) {
9495
assert.Equal(t, v.expected, IsEmptyString(v.s))
9596
}
9697
}
98+
99+
func Test_NormalizeEOL(t *testing.T) {
100+
data1 := []string{
101+
"",
102+
"This text starts with empty lines",
103+
"another",
104+
"",
105+
"",
106+
"",
107+
"Some other empty lines in the middle",
108+
"more.",
109+
"And more.",
110+
"Ends with empty lines too.",
111+
"",
112+
"",
113+
"",
114+
}
115+
116+
data2 := []string{
117+
"This text does not start with empty lines",
118+
"another",
119+
"",
120+
"",
121+
"",
122+
"Some other empty lines in the middle",
123+
"more.",
124+
"And more.",
125+
"Ends without EOLtoo.",
126+
}
127+
128+
buildEOLData := func(data []string, eol string) []byte {
129+
return []byte(strings.Join(data, eol))
130+
}
131+
132+
dos := buildEOLData(data1, "\r\n")
133+
unix := buildEOLData(data1, "\n")
134+
mac := buildEOLData(data1, "\r")
135+
136+
assert.Equal(t, unix, NormalizeEOL(dos))
137+
assert.Equal(t, unix, NormalizeEOL(mac))
138+
assert.Equal(t, unix, NormalizeEOL(unix))
139+
140+
dos = buildEOLData(data2, "\r\n")
141+
unix = buildEOLData(data2, "\n")
142+
mac = buildEOLData(data2, "\r")
143+
144+
assert.Equal(t, unix, NormalizeEOL(dos))
145+
assert.Equal(t, unix, NormalizeEOL(mac))
146+
assert.Equal(t, unix, NormalizeEOL(unix))
147+
148+
assert.Equal(t, []byte("one liner"), NormalizeEOL([]byte("one liner")))
149+
assert.Equal(t, []byte("\n"), NormalizeEOL([]byte("\n")))
150+
assert.Equal(t, []byte("\ntwo liner"), NormalizeEOL([]byte("\ntwo liner")))
151+
assert.Equal(t, []byte("two liner\n"), NormalizeEOL([]byte("two liner\n")))
152+
assert.Equal(t, []byte{}, NormalizeEOL([]byte{}))
153+
154+
assert.Equal(t, []byte("mix\nand\nmatch\n."), NormalizeEOL([]byte("mix\r\nand\rmatch\n.")))
155+
}

0 commit comments

Comments
 (0)