diff --git a/cli/go.mod b/cli/go.mod index f578ccd00..a7aa1802e 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -12,6 +12,7 @@ require ( github.com/charmbracelet/ultraviolet v0.0.0-20261001125412-878653296cfd github.com/charmbracelet/x/ansi v0.11.9 github.com/charmbracelet/x/term v0.2.2 + github.com/doug/termtex v0.0.0-20260918165034-c407d5cf5251 github.com/google/jsonschema-go v0.4.3 github.com/klauspost/compress v1.20.1 github.com/lucasb-eyer/go-colorful v1.4.1 @@ -20,6 +21,7 @@ require ( github.com/rivo/uniseg v0.4.7 github.com/sahilm/fuzzy v0.1.3 github.com/spf13/cobra v1.10.2 + github.com/yuin/goldmark v1.8.6 go.yaml.in/yaml/v3 v3.0.5 golang.org/x/sys v0.48.0 golang.org/x/text v0.42.0 @@ -43,10 +45,11 @@ require ( github.com/muesli/cancelreader v0.2.2 // indirect github.com/spf13/pflag v1.0.10 // indirect github.com/xo/terminfo v1.2.0 // indirect - github.com/yuin/goldmark v1.8.6 // indirect github.com/yuin/goldmark-emoji v1.0.6 // indirect golang.org/x/net v0.60.0 // indirect golang.org/x/sync v0.23.0 // indirect ) replace github.com/charmbracelet/ultraviolet => tangled.org/okami.mom/ultraviolet v0.0.0-20261007174847-7bfbaab9c98a + +replace charm.land/glamour/v2 => tangled.org/okami.mom/glamour.git/v2 v2.0.0-20261013005126-4046973d5934 diff --git a/cli/go.sum b/cli/go.sum index 7cdcddce8..9fa10fe6f 100644 --- a/cli/go.sum +++ b/cli/go.sum @@ -2,8 +2,6 @@ charm.land/bubbles/v2 v2.2.1 h1:Fq1+qm5hV6GkvzLQDhCBpXXE5tLgvh1PRriCLwSvIQU= charm.land/bubbles/v2 v2.2.1/go.mod h1:wdMgn+sje1KNXdwFizIWjbf328fIUBxqEmJ/vYPo8yc= charm.land/bubbletea/v2 v2.0.10 h1:oolvo20VBpI0PfqE7iFjkZ1bx0WpmXGfnKz5Yldjq5o= charm.land/bubbletea/v2 v2.0.10/go.mod h1:QOatcnhOjYIfxzUSTz6raF7Ex4R/rIuHa3SnBdCCpMc= -charm.land/glamour/v2 v2.0.1 h1:xl+r00A4aJWU0z8fgwKd9fQQ4rsphqGUzuEiXZP5n+c= -charm.land/glamour/v2 v2.0.1/go.mod h1:jo9z8XqVKPeEFMVdvCRLGk++RyJ3CdUwgNr7EvXLw3k= charm.land/lipgloss/v2 v2.0.6 h1:EaGKeuA8FvF+v2BT5VmZd2LoYLaMZJXA5n34th8nCIQ= charm.land/lipgloss/v2 v2.0.6/go.mod h1:ipDDJNSGa1hlwDtSfW1s2/xR8Vdhbut4PXh2zEKZd0Q= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= @@ -48,6 +46,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2/v2 v2.8.3 h1:4iBHVtmyAsQOOthVo+0v0RxktjLPDOTKx9Nuhf+PpD0= github.com/dlclark/regexp2/v2 v2.8.3/go.mod h1:avUrQvPaLz2DrFNHJF0taWAFFX2C1GMSSoeiqFjcBmU= +github.com/doug/termtex v0.0.0-20260918165034-c407d5cf5251 h1:E9oaEwpD8bHvNbTcNuS3ArwK+aAP3CVNzcJTIuARzNk= +github.com/doug/termtex v0.0.0-20260918165034-c407d5cf5251/go.mod h1:jmLFSaI+MNAXoomKqZk20dkVWzY/3DpFp8EdWe1wokg= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/jsonschema-go v0.4.3 h1:/DBOLZTfDow7pe2GmaJNhltueGTtDKICi8V8p+DQPd0= @@ -116,5 +116,7 @@ golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +tangled.org/okami.mom/glamour.git/v2 v2.0.0-20261013005126-4046973d5934 h1:hCNWCM4lsemGpMN+xByOSQn0kj+WpN1WgXI2WHhxY3Y= +tangled.org/okami.mom/glamour.git/v2 v2.0.0-20261013005126-4046973d5934/go.mod h1:jo9z8XqVKPeEFMVdvCRLGk++RyJ3CdUwgNr7EvXLw3k= tangled.org/okami.mom/ultraviolet v0.0.0-20261007174847-7bfbaab9c98a h1:RgAK+HDWiZ3fquSkDs5WjqkyJREqsUJNE2w7ZoHDu04= tangled.org/okami.mom/ultraviolet v0.0.0-20261007174847-7bfbaab9c98a/go.mod h1:D9SVXVpAsD1pP6xR6BiERbe4+dUU6EW9US1TFrNutGA= diff --git a/cli/internal/tui/ink.go b/cli/internal/tui/ink.go index e4345d158..5be2b22d6 100644 --- a/cli/internal/tui/ink.go +++ b/cli/internal/tui/ink.go @@ -33,6 +33,9 @@ type ink struct { brandFrom, brandTo string // bg is the terminal's background, which language colors adapt to. bg string + // heading, strong, inlineCode, link and math color markdown's marks, + // each a palette hue made legible on bg. + heading, strong, inlineCode, link, math string } // APCA contrast targets (Lc) for each step: about 60 suits content text, @@ -54,6 +57,9 @@ const ( secondaryLc = 45 decorLc = 22 busyLc = 30 + // headings and strong text stand out from prose; inline code, links + // and math step down to code's contrast + headingLc = 75 ) type rgb struct{ r, g, b float64 } @@ -175,12 +181,13 @@ type termColors struct { const ( ansiRed = 1 ansiGreen = 2 + ansiYellow = 3 ansiBlue = 4 ansiMagenta = 5 ansiCyan = 6 ) -var queriedPalette = []int{ansiRed, ansiGreen, ansiBlue, ansiMagenta, ansiCyan} +var queriedPalette = []int{ansiRed, ansiGreen, ansiYellow, ansiBlue, ansiMagenta, ansiCyan} // minTrustLc is the least contrast a reported text color may have with the // reported background. Below it the reply describes something other than @@ -224,9 +231,37 @@ func mixInk(c termColors) (ink, bool) { if hasCyan && hasBlue && hasMagenta { out.brandFrom, out.brandTo = cyan.hex(), blue.mix(magenta, 0.5).hex() } + markdownAccents(&out, c.palette, bg) return out, true } +// markdownAccents colors markdown from the palette: headings in the +// brand's violet, strong text in the yellow pulled toward red, inline code +// green, links blue and math cyan. +func markdownAccents(out *ink, palette map[int]rgb, bg rgb) { + accent := func(c rgb, target float64) string { return legible(c, bg, target).hex() } + red, hasRed := palette[ansiRed] + if yellow, ok := palette[ansiYellow]; ok { + if hasRed { + yellow = yellow.mix(red, 0.35) + } + out.strong = accent(yellow, headingLc) + } + blue, hasBlue := palette[ansiBlue] + if magenta, ok := palette[ansiMagenta]; ok && hasBlue { + out.heading = accent(blue.mix(magenta, 0.5), headingLc) + } + if hasBlue { + out.link = accent(blue, codeLc) + } + if green, ok := palette[ansiGreen]; ok { + out.inlineCode = accent(green, codeLc) + } + if cyan, ok := palette[ansiCyan]; ok { + out.math = accent(cyan, codeLc) + } +} + var oscColor = regexp.MustCompile(`\x1b\](10|11|4;(\d+));rgb:([0-9a-fA-F]{1,4})/([0-9a-fA-F]{1,4})/([0-9a-fA-F]{1,4})`) // parseColors reads OSC 10, 11 and 4 replies, whose channels carry one to diff --git a/cli/internal/tui/markdown.go b/cli/internal/tui/markdown.go index db1f9c208..7b96ec957 100644 --- a/cli/internal/tui/markdown.go +++ b/cli/internal/tui/markdown.go @@ -72,6 +72,10 @@ func codeTheme(colors ink) string { return name } +// accent is a markdown accent color, or the palette slot when the terminal +// did not report its colors. +func accent(hex, slot string) *string { return new(cmp.Or(hex, slot)) } + func markdownStyle() ansi.StyleConfig { style := styles.NoTTYStyleConfig zero, bold := uint(0), true @@ -81,9 +85,12 @@ func markdownStyle() ansi.StyleConfig { for _, heading := range []*ansi.StyleBlock{&style.H1, &style.H2, &style.H3, &style.H4, &style.H5, &style.H6} { heading.Prefix = "" heading.Bold = &bold + heading.Color = accent(transcriptInk.heading, "5") } - style.Strong = ansi.StylePrimitive{Bold: &bold} + style.Strong = ansi.StylePrimitive{Bold: &bold, Color: accent(transcriptInk.strong, "3")} style.Emph = ansi.StylePrimitive{Italic: &bold} + style.Code = ansi.StyleBlock{StylePrimitive: ansi.StylePrimitive{Color: accent(transcriptInk.inlineCode, "2")}} + style.LinkText = ansi.StylePrimitive{Color: accent(transcriptInk.link, "4")} style.Table.Margin = &zero style.BlockQuote.IndentToken = new(quoteBar()) style.CodeBlock.Margin = &zero @@ -177,6 +184,7 @@ func markdownRenderer(width int) (*glamour.TermRenderer, error) { } renderer, err := glamour.NewTermRenderer( glamour.WithStyles(markdownStyle()), glamour.WithWordWrap(width), glamour.WithPreservedNewLines(), + mathOptions(accent(transcriptInk.math, "6")), ) if err != nil { return nil, err @@ -242,10 +250,23 @@ func opensBlock(line string) bool { return line != "" && strings.IndexByte(" \t-*+>|", line[0]) < 0 && !orderedItem.MatchString(line) } -// fenceAfter is the code fence open after line, given the one open before. +// fenceAfter is the code fence or display math open after line, given the +// one open before. Display math is kept by its closer, $$ or \]. func fenceAfter(open, line string) string { + if open == "$$" || open == `\]` { + if strings.Contains(line, open) { + return "" + } + return open + } trimmed := strings.TrimLeft(line, " ") - if len(line)-len(trimmed) > 3 || trimmed == "" || (trimmed[0] != '`' && trimmed[0] != '~') { + if len(line)-len(trimmed) > 3 || trimmed == "" { + return open + } + if closer := displayMathLeftOpen(trimmed); open == "" && closer != "" { + return closer + } + if trimmed[0] != '`' && trimmed[0] != '~' { return open } run := trimmed[:len(trimmed)-len(strings.TrimLeft(trimmed, trimmed[:1]))] diff --git a/cli/internal/tui/markdown_test.go b/cli/internal/tui/markdown_test.go index 3ac1ac0d7..98d4b2196 100644 --- a/cli/internal/tui/markdown_test.go +++ b/cli/internal/tui/markdown_test.go @@ -89,6 +89,24 @@ para --- tail`, + `the roots are + +$$ +x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} + +\text{for } a \neq 0 +$$ +and inline $e^{i\pi} + 1 = 0$, but $5 and $HOME/$USER stay text. + +- a sum \(\sum_{i=1}^{n} i\) in a list + + \[ + \sum_{i=1}^{n} i = \frac{n(n+1)}{2} + \] + +> **bold $\alpha$** in a quote + +done`, } // Block by block, every prefix renders exactly as the whole prefix does, and diff --git a/cli/internal/tui/math.go b/cli/internal/tui/math.go new file mode 100644 index 000000000..1fa74577f --- /dev/null +++ b/cli/internal/tui/math.go @@ -0,0 +1,239 @@ +package tui + +import ( + "bytes" + "io" + "strings" + "unicode" + "unicode/utf8" + + "charm.land/glamour/v2" + "charm.land/glamour/v2/ansi" + "github.com/doug/termtex" + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/text" + "github.com/yuin/goldmark/util" +) + +// Math in a reply: $…$ and \(…\) inline, $$…$$ and \[…\] on lines of their +// own as display math. termtex lays both out in Unicode. +var ( + kindInlineMath = ast.NewNodeKind("InlineMath") + kindDisplayMath = ast.NewNodeKind("DisplayMath") +) + +type inlineMath struct { + ast.BaseInline + tex, source string +} + +func (n *inlineMath) Kind() ast.NodeKind { return kindInlineMath } +func (n *inlineMath) Dump(source []byte, level int) { ast.DumpHelper(n, source, level, nil, nil) } + +// displayMath is closed once its closing delimiter arrives; a reply still +// streaming may not have sent it yet. +type displayMath struct { + ast.BaseBlock + open, close string + tex []byte + closed bool +} + +func (n *displayMath) Kind() ast.NodeKind { return kindDisplayMath } +func (n *displayMath) Dump(source []byte, level int) { ast.DumpHelper(n, source, level, nil, nil) } + +// source is the math as written, with the closer once it arrived. +func (n *displayMath) source() string { + written := n.open + string(n.tex) + if n.closed { + written += n.close + } + return strings.TrimRight(written, "\n") +} + +type mathExtension struct{} + +func (mathExtension) Extend(m goldmark.Markdown) { + m.Parser().AddOptions( + parser.WithBlockParsers(util.Prioritized(displayMathParser{}, 650)), + parser.WithInlineParsers(util.Prioritized(inlineMathParser{}, 150)), + ) +} + +// mathOptions add the math parsers and render their nodes in color. +func mathOptions(color *string) glamour.TermRendererOption { + return glamour.WithOptions( + glamour.WithExtensions(mathExtension{}), + glamour.WithElement(kindInlineMath, func(node ast.Node, _ []byte, _ ansi.RenderContext) ansi.Element { + return ansi.Element{Renderer: inlineMathElement{node.(*inlineMath), color}} + }), + glamour.WithElement(kindDisplayMath, func(node ast.Node, _ []byte, _ ansi.RenderContext) ansi.Element { + return ansi.Element{Entering: "\n", Exiting: "\n", Renderer: displayMathElement{node.(*displayMath), color}} + }), + ) +} + +type inlineMathElement struct { + node *inlineMath + color *string +} + +func (e inlineMathElement) Render(w io.Writer, ctx ansi.RenderContext) error { + return e.StyleOverrideRender(w, ctx, ansi.StylePrimitive{}) +} + +// StyleOverrideRender keeps the math color inside bold or a link. Math that +// needs more than one row, such as a matrix, stays as its source. +func (e inlineMathElement) StyleOverrideRender(w io.Writer, ctx ansi.RenderContext, outer ansi.StylePrimitive) error { + outer.Color = e.color + laid, err := termtex.Render(e.node.tex, termtex.Style{Inline: true}) + if err != nil || strings.Contains(laid, "\n") { + laid = e.node.source + } + return (&ansi.BaseElement{Token: laid, Style: outer, Verbatim: true}).Render(w, ctx) +} + +type displayMathElement struct { + node *displayMath + color *string +} + +// Render lays the math out to the block's width. Until it closes, or when +// termtex cannot read it, the source shows instead. +func (e displayMathElement) Render(w io.Writer, ctx ansi.RenderContext) error { + laid := e.node.source() + if e.node.closed { + if out, err := termtex.Render(strings.TrimSpace(string(e.node.tex)), termtex.Style{Width: ctx.Width()}); err == nil { + laid = out + } + } + return (&ansi.BaseElement{Token: laid, Style: ansi.StylePrimitive{Color: e.color}, Verbatim: true}).Render(w, ctx) +} + +// mathCloser is the delimiter that closes math opened at the start of line, +// and the opener's length. +func mathCloser(line []byte) (string, int) { + switch { + case bytes.HasPrefix(line, []byte("$$")): + return "$$", 2 + case bytes.HasPrefix(line, []byte(`\[`)): + return `\]`, 2 + case bytes.HasPrefix(line, []byte(`\(`)): + return `\)`, 2 + case bytes.HasPrefix(line, []byte("$")): + return "$", 1 + } + return "", 0 +} + +// displayMathLeftOpen is the closer of display math that line opens and +// does not close, or "". +func displayMathLeftOpen(line string) string { + closer, width := mathCloser([]byte(line)) + if (closer != "$$" && closer != `\]`) || strings.Contains(line[width:], closer) { + return "" + } + return closer +} + +type displayMathParser struct{} + +func (displayMathParser) Trigger() []byte { return []byte{'$', '\\'} } + +// Open starts display math on a line that begins with $$ or \[ and, if it +// closes on the same line, has nothing after the closer. +func (displayMathParser) Open(_ ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) { + line, _ := reader.PeekLine() + pos := pc.BlockOffset() + if pos < 0 { + return nil, parser.NoChildren + } + closer, width := mathCloser(line[pos:]) + if closer != "$$" && closer != `\]` { + return nil, parser.NoChildren + } + node := &displayMath{open: string(line[pos : pos+width]), close: closer} + rest := line[pos+width:] + if end := bytes.Index(rest, []byte(closer)); end >= 0 { + if !util.IsBlank(rest[end+len(closer):]) { + return nil, parser.NoChildren + } + node.tex, node.closed = append(node.tex, rest[:end]...), true + } else { + node.tex = append(node.tex, rest...) + } + reader.AdvanceToEOL() + return node, parser.NoChildren +} + +func (displayMathParser) Continue(node ast.Node, reader text.Reader, _ parser.Context) parser.State { + n := node.(*displayMath) + if n.closed { + return parser.Close + } + line, _ := reader.PeekLine() + if end := bytes.Index(line, []byte(n.close)); end >= 0 { + n.tex, n.closed = append(n.tex, line[:end]...), true + reader.AdvanceToEOL() + return parser.Close + } + n.tex = append(n.tex, line...) + reader.AdvanceToEOL() + return parser.Continue | parser.NoChildren +} + +func (displayMathParser) Close(ast.Node, text.Reader, parser.Context) {} +func (displayMathParser) CanInterruptParagraph() bool { return true } +func (displayMathParser) CanAcceptIndentedLine() bool { return false } + +type inlineMathParser struct{} + +func (inlineMathParser) Trigger() []byte { return []byte{'$', '\\'} } + +func (inlineMathParser) Parse(_ ast.Node, block text.Reader, _ parser.Context) ast.Node { + line, _ := block.PeekLine() + tex, length := scanInlineMath(line, block.PrecendingCharacter()) + if length == 0 { + return nil + } + block.Advance(length) + return &inlineMath{tex: tex, source: string(line[:length])} +} + +// scanInlineMath is the math at the start of line and its length in bytes, +// or 0 when line does not start any. A lone $ follows Pandoc and goes +// further, so prices and shell variables stay text: it opens before a +// non-space that follows no word character, and closes after a non-space +// before anything but a word character. +func scanInlineMath(line []byte, before rune) (string, int) { + closer, width := mathCloser(line) + if closer == "" { + return "", 0 + } + if closer == "$" && (wordRune(before) || len(line) < 2 || unicode.IsSpace(rune(line[1]))) { + return "", 0 + } + for i := width; i < len(line) && line[i] != '\n'; i++ { + if !bytes.HasPrefix(line[i:], []byte(closer)) { + if closer == "$" && line[i] == '\\' { + i++ + } + continue + } + end := i + len(closer) + after, _ := utf8.DecodeRune(line[end:]) + if closer == "$" && (unicode.IsSpace(rune(line[i-1])) || wordRune(after)) { + continue + } + tex := strings.TrimSpace(string(line[width:i])) + if tex == "" { + return "", 0 + } + return tex, end + } + return "", 0 +} + +func wordRune(r rune) bool { return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' } diff --git a/cli/internal/tui/math_test.go b/cli/internal/tui/math_test.go new file mode 100644 index 000000000..bb6e3181d --- /dev/null +++ b/cli/internal/tui/math_test.go @@ -0,0 +1,33 @@ +// Inline math has to leave the dollar signs of prices and shell variables +// alone. Replies in e2e are scripted, so only this table pins which ones +// open math. +package tui + +import "testing" + +func TestScanInlineMath(t *testing.T) { + for _, tc := range []struct { + line string + before rune + tex string + length int + }{ + {line: "$E = mc^2$ holds", before: ' ', tex: "E = mc^2", length: 10}, + {line: `\(\alpha\) and`, before: ' ', tex: `\alpha`, length: 10}, + {line: "$$x$$ inline", before: ' ', tex: "x", length: 5}, + {line: `$a \$ b$`, before: ' ', tex: `a \$ b`, length: 8}, + {line: "$5 or $10", before: ' '}, + {line: "$HOME/$USER", before: ' '}, + {line: "$FOO$BAR", before: ' '}, + {line: "$x$", before: 'a'}, + {line: "$ x$", before: ' '}, + {line: "$x $", before: ' '}, + {line: "$x\n$", before: ' '}, + {line: "$$", before: ' '}, + } { + tex, length := scanInlineMath([]byte(tc.line), tc.before) + if tex != tc.tex || length != tc.length { + t.Errorf("%q after %q: got %q, %d; want %q, %d", tc.line, tc.before, tex, length, tc.tex, tc.length) + } + } +} diff --git a/default.nix b/default.nix index c62bee02d..42b9355ad 100644 --- a/default.nix +++ b/default.nix @@ -165,7 +165,7 @@ src = clientSource; modRoot = "cli"; subPackages = ["cmd/albedo"]; - vendorHash = "sha256-anejjYuFbxXkUn5L/jdNQXkR8oTBfv/9zjxCEZ/Qs9U="; + vendorHash = "sha256-O3t1orYEjxNFX7JQXVYsocslyw1bp4LhrSfgCC06HYU="; nativeBuildInputs = [python311 makeWrapper]; ALBEDO_NO_BROWSER = "1"; ALBEDO_TEST_DAEMON = "${server}/bin/albedo-daemon"; diff --git a/robot-docs/tui.md b/robot-docs/tui.md index cab741f6c..265fdeee2 100644 --- a/robot-docs/tui.md +++ b/robot-docs/tui.md @@ -76,6 +76,14 @@ each screen calls `registerShots("", states...)` from its `shot_ cover every state a person would want to look at: the list, a filter typed, no match, each confirm, loading, empty, load failed, a notice, a form open. look at the sheets after a layout change; text dumps hid a patchwork selection and a collapsed footer that the pictures showed at once. +## markdown + +replies render through glamour, from our fork `tangled.org/okami.mom/glamour` (`replace` in `cli/go.mod`; v2.0.1 plus hooks). `glamour.WithExtensions` adds goldmark parsers and `glamour.WithElement(kind, fn)` renders a node kind, new or built in; `ansi.RenderContext.Width()` is the room left inside the current block. upstream glamour has neither, and prints a warning to stdout for a node kind it does not know, which corrupts the screen. + +to add a construct, write its goldmark parser and node, then its element, in one file beside `markdown.go`, and pass both in `markdownRenderer`. `math.go` is the example: `$…$` and `\(…\)` inline, `$$…$$` and `\[…\]` on their own lines as display math, laid out in unicode by `termtex`. a lone `$` opens only before a non-space and after no word character, and closes only before no word character, so `$5` and `$HOME/$USER` stay text. display math that has not closed yet, or that termtex cannot read, shows as its source. `fenceAfter` treats open display math like a code fence, so streaming never splits it into blocks. use `ansi.BaseElement{Verbatim: true}` for laid-out text; without it glamour removes markdown escapes from the token. + +markdown's colors are ink accents (`markdownAccents` in `ink.go`), palette hues made legible on the background: headings violet, strong text yellow toward red, inline code green, links blue, math cyan. headings and strong text sit at a higher contrast than the others. without reported colors each falls back to its palette slot. + ## terminal output transcript rows carry private `albedo:` APC marks for copy, wrap joining and click targets. they stay in the chat's internal rows, but `AppModel.View` removes them from the final content before Bubble Tea sees it. ultraviolet preserves APC payloads; terminal.app prints unknown ones, which corrupts the layout. remove only our private marks, not styling, hyperlinks or graphics sequences. the real-daemon TUI driver's view assertion checks the raw content before `ansi.Strip` can hide a leak.