Documentation
¶
Overview ¶
Package render provides terminal-friendly markdown and diff rendering.
Markdown is rendered via glamour with a cached renderer keyed on (width, style) for one-shot CLI output. MarkdownRenderer provides a stateful cache keyed by caller identity and width for TUIs that repaint stable documents while background refreshes may change their content. Diffs are highlighted with chroma using a unified diff lexer. All renderers in this package are safe for concurrent use.
Index ¶
- func ColorToken(c color.Color) string
- func Diff(text string) string
- func DiffStyled(text string, opts DiffOptions) string
- func DiffWithDelta(text string, opts DiffOptions) (string, error)
- func Markdown(text string, width int, style string) string
- func StyleFromPalette(p MarkdownPalette) ansi.StyleConfig
- type Background
- type DiffOptions
- type MarkdownOption
- type MarkdownPalette
- type MarkdownRenderer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColorToken ¶
ColorToken converts a color into the token format glamour style configs use.
ANSI palette indexes pass through as their index string, such as "4" or "212". Round-tripping an indexed color through RGBA bakes in the standard VGA value and visibly drifts from themed UI rendered around it.
func Diff ¶
Diff highlights a unified diff string using chroma syntax highlighting. Returns the input unchanged if highlighting fails.
func DiffStyled ¶
func DiffStyled(text string, opts DiffOptions) string
DiffStyled renders a diff using delta when available, falling back to Diff. When RepoURL and CommitSHA are set, delta file links point at that GitHub blob.
func DiffWithDelta ¶
func DiffWithDelta(text string, opts DiffOptions) (string, error)
DiffWithDelta renders a diff through delta. It returns an error when delta is unavailable or execution fails.
func Markdown ¶
Markdown renders markdown text for terminal display using glamour. style is a glamour style name ("dracula", "dark", "light", "notty"). Falls back to indented plain text on error or when width is zero. Safe for concurrent use.
func StyleFromPalette ¶
func StyleFromPalette(p MarkdownPalette) ansi.StyleConfig
StyleFromPalette derives a glamour style from p.
The derived style clears document margins because panes own their padding, removes filled backgrounds from H1 and inline code so theme color does not create UI blocks, and pins list/bold/emphasis colors to Text so glamour defaults do not leak into themed prose.
Types ¶
type Background ¶
type Background string
Background selects the light or dark glamour base used by StyleFromPalette.
const ( // BackgroundDark uses glamour's dark defaults as the markdown base. BackgroundDark Background = "dark" // BackgroundLight uses glamour's light defaults as the markdown base. BackgroundLight Background = "light" )
type DiffOptions ¶
type DiffOptions struct {
// DeltaBin is the path to the delta binary. When empty, DiffStyled will
// try to find delta on PATH.
DeltaBin string
// RepoURL enables GitHub blob hyperlinks in delta output when set together
// with CommitSHA, e.g. "https://github.com/owner/repo".
RepoURL string
// CommitSHA is the commit used for blob hyperlinks in delta output.
CommitSHA string
}
DiffOptions configures optional external diff rendering.
type MarkdownOption ¶
type MarkdownOption func(*MarkdownRenderer)
MarkdownOption configures a MarkdownRenderer.
func WithMaxCacheEntries ¶
func WithMaxCacheEntries(n int) MarkdownOption
WithMaxCacheEntries caps both rendered-output and per-width glamour renderer caches. On overflow the affected cache is reset wholesale because resize storms produce many cheap-to-rebuild widths.
func WithTrimPadding ¶
func WithTrimPadding(trim bool) MarkdownOption
WithTrimPadding controls whether Render trims glamour's outer newlines. Panes usually own their spacing, so trimming is enabled by default.
type MarkdownPalette ¶
type MarkdownPalette struct {
Base Background
Text color.Color
Heading color.Color
H1 color.Color
Link color.Color
Code color.Color
Dim color.Color
}
MarkdownPalette carries the small set of app-theme colors needed to derive a glamour style while leaving code-block chroma and unlisted elements on the selected light or dark base.
type MarkdownRenderer ¶
type MarkdownRenderer struct {
// contains filtered or unexported fields
}
MarkdownRenderer renders markdown with identity-keyed, content-invalidating caches for repaint-heavy terminal UIs.
Example ¶
package main
import (
"fmt"
"image/color"
"github.com/gechr/primer/render"
)
func main() {
style := render.StyleFromPalette(render.MarkdownPalette{
Base: render.BackgroundDark,
Text: color.RGBA{R: 0xee, G: 0xee, B: 0xee, A: 0xff},
Heading: color.RGBA{R: 0x7a, G: 0xdf, B: 0xd6, A: 0xff},
H1: color.RGBA{R: 0xff, G: 0xb8, B: 0x6c, A: 0xff},
Link: color.RGBA{R: 0x8a, G: 0xb4, B: 0xf8, A: 0xff},
Code: color.RGBA{R: 0xff, G: 0x79, B: 0xc6, A: 0xff},
Dim: color.RGBA{R: 0x88, G: 0x88, B: 0x88, A: 0xff},
})
renderer := render.NewMarkdownRenderer(style)
out := renderer.Render("issue-123", 72, "# Summary\n\nRepeated dashboard content.")
fmt.Println(out != "")
}
Output: true
func NewMarkdownRenderer ¶
func NewMarkdownRenderer(style ansi.StyleConfig, opts ...MarkdownOption) *MarkdownRenderer
NewMarkdownRenderer creates a renderer for repeated markdown rendering.
The renderer is safe for concurrent use. It serializes access because glamour renderers keep internal buffers; the lock cost is negligible beside markdown parsing and ANSI rendering.
func (*MarkdownRenderer) Render ¶
func (r *MarkdownRenderer) Render(id string, width int, md string) string
Render returns md rendered at width, cached under (id, width), and invalidated when md's content hash changes.
Widths smaller than one are clamped so Bubble Tea resize transients still render something. Glamour construction or render errors return the raw markdown because stale text is better than a blank pane.