Documentation
¶
Overview ¶
Package tviewmd renders markdown to tview color-tagged text.
It parses CommonMark + GFM markdown (via goldmark) into a renderer-neutral block/segment model, then emits tview color tags directly — suitable for a tview.TextView with SetWrap(true). No ANSI round-trip is required.
The primary entry points are Render (parse + render) and RenderTView (render pre-parsed blocks). Callers that want to inspect or transform the document can use Parse to obtain the intermediate []Block representation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Render ¶
Render parses src and renders it to tview color-tagged text. It is the convenience entry point for callers that do not need the intermediate []Block.
Render is safe to call on each streaming update: it fully re-renders the accumulated buffer and escapes literal brackets, so incremental re-rendering never produces unbalanced or injected tview tags. For a buffered streaming accumulator see Renderer.
func RenderTView ¶
RenderTView renders parsed blocks to tview color-tagged text suitable for a tview.TextView configured with SetWrap(true). The output contains tview color tags ([fg:bg:flags] ... [-:-:-]); the TextView performs word-wrapping.
Types ¶
type Block ¶
type Block struct {
Kind BlockKind
Level int // heading level 1-6 (BlockHeading only)
Segments []Segment // paragraph, heading, blockquote inline content
Code CodeBlock // BlockCodeBlock only
Items []ListItem // BlockList only
Table TableData // BlockTable only
}
Block is one block-level element. The field matching Kind is populated; the others are zero-valued.
type BlockKind ¶
type BlockKind int
BlockKind enumerates block-level markdown elements.
const ( // BlockParagraph is a run of inline content (text + emphasis + code + links). BlockParagraph BlockKind = iota // BlockHeading is a level 1-6 heading. Level holds the depth. BlockHeading // BlockCodeBlock is a fenced or indented code block. Code holds language+source. BlockCodeBlock // BlockList is an ordered or unordered list. Items holds the entries. BlockList // BlockBlockquote is a quoted block. Segments holds the flattened inline content. BlockBlockquote // BlockTable is a GFM table. Table holds header, rows, and column alignments. BlockTable // BlockThematicBreak is a horizontal rule. BlockThematicBreak )
type CodeBlock ¶
type CodeBlock struct {
Lang string // language hint from the info string (may be empty)
Source string
}
CodeBlock holds a fenced or indented code block.
type ListItem ¶
type ListItem struct {
Segments []Segment
Children []ListItem // nested list items
Ordered bool
// Index is the 1-based ordinal for ordered items.
Index int
}
ListItem is one entry in a List block.
type Options ¶
type Options struct {
// Width is used for horizontal-rule length and table column sizing. 0 = 80.
Width int
// Theme is the color theme. The zero value uses DefaultTheme().
Theme Theme
}
Options configure rendering. The zero value renders with the default theme and an 80-column width for rules and tables.
type Renderer ¶ added in v0.2.0
type Renderer struct {
// contains filtered or unexported fields
}
Renderer incrementally renders a markdown stream to tview color-tagged text.
It is the streaming-oriented counterpart to Render: callers Append (or Write) text as it arrives — e.g. from an LLM token stream — and call String on a throttled tick to obtain the current fully-rendered document for a tview.TextView.SetText call.
Unlike a naive line-splitting approach, String renders the entire accumulated buffer through the same Parse+RenderTView pipeline as Render. Markdown is block-oriented (a paragraph, list, or code fence is only complete once its closing construct arrives), so any line- or block-boundary split can render the settled portion differently from a final full render and produces a visible raw→formatted flash. Rendering the whole buffer avoids both; a trailing unclosed construct (an open code fence, a "**" mid-emphasis) is handled gracefully by goldmark and resolves as soon as its closer streams in. The Parse+RenderTView pipeline is sub-millisecond for typical LLM responses, so full re-render per flush is cheap; repeated calls without new input return the cached output.
A Renderer is not safe for concurrent use. Drive it from a single goroutine — in a tview app, from a QueueUpdateDraw/QueueUpdate callback.
func NewRenderer ¶ added in v0.2.0
NewRenderer returns a Renderer that renders with the given Options.
func (*Renderer) Append ¶ added in v0.2.0
Append adds markdown source to the stream and marks the rendered output stale.
func (*Renderer) Reset ¶ added in v0.2.0
func (r *Renderer) Reset()
Reset discards all accumulated source and rendered output.
type Segment ¶
type Segment struct {
Text string
Style Style
// Link, when non-empty, marks Text as the link label and Link as the URL.
Link string
// Code marks the segment as inline code. Renderers may use a distinct
// background/foreground for code spans.
Code bool
}
Segment is a single styled inline run of text. Exactly one rendering hint applies: Code, Link, or a plain Style.
type Style ¶
type Style struct {
FG string
BG string
Bold bool
Italic bool
Underline bool
Strikethrough bool
Dim bool
}
Style is renderer-neutral inline formatting. Empty color fields mean "inherit the surrounding style"; the renderer maps the flags to its native emphasis syntax.
type TableData ¶
type TableData struct {
Header []TableCell
Rows [][]TableCell
// Aligns is the per-column alignment declared in the delimiter row.
Aligns []TextAlign
}
TableData holds a parsed GFM table.
type Theme ¶
type Theme struct {
// Heading colors per level (index 0 = H1). Used by the tview backend.
Heading [6]string
Link string
InlineCodeFG string
InlineCodeBG string
CodeBlockFG string
QuoteFG string
Hr string
}
Theme holds the color scheme used by renderers. Colors are expressed in any form the target backend understands (tview accepts named colors like "red", hex like "#ff8c42", and "default").
func DefaultTheme ¶
func DefaultTheme() Theme
DefaultTheme returns a dark-terminal-friendly theme tuned for readability on a black/default background.