mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-26 05:30:54 +03:00
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code. Also, * Lower case the default output format names; this is in line with the custom ones (map keys) and how it's treated all the places. This avoids doing `stringds.EqualFold` everywhere. Closes #10896 Closes #10620
This commit is contained in:
parent
6aededf6b4
commit
241b21b0fd
337 changed files with 13377 additions and 14898 deletions
|
@ -15,7 +15,6 @@ package minifiers
|
|||
|
||||
import (
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/docshelper"
|
||||
"github.com/gohugoio/hugo/parser"
|
||||
"github.com/spf13/cast"
|
||||
|
@ -29,7 +28,7 @@ import (
|
|||
"github.com/tdewolff/minify/v2/xml"
|
||||
)
|
||||
|
||||
var defaultTdewolffConfig = tdewolffConfig{
|
||||
var defaultTdewolffConfig = TdewolffConfig{
|
||||
HTML: html.Minifier{
|
||||
KeepDocumentTags: true,
|
||||
KeepConditionalComments: true,
|
||||
|
@ -52,7 +51,7 @@ var defaultTdewolffConfig = tdewolffConfig{
|
|||
},
|
||||
}
|
||||
|
||||
type tdewolffConfig struct {
|
||||
type TdewolffConfig struct {
|
||||
HTML html.Minifier
|
||||
CSS css.Minifier
|
||||
JS js.Minifier
|
||||
|
@ -61,7 +60,7 @@ type tdewolffConfig struct {
|
|||
XML xml.Minifier
|
||||
}
|
||||
|
||||
type minifyConfig struct {
|
||||
type MinifyConfig struct {
|
||||
// Whether to minify the published output (the HTML written to /public).
|
||||
MinifyOutput bool
|
||||
|
||||
|
@ -72,30 +71,20 @@ type minifyConfig struct {
|
|||
DisableSVG bool
|
||||
DisableXML bool
|
||||
|
||||
Tdewolff tdewolffConfig
|
||||
Tdewolff TdewolffConfig
|
||||
}
|
||||
|
||||
var defaultConfig = minifyConfig{
|
||||
var defaultConfig = MinifyConfig{
|
||||
Tdewolff: defaultTdewolffConfig,
|
||||
}
|
||||
|
||||
func decodeConfig(cfg config.Provider) (conf minifyConfig, err error) {
|
||||
func DecodeConfig(v any) (conf MinifyConfig, err error) {
|
||||
conf = defaultConfig
|
||||
|
||||
// May be set by CLI.
|
||||
conf.MinifyOutput = cfg.GetBool("minifyOutput")
|
||||
|
||||
v := cfg.Get("minify")
|
||||
if v == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Legacy.
|
||||
if b, ok := v.(bool); ok {
|
||||
conf.MinifyOutput = b
|
||||
return
|
||||
}
|
||||
|
||||
m := maps.ToStringMap(v)
|
||||
|
||||
// Handle upstream renames.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2019 The Hugo Authors. All rights reserved.
|
||||
// Copyright 2023 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -11,18 +11,19 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package minifiers
|
||||
package minifiers_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/config/testconfig"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
v := config.NewWithTestDefaults()
|
||||
v := config.New()
|
||||
|
||||
v.Set("minify", map[string]any{
|
||||
"disablexml": true,
|
||||
|
@ -33,9 +34,7 @@ func TestConfig(t *testing.T) {
|
|||
},
|
||||
})
|
||||
|
||||
conf, err := decodeConfig(v)
|
||||
|
||||
c.Assert(err, qt.IsNil)
|
||||
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
|
||||
|
||||
c.Assert(conf.MinifyOutput, qt.Equals, false)
|
||||
|
||||
|
@ -52,12 +51,11 @@ func TestConfig(t *testing.T) {
|
|||
|
||||
func TestConfigLegacy(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
v := config.NewWithTestDefaults()
|
||||
v := config.New()
|
||||
|
||||
// This was a bool < Hugo v0.58.
|
||||
v.Set("minify", true)
|
||||
|
||||
conf, err := decodeConfig(v)
|
||||
c.Assert(err, qt.IsNil)
|
||||
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
|
||||
c.Assert(conf.MinifyOutput, qt.Equals, true)
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ type Client struct {
|
|||
// Transformer returns a func that can be used in the transformer publishing chain.
|
||||
// TODO(bep) minify config etc
|
||||
func (m Client) Transformer(mediatype media.Type) transform.Transformer {
|
||||
_, params, min := m.m.Match(mediatype.Type())
|
||||
_, params, min := m.m.Match(mediatype.Type)
|
||||
if min == nil {
|
||||
// No minifier for this MIME type
|
||||
return nil
|
||||
|
@ -54,7 +54,7 @@ func (m Client) Transformer(mediatype media.Type) transform.Transformer {
|
|||
|
||||
// Minify tries to minify the src into dst given a MIME type.
|
||||
func (m Client) Minify(mediatype media.Type, dst io.Writer, src io.Reader) error {
|
||||
return m.m.Minify(mediatype.Type(), dst, src)
|
||||
return m.m.Minify(mediatype.Type, dst, src)
|
||||
}
|
||||
|
||||
// noopMinifier implements minify.Minifier [1], but doesn't minify content. This means
|
||||
|
@ -74,13 +74,9 @@ func (m noopMinifier) Minify(_ *minify.M, w io.Writer, r io.Reader, _ map[string
|
|||
// New creates a new Client with the provided MIME types as the mapping foundation.
|
||||
// The HTML minifier is also registered for additional HTML types (AMP etc.) in the
|
||||
// provided list of output formats.
|
||||
func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provider) (Client, error) {
|
||||
conf, err := decodeConfig(cfg)
|
||||
|
||||
func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.AllProvider) (Client, error) {
|
||||
conf := cfg.GetConfigSection("minify").(MinifyConfig)
|
||||
m := minify.New()
|
||||
if err != nil {
|
||||
return Client{}, err
|
||||
}
|
||||
|
||||
// We use the Type definition of the media types defined in the site if found.
|
||||
addMinifier(m, mediaTypes, "css", getMinifier(conf, "css"))
|
||||
|
@ -99,7 +95,7 @@ func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provid
|
|||
addMinifier(m, mediaTypes, "html", getMinifier(conf, "html"))
|
||||
for _, of := range outputFormats {
|
||||
if of.IsHTML {
|
||||
m.Add(of.MediaType.Type(), getMinifier(conf, "html"))
|
||||
m.Add(of.MediaType.Type, getMinifier(conf, "html"))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,7 +104,7 @@ func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provid
|
|||
|
||||
// getMinifier returns the appropriate minify.MinifierFunc for the MIME
|
||||
// type suffix s, given the config c.
|
||||
func getMinifier(c minifyConfig, s string) minify.Minifier {
|
||||
func getMinifier(c MinifyConfig, s string) minify.Minifier {
|
||||
switch {
|
||||
case s == "css" && !c.DisableCSS:
|
||||
return &c.Tdewolff.CSS
|
||||
|
@ -130,6 +126,6 @@ func getMinifier(c minifyConfig, s string) minify.Minifier {
|
|||
func addMinifier(m *minify.M, mt media.Types, suffix string, min minify.Minifier) {
|
||||
types := mt.BySuffix(suffix)
|
||||
for _, t := range types {
|
||||
m.Add(t.Type(), min)
|
||||
m.Add(t.Type, min)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package minifiers
|
||||
package minifiers_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -21,15 +21,17 @@ import (
|
|||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/config/testconfig"
|
||||
"github.com/gohugoio/hugo/media"
|
||||
"github.com/gohugoio/hugo/minifiers"
|
||||
"github.com/gohugoio/hugo/output"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/tdewolff/minify/v2/html"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
v := config.NewWithTestDefaults()
|
||||
m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
|
||||
m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(afero.NewMemMapFs(), nil))
|
||||
|
||||
var rawJS string
|
||||
var minJS string
|
||||
|
@ -46,26 +48,22 @@ func TestNew(t *testing.T) {
|
|||
rawString string
|
||||
expectedMinString string
|
||||
}{
|
||||
{media.CSSType, " body { color: blue; } ", "body{color:blue}"},
|
||||
{media.RSSType, " <hello> Hugo! </hello> ", "<hello>Hugo!</hello>"}, // RSS should be handled as XML
|
||||
{media.JSONType, rawJSON, minJSON},
|
||||
{media.JavascriptType, rawJS, minJS},
|
||||
{media.Builtin.CSSType, " body { color: blue; } ", "body{color:blue}"},
|
||||
{media.Builtin.RSSType, " <hello> Hugo! </hello> ", "<hello>Hugo!</hello>"}, // RSS should be handled as XML
|
||||
{media.Builtin.JSONType, rawJSON, minJSON},
|
||||
{media.Builtin.JavascriptType, rawJS, minJS},
|
||||
// JS Regex minifiers
|
||||
{media.Type{MainType: "application", SubType: "ecmascript"}, rawJS, minJS},
|
||||
{media.Type{MainType: "application", SubType: "javascript"}, rawJS, minJS},
|
||||
{media.Type{MainType: "application", SubType: "x-javascript"}, rawJS, minJS},
|
||||
{media.Type{MainType: "application", SubType: "x-ecmascript"}, rawJS, minJS},
|
||||
{media.Type{MainType: "text", SubType: "ecmascript"}, rawJS, minJS},
|
||||
{media.Type{MainType: "text", SubType: "javascript"}, rawJS, minJS},
|
||||
{media.Type{MainType: "text", SubType: "x-javascript"}, rawJS, minJS},
|
||||
{media.Type{MainType: "text", SubType: "x-ecmascript"}, rawJS, minJS},
|
||||
{media.Type{Type: "application/ecmascript", MainType: "application", SubType: "ecmascript"}, rawJS, minJS},
|
||||
{media.Type{Type: "application/javascript", MainType: "application", SubType: "javascript"}, rawJS, minJS},
|
||||
{media.Type{Type: "application/x-javascript", MainType: "application", SubType: "x-javascript"}, rawJS, minJS},
|
||||
{media.Type{Type: "application/x-ecmascript", MainType: "application", SubType: "x-ecmascript"}, rawJS, minJS},
|
||||
{media.Type{Type: "text/ecmascript", MainType: "text", SubType: "ecmascript"}, rawJS, minJS},
|
||||
{media.Type{Type: "application/javascript", MainType: "text", SubType: "javascript"}, rawJS, minJS},
|
||||
// JSON Regex minifiers
|
||||
{media.Type{MainType: "application", SubType: "json"}, rawJSON, minJSON},
|
||||
{media.Type{MainType: "application", SubType: "x-json"}, rawJSON, minJSON},
|
||||
{media.Type{MainType: "application", SubType: "ld+json"}, rawJSON, minJSON},
|
||||
{media.Type{MainType: "text", SubType: "json"}, rawJSON, minJSON},
|
||||
{media.Type{MainType: "text", SubType: "x-json"}, rawJSON, minJSON},
|
||||
{media.Type{MainType: "text", SubType: "ld+json"}, rawJSON, minJSON},
|
||||
{media.Type{Type: "application/json", MainType: "application", SubType: "json"}, rawJSON, minJSON},
|
||||
{media.Type{Type: "application/x-json", MainType: "application", SubType: "x-json"}, rawJSON, minJSON},
|
||||
{media.Type{Type: "application/ld+json", MainType: "application", SubType: "ld+json"}, rawJSON, minJSON},
|
||||
{media.Type{Type: "application/json", MainType: "text", SubType: "json"}, rawJSON, minJSON},
|
||||
} {
|
||||
var b bytes.Buffer
|
||||
|
||||
|
@ -76,7 +74,7 @@ func TestNew(t *testing.T) {
|
|||
|
||||
func TestConfigureMinify(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
v := config.NewWithTestDefaults()
|
||||
v := config.New()
|
||||
v.Set("minify", map[string]any{
|
||||
"disablexml": true,
|
||||
"tdewolff": map[string]any{
|
||||
|
@ -85,7 +83,7 @@ func TestConfigureMinify(t *testing.T) {
|
|||
},
|
||||
},
|
||||
})
|
||||
m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
|
||||
m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(afero.NewMemMapFs(), v))
|
||||
|
||||
for _, test := range []struct {
|
||||
tp media.Type
|
||||
|
@ -93,9 +91,9 @@ func TestConfigureMinify(t *testing.T) {
|
|||
expectedMinString string
|
||||
errorExpected bool
|
||||
}{
|
||||
{media.HTMLType, "<hello> Hugo! </hello>", "<hello> Hugo! </hello>", false}, // configured minifier
|
||||
{media.CSSType, " body { color: blue; } ", "body{color:blue}", false}, // default minifier
|
||||
{media.XMLType, " <hello> Hugo! </hello> ", " <hello> Hugo! </hello> ", false}, // disable Xml minification
|
||||
{media.Builtin.HTMLType, "<hello> Hugo! </hello>", "<hello> Hugo! </hello>", false}, // configured minifier
|
||||
{media.Builtin.CSSType, " body { color: blue; } ", "body{color:blue}", false}, // default minifier
|
||||
{media.Builtin.XMLType, " <hello> Hugo! </hello> ", " <hello> Hugo! </hello> ", false}, // disable Xml minification
|
||||
} {
|
||||
var b bytes.Buffer
|
||||
if !test.errorExpected {
|
||||
|
@ -110,8 +108,7 @@ func TestConfigureMinify(t *testing.T) {
|
|||
|
||||
func TestJSONRoundTrip(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
v := config.NewWithTestDefaults()
|
||||
m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
|
||||
m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(nil, nil))
|
||||
|
||||
for _, test := range []string{`{
|
||||
"glossary": {
|
||||
|
@ -140,7 +137,7 @@ func TestJSONRoundTrip(t *testing.T) {
|
|||
m1 := make(map[string]any)
|
||||
m2 := make(map[string]any)
|
||||
c.Assert(json.Unmarshal([]byte(test), &m1), qt.IsNil)
|
||||
c.Assert(m.Minify(media.JSONType, &b, strings.NewReader(test)), qt.IsNil)
|
||||
c.Assert(m.Minify(media.Builtin.JSONType, &b, strings.NewReader(test)), qt.IsNil)
|
||||
c.Assert(json.Unmarshal(b.Bytes(), &m2), qt.IsNil)
|
||||
c.Assert(m1, qt.DeepEquals, m2)
|
||||
}
|
||||
|
@ -148,8 +145,8 @@ func TestJSONRoundTrip(t *testing.T) {
|
|||
|
||||
func TestBugs(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
v := config.NewWithTestDefaults()
|
||||
m, _ := New(media.DefaultTypes, output.DefaultFormats, v)
|
||||
v := config.New()
|
||||
m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(nil, v))
|
||||
|
||||
for _, test := range []struct {
|
||||
tp media.Type
|
||||
|
@ -157,9 +154,9 @@ func TestBugs(t *testing.T) {
|
|||
expectedMinString string
|
||||
}{
|
||||
// https://github.com/gohugoio/hugo/issues/5506
|
||||
{media.CSSType, " body { color: rgba(000, 000, 000, 0.7); }", "body{color:rgba(0,0,0,.7)}"},
|
||||
{media.Builtin.CSSType, " body { color: rgba(000, 000, 000, 0.7); }", "body{color:rgba(0,0,0,.7)}"},
|
||||
// https://github.com/gohugoio/hugo/issues/8332
|
||||
{media.HTMLType, "<i class='fas fa-tags fa-fw'></i> Tags", `<i class='fas fa-tags fa-fw'></i> Tags`},
|
||||
{media.Builtin.HTMLType, "<i class='fas fa-tags fa-fw'></i> Tags", `<i class='fas fa-tags fa-fw'></i> Tags`},
|
||||
} {
|
||||
var b bytes.Buffer
|
||||
|
||||
|
@ -171,7 +168,7 @@ func TestBugs(t *testing.T) {
|
|||
// Renamed to Precision in v2.7.0. Check that we support both.
|
||||
func TestDecodeConfigDecimalIsNowPrecision(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
v := config.NewWithTestDefaults()
|
||||
v := config.New()
|
||||
v.Set("minify", map[string]any{
|
||||
"disablexml": true,
|
||||
"tdewolff": map[string]any{
|
||||
|
@ -184,9 +181,8 @@ func TestDecodeConfigDecimalIsNowPrecision(t *testing.T) {
|
|||
},
|
||||
})
|
||||
|
||||
conf, err := decodeConfig(v)
|
||||
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
|
||||
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(conf.Tdewolff.CSS.Precision, qt.Equals, 3)
|
||||
|
||||
}
|
||||
|
@ -194,7 +190,7 @@ func TestDecodeConfigDecimalIsNowPrecision(t *testing.T) {
|
|||
// Issue 9456
|
||||
func TestDecodeConfigKeepWhitespace(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
v := config.NewWithTestDefaults()
|
||||
v := config.New()
|
||||
v.Set("minify", map[string]any{
|
||||
"tdewolff": map[string]any{
|
||||
"html": map[string]any{
|
||||
|
@ -203,9 +199,8 @@ func TestDecodeConfigKeepWhitespace(t *testing.T) {
|
|||
},
|
||||
})
|
||||
|
||||
conf, err := decodeConfig(v)
|
||||
conf := testconfig.GetTestConfigs(nil, v).Base.Minify
|
||||
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(conf.Tdewolff.HTML, qt.DeepEquals,
|
||||
html.Minifier{
|
||||
KeepComments: false,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue