markdown

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package markdown provides markdown parsing and rendering for terminal UIs. It uses goldmark for parsing and custom rendering to the compositor system.

Index

Constants

This section is empty.

Variables

View Source
var DoubleBoxDrawings = BoxDrawings{
	Horizontal:  "═",
	Vertical:    "║",
	TopLeft:     "╔",
	TopRight:    "╗",
	BottomLeft:  "╚",
	BottomRight: "╝",
	LeftT:       "╠",
	RightT:      "╣",
	TopT:        "╦",
	BottomT:     "╩",
	Cross:       "╬",
}

DoubleBoxDrawings uses double lines

View Source
var HeavyBoxDrawings = BoxDrawings{
	Horizontal:  "━",
	Vertical:    "┃",
	TopLeft:     "┏",
	TopRight:    "┓",
	BottomLeft:  "┗",
	BottomRight: "┛",
	LeftT:       "┣",
	RightT:      "┫",
	TopT:        "┳",
	BottomT:     "┻",
	Cross:       "╋",
}

HeavyBoxDrawings uses heavy lines for emphasis

View Source
var RoundedBoxDrawings = BoxDrawings{
	Horizontal:  "─",
	Vertical:    "│",
	TopLeft:     "╭",
	TopRight:    "╮",
	BottomLeft:  "╰",
	BottomRight: "╯",
	LeftT:       "├",
	RightT:      "┤",
	TopT:        "┬",
	BottomT:     "┴",
	Cross:       "┼",
}

RoundedBoxDrawings uses rounded corners for a softer look

View Source
var SharpBoxDrawings = BoxDrawings{
	Horizontal:  "─",
	Vertical:    "│",
	TopLeft:     "┌",
	TopRight:    "┐",
	BottomLeft:  "└",
	BottomRight: "┘",
	LeftT:       "├",
	RightT:      "┤",
	TopT:        "┬",
	BottomT:     "┴",
	Cross:       "┼",
}

SharpBoxDrawings uses sharp corners for a technical look

Functions

func MergeStyle

func MergeStyle(base, inline compositor.Style) compositor.Style

MergeStyle combines a base style with inline style attributes.

func Walk

func Walk(node ast.Node, fn WalkFunc) error

Walk traverses the AST tree calling fn for each node.

Types

type BoxDrawings

type BoxDrawings struct {
	Horizontal  string
	Vertical    string
	TopLeft     string
	TopRight    string
	BottomLeft  string
	BottomRight string
	LeftT       string
	RightT      string
	TopT        string
	BottomT     string
	Cross       string
}

Box drawing characters for table borders

type EnhancedTable

type EnhancedTable struct {
	Rows       []TableRow
	Columns    int
	ColWidths  []int
	ColAligns  []TableAlignment
	TotalWidth int
}

EnhancedTable represents a parsed markdown table with layout info

func ParseTable

func ParseTable(table *extast.Table, source []byte) *EnhancedTable

ParseTable parses a goldmark AST table into an EnhancedTable

func (*EnhancedTable) CalculateWidths

func (t *EnhancedTable) CalculateWidths(config TableRendererConfig, maxWidth int)

CalculateWidths computes optimal column widths

func (*EnhancedTable) Render

func (t *EnhancedTable) Render(config TableRendererConfig) []StyledLine

Render renders the table to styled lines

type EnhancedTableRenderer

type EnhancedTableRenderer struct {
	// contains filtered or unexported fields
}

EnhancedTableRenderer handles rich table rendering

func NewEnhancedTableRenderer

func NewEnhancedTableRenderer(config TableRendererConfig) *EnhancedTableRenderer

NewEnhancedTableRenderer creates a new table renderer

func (*EnhancedTableRenderer) RenderTable

func (r *EnhancedTableRenderer) RenderTable(table *extast.Table, source []byte, maxWidth int) []StyledLine

RenderTable renders a markdown table with enhanced formatting

type Highlighter

type Highlighter struct {
	// contains filtered or unexported fields
}

Highlighter applies syntax highlighting to fenced code blocks.

func NewHighlighter

func NewHighlighter(t *theme.Theme) *Highlighter

NewHighlighter returns a theme-aware highlighter.

func (*Highlighter) Highlight

func (h *Highlighter) Highlight(code, language string, cfg *StyleConfig) []StyledLine

Highlight tokenizes and styles code into markdown lines.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser wraps goldmark for markdown parsing.

func NewParser

func NewParser() *Parser

NewParser creates a new markdown parser with common extensions enabled.

func (*Parser) Parse

func (p *Parser) Parse(source []byte) ast.Node

Parse parses markdown source and returns the AST root.

func (*Parser) ParseString

func (p *Parser) ParseString(source string) ast.Node

ParseString is a convenience method for parsing string input.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer turns markdown into styled lines for the TUI.

func NewRenderer

func NewRenderer(t *theme.Theme) *Renderer

NewRenderer creates a renderer using the provided theme.

