@@ -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,13 @@ 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
31
+ IsActive bool
26
32
}
27
33
28
34
type SearchResultLanguages = internal.SearchResultLanguages
@@ -66,12 +72,15 @@ func writeStrings(buf *bytes.Buffer, strs ...string) error {
66
72
func searchResult (result * internal.SearchResult , startIndex , endIndex int ) (* Result , error ) {
67
73
startLineNum := 1 + strings .Count (result .Content [:startIndex ], "\n " )
68
74
75
+ var formattedLinesBuffer bytes.Buffer
76
+
69
77
contentLines := strings .SplitAfter (result .Content [startIndex :endIndex ], "\n " )
70
- lines := make (map [ int ]template. HTML , len (contentLines ))
78
+ lines := make ([] ResultLine , 0 , len (contentLines ))
71
79
index := startIndex
72
80
for i , line := range contentLines {
73
- var formattedLinesBuffer bytes.Buffer
74
81
var err error
82
+ l := ResultLine {Num : startLineNum + i }
83
+
75
84
if index < result .EndIndex &&
76
85
result .StartIndex < index + len (line ) &&
77
86
result .StartIndex < result .EndIndex {
@@ -82,6 +91,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
82
91
line [openActiveIndex :closeActiveIndex ],
83
92
line [closeActiveIndex :],
84
93
)
94
+ l .IsActive = true
85
95
} else {
86
96
err = writeStrings (& formattedLinesBuffer ,
87
97
line ,
@@ -91,10 +101,21 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
91
101
return nil , err
92
102
}
93
103
94
- lines [ startLineNum + i ], _ = highlight . Code ( result . Filename , "" , formattedLinesBuffer . String () )
104
+ lines = append ( lines , l )
95
105
index += len (line )
96
106
}
97
107
108
+ hl , _ := highlight .Code (result .Filename , "" , formattedLinesBuffer .String ())
109
+ highlightedLines := strings .Split (string (hl ), "\n " )
110
+
111
+ if len (highlightedLines ) != len (lines ) {
112
+ return nil , fmt .Errorf ("the length of line numbers [%d] don't match the length of highlighted contents [%d]" , len (lines ), len (highlightedLines ))
113
+ }
114
+
115
+ for i := 0 ; i < len (lines ); i ++ {
116
+ lines [i ].FormattedContent = highlightedLines [i ]
117
+ }
118
+
98
119
return & Result {
99
120
RepoID : result .RepoID ,
100
121
Filename : result .Filename ,
0 commit comments