@@ -95,49 +95,48 @@ func syntaxHighlight(message string) {
95
95
yellow := "\033 [33m" // Yellow color ANSI escape code
96
96
reset := "\033 [0m" // Reset ANSI escape code
97
97
98
+ processLine := func (line string ) string {
99
+ line = backtickRegex .ReplaceAllStringFunc (line , func (match string ) string {
100
+ return cyan + strings .Trim (match , "`" ) + reset
101
+ })
102
+ line = doubleQuoteRegex .ReplaceAllStringFunc (line , func (match string ) string {
103
+ return yellow + match + reset
104
+ })
105
+ return line
106
+ }
107
+
98
108
for _ , line := range lines {
99
- if strings .HasPrefix (strings .TrimSpace (line ), "```" ) {
109
+ trimmedLine := strings .TrimSpace (line )
110
+ if strings .HasPrefix (trimmedLine , "```" ) {
100
111
if inCodeBlock {
101
- // Ending a code block, apply syntax highlighting
102
112
iterator , err := currentLexer .Tokenise (nil , codeBuffer .String ())
103
113
if err == nil {
104
114
formatter .Format (os .Stdout , style , iterator )
105
115
}
106
- fmt .Println () // Ensure there's a newline after the code block
116
+ fmt .Println ()
107
117
codeBuffer .Reset ()
108
118
inCodeBlock = false
109
119
} else {
110
- // Starting a code block
111
120
inCodeBlock = true
112
- lang := strings .TrimPrefix (strings . TrimSpace ( line ) , "```" )
121
+ lang := strings .TrimPrefix (trimmedLine , "```" )
113
122
currentLexer = lexers .Get (lang )
114
123
if currentLexer == nil {
115
124
currentLexer = lexers .Fallback
116
125
}
117
- continue // Skip the line with opening backticks
118
126
}
119
127
} else if inCodeBlock {
120
- codeBuffer .WriteString (line + "\n " ) // Collect code lines
128
+ codeBuffer .WriteString (line + "\n " )
121
129
} else {
122
- // Process and set colors
123
- processedLine := line
124
- processedLine = backtickRegex .ReplaceAllStringFunc (processedLine , func (match string ) string {
125
- return cyan + strings .Trim (match , "`" ) + reset
126
- })
127
- processedLine = doubleQuoteRegex .ReplaceAllStringFunc (processedLine , func (match string ) string {
128
- return yellow + match + reset
129
- })
130
- fmt .Println (" " + processedLine ) // Print with white color
130
+ fmt .Println (" " + processLine (line ))
131
131
}
132
132
}
133
133
134
- // Flush the remaining content if still in a code block
135
134
if inCodeBlock {
136
135
iterator , err := currentLexer .Tokenise (nil , codeBuffer .String ())
137
136
if err == nil {
138
137
formatter .Format (os .Stdout , style , iterator )
139
138
}
140
- fmt .Println () // Ensure there's a newline after the code block
139
+ fmt .Println ()
141
140
}
142
141
}
143
142
0 commit comments