-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: strip HTML
- Loading branch information
Showing
4 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package utils | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/microcosm-cc/bluemonday" | ||
) | ||
|
||
var ( | ||
bmStrict *bluemonday.Policy | ||
once sync.Once | ||
) | ||
|
||
// StripHTML strips all HTML from a string, duh | ||
func StripHTML(s string) string { | ||
once.Do(func() { | ||
bmStrict = bluemonday.StrictPolicy() | ||
}) | ||
return bmStrict.Sanitize(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_StripHTML(t *testing.T) { | ||
t.Run("no html", func(t *testing.T) { | ||
assert.Equal(t, "podo wae masbro", | ||
StripHTML("podo wae masbro")) | ||
}) | ||
|
||
t.Run("with html", func(t *testing.T) { | ||
assert.Equal(t, "dang sarupa bah", | ||
StripHTML("<a href=\"http://lappetkaw.com\">dang sarupa bah</a>")) | ||
}) | ||
|
||
t.Run("with incomplete html", func(t *testing.T) { | ||
assert.Equal(t, ">plz click hier", | ||
StripHTML("><a href='https://www.downloadmoreram.com'>plz click hier")) | ||
}) | ||
} |