Documentation
¶
Overview ¶
Package markdown wraps Glamour for cached markdown-to-ANSI rendering with width-based revalidation, falling back to plain text wrapping for narrow terminals.
RenderContent returns styled lines for existing document/issue/preview consumers. RenderMapped is an adjacent result: the same lines plus source anchors derived from goldmark block positions and wrap math, so a click or scroll offset can be mapped back to a source line without parsing ANSI.
This package is the single owner of the Sidecar-palette-to-Glamour mapping (see theme.go). Markdown colors are derived from the normalized semantic palette, fenced code uses the palette's SyntaxTheme, and markdownTheme selects the structural dark/light preset — or, for any other value, an explicit externally owned full-style override. Consumers that keep their own render or layout caches must include Renderer.StyleKey in their cache key; they must not read palette fields or build Markdown styles themselves.
Index ¶
Constants ¶
const ( // MinWidthForMarkdown is the minimum terminal width for markdown rendering. // Below this, falls back to plain text wrapping. MinWidthForMarkdown = 30 // MaxCacheEntries is the maximum number of cached renders before eviction. MaxCacheEntries = 100 )
Variables ¶
This section is empty.
Functions ¶
func BuildStyle ¶ added in v1.2.0
func BuildStyle(snapshot ThemeSnapshot) (ansi.StyleConfig, string)
BuildStyle returns the Glamour style config derived from the snapshot along with its style key.
func CompactDocument ¶ added in v1.2.0
func CompactDocument(r *Renderer)
CompactDocument drops Glamour's document margin and block prefix/suffix. Hosts that already pad the pane (Notes) use this so the body origin matches a plain wrap of the same width — entering edit then changes the cursor, not the frame.
func RendersMarkdownAt ¶ added in v1.5.0
RendersMarkdownAt reports whether a render at this width goes through Glamour rather than the plain-wrap fallback. Every entry point in this package applies the same gate; it is exported because a caller holding the output sometimes has to know which of the two it got. The content-link RendererOwned trust bit is the case that matters: below this width the "rendered" view is the document's own bytes, wrapped, and an escape sequence in it is authored content rather than something this package chose to emit.
Types ¶
type Anchor ¶ added in v1.0.3
type Anchor struct {
SourceLine int // 0-based source line
SourceCol int // 0-based rune offset on that line
BlockStart int // 0-based first source line of the containing block
Precise bool // line-accurate when true
}
Anchor maps one rendered visual row back to a source location. Precise is true when SourceLine is line-accurate (ordinary paragraphs and headings via wrap math). Tables, fences, and other heavily reshaped constructs degrade to the top of their block with Precise=false.
type MappedRender ¶ added in v1.0.3
MappedRender is a rendered markdown document plus a parallel source map. Lines[i] is what the user sees; Anchors[i] says which source line produced it.
func MapWrappedSource ¶ added in v1.0.3
func MapWrappedSource(content string, width int) MappedRender
MapWrappedSource wraps each source line at width and maps every visual row back to that source line plus the starting column of the wrap segment. This is the raw-view / narrow-fallback visual-row policy: always wrap, never truncate.
func (MappedRender) At ¶ added in v1.0.3
func (m MappedRender) At(visualRow int) Anchor
At returns the source anchor for a visual row. Out-of-range rows clamp to the nearest mapped line.
func (MappedRender) Click ¶ added in v1.0.3
func (m MappedRender) Click(scrollOff, viewRow int) Anchor
Click maps a viewport-relative row plus the view's scroll offset to source.
func (MappedRender) VisualRowForSource ¶ added in v1.0.3
func (m MappedRender) VisualRowForSource(sourceLine, sourceCol int) int
VisualRowForSource returns the first visual row whose source position is at or before (sourceLine, sourceCol). Used when leaving edit so the rendered view can recover the line that was being edited.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer wraps Glamour for markdown rendering with caching.
func NewRenderer ¶
func NewRenderer(opts ...RendererOption) (*Renderer, error)
NewRenderer creates a new markdown renderer instance.
func (*Renderer) CompactsDocument ¶ added in v1.3.0
CompactsDocument reports whether this renderer drops Glamour's document margin and block prefix/suffix (see CompactDocument). Viewers that pad their own frames ask this before adopting an injected renderer: pairing their padding with a non-compact renderer compounds two insets onto one width and wraps the body well before the frame's right edge. Such a viewer builds its own compact renderer instead of flipping this one — the instance may be shared with viewers (docview) whose inset IS Glamour's margin.
func (*Renderer) RenderContent ¶
RenderContent renders markdown content to styled lines using the current theme snapshot.
func (*Renderer) RenderMapped ¶ added in v1.0.3
func (r *Renderer) RenderMapped(content string, width int) MappedRender
RenderMapped renders markdown and a source map for the same width. Existing RenderContent consumers are unchanged; this is an adjacent result.
type RendererOption ¶ added in v1.2.0
type RendererOption func(*Renderer)
RendererOption configures a Renderer at construction. Options are applied once; the resulting renderer is safe for concurrent use.
type ThemeSnapshot ¶ added in v1.2.0
type ThemeSnapshot struct {
Palette styles.ColorPalette
}
ThemeSnapshot is an immutable capture of the theme inputs that affect Markdown rendering. Take one per render operation so a concurrent theme change cannot mix one palette's cache key with another palette's style.
func CurrentThemeSnapshot ¶ added in v1.2.0
func CurrentThemeSnapshot() ThemeSnapshot
CurrentThemeSnapshot captures the active palette. ApplyThemeColors stores an already-normalized palette, so this must not re-normalize: NormalizePalette mutates shared slices and is not safe to call concurrently.
func (ThemeSnapshot) StyleKey ¶ added in v1.2.0
func (s ThemeSnapshot) StyleKey() string
StyleKey is a stable identity for the Glamour style a snapshot produces. It covers every effective input: palette colors, syntax theme, markdown theme, and the resolved contents of an explicit full-style file.