Documentation
¶
Overview ¶
Package markdown shows Markdown as jamb elements: a read-only view of a document in CommonMark, with the GitHub extensions for tables, strikethrough, task lists, autolinks and footnotes. The text is parsed with goldmark, and the view builds elements from its syntax tree; nothing is rendered to HTML.
ui.Div().ScrollY().Child(markdown.View(readme).BaseDir(dir).MaxWidth(ui.Px(820)))
The view is a column of blocks and does not scroll by itself: place it in a scrolling container, as above. It parses its text once, and again only when the text changes, so a view built on each render of its component costs a parse only for a new text.
Each block's text is a rich text, which the user can select and copy, one block at a time. Headings are headings for assistive technology, with their levels; lists are lists of items; links are links, which Tab reaches and Enter follows; a task's check box reports whether it is done.
Links open with the system, through ui.Context.OpenURL, which opens http, https and mailto URLs only. A link to "#name" moves to the heading whose anchor is name, as on GitHub. OnLink replaces all of that with the app's own handler. Images are read by a loader: by default, from the local files named relative to BaseDir. The view downloads nothing: the default loader refuses URLs and network paths, and an image that cannot be shown is replaced by its alternative text. HTML in the text is shown as it is written, and HTML comments are left out; no HTML is interpreted.
Code blocks are in the monospace font, and scroll sideways when a line is long. They are plain unless the view has a CodeHighlighter (see Code), such as the one of the mdcode package, which colors them with the theme's syntax colors.
The words that the view adds, such as the name of a table or of an image without alternative text, are the Markdown fields of the window's ui.Strings. The parts of the view are styled with Style (see Styles).
Index ¶
- type CodeHighlighter
- type CodeSpan
- type Style
- type ViewElement
- func (v *ViewElement) BaseDir(dir string) *ViewElement
- func (v *ViewElement) Code(h CodeHighlighter) *ViewElement
- func (v *ViewElement) GridSpan(cols, rows int) *ViewElement
- func (v *ViewElement) Grow(f float32) *ViewElement
- func (v *ViewElement) Height(l ui.Length) *ViewElement
- func (v *ViewElement) Images(load func(src string) (*ui.ImageSource, error)) *ViewElement
- func (v *ViewElement) Key(k string) *ViewElement
- func (v *ViewElement) Label(s string) *ViewElement
- func (v *ViewElement) MaxHeight(l ui.Length) *ViewElement
- func (v *ViewElement) MaxWidth(l ui.Length) *ViewElement
- func (v *ViewElement) MinHeight(l ui.Length) *ViewElement
- func (v *ViewElement) MinWidth(l ui.Length) *ViewElement
- func (v *ViewElement) OnLink(fn func(url string)) *ViewElement
- func (v *ViewElement) OnTaskToggle(fn func(index int, checked bool)) *ViewElement
- func (v *ViewElement) Render(ctx *ui.Context) ui.Element
- func (v *ViewElement) Styles(st Style) *ViewElement
- func (v *ViewElement) Width(l ui.Length) *ViewElement
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CodeHighlighter ¶
type CodeHighlighter interface {
// HighlightCode returns the colored ranges of code, which is written in the
// language lang: the first word of the code block's fence, as it is written, such
// as "go" or "Python", or "" for a block without one. It returns false when it has
// no ranges for the code: when it does not know the language, or while it is still
// working on them. In the second case it calls ready once they are done, from any
// goroutine, and the view asks again. The view calls it while it renders, on the UI
// thread, so it must return at once.
//
// ctx is done when the view no longer shows the block: when the view goes away, or
// when its text changes and the new text does not ask for the same code again. The
// highlighter may then stop the work for the block and not call ready.
HighlightCode(ctx context.Context, lang, code string, ready func()) ([]CodeSpan, bool)
}
CodeHighlighter colors the code blocks of a view (see ViewElement.Code). The mdcode package provides one built on the tree-sitter grammars of jamb's code editor; an app can write its own.
type CodeSpan ¶
type CodeSpan struct {
// Start and End are the byte offsets of the range in the code.
Start, End int
// Capture is the name of the range's syntax capture.
Capture string
}
CodeSpan is a range of code, [Start, End) in bytes, and its syntax capture: a tree-sitter capture name, such as "keyword" or "string.escape". The view colors it with the theme's Syntax style for the name, or for the name without its last dotted parts when the theme has no style for it, as the code editor does. A span later in the list wins over an earlier one where they overlap.
type Style ¶
type Style struct {
// Document is the column that holds the blocks: its Gap is the space between
// them, and its padding, background and text apply to the whole document.
Document ui.Style
// Paragraph is the text of a paragraph: its size, color and line height.
Paragraph ui.Style
// Heading1 to Heading6 are the boxes of the headings of each level, and their
// text: size, weight, color, and a border or padding.
Heading1, Heading2, Heading3, Heading4, Heading5, Heading6 ui.Style
// Link is the text of a link: its TextColor and Background.
Link ui.Style
// LinkHover is the text of a link under the pointer: its TextColor and
// Background. A link is underlined while the pointer is over it.
LinkHover ui.Style
// InlineCode is the text of code in a line: its TextColor and Background. It is
// always in the monospace font.
InlineCode ui.Style
// CodeBlock is the box of a code block, which scrolls sideways when a line is
// long, and its text: background, border, radius, padding and font.
CodeBlock ui.Style
// Quote is the box of a block quote: its border, padding, gap and text color.
Quote ui.Style
// List is the column of the items of a tight list: its Gap is the space between
// them. A loose list spaces its items as the Document spaces its blocks.
List ui.Style
// ListMarker is the bullet or the number of a list item: its color, size and
// width.
ListMarker ui.Style
// Task is the box of a task's check box, and TaskChecked its look when the task
// is done, over Task. The check mark takes TaskChecked's TextColor.
Task, TaskChecked ui.Style
// Table is the box of a table: its BorderColor and BorderWidth are also the lines
// between the cells.
Table ui.Style
// TableHeader is a cell of the header row, over TableCell.
TableHeader ui.Style
// TableCell is a cell of a table: its padding, background and text.
TableCell ui.Style
// Rule is a thematic break: a box as wide as the document, of the rule's Height
// and Background.
Rule ui.Style
// ImagePlaceholder is the box shown in place of an image that is loading or
// cannot be shown, with its alternative text.
ImagePlaceholder ui.Style
}
Style is the look of the parts of a Markdown view. A part's style merges over the built-in defaults, then over the Style that the theme holds for every view (set with ui.SetThemeStyle), then over the Styles of the view, field by field.
Under high contrast the colors of these parts are the system colors, whatever is set here or in the theme; their sizes, spacing and fonts still apply.
type ViewElement ¶
ViewElement is a Markdown view, made by View and configured with the methods below, each of which returns the view so that they chain. It is a ui.Element, so it is placed in a tree as it is. It is as wide as its parent and as high as its content unless the sizing methods say otherwise.
func (*ViewElement) BaseDir ¶
func (v *ViewElement) BaseDir(dir string) *ViewElement
BaseDir sets the directory that the default image loader reads relative paths from. Without it, they are relative to the working directory. The default loader refuses URLs (http, https, data and the other schemes) and network paths (\\server\share), so it never reaches the network; it reads regular files of up to 32 MB in the formats of ui.ImageFromBytes.
func (*ViewElement) Code ¶
func (v *ViewElement) Code(h CodeHighlighter) *ViewElement
Code sets the highlighter of the view's code blocks. Without one, code blocks are plain. See CodeHighlighter, and the mdcode package for one built on the code editor's grammars.
func (*ViewElement) GridSpan ¶
func (v *ViewElement) GridSpan(cols, rows int) *ViewElement
GridSpan sets how many columns and rows the view covers in its parent's grid.
func (*ViewElement) Grow ¶
func (v *ViewElement) Grow(f float32) *ViewElement
Grow makes the view take a share of the free space on its parent's main axis.
func (*ViewElement) Height ¶
func (v *ViewElement) Height(l ui.Length) *ViewElement
Height sets the height of the view, which is that of its content by default.
func (*ViewElement) Images ¶
func (v *ViewElement) Images(load func(src string) (*ui.ImageSource, error)) *ViewElement
Images sets the loader of the view's images, which is given each image's source as it is written in the text. It runs on a goroutine of its own, so it may read files or wait; the view shows a placeholder until it returns, and the image's alternative text if it returns an error. Results are kept by the view for each source. The default loader reads local files (see BaseDir).
func (*ViewElement) Key ¶
func (v *ViewElement) Key(k string) *ViewElement
Key gives the view a stable identity among its siblings, so its state (the parsed text, the images, the selection of its text) survives reordering.
func (*ViewElement) Label ¶
func (v *ViewElement) Label(s string) *ViewElement
Label names the view for assistive technology, which then reads it as a group of that name, such as "Preview". Without a label the blocks are read in the window's order, with no group around them.
func (*ViewElement) MaxHeight ¶
func (v *ViewElement) MaxHeight(l ui.Length) *ViewElement
MaxHeight sets the largest height of the view.
func (*ViewElement) MaxWidth ¶
func (v *ViewElement) MaxWidth(l ui.Length) *ViewElement
MaxWidth sets the largest width of the view, such as a comfortable line length.
func (*ViewElement) MinHeight ¶
func (v *ViewElement) MinHeight(l ui.Length) *ViewElement
MinHeight sets the smallest height of the view.
func (*ViewElement) MinWidth ¶
func (v *ViewElement) MinWidth(l ui.Length) *ViewElement
MinWidth sets the smallest width of the view.
func (*ViewElement) OnLink ¶
func (v *ViewElement) OnLink(fn func(url string)) *ViewElement
OnLink makes fn handle every link the user follows, with the destination as it is written in the text: absolute URLs, relative paths and "#name" anchors alike, and images inside links. It replaces the view's own handling (see the package documentation). fn runs on the UI thread; it decides what is safe to open.
func (*ViewElement) OnTaskToggle ¶
func (v *ViewElement) OnTaskToggle(fn func(index int, checked bool)) *ViewElement
OnTaskToggle makes the check boxes of task list items operable: a click, or Space while one has focus, calls fn with the task's index (0 for the first task of the document, in the order of the text) and the state it asks for. The view does not change the text: fn changes it, and the view shows the new text. Without OnTaskToggle the check boxes only show whether each task is done.
func (*ViewElement) Render ¶
func (v *ViewElement) Render(ctx *ui.Context) ui.Element
Render builds the view's elements. The view is an element: place it in a tree with ui.Div().Child or any other container. Its parsed text and its loaded images are kept by the element, so give it a Key when its place in the tree may change.
func (*ViewElement) Styles ¶
func (v *ViewElement) Styles(st Style) *ViewElement
Styles sets the look of the parts of the view, for this view only. Set fields win over the theme's Style and the defaults. Calling Styles again merges with the earlier values.
func (*ViewElement) Width ¶
func (v *ViewElement) Width(l ui.Length) *ViewElement
The sizing methods give the view's box a size, as they do for the widgets of the ui package, and win over the Document part of Styles. See Sizing in docs/styling.md.