markdown.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package styles
  2. import (
  3. "fmt"
  4. "image/color"
  5. "github.com/charmbracelet/glamour/v2"
  6. "github.com/charmbracelet/glamour/v2/ansi"
  7. )
  8. // lipglossColorToHex converts a color.Color to hex string
  9. func lipglossColorToHex(c color.Color) string {
  10. r, g, b, _ := c.RGBA()
  11. return fmt.Sprintf("#%02x%02x%02x", r>>8, g>>8, b>>8)
  12. }
  13. // Helper functions for style pointers
  14. func boolPtr(b bool) *bool { return &b }
  15. func stringPtr(s string) *string { return &s }
  16. func uintPtr(u uint) *uint { return &u }
  17. // returns a glamour TermRenderer configured with the current theme
  18. func GetMarkdownRenderer(width int) *glamour.TermRenderer {
  19. t := CurrentTheme()
  20. r, _ := glamour.NewTermRenderer(
  21. glamour.WithStyles(t.S().Markdown),
  22. glamour.WithWordWrap(width),
  23. )
  24. return r
  25. }
  26. // returns a glamour TermRenderer with no colors (plain text with structure)
  27. func GetPlainMarkdownRenderer(width int) *glamour.TermRenderer {
  28. r, _ := glamour.NewTermRenderer(
  29. glamour.WithStyles(PlainMarkdownStyle()),
  30. glamour.WithWordWrap(width),
  31. )
  32. return r
  33. }
  34. // PlainMarkdownStyle returns a glamour style config with no colors
  35. func PlainMarkdownStyle() ansi.StyleConfig {
  36. t := CurrentTheme()
  37. bgColor := stringPtr(lipglossColorToHex(t.BgBaseLighter))
  38. fgColor := stringPtr(lipglossColorToHex(t.FgMuted))
  39. return ansi.StyleConfig{
  40. Document: ansi.StyleBlock{
  41. StylePrimitive: ansi.StylePrimitive{
  42. Color: fgColor,
  43. BackgroundColor: bgColor,
  44. },
  45. },
  46. BlockQuote: ansi.StyleBlock{
  47. StylePrimitive: ansi.StylePrimitive{
  48. Color: fgColor,
  49. BackgroundColor: bgColor,
  50. },
  51. Indent: uintPtr(1),
  52. IndentToken: stringPtr("│ "),
  53. },
  54. List: ansi.StyleList{
  55. LevelIndent: defaultListIndent,
  56. },
  57. Heading: ansi.StyleBlock{
  58. StylePrimitive: ansi.StylePrimitive{
  59. BlockSuffix: "\n",
  60. Bold: boolPtr(true),
  61. Color: fgColor,
  62. BackgroundColor: bgColor,
  63. },
  64. },
  65. H1: ansi.StyleBlock{
  66. StylePrimitive: ansi.StylePrimitive{
  67. Prefix: " ",
  68. Suffix: " ",
  69. Bold: boolPtr(true),
  70. Color: fgColor,
  71. BackgroundColor: bgColor,
  72. },
  73. },
  74. H2: ansi.StyleBlock{
  75. StylePrimitive: ansi.StylePrimitive{
  76. Prefix: "## ",
  77. Color: fgColor,
  78. BackgroundColor: bgColor,
  79. },
  80. },
  81. H3: ansi.StyleBlock{
  82. StylePrimitive: ansi.StylePrimitive{
  83. Prefix: "### ",
  84. Color: fgColor,
  85. BackgroundColor: bgColor,
  86. },
  87. },
  88. H4: ansi.StyleBlock{
  89. StylePrimitive: ansi.StylePrimitive{
  90. Prefix: "#### ",
  91. Color: fgColor,
  92. BackgroundColor: bgColor,
  93. },
  94. },
  95. H5: ansi.StyleBlock{
  96. StylePrimitive: ansi.StylePrimitive{
  97. Prefix: "##### ",
  98. Color: fgColor,
  99. BackgroundColor: bgColor,
  100. },
  101. },
  102. H6: ansi.StyleBlock{
  103. StylePrimitive: ansi.StylePrimitive{
  104. Prefix: "###### ",
  105. Color: fgColor,
  106. BackgroundColor: bgColor,
  107. },
  108. },
  109. Strikethrough: ansi.StylePrimitive{
  110. CrossedOut: boolPtr(true),
  111. Color: fgColor,
  112. BackgroundColor: bgColor,
  113. },
  114. Emph: ansi.StylePrimitive{
  115. Italic: boolPtr(true),
  116. Color: fgColor,
  117. BackgroundColor: bgColor,
  118. },
  119. Strong: ansi.StylePrimitive{
  120. Bold: boolPtr(true),
  121. Color: fgColor,
  122. BackgroundColor: bgColor,
  123. },
  124. HorizontalRule: ansi.StylePrimitive{
  125. Format: "\n--------\n",
  126. Color: fgColor,
  127. BackgroundColor: bgColor,
  128. },
  129. Item: ansi.StylePrimitive{
  130. BlockPrefix: "• ",
  131. Color: fgColor,
  132. BackgroundColor: bgColor,
  133. },
  134. Enumeration: ansi.StylePrimitive{
  135. BlockPrefix: ". ",
  136. Color: fgColor,
  137. BackgroundColor: bgColor,
  138. },
  139. Task: ansi.StyleTask{
  140. StylePrimitive: ansi.StylePrimitive{
  141. Color: fgColor,
  142. BackgroundColor: bgColor,
  143. },
  144. Ticked: "[✓] ",
  145. Unticked: "[ ] ",
  146. },
  147. Link: ansi.StylePrimitive{
  148. Underline: boolPtr(true),
  149. Color: fgColor,
  150. BackgroundColor: bgColor,
  151. },
  152. LinkText: ansi.StylePrimitive{
  153. Bold: boolPtr(true),
  154. Color: fgColor,
  155. BackgroundColor: bgColor,
  156. },
  157. Image: ansi.StylePrimitive{
  158. Underline: boolPtr(true),
  159. Color: fgColor,
  160. BackgroundColor: bgColor,
  161. },
  162. ImageText: ansi.StylePrimitive{
  163. Format: "Image: {{.text}} →",
  164. Color: fgColor,
  165. BackgroundColor: bgColor,
  166. },
  167. Code: ansi.StyleBlock{
  168. StylePrimitive: ansi.StylePrimitive{
  169. Prefix: " ",
  170. Suffix: " ",
  171. Color: fgColor,
  172. BackgroundColor: bgColor,
  173. },
  174. },
  175. CodeBlock: ansi.StyleCodeBlock{
  176. StyleBlock: ansi.StyleBlock{
  177. StylePrimitive: ansi.StylePrimitive{
  178. Color: fgColor,
  179. BackgroundColor: bgColor,
  180. },
  181. Margin: uintPtr(defaultMargin),
  182. },
  183. },
  184. Table: ansi.StyleTable{
  185. StyleBlock: ansi.StyleBlock{
  186. StylePrimitive: ansi.StylePrimitive{
  187. Color: fgColor,
  188. BackgroundColor: bgColor,
  189. },
  190. },
  191. },
  192. DefinitionDescription: ansi.StylePrimitive{
  193. BlockPrefix: "\n ",
  194. Color: fgColor,
  195. BackgroundColor: bgColor,
  196. },
  197. }
  198. }