-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.go
More file actions
446 lines (415 loc) · 13.4 KB
/
Copy pathhighlight.go
File metadata and controls
446 lines (415 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package main
import (
"bytes"
"fmt"
"path/filepath"
"regexp"
"strings"
mermaidCmd "github.com/AlexanderGrooff/mermaid-ascii/cmd"
"github.com/AlexanderGrooff/mermaid-ascii/pkg/diagram"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/glamour/ansi"
"github.com/charmbracelet/lipgloss"
xansi "github.com/charmbracelet/x/ansi"
)
var (
style = styles.Get("monokai")
formatter = formatters.Get("terminal256")
mermaidBlockRe = regexp.MustCompile("(?s)```mermaid\\n(.*?)```")
)
func highlight(content, filename string) string {
lexer := lexers.Match(filename)
if lexer == nil {
lexer = lexers.Analyse(content)
}
if lexer == nil {
lexer = lexers.Fallback
}
lexer = chroma.Coalesce(lexer)
iterator, err := lexer.Tokenise(nil, content)
if err != nil {
return content
}
var buf bytes.Buffer
if err := formatter.Format(&buf, style, iterator); err != nil {
return content
}
return buf.String()
}
func highlightDiff(diff, filename string) string {
lines := strings.Split(diff, "\n")
// Extract just the code lines (strip diff prefix) for syntax highlighting
var codeLines []string
var lineTypes []byte // '+', '-', '@', 'h' (header), ' ' (context)
for _, line := range lines {
switch {
case strings.HasPrefix(line, "+++"), strings.HasPrefix(line, "---"):
lineTypes = append(lineTypes, 'h')
codeLines = append(codeLines, line)
case strings.HasPrefix(line, "@@"):
lineTypes = append(lineTypes, '@')
codeLines = append(codeLines, line)
case strings.HasPrefix(line, "+"):
lineTypes = append(lineTypes, '+')
codeLines = append(codeLines, line[1:])
case strings.HasPrefix(line, "-"):
lineTypes = append(lineTypes, '-')
codeLines = append(codeLines, line[1:])
default:
if len(line) > 0 && line[0] == ' ' {
lineTypes = append(lineTypes, ' ')
codeLines = append(codeLines, line[1:])
} else {
lineTypes = append(lineTypes, 'h')
codeLines = append(codeLines, line)
}
}
}
// Syntax highlight all code lines as one block
codeBlock := strings.Join(codeLines, "\n")
highlighted := highlight(codeBlock, filename)
hlLines := strings.Split(highlighted, "\n")
// Pad if needed
for len(hlLines) < len(lineTypes) {
hlLines = append(hlLines, "")
}
// Reassemble with colored prefixes and background tints
for i, lt := range lineTypes {
switch lt {
case '+':
hlLines[i] = diffAddedPrefixStyle.Render("+") + injectBg(hlLines[i], diffAddedBgColor)
case '-':
hlLines[i] = diffDeletedPrefixStyle.Render("-") + injectBg(hlLines[i], diffDeletedBgColor)
case '@':
hlLines[i] = diffHunkStyle.Render(lines[i])
case 'h':
hlLines[i] = diffHeaderStyle.Render(lines[i])
default:
hlLines[i] = " " + hlLines[i]
}
}
return strings.Join(hlLines, "\n")
}
// injectBg applies a background color that persists through ANSI resets.
// It prepends the bg escape, re-injects it after every reset, and appends a final reset.
func injectBg(s, bgEsc string) string {
s = strings.ReplaceAll(s, "\033[0m", "\033[0m"+bgEsc)
return bgEsc + s + "\033[0m"
}
func boolPtr(b bool) *bool { return &b }
func stringPtr(s string) *string { return &s }
func uintPtr(u uint) *uint { return &u }
// Tokyo Night–inspired Glamour style to match the app theme.
var mdStyle = ansi.StyleConfig{
Document: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockPrefix: "\n",
BlockSuffix: "\n",
Color: stringPtr("#a9b1d6"),
},
Margin: uintPtr(2),
},
Heading: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockSuffix: "\n",
Bold: boolPtr(true),
},
},
H1: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "H1OPEN",
Suffix: "H1CLOSE",
Color: stringPtr("#ffffff"),
Bold: boolPtr(true),
Upper: boolPtr(true),
},
},
H2: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "H2OPEN",
Suffix: "H2CLOSE",
Color: stringPtr("#c0caf5"),
Bold: boolPtr(true),
},
},
H3: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "H3OPEN",
Suffix: "H3CLOSE",
Color: stringPtr("#a9b1d6"),
Bold: boolPtr(true),
},
},
H4: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "H4OPEN",
Suffix: "H4CLOSE",
Color: stringPtr("#a9b1d6"),
Bold: boolPtr(true),
},
},
H5: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "H5OPEN",
Suffix: "H5CLOSE",
Color: stringPtr("#7aa2f7"),
Bold: boolPtr(true),
},
},
H6: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "H6OPEN",
Suffix: "H6CLOSE",
Color: stringPtr("#565f89"),
Bold: boolPtr(true),
},
},
BlockQuote: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Color: stringPtr("#565f89"),
Italic: boolPtr(true),
},
Indent: uintPtr(1),
IndentToken: stringPtr("│ "),
},
Paragraph: ansi.StyleBlock{},
List: ansi.StyleList{
LevelIndent: 2,
StyleBlock: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Color: stringPtr("#a9b1d6"),
},
},
},
Item: ansi.StylePrimitive{
BlockPrefix: "• ",
Color: stringPtr("#a9b1d6"),
},
Enumeration: ansi.StylePrimitive{
BlockPrefix: ". ",
Color: stringPtr("#7aa2f7"),
},
Task: ansi.StyleTask{
Ticked: "[✓] ",
Unticked: "[ ] ",
StylePrimitive: ansi.StylePrimitive{
Color: stringPtr("#9ece6a"),
},
},
Strong: ansi.StylePrimitive{
Bold: boolPtr(true),
Color: stringPtr("#c0caf5"),
},
Emph: ansi.StylePrimitive{
Italic: boolPtr(true),
Color: stringPtr("#bb9af7"),
},
Strikethrough: ansi.StylePrimitive{
CrossedOut: boolPtr(true),
Color: stringPtr("#565f89"),
},
HorizontalRule: ansi.StylePrimitive{
Color: stringPtr("#3b4261"),
Format: "\nHRPLACEHOLDER\n",
},
Link: ansi.StylePrimitive{
Color: stringPtr("#7dcfff"),
Underline: boolPtr(true),
},
LinkText: ansi.StylePrimitive{
Color: stringPtr("#7aa2f7"),
Bold: boolPtr(true),
},
Image: ansi.StylePrimitive{
Color: stringPtr("#bb9af7"),
Underline: boolPtr(true),
},
ImageText: ansi.StylePrimitive{
Color: stringPtr("#bb9af7"),
Format: "🖼 {{.text}}",
},
Code: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Color: stringPtr("#9ece6a"),
BackgroundColor: stringPtr("#24283b"),
Prefix: " ",
Suffix: " ",
},
},
CodeBlock: ansi.StyleCodeBlock{
StyleBlock: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Color: stringPtr("#a9b1d6"),
},
Margin: uintPtr(2),
},
Chroma: &ansi.Chroma{
Text: ansi.StylePrimitive{Color: stringPtr("#a9b1d6")},
Error: ansi.StylePrimitive{Color: stringPtr("#f7768e")},
Comment: ansi.StylePrimitive{Color: stringPtr("#565f89"), Italic: boolPtr(true)},
CommentPreproc: ansi.StylePrimitive{Color: stringPtr("#565f89")},
Keyword: ansi.StylePrimitive{Color: stringPtr("#bb9af7")},
KeywordReserved: ansi.StylePrimitive{Color: stringPtr("#bb9af7")},
KeywordNamespace: ansi.StylePrimitive{Color: stringPtr("#7dcfff")},
KeywordType: ansi.StylePrimitive{Color: stringPtr("#2ac3de")},
Operator: ansi.StylePrimitive{Color: stringPtr("#89ddff")},
Punctuation: ansi.StylePrimitive{Color: stringPtr("#a9b1d6")},
Name: ansi.StylePrimitive{Color: stringPtr("#c0caf5")},
NameBuiltin: ansi.StylePrimitive{Color: stringPtr("#7aa2f7")},
NameTag: ansi.StylePrimitive{Color: stringPtr("#f7768e")},
NameAttribute: ansi.StylePrimitive{Color: stringPtr("#bb9af7")},
NameClass: ansi.StylePrimitive{Color: stringPtr("#2ac3de")},
NameConstant: ansi.StylePrimitive{Color: stringPtr("#ff9e64")},
NameDecorator: ansi.StylePrimitive{Color: stringPtr("#ff9e64")},
NameFunction: ansi.StylePrimitive{Color: stringPtr("#7aa2f7")},
LiteralNumber: ansi.StylePrimitive{Color: stringPtr("#ff9e64")},
LiteralString: ansi.StylePrimitive{Color: stringPtr("#9ece6a")},
LiteralStringEscape: ansi.StylePrimitive{Color: stringPtr("#89ddff")},
GenericDeleted: ansi.StylePrimitive{Color: stringPtr("#f7768e")},
GenericInserted: ansi.StylePrimitive{Color: stringPtr("#9ece6a")},
GenericEmph: ansi.StylePrimitive{Italic: boolPtr(true)},
GenericStrong: ansi.StylePrimitive{Bold: boolPtr(true)},
GenericSubheading: ansi.StylePrimitive{Color: stringPtr("#7dcfff")},
Background: ansi.StylePrimitive{BackgroundColor: stringPtr("#1a1b26")},
},
},
Table: ansi.StyleTable{
StyleBlock: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Color: stringPtr("#a9b1d6"),
},
},
CenterSeparator: stringPtr("┼"),
ColumnSeparator: stringPtr("│"),
RowSeparator: stringPtr("─"),
},
DefinitionTerm: ansi.StylePrimitive{
Color: stringPtr("#7aa2f7"),
Bold: boolPtr(true),
},
DefinitionDescription: ansi.StylePrimitive{
Color: stringPtr("#a9b1d6"),
BlockPrefix: " ",
},
}
var (
hrLineStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#3b4261"))
// Heading background styles — gradient from bright to dim.
headingStyles = map[string]lipgloss.Style{
"H1": lipgloss.NewStyle().Background(lipgloss.Color("#7aa2f7")).Foreground(lipgloss.Color("#ffffff")).Bold(true),
"H2": lipgloss.NewStyle().Background(lipgloss.Color("#3d59a1")).Foreground(lipgloss.Color("#c0caf5")).Bold(true),
"H3": lipgloss.NewStyle().Background(lipgloss.Color("#2e3a5e")).Foreground(lipgloss.Color("#a9b1d6")).Bold(true),
"H4": lipgloss.NewStyle().Background(lipgloss.Color("#283350")).Foreground(lipgloss.Color("#a9b1d6")).Bold(true),
"H5": lipgloss.NewStyle().Background(lipgloss.Color("#24283b")).Foreground(lipgloss.Color("#7aa2f7")).Bold(true),
"H6": lipgloss.NewStyle().Background(lipgloss.Color("#1f2335")).Foreground(lipgloss.Color("#565f89")).Bold(true),
}
)
func renderMarkdown(content string, width int) string {
r, err := glamour.NewTermRenderer(
glamour.WithStyles(mdStyle),
glamour.WithWordWrap(width),
)
if err != nil {
return content
}
out, err := r.Render(content)
if err != nil {
return content
}
return strings.TrimRight(out, "\n")
}
// addTableRowSeparators is a no-op placeholder. Glamour renders tables
// with a header separator (┼/─) and tightly packed data rows by default.
func addTableRowSeparators(out string) string {
return out
}
// postProcessMarkdown applies full-width headings and HRs.
// Called after mermaid splicing so the width scan sees all content.
func postProcessMarkdown(out string, minWidth int) string {
fullWidth := minWidth
for _, line := range strings.Split(out, "\n") {
if w := lipgloss.Width(line); w > fullWidth {
fullWidth = w
}
}
// Replace HR placeholders with full-width rules.
hr := hrLineStyle.Render(strings.Repeat("─", fullWidth))
out = strings.ReplaceAll(out, "HRPLACEHOLDER", hr)
// Replace heading placeholders with full-width background bars.
lines := strings.Split(out, "\n")
for i, line := range lines {
for level := 1; level <= 6; level++ {
tag := fmt.Sprintf("H%d", level)
open := tag + "OPEN"
close := tag + "CLOSE"
if strings.Contains(line, open) && strings.Contains(line, close) {
plain := xansi.Strip(line)
plain = strings.Replace(plain, open, "", 1)
plain = strings.Replace(plain, close, "", 1)
plain = strings.TrimSpace(plain)
pad := fullWidth - len(plain) - 2
if pad < 0 {
pad = 0
}
text := " " + plain + strings.Repeat(" ", pad+1)
lines[i] = headingStyles[tag].Render(text)
break
}
}
}
return strings.TrimRight(strings.Join(lines, "\n"), "\n")
}
func highlightContent(content, filename string) string {
ext := filepath.Ext(filename)
if ext == "" || strings.HasPrefix(filepath.Base(filename), ".") {
return highlight(content, filename)
}
return highlight(content, filename)
}
func renderMermaid(content string) (string, error) {
return mermaidCmd.RenderDiagram(content, diagram.DefaultConfig())
}
func renderMarkdownWithMermaid(content string, width int) string {
// Render mermaid blocks and stash them behind placeholders so Glamour
// doesn't word-wrap the ASCII art.
rendered := map[string]string{}
idx := 0
stripped := mermaidBlockRe.ReplaceAllStringFunc(content, func(block string) string {
matches := mermaidBlockRe.FindStringSubmatch(block)
if len(matches) < 2 {
return block
}
ascii, err := renderMermaid(matches[1])
if err != nil {
return block + "\n\n> Mermaid render error: " + err.Error()
}
key := fmt.Sprintf("MERMAIDPLACEHOLDER%d", idx)
idx++
rendered[key] = ascii
return key
})
// Render the markdown (mermaid blocks are now just placeholder text).
out := renderMarkdown(stripped, width)
// Add table row separators BEFORE mermaid splicing — at this point
// only Glamour table │ characters exist, no mermaid box-drawing.
out = addTableRowSeparators(out)
// Splice the raw ASCII diagrams back in.
// Glamour wraps the placeholder in paragraph styling (leading spaces, etc.)
// so we find the full line containing the placeholder and replace the whole line.
for key, ascii := range rendered {
lines := strings.Split(out, "\n")
for i, line := range lines {
if strings.Contains(line, key) {
lines[i] = ascii
break
}
}
out = strings.Join(lines, "\n")
}
// Post-process headings/HR after mermaid splicing so width calc sees everything.
return postProcessMarkdown(out, width)
}