mjml

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 15 Imported by: 1

Documentation

Overview

Package mjml provides the core MJML component system and rendering functionality. It converts parsed MJML AST nodes into renderable components that generate HTML output.

Index

Constants

This section is empty.

Variables

View Source
var ParseMJML = parser.ParseMJML

ParseMJML re-exports the parser function for convenience

Functions

func Render

func Render(mjmlContent string, opts ...RenderOption) (string, error)

Render provides the main MJML to HTML conversion function

func RenderComponentString added in v0.1.2

func RenderComponentString(c Component) (string, error)

RenderComponentString renders the given Component to a string. It creates a strings.Builder writer and passes it to the Component's Render method. This function returns the rendered output as a string, or an error if rendering fails. Where possible, it is recommended to use the component's Render function directly for better performance and flexibility.

func RenderFromAST

func RenderFromAST(ast *MJMLNode, opts ...RenderOption) (string, error)

RenderFromAST renders HTML from a pre-parsed AST

func SetASTCacheCleanupIntervalOnce added in v0.5.0

func SetASTCacheCleanupIntervalOnce(d time.Duration)

SetASTCacheCleanupIntervalOnce sets how often expired AST cache entries are removed. Only the first call has an effect. By default this is half of the AST cache TTL.

func SetASTCacheTTLOnce added in v0.5.0

func SetASTCacheTTLOnce(d time.Duration)

SetASTCacheTTLOnce sets the time-to-live for cached AST entries. Only the first call has an effect; subsequent calls are ignored. The cleanup interval defaults to half of this value unless explicitly set.

func StopASTCacheCleanup added in v0.5.0

func StopASTCacheCleanup()

StopASTCacheCleanup stops the background cache cleanup goroutine.

Types

type Component

type Component = components.Component

Component is an alias for the components.Component interface

func CreateComponent

func CreateComponent(node *parser.MJMLNode, opts *options.RenderOpts) (Component, error)

CreateComponent creates a component from an MJML AST node

func NewFromAST

func NewFromAST(ast *MJMLNode, opts ...RenderOption) (Component, error)

NewFromAST creates a component from a pre-parsed AST (alias for CreateComponent)

type Error added in v0.10.0

type Error struct {
	Message string        `json:"message"`
	Details []ErrorDetail `json:"details"`
}

Error addresses errors that occur during the compilation of MJML to HTML. It represents errors returned by the MJML engine, providing a general message and a list of detailed errors, each including the line number, error message, and the tag name where the error occurred.

Error is a direct re-interprtation of the same type present in https://github.com/Boostport/mjml-go

func ErrInvalidAttribute added in v0.10.0

func ErrInvalidAttribute(tagName, attrName string, line int) *Error

func (*Error) Append added in v0.10.0

func (e *Error) Append(other *Error)

Append merges another Error into this one, combining all details. The message from the current error is preserved.

func (Error) Error added in v0.10.0

func (e Error) Error() string

type ErrorDetail added in v0.10.0

type ErrorDetail struct {
	Line    int    `json:"line"`
	Message string `json:"message"`
	TagName string `json:"tagName"`
}

ErrorDetail represents a single error detail with line number, message, and tag name.

type MJMLComponent

type MJMLComponent struct {
	*components.BaseComponent
	Head *components.MJHeadComponent
	Body *components.MJBodyComponent
	// contains filtered or unexported fields
}

MJMLComponent represents the root MJML component

func (*MJMLComponent) GetTagName

func (c *MJMLComponent) GetTagName() string

func (*MJMLComponent) RegisterCarouselCSS added in v0.4.0

func (c *MJMLComponent) RegisterCarouselCSS(css string)

RegisterCarouselCSS allows carousel components to register their CSS

func (*MJMLComponent) Render

func (c *MJMLComponent) Render(w io.StringWriter) error

Render implements optimized Writer-based rendering for MJMLComponent

func (*MJMLComponent) RequestMobileCSS

func (c *MJMLComponent) RequestMobileCSS()

RequestMobileCSS allows components to request mobile CSS to be added

type MJMLNode

type MJMLNode = parser.MJMLNode

Type alias for convenience

type RenderOption

type RenderOption func(*RenderOpts)

RenderOption is a functional option for configuring MJML rendering

func WithCache added in v0.5.0

func WithCache() RenderOption

WithCache enables AST caching

func WithDebugTags

func WithDebugTags(enabled bool) RenderOption

WithDebugTags enables or disables debug tag inclusion in the rendered output

type RenderOpts

type RenderOpts = options.RenderOpts

RenderOpts is an alias for convenience

type RenderResult

type RenderResult struct {
	HTML string
	AST  *MJMLNode
}

RenderResult contains both the rendered HTML and the MJML AST

func RenderWithAST

func RenderWithAST(mjmlContent string, opts ...RenderOption) (*RenderResult, error)

RenderWithAST provides the internal MJML to HTML conversion function that returns both HTML and AST

Directories

Path Synopsis
Package debug provides logging functionality for development and troubleshooting.
Package debug provides logging functionality for development and troubleshooting.
Package html provides utilities for generating email-compatible HTML from MJML components.
Package html provides utilities for generating email-compatible HTML from MJML components.
Package options contains render options for MJML components
Package options contains render options for MJML components
Package styles provides utilities for parsing and applying CSS values to HTML elements.
Package styles provides utilities for parsing and applying CSS values to HTML elements.

Jump to

Keyboard shortcuts

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