chroma.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package styles
  2. import (
  3. "github.com/alecthomas/chroma/v2"
  4. "github.com/charmbracelet/glamour/v2/ansi"
  5. )
  6. func chromaStyle(style ansi.StylePrimitive) string {
  7. var s string
  8. if style.Color != nil {
  9. s = *style.Color
  10. }
  11. if style.BackgroundColor != nil {
  12. if s != "" {
  13. s += " "
  14. }
  15. s += "bg:" + *style.BackgroundColor
  16. }
  17. if style.Italic != nil && *style.Italic {
  18. if s != "" {
  19. s += " "
  20. }
  21. s += "italic"
  22. }
  23. if style.Bold != nil && *style.Bold {
  24. if s != "" {
  25. s += " "
  26. }
  27. s += "bold"
  28. }
  29. if style.Underline != nil && *style.Underline {
  30. if s != "" {
  31. s += " "
  32. }
  33. s += "underline"
  34. }
  35. return s
  36. }
  37. func GetChromaTheme() chroma.StyleEntries {
  38. t := CurrentTheme()
  39. rules := t.S().Markdown.CodeBlock
  40. return chroma.StyleEntries{
  41. chroma.Text: chromaStyle(rules.Chroma.Text),
  42. chroma.Error: chromaStyle(rules.Chroma.Error),
  43. chroma.Comment: chromaStyle(rules.Chroma.Comment),
  44. chroma.CommentPreproc: chromaStyle(rules.Chroma.CommentPreproc),
  45. chroma.Keyword: chromaStyle(rules.Chroma.Keyword),
  46. chroma.KeywordReserved: chromaStyle(rules.Chroma.KeywordReserved),
  47. chroma.KeywordNamespace: chromaStyle(rules.Chroma.KeywordNamespace),
  48. chroma.KeywordType: chromaStyle(rules.Chroma.KeywordType),
  49. chroma.Operator: chromaStyle(rules.Chroma.Operator),
  50. chroma.Punctuation: chromaStyle(rules.Chroma.Punctuation),
  51. chroma.Name: chromaStyle(rules.Chroma.Name),
  52. chroma.NameBuiltin: chromaStyle(rules.Chroma.NameBuiltin),
  53. chroma.NameTag: chromaStyle(rules.Chroma.NameTag),
  54. chroma.NameAttribute: chromaStyle(rules.Chroma.NameAttribute),
  55. chroma.NameClass: chromaStyle(rules.Chroma.NameClass),
  56. chroma.NameConstant: chromaStyle(rules.Chroma.NameConstant),
  57. chroma.NameDecorator: chromaStyle(rules.Chroma.NameDecorator),
  58. chroma.NameException: chromaStyle(rules.Chroma.NameException),
  59. chroma.NameFunction: chromaStyle(rules.Chroma.NameFunction),
  60. chroma.NameOther: chromaStyle(rules.Chroma.NameOther),
  61. chroma.Literal: chromaStyle(rules.Chroma.Literal),
  62. chroma.LiteralNumber: chromaStyle(rules.Chroma.LiteralNumber),
  63. chroma.LiteralDate: chromaStyle(rules.Chroma.LiteralDate),
  64. chroma.LiteralString: chromaStyle(rules.Chroma.LiteralString),
  65. chroma.LiteralStringEscape: chromaStyle(rules.Chroma.LiteralStringEscape),
  66. chroma.GenericDeleted: chromaStyle(rules.Chroma.GenericDeleted),
  67. chroma.GenericEmph: chromaStyle(rules.Chroma.GenericEmph),
  68. chroma.GenericInserted: chromaStyle(rules.Chroma.GenericInserted),
  69. chroma.GenericStrong: chromaStyle(rules.Chroma.GenericStrong),
  70. chroma.GenericSubheading: chromaStyle(rules.Chroma.GenericSubheading),
  71. chroma.Background: chromaStyle(rules.Chroma.Background),
  72. }
  73. }