@@ -5,6 +5,7 @@ package markup_test
5
5
6
6
import (
7
7
"context"
8
+ "html/template"
8
9
"io"
9
10
"os"
10
11
"strings"
@@ -13,10 +14,12 @@ import (
13
14
"code.gitea.io/gitea/models/unittest"
14
15
"code.gitea.io/gitea/modules/emoji"
15
16
"code.gitea.io/gitea/modules/git"
17
+ "code.gitea.io/gitea/modules/highlight"
16
18
"code.gitea.io/gitea/modules/log"
17
19
"code.gitea.io/gitea/modules/markup"
18
20
"code.gitea.io/gitea/modules/markup/markdown"
19
21
"code.gitea.io/gitea/modules/setting"
22
+ "code.gitea.io/gitea/modules/translation"
20
23
"code.gitea.io/gitea/modules/util"
21
24
22
25
"github.com/stretchr/testify/assert"
@@ -688,3 +691,57 @@ func TestIsFullURL(t *testing.T) {
688
691
assert .
True (
t ,
markup .
IsFullURLString (
"mailto:[email protected] " ))
689
692
assert .False (t , markup .IsFullURLString ("/foo:bar" ))
690
693
}
694
+
695
+ func TestRender_FilePreview (t * testing.T ) {
696
+ setting .AppURL = markup .TestAppURL
697
+ markup .Init (& markup.ProcessorHelper {
698
+ GetRepoFileContent : func (ctx context.Context , ownerName string , repoName string , commitSha string , filePath string ) ([]template.HTML , error ) {
699
+ buf := []byte ( "A\n B\n C\n D\n " )
700
+ return highlight .PlainText (buf ), nil
701
+ },
702
+ GetLocale : func (ctx context.Context ) (translation.Locale , error ) {
703
+ return translation .NewLocale ("en-US" ), nil
704
+ },
705
+ })
706
+
707
+ sha := "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
708
+ commitFilePreview := util .URLJoin (markup .TestRepoURL , "src" , "commit" , sha , "path" , "to" , "file.go" ) + "#L1-L2"
709
+
710
+ test := func (input , expected string ) {
711
+ buffer , err := markup .RenderString (& markup.RenderContext {
712
+ Ctx : git .DefaultContext ,
713
+ RelativePath : ".md" ,
714
+ Metas : localMetas ,
715
+ }, input )
716
+ assert .NoError (t , err )
717
+ assert .Equal (t , strings .TrimSpace (expected ), strings .TrimSpace (buffer ))
718
+ }
719
+
720
+ test (
721
+ commitFilePreview ,
722
+ `<p></p>` +
723
+ `<div class="file-preview-box">` +
724
+ `<div class="header">` +
725
+ `<a href="http://localhost:3000/gogits/gogs/src/commit/b6dd6210eaebc915fd5be5579c58cce4da2e2579/path/to/file.go#L1-L2" class="muted" rel="nofollow">path/to/file.go</a>` +
726
+ `<span class="text small grey">` +
727
+ `Lines 1 to 2 in <a href="http://localhost:3000/gogits/gogs/src/commit/b6dd6210eaebc915fd5be5579c58cce4da2e2579" class="text black" rel="nofollow">b6dd621</a>` +
728
+ `</span>` +
729
+ `</div>` +
730
+ `<div class="ui table">` +
731
+ `<table class="file-preview">` +
732
+ `<tbody>` +
733
+ `<tr>` +
734
+ `<td id="user-content-L1" class="lines-num"><span id="user-content-L1" data-line-number="1"></span></td>` +
735
+ `<td rel="L1" class="lines-code chroma"><code class="code-inner">A` + "\n " + `</code></td>` +
736
+ `</tr>` +
737
+ `<tr>` +
738
+ `<td id="user-content-L2" class="lines-num"><span id="user-content-L2" data-line-number="2"></span></td>` +
739
+ `<td rel="L2" class="lines-code chroma"><code class="code-inner">B` + "\n " + `</code></td>` +
740
+ `</tr>` +
741
+ `</tbody>` +
742
+ `</table>` +
743
+ `</div>` +
744
+ `</div>` +
745
+ `<p></p>` ,
746
+ )
747
+ }
0 commit comments