@@ -4,20 +4,10 @@ import (
44 "github.com/microcosm-cc/bluemonday"
55)
66
7- type ContentFilter struct {
8- HTMLPolicy * bluemonday.Policy
9- }
10-
11- func NewContentFilter () * ContentFilter {
12- p := bluemonday .NewPolicy ()
13- p .AllowElements ("b" , "blockquote" , "br" , "code" , "em" , "h1" , "h2" , "h3" , "h4" , "h5" , "h6" , "hr" , "i" , "li" , "ol" , "p" , "pre" , "strong" , "sub" , "sup" , "table" , "tbody" , "td" , "th" , "thead" , "tr" , "ul" )
14- p .AllowAttrs ("img" , "a" )
15- p .AllowAttrs ()
16- p .AllowURLSchemes ("https" )
7+ var policy * bluemonday.Policy
178
18- return & ContentFilter {
19- HTMLPolicy : p ,
20- }
9+ func Sanitize (input string ) string {
10+ return FilterHTMLTags (FilterInvisibleCharacters (input ))
2111}
2212
2313// FilterInvisibleCharacters removes invisible or control characters that should not appear
@@ -40,11 +30,25 @@ func FilterInvisibleCharacters(input string) string {
4030 return string (out )
4131}
4232
43- func (cf * ContentFilter ) FilterHtmlTags (input string ) string {
33+ func FilterHTMLTags (input string ) string {
34+ if policy == nil {
35+ policyInit ()
36+ }
4437 if input == "" {
4538 return input
4639 }
47- return cf .HTMLPolicy .Sanitize (input )
40+ return policy .Sanitize (input )
41+ }
42+
43+ func policyInit () {
44+ if policy != nil {
45+ return
46+ }
47+ policy = bluemonday .StrictPolicy ()
48+ policy .AllowElements ("b" , "blockquote" , "br" , "code" , "em" , "h1" , "h2" , "h3" , "h4" , "h5" , "h6" , "hr" , "i" , "li" , "ol" , "p" , "pre" , "strong" , "sub" , "sup" , "table" , "tbody" , "td" , "th" , "thead" , "tr" , "ul" )
49+ policy .AllowAttrs ("img" , "a" )
50+ policy .AllowURLSchemes ("https" )
51+ policy .AllowImages ()
4852}
4953
5054func shouldRemoveRune (r rune ) bool {
0 commit comments