How to create html minifier?
package main
import (
"fmt"
)
func HtmlMinify(html string) string {
// todo: minify html
return html
}
func main() {
htmlExample := `<li>
<a>Hello</a>
</li>`
minifiedHtml := HtmlMinify(htmlExample)
fmt.Println(minifiedHtml) // `<li><a>Hello</a></li>` is wanted
}
outputs:
<li>
<a>Hello</a>
</li>
But I want it to be
<li><a>Hello</a></li>
playground
Your example could involve simply removing spaces, but minifying html is a bit more complex than that (for instance, you don't want to remove spaces where they actually matters, like within a string).
You can see an example in:
I used tdewolff/minify:
package main
import (
"bytes"
"fmt"
"github.com/tdewolff/minify"
)
func HtmlMinify(html string) string {
m := minify.NewMinifierDefault()
b := &bytes.Buffer{}
if err := m.HTML(b, bytes.NewBufferString(html)); err != nil {
panic(err)
}
return b.String()
}
func main() {
htmlExample := `<li>
<a>Hello</a>
</li>`
minifiedHtml := HtmlMinify(htmlExample)
fmt.Println(minifiedHtml) // <li><a>Hello</a>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With