markdown

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 14 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 Revealed added in v0.1.17

func Revealed(el ui.Element) bool

Revealed reports whether el, a view View or Stream returned this frame, shows all of its source, done fading in. A chat shows a reply's time and actions once its reply is complete and Revealed, not as its last bytes arrive, while words still come in:

var md ui.Element
if msg.done {
	md = markdown.View(c, msg.text, &app.md)
} else {
	md = markdown.Stream(c, msg.text, &app.md)
}
if msg.done && markdown.Revealed(md) {
	ui.Text(c, msg.time)
}

It tells what this frame shows, so call it after View or Stream, and build what it decides after them. The view asks for a frame each refresh while it reveals or fades, so a caller that reads it every frame sees it turn true without other input, once the last unit shows and its fade ends. Text that shows at once (AtOnce, a View given text no Stream left to reveal, an edit) is revealed in the frame it comes; without a fade (Reveal.Fade negative, or the desktop's Reduce Motion), text is revealed once its last unit shows. It turns false again when more text comes. Of a Stream, it tells only that the text that came so far shows: whether more is coming is the caller's to know, and text held back for a sentence, a line or a paragraph to end keeps it false until View shows it. It is false for an element neither returned.

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 InlineObject added in v0.1.13

type InlineObject struct {
	Width, Height, Depth float32
	Draw                 func(p *ui.Painter, r ui.Rect)
}

InlineObject is math within text, as Options.InlineMath makes it, or a link's icon, as Options.LinkIcon does: a box Width wide, Height above the baseline and Depth below it, in DIPs, which Draw paints in r, its box in the window. Draw only paints: it may run more than once a frame.

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

	// LinkMenu, when set, builds the context menu of a link, given its
	// destination, as a chat's Copy Path and Reveal in Finder for a link
	// to a file. The link's menu replaces the one around the view, as its
	// Selectable container's, since the innermost element with a context
	// menu gets the click; m.EditItems adds the text's Copy and Select
	// All to it. Links to #a-heading, footnote references and a link
	// whose destination is still coming keep the menu around them.
	LinkMenu func(m *ui.Menu, url string)
	// 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)
	// LinkIcon, when set, makes an object at the start of a link's text,
	// from its destination and the size of the text around it in DIPs:
	// one on the baseline, inside the link, which stays on the line of
	// the link's first word and copies as nothing. When it returns false,
	// the link has none; a link still being written has none yet. It is
	// asked again whenever the paragraph changes: keep what it makes.
	LinkIcon func(dest string, size float32) (InlineObject, bool)
	// Anchor scrolls to the heading of this slug, with or without its "#",
	// when the document first shows, when another document replaces it,
	// and when Anchor changes: a docs reader opens guide.md#install at
	// its heading. An OnLink that takes #a-heading links sets it.
	Anchor string
	// AnchorVersion scrolls to Anchor again when it changes, for the same
	// heading asked for twice.
	AnchorVersion uint64
	// Resolve, when set, makes mentions (@octocat) and references to
	// issues and pull requests (#123) links: it returns where one goes,
	// which OnLink, or else the router or a ui.Link, opens, as a path of
	// the app's to open it in place, or false to leave it text, as
	// GitHub does for a person or an issue it does not know. Without it,
	// they show as text. It is asked for each one shown whenever the view
	// builds, so it looks up what the app has at hand, as in a map, and
	// paragraphs follow when its answers change.
	Resolve func(in parser.Inline) (dest string, ok bool)
	// CodeLink, when set, makes code within text a link where it returns
	// one: a chat links `src/app.go:12` to the file it names. It is asked
	// for each piece of code shown whenever the view builds, as Resolve
	// is, and paragraphs follow when its answers change. The code keeps
	// its look; its source is untouched, so a document streaming in
	// parses as it would without.
	CodeLink func(code string) (dest string, ok bool)
	// Preview, when set, returns what shows while the pointer rests on a
	// link to dest, a mention's or a reference's included: a function
	// filling the tip, as ui.TooltipBase does, or nil for the address
	// alone. It runs as the view builds, so it shows what the app has at
	// hand, fetching the rest in the background.
	Preview func(c *ui.Context, dest string) func(tip ui.Element)
	// 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)
	// InlineMath, when set, makes math within text an object of the
	// text, from its TeX and the size of the text around it in DIPs: one
	// on the baseline, which wraps with the words and copies as its
	// source, as $x_i$. When it returns false, the math shows as its
	// TeX. It is asked again whenever the paragraph changes, as every
	// frame of a paragraph streaming in: keep what it makes.
	InlineMath func(tex string, size float32) (InlineObject, bool)
	// 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. Within another view, as a block Block builds,
	// it shares that view's, whose Copy as Markdown copies its text as
	// the Markdown of its own source.
	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. In Chinese
	// and Japanese, written without spaces, each ideograph and kana is a
	// word.
	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
	// LinkWeight is the smallest weight of link text, so links stand out
	// among prose without lifting strong text down to it: 0 for no floor.
	LinkWeight 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
	// Alerts color the bar, icon and title of GitHub's alerts, by kind:
	// Note, Tip, Important, Warning and Caution, GitHub's colors by
	// default, in light or dark as the window theme is.
	Alerts [5]Color

	// 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