This change introduces HTML minification to reduce the size of email attachments, which is particularly beneficial for digests with a large number of entries. The minification is enabled by default and can be configured via a new digest.compress option in config.yaml. Additionally, the preview-html script now supports a -minify flag (e.g., make preview-html minify=false) to allow for easy testing of both minified and non-minified output. This implementation uses the tdewolff/minify library to handle HTML, CSS, and JavaScript minification safely and efficiently. Reviewed-on: #23 Co-authored-by: eleith Co-committed-by: eleith
29 lines
571 B
Go
29 lines
571 B
Go
package digest
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/tdewolff/minify/v2"
|
|
"github.com/tdewolff/minify/v2/css"
|
|
"github.com/tdewolff/minify/v2/html"
|
|
"github.com/tdewolff/minify/v2/js"
|
|
)
|
|
|
|
func MinifyHTML(input []byte, enabled bool) ([]byte, error) {
|
|
if !enabled {
|
|
return input, nil
|
|
}
|
|
|
|
m := minify.New()
|
|
m.AddFunc("text/css", css.Minify)
|
|
m.AddFunc("application/javascript", js.Minify)
|
|
m.AddFunc("text/html", html.Minify)
|
|
|
|
var buf bytes.Buffer
|
|
if err := m.Minify("text/html", &buf, bytes.NewReader(input)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|