@@ -6,7 +6,7 @@ package code
6
6
import (
7
7
"bytes"
8
8
"context"
9
- "html/template "
9
+ "fmt "
10
10
"strings"
11
11
12
12
"code.gitea.io/gitea/modules/highlight"
@@ -22,7 +22,12 @@ type Result struct {
22
22
UpdatedUnix timeutil.TimeStamp
23
23
Language string
24
24
Color string
25
- Lines map [int ]template.HTML
25
+ Lines []ResultLine
26
+ }
27
+
28
+ type ResultLine struct {
29
+ Num int
30
+ FormattedContent string
26
31
}
27
32
28
33
type SearchResultLanguages = internal.SearchResultLanguages
@@ -66,11 +71,12 @@ func writeStrings(buf *bytes.Buffer, strs ...string) error {
66
71
func searchResult (result * internal.SearchResult , startIndex , endIndex int ) (* Result , error ) {
67
72
startLineNum := 1 + strings .Count (result .Content [:startIndex ], "\n " )
68
73
74
+ var formattedLinesBuffer bytes.Buffer
75
+
69
76
contentLines := strings .SplitAfter (result .Content [startIndex :endIndex ], "\n " )
70
- lines := make (map [ int ]template. HTML , len (contentLines ))
77
+ lines := make ([] ResultLine , 0 , len (contentLines ))
71
78
index := startIndex
72
79
for i , line := range contentLines {
73
- var formattedLinesBuffer bytes.Buffer
74
80
var err error
75
81
if index < result .EndIndex &&
76
82
result .StartIndex < index + len (line ) &&
@@ -91,10 +97,21 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
91
97
return nil , err
92
98
}
93
99
94
- lines [ startLineNum + i ], _ = highlight . Code ( result . Filename , "" , formattedLinesBuffer . String () )
100
+ lines = append ( lines , ResultLine { Num : startLineNum + i } )
95
101
index += len (line )
96
102
}
97
103
104
+ hl , _ := highlight .Code (result .Filename , "" , formattedLinesBuffer .String ())
105
+ highlightedLines := strings .Split (string (hl ), "\n " )
106
+
107
+ if len (highlightedLines ) != len (lines ) {
108
+ return nil , fmt .Errorf ("the length of line numbers [%d] don't match the length of highlighted contents [%d]" , len (lines ), len (highlightedLines ))
109
+ }
110
+
111
+ for i := 0 ; i < len (lines ); i ++ {
112
+ lines [i ].FormattedContent = highlightedLines [i ]
113
+ }
114
+
98
115
return & Result {
99
116
RepoID : result .RepoID ,
100
117
Filename : result .Filename ,
0 commit comments