1
1
package views
2
2
3
3
import (
4
- "bytes"
5
4
"context"
6
5
"fmt"
7
6
"io"
@@ -36,32 +35,29 @@ func formatDurationSince(t time.Time) string {
36
35
}
37
36
38
37
// highlightContent applies syntax highlighting to the content
39
- func highlightContent (content string , contentType string ) string {
40
- // Split content type before ;
41
- contentType = strings .Split (contentType , ";" )[0 ]
38
+ func highlightContent (content string , contentType string ) templ.Component {
39
+ return templ .ComponentFunc (func (ctx context.Context , w io.Writer ) error {
40
+ // Split content type before ;
41
+ contentType = strings .Split (contentType , ";" )[0 ]
42
42
43
- lexer := lexers .MatchMimeType (contentType )
44
- if lexer == nil {
45
- fmt .Printf ("DEBUG: mime type not matched: %q\n " , contentType )
46
- lexer = lexers .Fallback
47
- } else {
48
- fmt .Printf ("DEBUG: mime type matched lexer: %q\n " , lexer .Config ().Name )
49
- }
43
+ lexer := lexers .MatchMimeType (contentType )
44
+ if lexer == nil {
45
+ fmt .Printf ("DEBUG: mime type not matched: %q\n " , contentType )
46
+ lexer = lexers .Fallback
47
+ } else {
48
+ fmt .Printf ("DEBUG: mime type matched lexer: %q\n " , lexer .Config ().Name )
49
+ }
50
50
51
- formatter , style := chromaFormatterAndStyle ()
51
+ formatter , style := chromaFormatterAndStyle ()
52
52
53
- iterator , err := lexer .Tokenise (nil , content )
54
- if err != nil {
55
- return content
56
- }
53
+ iterator , err := lexer .Tokenise (nil , content )
54
+ if err != nil {
55
+ return err
56
+ }
57
57
58
- var buf bytes.Buffer
59
- err = formatter .Format (& buf , style , iterator )
60
- if err != nil {
61
- return content
62
- }
63
-
64
- return buf .String ()
58
+ err = formatter .Format (w , style , iterator )
59
+ return err
60
+ })
65
61
}
66
62
67
63
func chromaFormatterAndStyle () (* html.Formatter , * chroma.Style ) {
@@ -84,6 +80,8 @@ func chromaStyles() templ.Component {
84
80
_ , _ = io .WriteString (w , "<style>" )
85
81
formatter , style := chromaFormatterAndStyle ()
86
82
err := formatter .WriteCSS (w , style )
83
+
84
+ _ , _ = io .WriteString (w , ".chroma { white-space: pre-wrap; }\n " )
87
85
_ , _ = io .WriteString (w , "</style>" )
88
86
return err
89
87
})
0 commit comments