@@ -26,7 +26,7 @@ import (
26
26
)
27
27
28
28
// don't index files larger than this many bytes for performance purposes
29
- const sizeLimit = 1000000
29
+ const sizeLimit = 1024 * 1024
30
30
31
31
var (
32
32
// For custom user mapping
@@ -60,7 +60,7 @@ func NewContext() {
60
60
func Code (fileName , language , code string ) string {
61
61
NewContext ()
62
62
63
- // diff view newline will be passed as empty, change to literal \n so it can be copied
63
+ // diff view newline will be passed as empty, change to literal '\n' so it can be copied
64
64
// preserve literal newline in blame view
65
65
if code == "" || code == "\n " {
66
66
return "\n "
@@ -106,6 +106,11 @@ func Code(fileName, language, code string) string {
106
106
return CodeFromLexer (lexer , code )
107
107
}
108
108
109
+ type nopPreWrapper struct {}
110
+
111
+ func (nopPreWrapper ) Start (code bool , styleAttr string ) string { return "" }
112
+ func (nopPreWrapper ) End (code bool ) string { return "" }
113
+
109
114
// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
110
115
func CodeFromLexer (lexer chroma.Lexer , code string ) string {
111
116
formatter := html .New (html .WithClasses (true ),
@@ -128,9 +133,9 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string {
128
133
return code
129
134
}
130
135
131
- htmlw .Flush ()
136
+ _ = htmlw .Flush ()
132
137
// Chroma will add newlines for certain lexers in order to highlight them properly
133
- // Once highlighted, strip them here so they don't cause copy/paste trouble in HTML output
138
+ // Once highlighted, strip them here, so they don't cause copy/paste trouble in HTML output
134
139
return strings .TrimSuffix (htmlbuf .String (), "\n " )
135
140
}
136
141
@@ -143,7 +148,7 @@ func File(numLines int, fileName, language string, code []byte) []string {
143
148
}
144
149
formatter := html .New (html .WithClasses (true ),
145
150
html .WithLineNumbers (false ),
146
- html .PreventSurroundingPre ( true ),
151
+ html .WithPreWrapper ( nopPreWrapper {} ),
147
152
)
148
153
149
154
if formatter == nil {
@@ -191,27 +196,19 @@ func File(numLines int, fileName, language string, code []byte) []string {
191
196
return plainText (string (code ), numLines )
192
197
}
193
198
194
- htmlw .Flush ()
199
+ _ = htmlw .Flush ()
195
200
finalNewLine := false
196
201
if len (code ) > 0 {
197
202
finalNewLine = code [len (code )- 1 ] == '\n'
198
203
}
199
204
200
- m := make ([]string , 0 , numLines )
201
- for _ , v := range strings .SplitN (htmlbuf .String (), "\n " , numLines ) {
202
- content := v
203
- // need to keep lines that are only \n so copy/paste works properly in browser
204
- if content == "" {
205
- content = "\n "
206
- } else if content == `</span><span class="w">` {
207
- content += "\n </span>"
208
- } else if content == `</span></span><span class="line"><span class="cl">` {
209
- content += "\n "
210
- }
211
- content = strings .TrimSuffix (content , `<span class="w">` )
212
- content = strings .TrimPrefix (content , `</span>` )
213
- m = append (m , content )
205
+ m := strings .SplitN (htmlbuf .String (), `</span></span><span class="line"><span class="cl">` , numLines )
206
+ if len (m ) > 0 {
207
+ m [0 ] = m [0 ][len (`<span class="line"><span class="cl">` ):]
208
+ last := m [len (m )- 1 ]
209
+ m [len (m )- 1 ] = last [:len (last )- len (`</span></span>` )]
214
210
}
211
+
215
212
if finalNewLine {
216
213
m = append (m , "<span class=\" w\" >\n </span>" )
217
214
}
@@ -221,14 +218,14 @@ func File(numLines int, fileName, language string, code []byte) []string {
221
218
222
219
// return unhiglighted map
223
220
func plainText (code string , numLines int ) []string {
224
- m := make ([] string , 0 , numLines )
225
- for _ , v := range strings . SplitN ( code , " \n " , numLines ) {
226
- content := v
221
+ m := strings . SplitN ( code , " \n " , numLines )
222
+
223
+ for i , content := range m {
227
224
// need to keep lines that are only \n so copy/paste works properly in browser
228
225
if content == "" {
229
226
content = "\n "
230
227
}
231
- m = append ( m , gohtml .EscapeString (content ) )
228
+ m [ i ] = gohtml .EscapeString (content )
232
229
}
233
230
return m
234
231
}
0 commit comments