render

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package render draws a post's web version from its MJML (design expansion phase 7, brief §3.1): the email's blocks in the site's theme and its 680 px reading column, not the email's tables.

  • Personalization uses each token's fallback: "Hi {{ first_name | default: "there" }}" reads "Hi there".
  • Blocks set to "Email only" in their Visibility section (rc-show-on) are left out; the footer, unsubscribe, view-online and referral blocks are Email only unless they say otherwise.
  • Conditional blocks (rc-visible, internal/visibility) are hidden on the web, except blocks for a paid tier: those are drawn apart (Paid), and the page shows them to readers who hold the tier and the paywall card to everyone else.
  • The "Free preview ends here" marker cuts the preview: readers without access get the post down to it.

The output is HTML the site may serve: only known elements and attributes survive (no styles, scripts, frames or forms), links and images are http(s) or root-relative.

Index

Constants

View Source
const (
	// AttrShowOn is a block's "Show on": "email" (Email only), "web" (Web
	// only), or absent (email and web).
	AttrShowOn = mjml.AttrShowOn
	// AttrPreview marks the "Free preview ends here" block (an mj-raw).
	AttrPreview = "rc-preview"
	// PreviewComment is the marker block's content.
	PreviewComment = "<!-- rc:preview-end -->"
	// PaidKind is Visibility's Paid access condition (Earn, X16: earn.KindPaid):
	// PaidAny shows a block to any paid tier, PaidAtLeast to the tier in its
	// value and above. Its third operator, free subscribers only, isn't for a
	// paid tier: on the web that block is hidden like other conditional blocks.
	PaidKind    = "paid"
	PaidAny     = "any"
	PaidAtLeast = "at_least"
)

Attributes of ReputeCast's own on MJML elements (they never reach an email: internal/mjml.PrepareCompile strips rc-* attributes).

View Source
const (
	ShowOnEmail = "email"
	ShowOnWeb   = "web"
	ShowOnBoth  = "both"
)

Show-on values.

Variables

View Source
var PaidPlaceholderRe = regexp.MustCompile(`<!--rc:paid ([a-z0-9]{1,24})-->`)

PaidPlaceholderRe finds paid placeholders.

Functions

func IsMarker

func IsMarker(n *mjml.Node) bool

IsMarker reports whether an element is the free preview marker.

func PaidPlaceholder

func PaidPlaceholder(id string) string

PaidPlaceholder marks where a paid block sits in Body.

func ReadingMinutes

func ReadingMinutes(words int) int

ReadingMinutes is the reading time of a post: 230 words a minute, at least one.

Types

type Fallback

type Fallback struct {
	Token    string `json:"token"`
	Fallback string `json:"fallback"`
	HasValue bool   `json:"hasFallback"`
	// Source is the text around the token as written ("Hi {{ first_name |
	// default: "there" }}") and Sample how the web reads it ("Hi there").
	Source string `json:"source"`
	Sample string `json:"sample"`
}

Fallback is a personalization token the web shows with its fallback.

type Hidden

type Hidden struct {
	// ID is the block's Visibility id when it has one (the composer
	// selects it), else "".
	ID string `json:"id,omitempty"`
	// Name is the block's own name (rc-name), else "".
	Name string `json:"name,omitempty"`
	// Kind names a well-known block: view_online, footer, referral,
	// header, poll, or block.
	Kind string `json:"kind"`
	// Reason: email_only (its Show on, or the default for its kind),
	// conditional (a Visibility rule), hidden ("Hide for everyone").
	Reason string `json:"reason"`
	// Condition is the first condition's label ("Beta Program").
	Condition string `json:"condition,omitempty"`
	// Line is the block's line in the MJML.
	Line int `json:"line"`
}

Hidden is a block left out of the web version.

type Options

type Options struct {
	// Links fill the email's link tokens on the web (preferences_url,
	// unsubscribe_url and view_in_browser_url point to pages of the site).
	Links map[string]string
	// PublicURL absolutizes root-relative /media/ images ("" leaves them).
	PublicURL string
	// CoverSrc is the post's cover: the first image showing it is left out
	// of the body (the page shows the cover above the text).
	CoverSrc string
	// Title is the post's title: a first heading saying the same is left
	// out (the page shows the title).
	Title string
}

Options of one render.

type Paid struct {
	ID    string   `json:"id"`
	Tiers []string `json:"tiers"`
	HTML  string   `json:"html"`
	Text  string   `json:"text"`
}

Paid is a block for a paid tier: shown to readers holding one of Tiers (empty: any paid tier), the paywall card in its place for everyone else.

type Result

type Result struct {
	// Body is the whole post. Paid blocks appear in it as PaidPlaceholder(id).
	Body string
	// Preview is the post down to the free preview marker ("" without one).
	Preview string
	// HasMarker reports a marker; MarkerAfter counts the blocks before it.
	HasMarker   bool
	MarkerAfter int
	// Text is the whole post's text, PreviewText the part before the
	// marker (search reads only what a visitor may read).
	Text        string
	PreviewText string
	Words       int
	Hidden      []Hidden
	Fallbacks   []Fallback
	Paid        []Paid
}

Result is a post's web version.

func Render

func Render(src string, o Options) (Result, error)

Render draws a post's web version from its MJML.

Jump to

Keyboard shortcuts

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