2022-12-29 08:30:06 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StripHTML will strip HTML tags from a string.
|
|
|
|
func StripHTML(s string) string {
|
|
|
|
p := bluemonday.NewPolicy()
|
|
|
|
return p.Sanitize(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeSafeStringOfLength will take a string and strip HTML tags,
|
|
|
|
// trim whitespace, and limit the length.
|
|
|
|
func MakeSafeStringOfLength(s string, length int) string {
|
|
|
|
newString := s
|
|
|
|
newString = StripHTML(newString)
|
|
|
|
|
2023-07-11 20:44:09 +03:00
|
|
|
// Convert utf-8 string into Unicode code points.
|
|
|
|
codePoints := []rune(newString)
|
|
|
|
|
|
|
|
if len(codePoints) > length {
|
|
|
|
codePoints = codePoints[:length]
|
2022-12-29 08:30:06 +03:00
|
|
|
}
|
|
|
|
|
2023-07-11 20:44:09 +03:00
|
|
|
newString = string(codePoints)
|
2022-12-29 08:30:06 +03:00
|
|
|
newString = strings.ReplaceAll(newString, "\r", "")
|
|
|
|
newString = strings.TrimSpace(newString)
|
|
|
|
|
|
|
|
return newString
|
|
|
|
}
|