markdown.go

 1package common
 2
 3import (
 4	"image/color"
 5
 6	"charm.land/glamour/v2"
 7	"github.com/alecthomas/chroma/v2/formatters"
 8	"github.com/charmbracelet/crush/internal/ui/styles"
 9	"github.com/charmbracelet/crush/internal/ui/xchroma"
10)
11
12const formatterName = "crush"
13
14func init() {
15	// NOTE: Glamour does not offer us an option to pass the formatter
16	// implementation directly. We need to register and use by name.
17	var zero color.Color
18	formatters.Register(formatterName, xchroma.Formatter(zero, nil))
19}
20
21// MarkdownRenderer returns a glamour [glamour.TermRenderer] configured with
22// the given styles and width.
23func MarkdownRenderer(sty *styles.Styles, width int) *glamour.TermRenderer {
24	r, _ := glamour.NewTermRenderer(
25		glamour.WithStyles(sty.Markdown),
26		glamour.WithWordWrap(width),
27		glamour.WithChromaFormatter(formatterName),
28	)
29	return r
30}
31
32// PlainMarkdownRenderer returns a glamour [glamour.TermRenderer] with no colors
33// (plain text with structure) and the given width.
34func PlainMarkdownRenderer(sty *styles.Styles, width int) *glamour.TermRenderer {
35	r, _ := glamour.NewTermRenderer(
36		glamour.WithStyles(sty.PlainMarkdown),
37		glamour.WithWordWrap(width),
38		glamour.WithChromaFormatter(formatterName),
39	)
40	return r
41}