markdown

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultReveal = Reveal{}

DefaultReveal is the Reveal of Stream without Options.Reveal.

Functions

func DefaultCodeBlock

func DefaultCodeBlock(c *ui.Context, b *CodeBlockView) ui.Element

DefaultCodeBlock builds a code block as views do without hooks: a box with the language and a copy button above the highlighted code.

func FileImages

func FileImages(dir string) func(url string) (ui.ImageSource, error)

FileImages returns a LoadImage for the files of directory dir, which paths without a scheme are relative to: PNG, JPEG, GIF, WebP, BMP and SVG. Other addresses do not load.

func Slug

func Slug(text string) string

Slug returns the anchor of a heading of text, as GitHub makes it: in lower case, spaces as hyphens, without punctuation.

func Stream

func Stream(c *ui.Context, src string, o *Options) ui.Element

Stream shows src, Markdown still being written, as a language model's reply: text appended to src since the last frame is parsed alone, the end shows what its half-written syntax will most likely become (see Document.SetStreaming), and text comes in as Options.Reveal says, smoothly, fading in. Show the message with View once it is complete, in the same place, so that its parse carries on; what is still being revealed then still comes in.

if msg.done {
	markdown.View(c, msg.text, &app.md)
} else {
	markdown.Stream(c, msg.text, &app.md)
}

func View

func View(c *ui.Context, src string, o *Options) ui.Element

View shows src, finished Markdown, as it reads: CommonMark and what Options.Extensions add, parsed again only where src changed since the last frame. Its text is selectable, copied as text, as HTML, which keeps its formatting where it is pasted (a rich text editor, mail, a word processor), and as Markdown (transfer.Markdown), or, with the context menu's Copy as Markdown (Shift+Cmd+C, Shift+Ctrl+C on Linux and Windows), as Markdown's text. o may be nil.

markdown.View(c, app.readme, &app.md)

Types

type CodeBlockView

type CodeBlockView struct {
	// Language is the language its fence names, as "go", and Info the
	// whole info string after the fence.
	Language, Info string
	// Open tells a block still being written, whose closing fence has not
	// come: heavy renderers, as of diagrams, wait for it to close.
	Open bool
	// Index is the block's in the Document.
	Index int
	// contains filtered or unexported fields
}

CodeBlockView is a code block as Options.CodeBlock and Options.Code build it.

func (*CodeBlockView) Body

func (b *CodeBlockView) Body(c *ui.Context) ui.Element

Body builds the block's code, highlighted, selectable with the view's text, scrolling sideways unless Options.WrapCode.

func (*CodeBlockView) Code

func (b *CodeBlockView) Code() string

Code returns the block's code, its lines ending with newlines.

func (*CodeBlockView) CopyButton

func (b *CodeBlockView) CopyButton(c *ui.Context) ui.Element

CopyButton builds a button copying the block's code, which shows that it did for two seconds, and waits for a block being written to close.

type Color

type Color = ui.Color

Color is a color of ui.

type HeadingStyle

type HeadingStyle struct {
	// Size is their size in ems, and Weight their weight.
	Size   float32
	Weight int
	// Color colors them: Theme.Text when zero.
	Color Color
	// Top and Bottom are the room above and below them in ems, which
	// replaces Theme.BlockSpacing around them.
	Top, Bottom float32
	// Rule draws a line below them, as GitHub does below headings of
	// levels 1 and 2.
	Rule bool
}

HeadingStyle is how headings of one level look.

type Options

type Options struct {
	// Theme is the look: DefaultTheme of the window's theme when nil,
	// whose values those the theme leaves zero take.
	Theme *Theme
	// Extensions is the syntax read beyond CommonMark: Default when zero.
	Extensions parser.Extensions

	// OnLink, when set, handles a click on a link, given its destination.
	// Without it, links to http, https and mailto addresses open as a
	// ui.Link opens them, a path without a scheme goes there in the
	// router around the view, a link to #a-heading scrolls to that
	// heading, and links of other schemes do nothing.
	OnLink func(url string)
	// LoadImage loads the image at url, on a goroutine of its own, for the
	// views to show; until it has, they show its description. Without
	// it, images show as their descriptions. FileImages loads files.
	LoadImage func(url string) (ui.ImageSource, error)

	// CodeBlock, when set, builds code blocks in place of DefaultCodeBlock,
	// which it can call to wrap it; Code builds the code blocks of the
	// languages it lists, as "mermaid", in place of either.
	CodeBlock func(c *ui.Context, b *CodeBlockView)
	Code      map[string]func(c *ui.Context, b *CodeBlockView)
	// WrapCode wraps the lines of code blocks rather than scrolling them
	// sideways.
	WrapCode bool
	// Math, when set, builds blocks of display math from their TeX, which
	// show as their TeX without it; math within text shows as its TeX.
	Math func(c *ui.Context, tex string)
	// HTML, when set, builds HTML blocks from their HTML, which show as
	// their HTML without it; within text, the tags of formatting (<b>,
	// <i>, <code>, <br>, <mark>, <sub>, …) apply, and other tags show as
	// text.
	HTML func(c *ui.Context, html string)
	// Block, when set, is asked first for each block, and builds it in
	// its place when it returns true.
	Block func(c *ui.Context, d *parser.Document, i int) bool

	// Reveal is how Stream shows text as it comes: DefaultReveal when nil.
	Reveal *Reveal
	// ShareSelection leaves the selection to a Selectable container around
	// the view, as one across a chat's messages, rather than giving the
	// view one of its own.
	ShareSelection bool
	// contains filtered or unexported fields
}

