Go Sanitise HTML
A simple library to sanitise untrusted HTML. Unwraps any element not in the
white-list, and the same for attributes of allowed elements.
Usage
go get git.sr.ht/~gc/go-sanitise-html
You can sanitise a HTML string with the following example:
package main
import (
"fmt"
"git.sr.ht/~gc/go-sanitise-html"
)
func main() {
s := sanitisehtml.New(map[string][]string{
"p": {},
"div": {},
"a": {"href"},
})
safe, err := s.Sanitise("<div><p>Hello <strong>World</strong></p> - <a href=\"https://georgecraggs.co.uk\" target=\"_blank\">Link</a></div>")
if err != nil {
fmt.Printf("Error parsing: %s", err)
}
fmt.Println(safe)
// "<div><p>Hello World</p> - <a href="https://georgecraggs.co.uk">Link</a></div>"
}
License
LGPL-3.0-only