Skip to content

Commit e58dcc0

Browse files
committed
add server-side convertion
1 parent 33d90e1 commit e58dcc0

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

modules/util/string.go

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

44
package util
55

6-
import "github.com/yuin/goldmark/util"
6+
import (
7+
"strings"
8+
"github.com/yuin/goldmark/util"
9+
)
710

811
func isSnakeCaseUpper(c byte) bool {
912
return 'A' <= c && c <= 'Z'
@@ -85,3 +88,11 @@ func ToSnakeCase(input string) string {
8588
}
8689
return util.BytesToReadOnlyString(res)
8790
}
91+
92+
func ConvertToLF(str string) string {
93+
return strings.ReplaceAll(str, "\r", "")
94+
}
95+
96+
func ConvertToCRLF(str string) string {
97+
return strings.ReplaceAll(ConvertToLF(str), "\n", "\r\n")
98+
}

modules/util/string_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ func TestToSnakeCase(t *testing.T) {
4545
assert.Equal(t, expected, ToSnakeCase(input))
4646
}
4747
}
48+
49+
func TestConvertToLF(t *testing.T) {
50+
assert.Equal(t, "\na\nbc\n\n", ConvertToLF("\r\na\r\nb\rc\n\n"))
51+
}
52+
53+
func TestConvertToCRLF(t *testing.T) {
54+
assert.Equal(t, "\r\na\r\nbc\r\n\r\n", ConvertToCRLF("\r\na\r\nb\rc\n\n"))
55+
}

routers/web/repo/editor.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
277277
operation = "create"
278278
}
279279

280+
content := form.Content
281+
if setting.UI.EditorEol == "CRLF" {
282+
content = util.ConvertToCRLF(content)
283+
} else {
284+
content = utils.ConvertToLF(content)
285+
}
286+
280287
if _, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
281288
LastCommitID: form.LastCommit,
282289
OldBranch: ctx.Repo.BranchName,
@@ -287,7 +294,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
287294
Operation: operation,
288295
FromTreePath: ctx.Repo.TreePath,
289296
TreePath: form.TreePath,
290-
ContentReader: strings.NewReader(form.Content),
297+
ContentReader: strings.NewReader(content),
291298
},
292299
},
293300
Signoff: form.Signoff,

0 commit comments

Comments
 (0)