emailmd

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package emailmd renders Markdown into what a newsletter needs: email HTML with every style inlined (mail clients ignore most stylesheets), clean HTML for the web archive, and a readable plain-text part.

Index

Constants

View Source
const (
	MaxImage   = 1 << 20   // bytes
	HeavyImage = 300 << 10 // above this, an image slows the issue down
)

Image limits: formats every mail client shows (Outlook shows neither WebP nor SVG), small enough that an issue stays quick on a phone.

Variables

View Source
var DefaultTheme = Theme{
	Accent: "#0f766e",
	Font:   `-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif`,
}

DefaultTheme reads well everywhere.

View Source
var ImageTypes = map[string]string{"image/jpeg": ".jpg", "image/png": ".png", "image/gif": ".gif"}

ImageTypes maps the accepted content types to file extensions.

Functions

func CheckImage

func CheckImage(p string, data []byte) (string, error)

CheckImage validates an image file an issue references: a clean relative path and an accepted format within the size limit. It returns the type.

func Email

func Email(l Layout) (string, error)

Email renders the full email document.

func FrontMatter

func FrontMatter(src string) (map[string]string, string)

FrontMatter splits optional "---" key: value lines from the body.

func Inline

func Inline(fragment string, t Theme) string

Inline adds inline styles to rendered HTML for email. A paragraph holding nothing but one link whose text ends in "→" becomes a button.

func Local

func Local(src string) bool

Local reports whether an image source is a relative file path.

func LocalImages

func LocalImages(source string) []string

LocalImages returns the image sources of Markdown that are paths, not URLs: files next to the Markdown that must be uploaded with it.

func MarkButtons

func MarkButtons(fragment string) string

MarkButtons gives the button links of a web fragment class="button".

func RewriteImages

func RewriteImages(fragment string, fn func(src string) string) string

RewriteImages replaces the src of every image in an HTML fragment.

func RewriteLinks(fragment string, fn func(i int, href string) string) string

RewriteLinks replaces every http(s) href in fragment with fn(index, url), counting in document order like Doc.Links.

func Text

func Text(source string) string

Text turns Markdown into the plain-text part: links become "text (url)", images their alt text, emphasis markers and inline HTML disappear.

Types

type Doc

type Doc struct {
	HTML    string   // semantic HTML for the web
	Title   string   // text of the first heading
	Summary string   // text of the first paragraph
	Links   []string // http(s) link targets in document order, duplicates kept
}

Doc is rendered Markdown.

func Render

func Render(source string) (Doc, error)

Render converts Markdown to HTML and extracts title, summary and links.

type Layout

type Layout struct {
	Lang       string
	Title      string // subject
	Preheader  string // preview text shown by inboxes after the subject
	Brand      string // list name, shown above the content
	BrandURL   string
	Body       template.HTML // inlined content
	FooterHTML template.HTML // unsubscribe, address, legal links
	Pixel      string        // open-tracking image URL, empty when off
	Theme      Theme
}

Layout is the frame around a newsletter.

type Theme

type Theme struct {
	Accent string       // links and buttons, e.g. #0f766e
	Font   template.CSS // trusted: set by lfr, never by readers
}

Theme colors and fonts of an email.

Jump to

Keyboard shortcuts

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