markdown.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package styles
  2. import (
  3. "github.com/charmbracelet/glamour/ansi"
  4. "github.com/charmbracelet/lipgloss"
  5. )
  6. const defaultMargin = 1
  7. // Helper functions for style pointers
  8. func boolPtr(b bool) *bool { return &b }
  9. func stringPtr(s string) *string { return &s }
  10. func uintPtr(u uint) *uint { return &u }
  11. // CatppuccinMarkdownStyle is the Catppuccin Mocha style for Glamour markdown rendering.
  12. func CatppuccinMarkdownStyle() ansi.StyleConfig {
  13. isDark := lipgloss.HasDarkBackground()
  14. if isDark {
  15. return catppuccinDark
  16. }
  17. return catppuccinLight
  18. }
  19. var catppuccinDark = ansi.StyleConfig{
  20. Document: ansi.StyleBlock{
  21. StylePrimitive: ansi.StylePrimitive{
  22. BlockPrefix: "\n",
  23. BlockSuffix: "",
  24. Color: stringPtr(dark.Text().Hex),
  25. },
  26. Margin: uintPtr(defaultMargin),
  27. },
  28. BlockQuote: ansi.StyleBlock{
  29. StylePrimitive: ansi.StylePrimitive{
  30. Color: stringPtr(dark.Yellow().Hex),
  31. Italic: boolPtr(true),
  32. Prefix: "┃ ",
  33. },
  34. Indent: uintPtr(1),
  35. Margin: uintPtr(defaultMargin),
  36. },
  37. List: ansi.StyleList{
  38. LevelIndent: defaultMargin,
  39. StyleBlock: ansi.StyleBlock{
  40. StylePrimitive: ansi.StylePrimitive{
  41. Color: stringPtr(dark.Text().Hex),
  42. },
  43. },
  44. },
  45. Heading: ansi.StyleBlock{
  46. StylePrimitive: ansi.StylePrimitive{
  47. BlockSuffix: "\n",
  48. Color: stringPtr(dark.Mauve().Hex),
  49. Bold: boolPtr(true),
  50. },
  51. },
  52. H1: ansi.StyleBlock{
  53. StylePrimitive: ansi.StylePrimitive{
  54. Prefix: "# ",
  55. Color: stringPtr(dark.Lavender().Hex),
  56. Bold: boolPtr(true),
  57. BlockPrefix: "\n",
  58. },
  59. },
  60. H2: ansi.StyleBlock{
  61. StylePrimitive: ansi.StylePrimitive{
  62. Prefix: "## ",
  63. Color: stringPtr(dark.Mauve().Hex),
  64. Bold: boolPtr(true),
  65. },
  66. },
  67. H3: ansi.StyleBlock{
  68. StylePrimitive: ansi.StylePrimitive{
  69. Prefix: "### ",
  70. Color: stringPtr(dark.Pink().Hex),
  71. Bold: boolPtr(true),
  72. },
  73. },
  74. H4: ansi.StyleBlock{
  75. StylePrimitive: ansi.StylePrimitive{
  76. Prefix: "#### ",
  77. Color: stringPtr(dark.Flamingo().Hex),
  78. Bold: boolPtr(true),
  79. },
  80. },
  81. H5: ansi.StyleBlock{
  82. StylePrimitive: ansi.StylePrimitive{
  83. Prefix: "##### ",
  84. Color: stringPtr(dark.Rosewater().Hex),
  85. Bold: boolPtr(true),
  86. },
  87. },
  88. H6: ansi.StyleBlock{
  89. StylePrimitive: ansi.StylePrimitive{
  90. Prefix: "###### ",
  91. Color: stringPtr(dark.Rosewater().Hex),
  92. Bold: boolPtr(true),
  93. },
  94. },
  95. Strikethrough: ansi.StylePrimitive{
  96. CrossedOut: boolPtr(true),
  97. Color: stringPtr(dark.Overlay1().Hex),
  98. },
  99. Emph: ansi.StylePrimitive{
  100. Color: stringPtr(dark.Yellow().Hex),
  101. Italic: boolPtr(true),
  102. },
  103. Strong: ansi.StylePrimitive{
  104. Bold: boolPtr(true),
  105. Color: stringPtr(dark.Peach().Hex),
  106. },
  107. HorizontalRule: ansi.StylePrimitive{
  108. Color: stringPtr(dark.Overlay0().Hex),
  109. Format: "\n─────────────────────────────────────────\n",
  110. },
  111. Item: ansi.StylePrimitive{
  112. BlockPrefix: "• ",
  113. Color: stringPtr(dark.Blue().Hex),
  114. },
  115. Enumeration: ansi.StylePrimitive{
  116. BlockPrefix: ". ",
  117. Color: stringPtr(dark.Sky().Hex),
  118. },
  119. Task: ansi.StyleTask{
  120. StylePrimitive: ansi.StylePrimitive{},
  121. Ticked: "[✓] ",
  122. Unticked: "[ ] ",
  123. },
  124. Link: ansi.StylePrimitive{
  125. Color: stringPtr(dark.Sky().Hex),
  126. Underline: boolPtr(true),
  127. },
  128. LinkText: ansi.StylePrimitive{
  129. Color: stringPtr(dark.Pink().Hex),
  130. Bold: boolPtr(true),
  131. },
  132. Image: ansi.StylePrimitive{
  133. Color: stringPtr(dark.Sapphire().Hex),
  134. Underline: boolPtr(true),
  135. Format: "🖼 {{.text}}",
  136. },
  137. ImageText: ansi.StylePrimitive{
  138. Color: stringPtr(dark.Pink().Hex),
  139. Format: "{{.text}}",
  140. },
  141. Code: ansi.StyleBlock{
  142. StylePrimitive: ansi.StylePrimitive{
  143. Color: stringPtr(dark.Green().Hex),
  144. Prefix: "",
  145. Suffix: "",
  146. },
  147. },
  148. CodeBlock: ansi.StyleCodeBlock{
  149. StyleBlock: ansi.StyleBlock{
  150. StylePrimitive: ansi.StylePrimitive{
  151. Prefix: " ",
  152. Color: stringPtr(dark.Text().Hex),
  153. },
  154. Margin: uintPtr(defaultMargin),
  155. },
  156. Chroma: &ansi.Chroma{
  157. Text: ansi.StylePrimitive{
  158. Color: stringPtr(dark.Text().Hex),
  159. },
  160. Error: ansi.StylePrimitive{
  161. Color: stringPtr(dark.Text().Hex),
  162. },
  163. Comment: ansi.StylePrimitive{
  164. Color: stringPtr(dark.Overlay1().Hex),
  165. },
  166. CommentPreproc: ansi.StylePrimitive{
  167. Color: stringPtr(dark.Pink().Hex),
  168. },
  169. Keyword: ansi.StylePrimitive{
  170. Color: stringPtr(dark.Pink().Hex),
  171. },
  172. KeywordReserved: ansi.StylePrimitive{
  173. Color: stringPtr(dark.Pink().Hex),
  174. },
  175. KeywordNamespace: ansi.StylePrimitive{
  176. Color: stringPtr(dark.Pink().Hex),
  177. },
  178. KeywordType: ansi.StylePrimitive{
  179. Color: stringPtr(dark.Sky().Hex),
  180. },
  181. Operator: ansi.StylePrimitive{
  182. Color: stringPtr(dark.Pink().Hex),
  183. },
  184. Punctuation: ansi.StylePrimitive{
  185. Color: stringPtr(dark.Text().Hex),
  186. },
  187. Name: ansi.StylePrimitive{
  188. Color: stringPtr(dark.Sky().Hex),
  189. },
  190. NameBuiltin: ansi.StylePrimitive{
  191. Color: stringPtr(dark.Sky().Hex),
  192. },
  193. NameTag: ansi.StylePrimitive{
  194. Color: stringPtr(dark.Pink().Hex),
  195. },
  196. NameAttribute: ansi.StylePrimitive{
  197. Color: stringPtr(dark.Green().Hex),
  198. },
  199. NameClass: ansi.StylePrimitive{
  200. Color: stringPtr(dark.Sky().Hex),
  201. },
  202. NameConstant: ansi.StylePrimitive{
  203. Color: stringPtr(dark.Mauve().Hex),
  204. },
  205. NameDecorator: ansi.StylePrimitive{
  206. Color: stringPtr(dark.Green().Hex),
  207. },
  208. NameFunction: ansi.StylePrimitive{
  209. Color: stringPtr(dark.Green().Hex),
  210. },
  211. LiteralNumber: ansi.StylePrimitive{
  212. Color: stringPtr(dark.Teal().Hex),
  213. },
  214. LiteralString: ansi.StylePrimitive{
  215. Color: stringPtr(dark.Yellow().Hex),
  216. },
  217. LiteralStringEscape: ansi.StylePrimitive{
  218. Color: stringPtr(dark.Pink().Hex),
  219. },
  220. GenericDeleted: ansi.StylePrimitive{
  221. Color: stringPtr(dark.Red().Hex),
  222. },
  223. GenericEmph: ansi.StylePrimitive{
  224. Color: stringPtr(dark.Yellow().Hex),
  225. Italic: boolPtr(true),
  226. },
  227. GenericInserted: ansi.StylePrimitive{
  228. Color: stringPtr(dark.Green().Hex),
  229. },
  230. GenericStrong: ansi.StylePrimitive{
  231. Color: stringPtr(dark.Peach().Hex),
  232. Bold: boolPtr(true),
  233. },
  234. GenericSubheading: ansi.StylePrimitive{
  235. Color: stringPtr(dark.Mauve().Hex),
  236. },
  237. },
  238. },
  239. Table: ansi.StyleTable{
  240. StyleBlock: ansi.StyleBlock{
  241. StylePrimitive: ansi.StylePrimitive{
  242. BlockPrefix: "\n",
  243. BlockSuffix: "\n",
  244. },
  245. },
  246. CenterSeparator: stringPtr("┼"),
  247. ColumnSeparator: stringPtr("│"),
  248. RowSeparator: stringPtr("─"),
  249. },
  250. DefinitionDescription: ansi.StylePrimitive{
  251. BlockPrefix: "\n ❯ ",
  252. Color: stringPtr(dark.Sapphire().Hex),
  253. },
  254. }
  255. var catppuccinLight = ansi.StyleConfig{
  256. Document: ansi.StyleBlock{
  257. StylePrimitive: ansi.StylePrimitive{
  258. BlockPrefix: "\n",
  259. BlockSuffix: "\n",
  260. Color: stringPtr(light.Text().Hex),
  261. },
  262. Margin: uintPtr(defaultMargin),
  263. },
  264. BlockQuote: ansi.StyleBlock{
  265. StylePrimitive: ansi.StylePrimitive{
  266. Color: stringPtr(light.Yellow().Hex),
  267. Italic: boolPtr(true),
  268. Prefix: "┃ ",
  269. },
  270. Indent: uintPtr(1),
  271. Margin: uintPtr(defaultMargin),
  272. },
  273. List: ansi.StyleList{
  274. LevelIndent: defaultMargin,
  275. StyleBlock: ansi.StyleBlock{
  276. StylePrimitive: ansi.StylePrimitive{
  277. Color: stringPtr(light.Text().Hex),
  278. },
  279. },
  280. },
  281. Heading: ansi.StyleBlock{
  282. StylePrimitive: ansi.StylePrimitive{
  283. BlockSuffix: "\n",
  284. Color: stringPtr(light.Mauve().Hex),
  285. Bold: boolPtr(true),
  286. },
  287. },
  288. H1: ansi.StyleBlock{
  289. StylePrimitive: ansi.StylePrimitive{
  290. Prefix: "# ",
  291. Color: stringPtr(light.Lavender().Hex),
  292. Bold: boolPtr(true),
  293. BlockPrefix: "\n",
  294. },
  295. },
  296. H2: ansi.StyleBlock{
  297. StylePrimitive: ansi.StylePrimitive{
  298. Prefix: "## ",
  299. Color: stringPtr(light.Mauve().Hex),
  300. Bold: boolPtr(true),
  301. },
  302. },
  303. H3: ansi.StyleBlock{
  304. StylePrimitive: ansi.StylePrimitive{
  305. Prefix: "### ",
  306. Color: stringPtr(light.Pink().Hex),
  307. Bold: boolPtr(true),
  308. },
  309. },
  310. H4: ansi.StyleBlock{
  311. StylePrimitive: ansi.StylePrimitive{
  312. Prefix: "#### ",
  313. Color: stringPtr(light.Flamingo().Hex),
  314. Bold: boolPtr(true),
  315. },
  316. },
  317. H5: ansi.StyleBlock{
  318. StylePrimitive: ansi.StylePrimitive{
  319. Prefix: "##### ",
  320. Color: stringPtr(light.Rosewater().Hex),
  321. Bold: boolPtr(true),
  322. },
  323. },
  324. H6: ansi.StyleBlock{
  325. StylePrimitive: ansi.StylePrimitive{
  326. Prefix: "###### ",
  327. Color: stringPtr(light.Rosewater().Hex),
  328. Bold: boolPtr(true),
  329. },
  330. },
  331. Strikethrough: ansi.StylePrimitive{
  332. CrossedOut: boolPtr(true),
  333. Color: stringPtr(light.Overlay1().Hex),
  334. },
  335. Emph: ansi.StylePrimitive{
  336. Color: stringPtr(light.Yellow().Hex),
  337. Italic: boolPtr(true),
  338. },
  339. Strong: ansi.StylePrimitive{
  340. Bold: boolPtr(true),
  341. Color: stringPtr(light.Peach().Hex),
  342. },
  343. HorizontalRule: ansi.StylePrimitive{
  344. Color: stringPtr(light.Overlay0().Hex),
  345. Format: "\n─────────────────────────────────────────\n",
  346. },
  347. Item: ansi.StylePrimitive{
  348. BlockPrefix: "• ",
  349. Color: stringPtr(light.Blue().Hex),
  350. },
  351. Enumeration: ansi.StylePrimitive{
  352. BlockPrefix: ". ",
  353. Color: stringPtr(light.Sky().Hex),
  354. },
  355. Task: ansi.StyleTask{
  356. StylePrimitive: ansi.StylePrimitive{},
  357. Ticked: "[✓] ",
  358. Unticked: "[ ] ",
  359. },
  360. Link: ansi.StylePrimitive{
  361. Color: stringPtr(light.Sky().Hex),
  362. Underline: boolPtr(true),
  363. },
  364. LinkText: ansi.StylePrimitive{
  365. Color: stringPtr(light.Pink().Hex),
  366. Bold: boolPtr(true),
  367. },
  368. Image: ansi.StylePrimitive{
  369. Color: stringPtr(light.Sapphire().Hex),
  370. Underline: boolPtr(true),
  371. Format: "🖼 {{.text}}",
  372. },
  373. ImageText: ansi.StylePrimitive{
  374. Color: stringPtr(light.Pink().Hex),
  375. Format: "{{.text}}",
  376. },
  377. Code: ansi.StyleBlock{
  378. StylePrimitive: ansi.StylePrimitive{
  379. Color: stringPtr(light.Green().Hex),
  380. Prefix: " ",
  381. Suffix: " ",
  382. },
  383. },
  384. CodeBlock: ansi.StyleCodeBlock{
  385. StyleBlock: ansi.StyleBlock{
  386. StylePrimitive: ansi.StylePrimitive{
  387. Prefix: " ",
  388. Color: stringPtr(light.Text().Hex),
  389. },
  390. Margin: uintPtr(defaultMargin),
  391. },
  392. Chroma: &ansi.Chroma{
  393. Text: ansi.StylePrimitive{
  394. Color: stringPtr(light.Text().Hex),
  395. },
  396. Error: ansi.StylePrimitive{
  397. Color: stringPtr(light.Text().Hex),
  398. },
  399. Comment: ansi.StylePrimitive{
  400. Color: stringPtr(light.Overlay1().Hex),
  401. },
  402. CommentPreproc: ansi.StylePrimitive{
  403. Color: stringPtr(light.Pink().Hex),
  404. },
  405. Keyword: ansi.StylePrimitive{
  406. Color: stringPtr(light.Pink().Hex),
  407. },
  408. KeywordReserved: ansi.StylePrimitive{
  409. Color: stringPtr(light.Pink().Hex),
  410. },
  411. KeywordNamespace: ansi.StylePrimitive{
  412. Color: stringPtr(light.Pink().Hex),
  413. },
  414. KeywordType: ansi.StylePrimitive{
  415. Color: stringPtr(light.Sky().Hex),
  416. },
  417. Operator: ansi.StylePrimitive{
  418. Color: stringPtr(light.Pink().Hex),
  419. },
  420. Punctuation: ansi.StylePrimitive{
  421. Color: stringPtr(light.Text().Hex),
  422. },
  423. Name: ansi.StylePrimitive{
  424. Color: stringPtr(light.Sky().Hex),
  425. },
  426. NameBuiltin: ansi.StylePrimitive{
  427. Color: stringPtr(light.Sky().Hex),
  428. },
  429. NameTag: ansi.StylePrimitive{
  430. Color: stringPtr(light.Pink().Hex),
  431. },
  432. NameAttribute: ansi.StylePrimitive{
  433. Color: stringPtr(light.Green().Hex),
  434. },
  435. NameClass: ansi.StylePrimitive{
  436. Color: stringPtr(light.Sky().Hex),
  437. },
  438. NameConstant: ansi.StylePrimitive{
  439. Color: stringPtr(light.Mauve().Hex),
  440. },
  441. NameDecorator: ansi.StylePrimitive{
  442. Color: stringPtr(light.Green().Hex),
  443. },
  444. NameFunction: ansi.StylePrimitive{
  445. Color: stringPtr(light.Green().Hex),
  446. },
  447. LiteralNumber: ansi.StylePrimitive{
  448. Color: stringPtr(light.Teal().Hex),
  449. },
  450. LiteralString: ansi.StylePrimitive{
  451. Color: stringPtr(light.Yellow().Hex),
  452. },
  453. LiteralStringEscape: ansi.StylePrimitive{
  454. Color: stringPtr(light.Pink().Hex),
  455. },
  456. GenericDeleted: ansi.StylePrimitive{
  457. Color: stringPtr(light.Red().Hex),
  458. },
  459. GenericEmph: ansi.StylePrimitive{
  460. Color: stringPtr(light.Yellow().Hex),
  461. Italic: boolPtr(true),
  462. },
  463. GenericInserted: ansi.StylePrimitive{
  464. Color: stringPtr(light.Green().Hex),
  465. },
  466. GenericStrong: ansi.StylePrimitive{
  467. Color: stringPtr(light.Peach().Hex),
  468. Bold: boolPtr(true),
  469. },
  470. GenericSubheading: ansi.StylePrimitive{
  471. Color: stringPtr(light.Mauve().Hex),
  472. },
  473. },
  474. },
  475. Table: ansi.StyleTable{
  476. StyleBlock: ansi.StyleBlock{
  477. StylePrimitive: ansi.StylePrimitive{
  478. BlockPrefix: "\n",
  479. BlockSuffix: "\n",
  480. },
  481. },
  482. CenterSeparator: stringPtr("┼"),
  483. ColumnSeparator: stringPtr("│"),
  484. RowSeparator: stringPtr("─"),
  485. },
  486. DefinitionDescription: ansi.StylePrimitive{
  487. BlockPrefix: "\n ❯ ",
  488. Color: stringPtr(light.Sapphire().Hex),
  489. },
  490. }