mirror of
https://github.com/gohugoio/hugo.git
synced 2024-12-28 13:10:48 +00:00
minifiers: Make JSON minification more generic
Add a regex matcher for json types. Specifically support LD+JSON which allows for google seo minification out of the box. Expanded JS/JSON minification testing.
This commit is contained in:
parent
37d6463479
commit
3dafe206e3
2 changed files with 40 additions and 7 deletions
|
@ -74,6 +74,7 @@ func New(mediaTypes media.Types, outputFormats output.Formats) Client {
|
||||||
addMinifierFunc(m, mediaTypes, "css", css.Minify)
|
addMinifierFunc(m, mediaTypes, "css", css.Minify)
|
||||||
addMinifierFunc(m, mediaTypes, "js", js.Minify)
|
addMinifierFunc(m, mediaTypes, "js", js.Minify)
|
||||||
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
|
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
|
||||||
|
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-|ld\\+)?json$"), json.Minify)
|
||||||
addMinifierFunc(m, mediaTypes, "json", json.Minify)
|
addMinifierFunc(m, mediaTypes, "json", json.Minify)
|
||||||
addMinifierFunc(m, mediaTypes, "svg", svg.Minify)
|
addMinifierFunc(m, mediaTypes, "svg", svg.Minify)
|
||||||
addMinifierFunc(m, mediaTypes, "xml", xml.Minify)
|
addMinifierFunc(m, mediaTypes, "xml", xml.Minify)
|
||||||
|
|
|
@ -28,14 +28,46 @@ func TestNew(t *testing.T) {
|
||||||
assert := require.New(t)
|
assert := require.New(t)
|
||||||
m := New(media.DefaultTypes, output.DefaultFormats)
|
m := New(media.DefaultTypes, output.DefaultFormats)
|
||||||
|
|
||||||
var b bytes.Buffer
|
var rawJS string
|
||||||
|
var minJS string
|
||||||
|
rawJS = " var foo =1 ; foo ++ ; "
|
||||||
|
minJS = "var foo=1;foo++;"
|
||||||
|
|
||||||
assert.NoError(m.Minify(media.CSSType, &b, strings.NewReader("body { color: blue; }")))
|
var rawJSON string
|
||||||
assert.Equal("body{color:blue}", b.String())
|
var minJSON string
|
||||||
|
rawJSON = " { \"a\" : 123 , \"b\":2, \"c\": 5 } "
|
||||||
|
minJSON = "{\"a\":123,\"b\":2,\"c\":5}"
|
||||||
|
|
||||||
b.Reset()
|
for _, test := range []struct {
|
||||||
|
tp media.Type
|
||||||
|
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},
|
||||||
|
// 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},
|
||||||
|
// 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},
|
||||||
|
} {
|
||||||
|
var b bytes.Buffer
|
||||||
|
|
||||||
|
assert.NoError(m.Minify(test.tp, &b, strings.NewReader(test.rawString)))
|
||||||
|
assert.Equal(test.expectedMinString, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
// 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())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue