-
Notifications
You must be signed in to change notification settings - Fork 1
/
texts.go
47 lines (37 loc) · 1.01 KB
/
texts.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
package linkycore
import "github.com/microcosm-cc/bluemonday"
var stripTagsPolicy = bluemonday.StripTagsPolicy()
// TruncateString - Truncate one string with with x words
func TruncateString(str string, length int, omission string) string {
if length <= 0 {
return ""
}
orgLen := len(str)
if orgLen <= length {
return str
}
if orgLen > length {
return str[:length] + omission
}
return str[:length]
// // Support Japanese
// // Ref: Range loops https://blog.golang.org/strings
// truncated := ""
// count := 0
// for _, char := range str {
// truncated += string(char)
// count++
// if count >= length {
// break
// }
// }
// return truncated
}
// StripTags - Remove tags from html text
func StripTags(str string) string {
return stripTagsPolicy.Sanitize(str)
}
// StripTagsAndTruncate - Remove all tags from string and then truncate if need with omission
func StripTagsAndTruncate(str string, length int, omission string) string {
return TruncateString(StripTags(str), length, omission)
}