package tui import ( "strings" "github.com/charmbracelet/glamour" "github.com/charmbracelet/glamour/ansi" glamstyles "github.com/charmbracelet/glamour/styles" ) const minMarkdownWidth = 20 func renderMarkdown(content string, width int, th theme) string { width = max(minMarkdownWidth, width) renderer, err := glamour.NewTermRenderer( glamour.WithStyles(markdownStyle(th)), glamour.WithWordWrap(width), ) if err != nil { return content } rendered, err := renderer.Render(content) if err != nil { return content } return strings.TrimRight(rendered, "\n") } func markdownStyle(th theme) ansi.StyleConfig { cfg := glamstyles.LightStyleConfig if th.Mode == ThemeDark { cfg = glamstyles.DarkStyleConfig } zero := uint(0) cfg.Document.Margin = &zero cfg.Document.BlockPrefix = "" cfg.Document.BlockSuffix = "" cfg.Document.Color = stringPtr(th.Text) cfg.Heading.Color = stringPtr(th.Assistant) cfg.H1.Prefix = "" cfg.H1.Suffix = "" cfg.H1.Color = stringPtr(th.Assistant) cfg.H1.BackgroundColor = nil cfg.H1.Bold = boolPtr(true) cfg.BlockQuote.Color = stringPtr(th.Muted) cfg.Link.Color = stringPtr(th.User) cfg.LinkText.Color = stringPtr(th.User) cfg.Code.Color = stringPtr(th.Tool) cfg.Code.BackgroundColor = stringPtr(th.SurfaceAlt) cfg.CodeBlock.Color = stringPtr(th.Text) cfg.CodeBlock.BackgroundColor = nil cfg.CodeBlock.Margin = &zero return cfg } func stringPtr(value string) *string { return &value } func boolPtr(value bool) *bool { return &value }