richtext

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package richtext is Godwit's rich text model: mail bodies, compose content and Markdown become a flat list of blocks that the native UI draws (spec 5.5), and compose content goes back to HTML.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLimits = Limits{20, 5000, 2000, 20000}

DefaultLimits are the budget for one mail (global constraints).

Functions

func Autospace

func Autospace(s string, size float32) []ui.Span

Autospace splits s into spans so that the character before each place where CJK text meets a Latin letter or digit is a span of its own with a quarter em (size/4) of LetterSpacing (CSS text-autospace: normal). No characters are added: the text of the spans is s.

func Render

func Render(c *ui.Context, d Doc, o RenderOptions) ui.Element

Render draws d read-only.

func ToHTML

func ToHTML(d Doc) string

ToHTML serializes d as stable HTML that the From functions read back: FromComposeHTML(ToHTML(d)) == d for compose content.

Types

type Block

type Block struct {
	Kind    BlockKind
	Level   int
	Ordered bool
	Inlines []Inline
	Rows    [][]Cell
	Header  bool
	Image   *Image
}

Block is one block. Which fields count depends on Kind.

type BlockKind

type BlockKind int

BlockKind is what a block is.

const (
	Paragraph BlockKind = iota
	Heading             // Level 1–6
	Quote               // Level is the quote depth, from 1
	ListItem            // Level is the list depth, from 1; Ordered for <ol>
	Pre
	Rule
	Table // Rows; Header when the first row is a header
	ImageBlock
)

type Cell

type Cell struct{ Inlines []Inline }

Cell is a table cell.

type Doc

type Doc struct{ Blocks []Block }

Doc is a document: blocks from top to bottom.

func FromComposeHTML

func FromComposeHTML(safe string) Doc

FromComposeHTML converts compose content (mail.SanitizeCompose output or a draft's editable part): links come from href, and only paragraphs, bold, italic, links, lists, quotes and data: images (a draft's signature images, for showing) stay; everything else becomes paragraph text.

func FromMailHTML

func FromMailHTML(safe string, lim Limits) (Doc, bool)

FromMailHTML converts a received mail's sanitized HTML (Message.HTML after mail.Sanitize): links come from data-href, images from data: or http(s) src. truncated reports that lim cut something off.

func FromMarkdown

func FromMarkdown(src string) Doc

FromMarkdown converts Markdown (a model's answer, say) through the same sanitizer as received mail, so it gets no more than mail does. Remote images are dropped: loading one would tell its server what was written.

func (Doc) PlainText

func (d Doc) PlainText() string

PlainText is the document's text: a line per block, table cells separated by tabs, images by their alt text.

type Image

type Image struct {
	Src, Alt      string
	Width, Height int
}

Image is an image block. Src is a data: URL (PNG, JPEG, GIF, WebP) or an http(s) URL; Width and Height are the HTML attributes, 0 when unknown.

type Inline

type Inline struct {
	Text                                  string
	Bold, Italic, Code, Strike, Underline bool
	Link                                  string
	Color                                 string // a CSS color, from the sanitized inline style
}

Inline is a run of text in one style. "\n" in Text is a line break.

type Limits

type Limits struct{ MaxDepth, MaxBlocks, MaxCells, MaxInlines int }

Limits bound what a conversion produces: element nesting (deeper subtrees become plain text), blocks, cells of one table and inlines of one block.

type Pos

type Pos struct{ Block, Rune int }

Pos is a place in a document: a rune of a block's text (its inlines' text together).

type RenderOptions

type RenderOptions struct {
	FontSize   float32
	Background ui.Color
	Images     func(src string) *ui.Bitmap
	OnLink     func(url string)
}

RenderOptions are how Render draws a document. FontSize defaults to the body font size, Background (what the document is drawn on, for the contrast check) to the theme's. Images returns a loaded image or nil for a placeholder. OnLink gets the target of an activated link; the host shows it before opening it.

Jump to

Keyboard shortcuts

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