@@ -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
@@ -60,7 +60,7 @@ func NewContext() {
6060func Code (fileName , language , code string ) string {
6161 NewContext ()
6262
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
6464 // preserve literal newline in blame view
6565 if code == "" || code == "\n " {
6666 return "\n "
@@ -106,6 +106,11 @@ func Code(fileName, language, code string) string {
106106 return CodeFromLexer (lexer , code )
107107}
108108
109+ type nopPreWrapper struct {}
110+
111+ func (nopPreWrapper ) Start (code bool , styleAttr string ) string { return "" }
112+ func (nopPreWrapper ) End (code bool ) string { return "" }
113+
109114// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
110115func CodeFromLexer (lexer chroma.Lexer , code string ) string {
111116 formatter := html .New (html .WithClasses (true ),
@@ -128,9 +133,9 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string {
128133 return code
129134 }
130135
131- htmlw .Flush ()
136+ _ = htmlw .Flush ()
132137 // 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
134139 return strings .TrimSuffix (htmlbuf .String (), "\n " )
135140}
136141
@@ -143,7 +148,7 @@ func File(numLines int, fileName, language string, code []byte) []string {
143148 }
144149 formatter := html .New (html .WithClasses (true ),
145150 html .WithLineNumbers (false ),
146- html .PreventSurroundingPre ( true ),
151+ html .WithPreWrapper ( nopPreWrapper {} ),
147152 )
148153
149154 if formatter == nil {
@@ -191,27 +196,19 @@ func File(numLines int, fileName, language string, code []byte) []string {
191196 return plainText (string (code ), numLines )
192197 }
193198
194- htmlw .Flush ()
199+ _ = htmlw .Flush ()
195200 finalNewLine := false
196201 if len (code ) > 0 {
197202 finalNewLine = code [len (code )- 1 ] == '\n'
198203 }
199204
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>` )]
214210 }
211+
215212 if finalNewLine {
216213 m = append (m , "<span class=\" w\" >\n </span>" )
217214 }
@@ -221,14 +218,14 @@ func File(numLines int, fileName, language string, code []byte) []string {
221218
222219// return unhiglighted map
223220func 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 {
227224 // need to keep lines that are only \n so copy/paste works properly in browser
228225 if content == "" {
229226 content = "\n "
230227 }
231- m = append ( m , gohtml .EscapeString (content ) )
228+ m [ i ] = gohtml .EscapeString (content )
232229 }
233230 return m
234231}
0 commit comments