markdown

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package markdown renders editor content. Content is stored as Markdown; raw HTML in the source is never rendered: goldmark drops each HTML block and inline tag and writes <!-- raw HTML omitted --> in its place (the text between inline tags stays). Media is referenced as ![alt](media:<uuid>) and resolved to a public URL at render time.

Index

Constants

View Source
const MediaPrefix = "media:"

MediaPrefix marks a link or image destination that refers to a media asset.

Variables

This section is empty.

Functions

func AbsoluteSrcset

func AbsoluteSrcset(base, srcset string) string

AbsoluteSrcset makes every address of srcset, such as "/a.webp 480w, /b.webp 800w", absolute from base, as Options.Base does with the srcset of an image Images gives; the descriptors (480w) stay as they are. An empty base, or one that is not an absolute address, leaves srcset as it is.

func HTML

func HTML(source string, resolve Resolver) template.HTML

HTML renders Markdown to safe HTML. A nil resolver leaves media links empty.

func Links(source string) []string

Links returns the distinct destinations of the links in source, in order, as written: those of text(address) and reference links, and the web addresses GFM turns into links, such as https://example.com. Links to media assets are media:<uuid>; images are not links.

func MediaIDs

func MediaIDs(source string) []string

MediaIDs returns the distinct media assets referenced by the source, in order.

func PlainMedia

func PlainMedia(source string, options Options) string

PlainMedia is source for readers of the Markdown itself, such as the text part of an e-mail, with its media references resolved by options: an image becomes a link to the image, [alt](address), with the alt text of options.Images when the Markdown gives none and the address of options.Images or else options.Media; a link to media gets the address of options.Media. A reference that does not resolve keeps its text. With options.Base, the destinations of links and images outside code are resolved from it, as Render does.

func Render

func Render(source string, options Options) template.HTML

Render renders Markdown to safe HTML with media resolved by options.

func Text

func Text(source string) string

Text renders Markdown as plain text on one line, for search and summaries: formatting, raw HTML and link targets are dropped, the words are kept (including image descriptions).

Types

type Image

type Image struct {
	URL    string
	Srcset string
	Sizes  string
	Alt    string
	Width  int
	Height int
}

Image is how an image asset is shown: the address of its src and, optionally, a srcset with sizes, the alt text used when the Markdown gives none, and the size of URL in pixels for the img's width and height attributes, so the browser keeps the image's place before it loads (0 leaves them out).

type ImageResolver

type ImageResolver func(id, alt string) (image Image, ok bool)

ImageResolver describes the image asset id. alt is the image's description in the Markdown; when it is empty, Image.Alt is used instead. ok is false when the resolver has nothing to add to Options.Media.

type Options

type Options struct {
	// Media turns a media ID into a public URL, for links and images; nil
	// leaves media links empty.
	Media Resolver
	// Images describes media images; nil, or an image it has nothing for,
	// uses Media's URL alone.
	Images ImageResolver
	// Base makes the site's own addresses absolute, for readers away from
	// the site, such as a feed or an e-mail. It is the absolute address
	// the content's relative links lead from, such as
	// "https://example.com/en/": link and image destinations without a
	// scheme are resolved from it as a browser resolves them, both paths
	// on the site ("/en/about" becomes "https://example.com/en/about") and
	// relative addresses ("about" becomes "https://example.com/en/about"),
	// also those Media and Images give, every address of an image's
	// srcset among them. Addresses with a scheme
	// ("https://…", "mailto:…") and a fragment alone ("#top") stay as they
	// are. Empty, or not an absolute address, leaves every destination as
	// it is.
	Base string
}

Options are how Render resolves media.

type Resolver

type Resolver func(id string) string

Resolver turns a media ID into a public URL. It returns "" for unknown assets.

Jump to

Keyboard shortcuts

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