Skip to content

Commit

Permalink
minimize HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Sep 2, 2023
1 parent 2b01894 commit 8a015c5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
32 changes: 16 additions & 16 deletions builder_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,23 @@ title: required

contents := readFile("index.html")
Eventually(contents).ShouldNot(gbytes.Say(`IGNORE ME`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/10.html">some 10 😂 title</a> 10`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/10-some-10-title.html">some 10 😂 title</a> 10`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/09.html">some 9 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/08.html">some 8 😂 title</a>`))
Eventually(contents).ShouldNot(gbytes.Say(`<a href="/posts/07.html">some 7 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/10.html>some 10 😂 title</a> 10`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/10-some-10-title.html>some 10 😂 title</a> 10`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/09.html>some 9 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/08.html>some 8 😂 title</a>`))
Eventually(contents).ShouldNot(gbytes.Say(`<a href=/posts/07.html>some 7 😂 title</a>`))

contents = readFile("index-all.html")
Eventually(contents).Should(gbytes.Say(`<a href="/posts/10.html">some 10 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/09.html">some 9 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/08.html">some 8 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/07.html">some 7 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/06.html">some 6 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/05.html">some 5 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/04.html">some 4 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/03.html">some 3 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/02.html">some 2 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href="/posts/01.html">some 1 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/10.html>some 10 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/09.html>some 9 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/08.html>some 8 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/07.html>some 7 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/06.html>some 6 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/05.html>some 5 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/04.html>some 4 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/03.html>some 3 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/02.html>some 2 😂 title</a>`))
Eventually(contents).Should(gbytes.Say(`<a href=/posts/01.html>some 1 😂 title</a>`))
})
})

Expand Down Expand Up @@ -235,7 +235,7 @@ title: required

buffer := gbytes.BufferWithBytes(contents)

Eventually(buffer).Should(gbytes.Say(`<h2 id="h2-heading">h2 Heading</h2>`))
Eventually(buffer).Should(gbytes.Say(`<h2 id=h2-heading>h2 Heading</h2>`))
})
})
})
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/onsi/gomega v1.27.10
github.com/otiai10/copy v1.12.0
github.com/samber/lo v1.38.1
github.com/tdewolff/minify v2.3.6+incompatible
github.com/yuin/goldmark v1.5.6
github.com/yuin/goldmark-emoji v1.0.2
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
Expand All @@ -34,6 +35,8 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/tdewolff/parse v2.3.4+incompatible // indirect
github.com/tdewolff/test v1.0.9 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.12.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tdewolff/minify v2.3.6+incompatible h1:2hw5/9ZvxhWLvBUnHE06gElGYz+Jv9R4Eys0XUzItYo=
github.com/tdewolff/minify v2.3.6+incompatible/go.mod h1:9Ov578KJUmAWpS6NeZwRZyT56Uf6o3Mcz9CEsg8USYs=
github.com/tdewolff/parse v2.3.4+incompatible h1:x05/cnGwIMf4ceLuDMBOdQ1qGniMoxpP46ghf0Qzh38=
github.com/tdewolff/parse v2.3.4+incompatible/go.mod h1:8oBwCsVmUkgHO8M5iCzSIDtpzXOT0WXX9cWhz+bIzJQ=
github.com/tdewolff/test v1.0.9 h1:SswqJCmeN4B+9gEAi/5uqT0qpi1y2/2O47V/1hhGZT0=
github.com/tdewolff/test v1.0.9/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
Expand Down
11 changes: 10 additions & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/gosimple/slug"
cp "github.com/otiai10/copy"
"github.com/samber/lo"
"github.com/tdewolff/minify"
mHTML "github.com/tdewolff/minify/html"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
highlighting "github.com/yuin/goldmark-highlighting"
Expand Down Expand Up @@ -209,8 +211,15 @@ func writeHTMLFiles(filenames []string, contents string) error {
return fmt.Errorf("could not create path (%s): %w", dirPath, err)
}

writer := &strings.Builder{}

err = mHTML.Minify(&minify.M{}, writer, strings.NewReader(contents), nil)
if err != nil {
return fmt.Errorf("could not minify: %w", err)
}

for _, filename := range filenames {
err = os.WriteFile(filename, []byte(contents), os.ModePerm)
err = os.WriteFile(filename, []byte(writer.String()), os.ModePerm)
if err != nil {
return fmt.Errorf("could not write path (%s): %w", filename, err)
}
Expand Down

0 comments on commit 8a015c5

Please sign in to comment.