diff --git a/modules/markup/file_preview.go b/modules/markup/file_preview.go
index 993df717e1..49a5f1e8ba 100644
--- a/modules/markup/file_preview.go
+++ b/modules/markup/file_preview.go
@@ -7,6 +7,7 @@ import (
"bufio"
"bytes"
"html/template"
+ "io"
"regexp"
"slices"
"strconv"
@@ -184,10 +185,12 @@ func newFilePreview(ctx *RenderContext, node *html.Node, locale translation.Loca
lineBuffer := new(bytes.Buffer)
for i := 0; i < lineCount; i++ {
buf, err := reader.ReadBytes('\n')
+ if err == nil || err == io.EOF {
+ lineBuffer.Write(buf)
+ }
if err != nil {
break
}
- lineBuffer.Write(buf)
}
// highlight the file...
diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go
index 42ce99903d..68d1ada5b3 100644
--- a/modules/markup/html_test.go
+++ b/modules/markup/html_test.go
@@ -688,10 +688,10 @@ func TestRender_FilePreview(t *testing.T) {
require.NoError(t, err)
defer gitRepo.Close()
- commit, err := gitRepo.GetCommit("HEAD")
+ commit, err := gitRepo.GetCommit(commitSha)
require.NoError(t, err)
- blob, err := commit.GetBlobByPath("path/to/file.go")
+ blob, err := commit.GetBlobByPath(filePath)
require.NoError(t, err)
return blob, nil
@@ -780,6 +780,38 @@ func TestRender_FilePreview(t *testing.T) {
},
)
})
+ t.Run("single-line", func(t *testing.T) {
+ testRender(
+ util.URLJoin(markup.TestRepoURL, "src", "commit", "4c1aaf56bcb9f39dcf65f3f250726850aed13cd6", "single-line.txt")+"#L1",
+ `
`+
+ ``+
+ ``+
+ `
`+
+ `
`+
+ ``+
+ ``+
+ ` | `+
+ `A`+` | `+
+ `
`+
+ ``+
+ `
`+
+ `
`+
+ `
`+
+ ``,
+ map[string]string{
+ "user": "gogits",
+ "repo": "gogs2",
+ },
+ )
+ })
t.Run("AppSubURL", func(t *testing.T) {
urlWithSub := util.URLJoin(markup.TestAppURL, "sub", markup.TestOrgRepo, "src", "commit", sha, "path", "to", "file.go") + "#L2-L3"
diff --git a/modules/markup/tests/repo/repo1_filepreview/objects/3f/ed9bce8610a52048747f627b3863374642c85c b/modules/markup/tests/repo/repo1_filepreview/objects/3f/ed9bce8610a52048747f627b3863374642c85c
new file mode 100644
index 0000000000..ebcf0765a5
Binary files /dev/null and b/modules/markup/tests/repo/repo1_filepreview/objects/3f/ed9bce8610a52048747f627b3863374642c85c differ
diff --git a/modules/markup/tests/repo/repo1_filepreview/objects/4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6 b/modules/markup/tests/repo/repo1_filepreview/objects/4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6
new file mode 100644
index 0000000000..b0857df8ab
Binary files /dev/null and b/modules/markup/tests/repo/repo1_filepreview/objects/4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6 differ
diff --git a/modules/markup/tests/repo/repo1_filepreview/objects/8c/7e5a667f1b771847fe88c01c3de34413a1b220 b/modules/markup/tests/repo/repo1_filepreview/objects/8c/7e5a667f1b771847fe88c01c3de34413a1b220
new file mode 100644
index 0000000000..c22450a204
Binary files /dev/null and b/modules/markup/tests/repo/repo1_filepreview/objects/8c/7e5a667f1b771847fe88c01c3de34413a1b220 differ
diff --git a/modules/markup/tests/repo/repo1_filepreview/refs/heads/master b/modules/markup/tests/repo/repo1_filepreview/refs/heads/master
index 49c348b41c..df25bf45f0 100644
--- a/modules/markup/tests/repo/repo1_filepreview/refs/heads/master
+++ b/modules/markup/tests/repo/repo1_filepreview/refs/heads/master
@@ -1 +1 @@
-190d9492934af498c3f669d6a2431dc5459e5b20
+4c1aaf56bcb9f39dcf65f3f250726850aed13cd6