-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentities.go
62 lines (56 loc) · 1.46 KB
/
entities.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 entities
import (
"unicode/utf16"
tb "gopkg.in/tucnak/telebot.v2"
)
var needEscape = make(map[rune]struct{})
func init() {
for _, r := range []rune{'_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'} {
needEscape[r] = struct{}{}
}
}
func ConvertToMarkdownV2(text string, messageEntities []tb.MessageEntity) string {
insertions := make(map[int]string)
for _, e := range messageEntities {
var before, after string
if e.Type == tb.EntityBold {
before = "*"
after = "*"
} else if e.Type == tb.EntityItalic {
before = "_"
after = "_"
} else if e.Type == tb.EntityUnderline {
before = "__"
after = "__"
} else if e.Type == tb.EntityStrikethrough {
before = "~"
after = "~"
} else if e.Type == tb.EntityCode {
before = "`"
after = "`"
} else if e.Type == tb.EntityCodeBlock {
before = "```" + e.Language
after = "```"
} else if e.Type == tb.EntityTextLink {
before = "["
after = "](" + e.URL + ")"
}
if before != "" {
insertions[e.Offset] += before
insertions[e.Offset+e.Length] += after
}
}
input := []rune(text)
var output []rune
utf16pos := 0
for _, c := range input {
output = append(output, []rune(insertions[utf16pos])...)
if _, has := needEscape[c]; has {
output = append(output, '\\')
}
output = append(output, c)
utf16pos += len(utf16.Encode([]rune{c}))
}
output = append(output, []rune(insertions[utf16pos])...)
return string(output)
}