-
Notifications
You must be signed in to change notification settings - Fork 1
/
sanitize.go
62 lines (51 loc) · 954 Bytes
/
sanitize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"strings"
"unicode"
"unicode/utf8"
)
const (
HTML_TAG_START = 60 // Unicode `<`
HTML_TAG_END = 62 // Unicode `>`
)
func removeAllWhitespace(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, str)
}
// See https://stackoverflow.com/a/64701836
func stripTags(s string) string {
var builder strings.Builder
builder.Grow(len(s) + utf8.UTFMax)
in := false
start := 0
end := 0
for i, c := range s {
if (i+1) == len(s) && end >= start {
builder.WriteString(s[end:])
}
if c != HTML_TAG_START && c != HTML_TAG_END {
continue
}
if c == HTML_TAG_START {
if !in {
start = i
}
in = true
builder.WriteString(s[end:start])
continue
}
in = false
end = i + 1
}
s = builder.String()
return s
}
func sanitizeHtml(s string) string {
content := stripTags(s)
content = removeAllWhitespace(content)
return content
}