Minify exclusions (#31)

* add minify exclusions support

* load exclusions

* fix bug

* load minify

* fix yaml type coersion

* skip dot files when building
This commit is contained in:
Facundo Olano 2024-03-15 19:55:50 -03:00 committed by GitHub
parent c2c981c156
commit 7bc5baa253
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 29 deletions

View file

@ -32,6 +32,7 @@ type Config struct {
HighlightTheme string
Minify bool
MinifyExclusions []string
LiveReload bool
LinkStatic bool
IncludeDrafts bool
@ -60,6 +61,7 @@ func Load(rootDir string) (*Config, error) {
Lang: "en",
HighlightTheme: "github",
Minify: true,
MinifyExclusions: make([]string, 0),
LiveReload: false,
LinkStatic: false,
IncludeDrafts: false,
@ -83,18 +85,23 @@ func Load(rootDir string) (*Config, error) {
}
// set user-provided overrides of declared config keys
// TODO less copypasty way of declaring config overrides
// FIXME less copypasty way of declaring config overrides
if url, found := config.overrides["url"]; found {
config.SiteUrl = url.(string)
}
if format, found := config.overrides["post_format"]; found {
config.PostFormat = format.(string)
}
if format, found := config.overrides["lang"]; found {
config.Lang = format.(string)
if lang, found := config.overrides["lang"]; found {
config.Lang = lang.(string)
}
if theme, found := config.overrides["highlight_theme"]; found {
config.HighlightTheme = theme.(string)
}
if exclusions, found := config.overrides["minify_exclusions"]; found {
for _, exclusion := range exclusions.([]interface{}) {
config.MinifyExclusions = append(config.MinifyExclusions, exclusion.(string))
}
if format, found := config.overrides["highlight_theme"]; found {
config.HighlightTheme = format.(string)
}
return config, nil

View file

@ -2,6 +2,7 @@ package markup
import (
"io"
"path/filepath"
"slices"
"github.com/tdewolff/minify/v2"
@ -15,19 +16,27 @@ var SUPPORTED_MINIFIERS = []string{".css", ".html", ".js", ".xml"}
type Minifier struct {
minifier *minify.M
exclusions []string
}
func LoadMinifier() Minifier {
func LoadMinifier(exclusions []string) Minifier {
minifier := minify.New()
minifier.AddFunc(".css", css.Minify)
minifier.AddFunc(".html", html.Minify)
minifier.AddFunc(".js", js.Minify)
minifier.AddFunc(".xml", xml.Minify)
return Minifier{minifier}
return Minifier{minifier, exclusions}
}
func (m *Minifier) Minify(extension string, contentReader io.Reader) io.Reader {
func (m *Minifier) Minify(path string, contentReader io.Reader) io.Reader {
for _, exclusion := range m.exclusions {
if matched, _ := filepath.Match(exclusion, path); matched {
return contentReader
}
}
extension := filepath.Ext(path)
if !slices.Contains(SUPPORTED_MINIFIERS, extension) {
return contentReader
}

View file

@ -72,7 +72,7 @@ func load(config config.Config) (*site, error) {
return nil, err
}
site.minifier = markup.LoadMinifier()
site.minifier = markup.LoadMinifier(config.MinifyExclusions)
return &site, nil
}
@ -256,6 +256,10 @@ func (site *site) build() error {
if err != nil {
return err
}
if strings.HasPrefix(filepath.Base(path), ".") {
// skip dot files and directories
return nil
}
subpath, _ := filepath.Rel(site.config.SrcDir, path)
targetPath := filepath.Join(site.config.TargetDir, subpath)
@ -338,7 +342,7 @@ func (site *site) buildFile(path string) error {
return err
}
if site.config.Minify {
contentReader = site.minifier.Minify(targetExt, contentReader)
contentReader = site.minifier.Minify(subpath, contentReader)
}
// write the file contents over to target