-
Notifications
You must be signed in to change notification settings - Fork 1
/
striphtml.go
65 lines (54 loc) · 964 Bytes
/
striphtml.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
63
64
65
package util
import (
"github.com/microcosm-cc/bluemonday"
"html"
"strings"
"sync"
"unicode/utf8"
)
const newLine = rune('\n')
const space = rune(' ')
var (
p *bluemonday.Policy
mu sync.Mutex
)
func init() {
mu.Lock()
defer mu.Unlock()
p = bluemonday.StrictPolicy()
}
func StripNewLine(s string, maxNewLine int) string {
var builder strings.Builder
builder.Grow(len(s) + utf8.UTFMax)
sNewLine := 0
sSpace := 0
for _, c := range s {
if c == newLine {
sNewLine++
if sNewLine <= maxNewLine {
builder.WriteRune(c)
}
continue
}
if c == space {
sSpace++
if sSpace == 1 {
builder.WriteRune(c)
}
continue
}
builder.WriteRune(c)
sNewLine = 0
sSpace = 0
}
return strings.TrimSpace(builder.String())
}
func StripHTMLTags(s string) string {
mu.Lock()
defer mu.Unlock()
s = html.UnescapeString(s)
s = p.Sanitize(s)
s = html.UnescapeString(s)
s = StripNewLine(s, 2)
return strings.TrimSpace(s)
}