Documentation
¶
Overview ¶
Package markdown provides a high-performance markdown renderer for terminal output. This is a custom implementation optimized for speed, replacing glamour for TUI rendering.
Index ¶
Constants ¶
const CodeBlockCopyIcon = "\u2398"
CodeBlockCopyIcon is the unicode glyph rendered at the top-right corner of every fenced code block. Clicking on it copies the block's raw content to the clipboard. It matches the glyph used for the message-level copy affordance so the visual language stays consistent; the two are disambiguated by line index, not by glyph.
Variables ¶
This section is empty.
Functions ¶
func ResetStyles ¶
func ResetStyles()
ResetStyles resets the cached markdown styles so they will be rebuilt on next use. Call this when the theme changes to pick up new colors.
Types ¶
type CodeBlock ¶ added in v1.59.0
CodeBlock describes a fenced code block emitted by the renderer.
Line is the 0-indexed line, within the renderer's output, where the copy icon is rendered on the code block's top padding row. Content holds the raw code (without ANSI styling) so callers can place it on the clipboard.
type FastRenderer ¶
type FastRenderer struct {
// contains filtered or unexported fields
}
FastRenderer is a high-performance markdown renderer optimized for terminal output. It directly parses and renders markdown without building an intermediate AST.
func NewFastRenderer ¶
func NewFastRenderer(width int) *FastRenderer
NewFastRenderer creates a new fast markdown renderer with the given width.
func (*FastRenderer) Render ¶
func (r *FastRenderer) Render(input string) (string, error)
Render parses and renders markdown content to styled terminal output.
func (*FastRenderer) RenderWithCodeBlocks ¶ added in v1.59.0
func (r *FastRenderer) RenderWithCodeBlocks(input string) (string, []CodeBlock, error)
RenderWithCodeBlocks renders markdown content and returns both the styled terminal output and the list of fenced code blocks emitted, in document order. Each entry's Line points at the rendered line that carries the clickable copy label for that block.
type IncrementalRenderer ¶ added in v1.59.0
type IncrementalRenderer struct {
// contains filtered or unexported fields
}
IncrementalRenderer is a markdown renderer specialized for streaming use: it remembers the most recently rendered "stable prefix" of the input and, when the next call extends that prefix, re-renders only the new trailing region instead of the whole document.
A "stable prefix" here is the portion of the input ending at the last block boundary that the underlying FastRenderer is guaranteed to render the same way regardless of what comes after it: a blank line that lies outside any fenced code block and is not flanked by list or blockquote lines (those constructs absorb blank lines into a single block, so the surrounding content is not yet "frozen").
The optimization is correct because the FastRenderer (and CommonMark in general) treats such blank lines as hard breaks between top-level blocks: rendering the document up to a stable boundary and rendering the rest independently produces the same output as rendering the full document, modulo the blank-line separator that joinPrefixAndTail re-inserts.
IncrementalRenderer is not safe for concurrent use.
func NewIncrementalRenderer ¶ added in v1.59.0
func NewIncrementalRenderer(width int) *IncrementalRenderer
NewIncrementalRenderer creates a new incremental renderer with the given terminal width.
func (*IncrementalRenderer) Render ¶ added in v1.59.0
func (r *IncrementalRenderer) Render(input string) (string, error)
Render produces styled terminal output for input. When successive calls pass inputs that share a long common prefix (the streaming case), only the suffix is parsed and rendered; the rest is served from the cached output.
func (*IncrementalRenderer) RenderWithCodeBlocks ¶ added in v1.59.0
func (r *IncrementalRenderer) RenderWithCodeBlocks(input string) (string, []CodeBlock, error)
RenderWithCodeBlocks behaves like Render but additionally returns the list of fenced code blocks in the rendered output. Each entry's Line is the 0-indexed line within the returned string where the block's copy label is drawn.
func (*IncrementalRenderer) Reset ¶ added in v1.59.0
func (r *IncrementalRenderer) Reset()
Reset drops the cached prefix without changing the width. Use when the underlying message is replaced by an unrelated new one.
func (*IncrementalRenderer) SetWidth ¶ added in v1.59.0
func (r *IncrementalRenderer) SetWidth(width int)
SetWidth updates the renderer width. Width changes invalidate the cache because the rendered output is width-dependent.