Options are how views show Markdown: their theme, what links and images do, how blocks render, and how text being written comes in. Keep one in the app's state and give it to every view of a kind, as the messages of a chat.

type Reveal

type Reveal struct {
	// Unit is what shows at a time: a word by default.
	Unit RevealUnit
	// CatchUp is how long text that came waits at most before it shows:
	// 250 ms when zero. Text comes in faster the more there is waiting.
	CatchUp time.Duration
	// Fade is how long text takes to fade in: 300 ms when zero, none when
	// negative, nor when the desktop asks for less motion.
	Fade time.Duration
}

Reveal is how Stream shows text as it comes: rather than in the bursts a network delivers, at an even pace that catches up with what came within CatchUp, a unit at a time, each fading in, as the chat apps show replies.

type RevealUnit

type RevealUnit uint8

RevealUnit is what Reveal shows at a time.

const (
	// ByWord shows a word and the spaces after it at a time.
	ByWord RevealUnit = iota
	// ByCharacter shows a character at a time.
	ByCharacter
	// BySentence shows a sentence at a time, once it is complete: up to
	// a period, a question or exclamation mark followed by a space, an
	// ideographic full stop, or the end of a line.
	BySentence
	// ByLine shows a line at a time, once it is complete.
	ByLine
	// ByParagraph shows a paragraph, a block between blank lines, at a
	// time, once it is complete.
	ByParagraph
	// AtOnce shows text as it comes, without pacing.
	AtOnce
)

type Theme

type Theme struct {
	// FontSize is the size of body text in DIPs: the window theme's
	// FontSize when zero.
	FontSize float32
	// Font is the family of text, the window theme's when empty, and
	// CodeFont that of code, the system's monospaced font when empty.
	Font, CodeFont string
	// LineHeight is how many times its font's line height a line of text
	// is: 1.5 by default, and CodeLineHeight that of code, 1.4.
	LineHeight, CodeLineHeight float32

	// Text colors text, Muted what is secondary (list markers, quotes,
	// a code block's language, footnotes), Link links, and Strong strong
	// text, the Text color when zero by default.
	Text, Muted, Link, Strong Color
	// StrongWeight is the weight of strong text: 600.
	StrongWeight int

	// Code colors code spans, with CodeBackground behind them; CodeSize is
	// their size in ems: 0.875.
	Code, CodeBackground Color
	CodeSize             float32

	// BlockSpacing is the space between blocks, in ems: 0.75. ItemSpacing
	// is that between the items of a tight list: 0.25.
	BlockSpacing, ItemSpacing float32
	// Headings are the styles of headings of levels 1 to 6.
	Headings [6]HeadingStyle
	// ListIndent is how far list items are from their list's edge, their
	// markers in that room, in ems: 1.6.
	ListIndent float32

	// QuoteBar colors the bar along block quotes, QuoteBarWidth DIPs
	// wide (3), and QuoteText their text.
	QuoteBar, QuoteText Color
	QuoteBarWidth       float32

	// Rule colors thematic breaks and the rules of headings.
	Rule Color

	// CodeBlockBackground and CodeBlockBorder color code blocks, with
	// corners of CodeBlockRadius DIPs (the window theme's Radius plus 2)
	// and CodeBlockPadding ems of room inside (0.85). CodeColors colors
	// their code's tokens: editor.DefaultTheme's colors when nil.
	CodeBlockBackground, CodeBlockBorder Color
	CodeBlockRadius, CodeBlockPadding    float32
	CodeColors                           *editor.Theme

	// TableBorder colors a table's lines, TableHeader the background of
	// its header row, and TableStripe that of every other row (none by
	// default). TableCellPadding is the room in cells, in ems: 0.6 across
	// and 0.4 down. TableMaxColumn is how wide a column grows before its
	// cells wrap, in ems: 24.
	TableBorder, TableHeader, TableStripe Color
	TableCellPadding                      [2]float32
	TableMaxColumn                        float32

	// Mark is the background of text marked with <mark>.
	Mark Color
	// Checkbox colors the check boxes of task lists that are checked.
	Checkbox Color
}

Theme is how Markdown looks: fonts, colors and spacing for each kind of block and inline. What it leaves zero takes the value of DefaultTheme for the window's theme, so that a theme sets only what it changes:

app.md.Theme = &markdown.Theme{Link: ui.Hex("#7c3aed"), CodeFont: "JetBrains Mono"}

Lengths in ems are times the FontSize.

func DefaultTheme

func DefaultTheme(t *ui.Theme) *Theme

DefaultTheme returns the Markdown theme of a window's theme: its font and colors, a code span tinted with its text color, code colored as the code editor colors it, and the spacing of the chat apps.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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