Skip to content

Commit 7da11f9

Browse files
committed
feat(config): add chroma syntax highlighting style support
- Add `ChromaStyle` function to convert glamour styles to chroma format - Add `GetChromaStyle` function with theme-based style generation - Add mutex protection for concurrent style registry access - Update import aliases to resolve naming conflicts with chroma styles - Support custom chroma themes with fallback to predefined styles
1 parent f72df29 commit 7da11f9

File tree

1 file changed

+109
-4
lines changed

1 file changed

+109
-4
lines changed

internal/config/style.go

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"fmt"
66
"os"
77
"strings"
8+
"sync"
89

10+
"github.com/alecthomas/chroma/v2"
11+
chromaStyles "github.com/alecthomas/chroma/v2/styles"
912
"github.com/charmbracelet/glamour/ansi"
10-
"github.com/charmbracelet/glamour/styles"
13+
glamourStyles "github.com/charmbracelet/glamour/styles"
1114
"github.com/charmbracelet/lipgloss"
1215
"github.com/go-viper/mapstructure/v2"
1316
"github.com/goccy/go-yaml"
@@ -17,8 +20,11 @@ import (
1720

1821
const (
1922
DefaultBorderColor = "#9999CC"
23+
chromaStyleTheme = "kyma"
2024
)
2125

26+
var chromaMutex = sync.Mutex{}
27+
2228
type Properties struct {
2329
Title string `yaml:"title"`
2430
Style StyleConfig `yaml:"style"`
@@ -173,16 +179,16 @@ func getLayout(layout string) (lipgloss.Style, error) {
173179
}
174180

175181
func getTheme(theme string) GlamourTheme {
176-
style, ok := styles.DefaultStyles[theme]
182+
style, ok := glamourStyles.DefaultStyles[theme]
177183
if !ok {
178184
jsonBytes, err := os.ReadFile(theme)
179185
if err != nil {
180-
return GlamourTheme{Style: styles.DarkStyleConfig, Name: "dark"}
186+
return GlamourTheme{Style: glamourStyles.DarkStyleConfig, Name: "dark"}
181187
}
182188

183189
var customStyle ansi.StyleConfig
184190
if err := json.Unmarshal(jsonBytes, &customStyle); err != nil {
185-
return GlamourTheme{Style: styles.DarkStyleConfig, Name: "dark"}
191+
return GlamourTheme{Style: glamourStyles.DarkStyleConfig, Name: "dark"}
186192
}
187193

188194
return GlamourTheme{Style: customStyle, Name: theme}
@@ -269,3 +275,102 @@ func NewProperties(properties string) (Properties, error) {
269275

270276
return p, nil
271277
}
278+
279+
func ChromaStyle(style ansi.StylePrimitive) string {
280+
var s string
281+
282+
if style.Color != nil {
283+
s = *style.Color
284+
}
285+
if style.BackgroundColor != nil {
286+
if s != "" {
287+
s += " "
288+
}
289+
s += "bg:" + *style.BackgroundColor
290+
}
291+
if style.Italic != nil && *style.Italic {
292+
if s != "" {
293+
s += " "
294+
}
295+
s += "italic"
296+
}
297+
if style.Bold != nil && *style.Bold {
298+
if s != "" {
299+
s += " "
300+
}
301+
s += "bold"
302+
}
303+
if style.Underline != nil && *style.Underline {
304+
if s != "" {
305+
s += " "
306+
}
307+
s += "underline"
308+
}
309+
310+
return s
311+
}
312+
313+
func GetChromaStyle(themeName string) *chroma.Style {
314+
customThemeName := chromaStyleTheme + "-" + themeName
315+
316+
chromaMutex.Lock()
317+
defer chromaMutex.Unlock()
318+
319+
if style, ok := chromaStyles.Registry[customThemeName]; ok {
320+
return style
321+
}
322+
323+
styleConfig := getTheme(themeName)
324+
style := styleConfig.Style
325+
if style.CodeBlock.Chroma != nil {
326+
style := chroma.MustNewStyle(customThemeName,
327+
chroma.StyleEntries{
328+
chroma.Text: ChromaStyle(style.CodeBlock.Chroma.Text),
329+
chroma.Error: ChromaStyle(style.CodeBlock.Chroma.Error),
330+
chroma.Comment: ChromaStyle(style.CodeBlock.Chroma.Comment),
331+
chroma.CommentPreproc: ChromaStyle(style.CodeBlock.Chroma.CommentPreproc),
332+
chroma.Keyword: ChromaStyle(style.CodeBlock.Chroma.Keyword),
333+
chroma.KeywordReserved: ChromaStyle(style.CodeBlock.Chroma.KeywordReserved),
334+
chroma.KeywordNamespace: ChromaStyle(style.CodeBlock.Chroma.KeywordNamespace),
335+
chroma.KeywordType: ChromaStyle(style.CodeBlock.Chroma.KeywordType),
336+
chroma.Operator: ChromaStyle(style.CodeBlock.Chroma.Operator),
337+
chroma.Punctuation: ChromaStyle(style.CodeBlock.Chroma.Punctuation),
338+
chroma.Name: ChromaStyle(style.CodeBlock.Chroma.Name),
339+
chroma.NameBuiltin: ChromaStyle(style.CodeBlock.Chroma.NameBuiltin),
340+
chroma.NameTag: ChromaStyle(style.CodeBlock.Chroma.NameTag),
341+
chroma.NameAttribute: ChromaStyle(style.CodeBlock.Chroma.NameAttribute),
342+
chroma.NameClass: ChromaStyle(style.CodeBlock.Chroma.NameClass),
343+
chroma.NameConstant: ChromaStyle(style.CodeBlock.Chroma.NameConstant),
344+
chroma.NameDecorator: ChromaStyle(style.CodeBlock.Chroma.NameDecorator),
345+
chroma.NameException: ChromaStyle(style.CodeBlock.Chroma.NameException),
346+
chroma.NameFunction: ChromaStyle(style.CodeBlock.Chroma.NameFunction),
347+
chroma.NameOther: ChromaStyle(style.CodeBlock.Chroma.NameOther),
348+
chroma.Literal: ChromaStyle(style.CodeBlock.Chroma.Literal),
349+
chroma.LiteralNumber: ChromaStyle(style.CodeBlock.Chroma.LiteralNumber),
350+
chroma.LiteralDate: ChromaStyle(style.CodeBlock.Chroma.LiteralDate),
351+
chroma.LiteralString: ChromaStyle(style.CodeBlock.Chroma.LiteralString),
352+
chroma.LiteralStringEscape: ChromaStyle(style.CodeBlock.Chroma.LiteralStringEscape),
353+
chroma.GenericDeleted: ChromaStyle(style.CodeBlock.Chroma.GenericDeleted),
354+
chroma.GenericEmph: ChromaStyle(style.CodeBlock.Chroma.GenericEmph),
355+
chroma.GenericInserted: ChromaStyle(style.CodeBlock.Chroma.GenericInserted),
356+
chroma.GenericStrong: ChromaStyle(style.CodeBlock.Chroma.GenericStrong),
357+
chroma.GenericSubheading: ChromaStyle(style.CodeBlock.Chroma.GenericSubheading),
358+
chroma.Background: ChromaStyle(style.CodeBlock.Chroma.Background),
359+
})
360+
chromaStyles.Register(style)
361+
return style
362+
}
363+
364+
switch themeName {
365+
case "dracula":
366+
return chromaStyles.Get("dracula")
367+
case "dark":
368+
return chromaStyles.Get("github-dark")
369+
case "light":
370+
return chromaStyles.Get("github")
371+
case "tokyo-night", "tokyonight":
372+
return chromaStyles.Get("tokyo-night")
373+
default:
374+
return chromaStyles.Fallback
375+
}
376+
}

0 commit comments

Comments
 (0)