@@ -26,7 +26,7 @@ import (
2626)
2727
2828// don't index files larger than this many bytes for performance purposes
29- const sizeLimit = 1000000
29+ const sizeLimit = 1024 * 1024
3030
3131var (
3232 // For custom user mapping
@@ -58,7 +58,7 @@ func NewContext() {
5858func Code (fileName , language , code string ) string {
5959 NewContext ()
6060
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
6262 // preserve literal newline in blame view
6363 if code == "" || code == "\n " {
6464 return "\n "
@@ -104,6 +104,11 @@ func Code(fileName, language, code string) string {
104104 return CodeFromLexer (lexer , code )
105105}
106106
107+ type nopPreWrapper struct {}
108+
109+ func (nopPreWrapper ) Start (code bool , styleAttr string ) string { return "" }
110+ func (nopPreWrapper ) End (code bool ) string { return "" }
111+
107112// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
108113func CodeFromLexer (lexer chroma.Lexer , code string ) string {
109114 formatter := html .New (html .WithClasses (true ),
@@ -126,9 +131,9 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string {
126131 return code
127132 }
128133
129- htmlw .Flush ()
134+ _ = htmlw .Flush ()
130135 // 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
132137 return strings .TrimSuffix (htmlbuf .String (), "\n " )
133138}
134139
@@ -141,7 +146,7 @@ func File(numLines int, fileName, language string, code []byte) []string {
141146 }
142147 formatter := html .New (html .WithClasses (true ),
143148 html .WithLineNumbers (false ),
144- html .PreventSurroundingPre ( true ),
149+ html .WithPreWrapper ( nopPreWrapper {} ),
145150 )
146151
147152 if formatter == nil {
@@ -189,27 +194,19 @@ func File(numLines int, fileName, language string, code []byte) []string {
189194 return plainText (string (code ), numLines )
190195 }
191196
192- htmlw .Flush ()
197+ _ = htmlw .Flush ()
193198 finalNewLine := false
194199 if len (code ) > 0 {
195200 finalNewLine = code [len (code )- 1 ] == '\n'
196201 }
197202
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>` )]
212208 }
209+
213210 if finalNewLine {
214211 m = append (m , "<span class=\" w\" >\n </span>" )
215212 }
@@ -219,14 +216,14 @@ func File(numLines int, fileName, language string, code []byte) []string {
219216
220217// return unhiglighted map
221218func 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 {
225222 // need to keep lines that are only \n so copy/paste works properly in browser
226223 if content == "" {
227224 content = "\n "
228225 }
229- m = append ( m , gohtml .EscapeString (content ) )
226+ m [ i ] = gohtml .EscapeString (content )
230227 }
231228 return m
232229}
0 commit comments