diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-08-28 14:18:12 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-08-28 17:00:53 +0200 |
commit | ebb56e8bdbfaf4f955326017e40b2805850871e9 (patch) | |
tree | 64935a5bfaeb5b2cc8e5c600ad2403d32d3de65c /minifiers | |
parent | 6b9934a26615ea614b1774770532cae9762a58d3 (diff) | |
download | hugo-ebb56e8bdbfaf4f955326017e40b2805850871e9.tar.gz hugo-ebb56e8bdbfaf4f955326017e40b2805850871e9.zip |
Improve minifier MIME type resolution
This commit also removes the deprecated `Suffix` from MediaType. Now use `Suffixes` and put the MIME type suffix in the type, e.g. `application/svg+xml`.
Fixes #5093
Diffstat (limited to 'minifiers')
-rw-r--r-- | minifiers/minifiers.go | 57 | ||||
-rw-r--r-- | minifiers/minifiers_test.go | 6 |
2 files changed, 22 insertions, 41 deletions
diff --git a/minifiers/minifiers.go b/minifiers/minifiers.go index 28058dcd8..073898815 100644 --- a/minifiers/minifiers.go +++ b/minifiers/minifiers.go @@ -71,60 +71,35 @@ func New(mediaTypes media.Types, outputFormats output.Formats) Client { } // We use the Type definition of the media types defined in the site if found. - addMinifierFunc(m, mediaTypes, "text/css", "css", css.Minify) - addMinifierFunc(m, mediaTypes, "application/javascript", "js", js.Minify) + addMinifierFunc(m, mediaTypes, "css", css.Minify) + addMinifierFunc(m, mediaTypes, "js", js.Minify) m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify) - addMinifierFunc(m, mediaTypes, "application/json", "json", json.Minify) - addMinifierFunc(m, mediaTypes, "image/svg+xml", "svg", svg.Minify) - addMinifierFunc(m, mediaTypes, "application/xml", "xml", xml.Minify) - addMinifierFunc(m, mediaTypes, "application/rss", "xml", xml.Minify) + addMinifierFunc(m, mediaTypes, "json", json.Minify) + addMinifierFunc(m, mediaTypes, "svg", svg.Minify) + addMinifierFunc(m, mediaTypes, "xml", xml.Minify) // HTML - addMinifier(m, mediaTypes, "text/html", "html", htmlMin) + addMinifier(m, mediaTypes, "html", htmlMin) for _, of := range outputFormats { if of.IsHTML { - addMinifier(m, mediaTypes, of.MediaType.Type(), "html", htmlMin) + m.Add(of.MediaType.Type(), htmlMin) } } - return Client{m: m} -} + return Client{m: m} -func addMinifier(m *minify.M, mt media.Types, typeString, suffix string, min minify.Minifier) { - resolvedTypeStr := resolveMediaTypeString(mt, typeString, suffix) - m.Add(resolvedTypeStr, min) - if resolvedTypeStr != typeString { - m.Add(typeString, min) - } } -func addMinifierFunc(m *minify.M, mt media.Types, typeString, suffix string, fn minify.MinifierFunc) { - resolvedTypeStr := resolveMediaTypeString(mt, typeString, suffix) - m.AddFunc(resolvedTypeStr, fn) - if resolvedTypeStr != typeString { - m.AddFunc(typeString, fn) +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) } } -func resolveMediaTypeString(types media.Types, typeStr, suffix string) string { - if m, found := resolveMediaType(types, typeStr, suffix); found { - return m.Type() +func addMinifierFunc(m *minify.M, mt media.Types, suffix string, min minify.MinifierFunc) { + types := mt.BySuffix(suffix) + for _, t := range types { + m.AddFunc(t.Type(), min) } - // Fall back to the default. - return typeStr -} - -// Make sure we match the matching pattern with what the user have actually defined -// in his or hers media types configuration. -func resolveMediaType(types media.Types, typeStr, suffix string) (media.Type, bool) { - if m, found := types.GetByType(typeStr); found { - return m, true - } - - if m, found := types.GetFirstBySuffix(suffix); found { - return m, true - } - - return media.Type{}, false - } diff --git a/minifiers/minifiers_test.go b/minifiers/minifiers_test.go index 6d72dc44e..a0f0f97b4 100644 --- a/minifiers/minifiers_test.go +++ b/minifiers/minifiers_test.go @@ -32,4 +32,10 @@ func TestNew(t *testing.T) { assert.NoError(m.Minify(media.CSSType, &b, strings.NewReader("body { color: blue; }"))) assert.Equal("body{color:blue}", b.String()) + + b.Reset() + + // RSS should be handled as XML + assert.NoError(m.Minify(media.RSSType, &b, strings.NewReader("<hello> Hugo! </hello> "))) + assert.Equal("<hello>Hugo!</hello>", b.String()) } |