premailer

package
v1.36.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 12 Imported by: 241

Documentation

Overview

Package premailer is for inline styling.

import (
	"fmt"
	"github.com/vanng822/go-premailer/premailer"
	"log"
)

func main() {
	prem, err := premailer.NewPremailerFromFile(inputFile, premailer.NewOptions())
	if err != nil {
		log.Fatal(err)
	}
	html, err := prem.Transform()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(html)
}
// Input

<html>
<head>
<title>Title</title>
<style type="text/css">
	h1 { width: 300px; color:red; }
	strong { text-decoration:none; }
</style>
</head>
<body>
	<h1>Hi!</h1>
	<p><strong>Yes!</strong></p>
</body>
</html>

// Output

<html>
<head>
<title>Title</title>
</head>
<body>
	<h1 style="color:red;width:300px" width="300">Hi!</h1>
	<p><strong style="text-decoration:none">Yes!</strong></p>
</body>
</html>

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Html2TextOption added in v1.30.0

type Html2TextOption func(*Html2TextOptions)
func WithOmitLinks(omit bool) Html2TextOption

func WithPrettyTables added in v1.30.0

func WithPrettyTables(pretty bool) Html2TextOption

func WithPrettyTablesOptions added in v1.30.0

func WithPrettyTablesOptions(opts *html2text.PrettyTablesOptions) Html2TextOption

func WithTextOnly added in v1.30.0

func WithTextOnly(textOnly bool) Html2TextOption

type Html2TextOptions added in v1.30.0

type Html2TextOptions struct {
	PrettyTables        bool                           // Turns on pretty ASCII rendering for table elements.
	OmitLinks           bool                           // Turns on omitting links
	TextOnly            bool                           // Returns only plain text
	PrettyTablesOptions *html2text.PrettyTablesOptions // Configures pretty ASCII rendering for table elements.
}

type Options

type Options struct {
	// Remove class attribute from element
	// Default false
	RemoveClasses bool
	// Copy related CSS properties into HTML attributes (e.g. background-color to bgcolor)
	// Default true
	CssToAttributes bool

	// If true, then style declarations that have "!important" will keep the "!important" in the final
	// style attribute
	// Example:
	//		<style>p { width: 100% !important }</style><p>Text</p>
	// gives
	//		<p style="width: 100% !important">Text</p>
	KeepBangImportant bool
}

Options for controlling behaviour

func NewOptions

func NewOptions(o ...PremailerOption) *Options

NewOptions return an Options instance with default value

type Premailer

type Premailer interface {
	// Transform process and inlining css
	// It start to collect the rules in the document style tags
	// Calculate specificity and sort the rules based on that
	// It then collects the affected elements
	// And applies the rules on those
	// The leftover rules will put back into a style element
	Transform() (string, error)
	// TransformText process and inlining css then convert to text
	// It first call Transform to get the processed html
	// Then convert the html to text
	// It accept Html2TextOption to control the conversion
	TransformText(...Html2TextOption) (string, error)
}

Premailer is the inteface of Premailer The inline html document is cached so multiple calls to TransformText or Transform will not re-process the html again

func NewPremailer

func NewPremailer(doc *goquery.Document, options *Options) Premailer

NewPremailer return a new instance of Premailer It take a Document as argument and it shouldn't be nil

func NewPremailerFromBytes

func NewPremailerFromBytes(doc []byte, options *Options) (Premailer, error)

NewPremailerFromBytes take in a document in byte and create a goquery.Document and then create and Premailer instance.

func NewPremailerFromFile

func NewPremailerFromFile(filename string, options *Options) (Premailer, error)

NewPremailerFromFile take an filename Read the content of this file and create a goquery.Document and then create and Premailer instance.

func NewPremailerFromString

func NewPremailerFromString(doc string, options *Options) (Premailer, error)

NewPremailerFromString take in a document in string format and create a goquery.Document and then create and Premailer instance.

type PremailerOption added in v1.30.0

type PremailerOption func(*Options)

func WithCssToAttributes added in v1.30.0

func WithCssToAttributes(cssToAttr bool) PremailerOption

func WithKeepBangImportant added in v1.30.0

func WithKeepBangImportant(keep bool) PremailerOption

func WithRemoveClasses added in v1.30.0

func WithRemoveClasses(remove bool) PremailerOption

Jump to

Keyboard shortcuts

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