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