func (*Renderer) CodeBlockBackground

func (r *Renderer) CodeBlockBackground() compositor.Style

CodeBlockBackground returns the default code block background style.

func (*Renderer) Render

func (r *Renderer) Render(source, content string) []StyledLine

Render parses markdown and returns styled lines for the given source.

func (*Renderer) RenderAST

func (r *Renderer) RenderAST(source string, root ast.Node, content string) []StyledLine

RenderAST renders a pre-parsed markdown AST.

func (*Renderer) RenderASTWidth

func (r *Renderer) RenderASTWidth(source string, root ast.Node, content string, maxWidth int) []StyledLine

RenderASTWidth renders a pre-parsed AST with width-sensitive blocks constrained to maxWidth.

func (*Renderer) RenderWidth

func (r *Renderer) RenderWidth(source, content string, maxWidth int) []StyledLine

RenderWidth parses markdown and constrains width-sensitive blocks such as tables to maxWidth. A non-positive width keeps their natural size.

type StyleConfig

type StyleConfig struct {
	// Headers
	H1 compositor.Style
	H2 compositor.Style
	H3 compositor.Style
	H4 compositor.Style
	H5 compositor.Style
	H6 compositor.Style

	// Inline styles
	Bold          compositor.Style
	Italic        compositor.Style
	BoldItalic    compositor.Style
	Code          compositor.Style
	Strikethrough compositor.Style
	Link          compositor.Style
	LinkURL       compositor.Style

	// Block elements
	Blockquote       compositor.Style
	BlockquoteBorder compositor.Style
	ListBullet       compositor.Style
	ListNumber       compositor.Style
	HorizontalRule   compositor.Style

	// Code blocks
	CodeBlockBorder     compositor.Style
	CodeBlockBG         compositor.Style
	CodeBlockLang       compositor.Style
	CodeBlockLineNumber compositor.Style

	// Tables
	TableHeader compositor.Style
	TableCell   compositor.Style
	TableBorder compositor.Style

	// Base text
	Text compositor.Style
}

StyleConfig maps markdown elements to compositor styles.

func DefaultStyleConfig

func DefaultStyleConfig(t *theme.Theme) *StyleConfig

DefaultStyleConfig returns style configuration based on the theme.

func (*StyleConfig) WithBaseText

func (c *StyleConfig) WithBaseText(base compositor.Style) *StyleConfig

WithBaseText returns a copy of the config with base text styles overridden.

type StyledLine

type StyledLine struct {
	Spans                  []StyledSpan
	Prefix                 []StyledSpan // Repeated prefix for wrapped lines (e.g., list bullets)
	Indent                 int          // Indentation level for nested structures
	IsCode                 bool         // Inside a code block
	IsCodeHeader           bool         // Header line for a code block
	Anchor                 string       // Anchor id for headings
	HeadingLevel           int          // Heading level (1-6)
	Language               string       // Language for syntax highlighting
	CodeLineNumberWidth    int          // Width of the code line number gutter
	CodeLineNumberOptional bool         // Line numbers appear on hover for short blocks
	BlankLine              bool         // Empty line for spacing
}

StyledLine represents a line composed of styled spans.

func Blank

func Blank() StyledLine

Blank creates an empty line for spacing.

func Plain

func Plain(text string, style compositor.Style) StyledLine

Plain creates a StyledLine from plain text with default style.

type StyledSpan

type StyledSpan struct {
	Text  string
	Style compositor.Style
}

StyledSpan represents a span of text with consistent styling.

type TableAlignment

type TableAlignment int

TableAlignment represents cell alignment

const (
	AlignLeft TableAlignment = iota
	AlignCenter
	AlignRight
)

type TableCell

type TableCell struct {
	Text      string
	Alignment TableAlignment
	IsHeader  bool
	Width     int // Computed width
}

TableCell represents a single cell in a table

type TableRendererConfig

type TableRendererConfig struct {
	BoxDrawings    BoxDrawings
	HeaderStyle    compositor.Style
	CellStyle      compositor.Style
	BorderStyle    compositor.Style
	Padding        int
	MinColumnWidth int
}

TableRendererConfig configures the table rendering

func DefaultTableRendererConfig

func DefaultTableRendererConfig(t *theme.Theme) TableRendererConfig

DefaultTableRendererConfig returns a default configuration

func TableStyleCompact

func TableStyleCompact(t *theme.Theme) TableRendererConfig

Alternative styles for different contexts

func TableStyleHeavy

func TableStyleHeavy(t *theme.Theme) TableRendererConfig

func TableStyleMinimal

func TableStyleMinimal(t *theme.Theme) TableRendererConfig

type TableRow

type TableRow struct {
	Cells    []TableCell
	IsHeader bool
}

TableRow represents a row in a table

type WalkFunc

type WalkFunc func(node ast.Node, entering bool) (ast.WalkStatus, error)

WalkFunc is called for each node during tree traversal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL