Repository navigation
Expand file tree
/
Copy pathmarkdown.go
More file actions
163 lines (125 loc) · 4.45 KB
/
Copy pathmarkdown.go
File metadata and controls
163 lines (125 loc) · 4.45 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package tg_markdown
import (
"fmt"
"regexp"
"strings"
)
func escapeTelegramMarkdownV2(text string) string {
telegramSpecialChars := "_*[]()~`>#+-=|{}.!"
for _, char := range telegramSpecialChars {
text = strings.ReplaceAll(text, string(char), "\\"+string(char))
}
return text
}
func escapeTelegramMarkdownV2CodeBlocks(text string) string {
backslashRegex := regexp.MustCompile(`\\([^ ]|\\|$)`)
text = backslashRegex.ReplaceAllStringFunc(text, func(match string) string {
if len(match) > 1 {
return fmt.Sprintf("\\\\%s", match[1:])
}
return "\\\\"
})
text = escapeTelegramMarkdownV2(text)
return text
}
func wrapLinksInMarkdown(text string) string {
re := regexp.MustCompile(`\bhttps?://[a-zA-Z0-9-.]+(.[a-zA-Z]{2,})(:[0-9]{1,5})?(/[a-zA-Z0-9-_.~%/?#=&+]*)?\b`)
wrappedText := re.ReplaceAllStringFunc(text, func(link string) string {
if strings.Contains(link, "](http") || strings.Contains(link, "[http") {
return link
}
return fmt.Sprintf("[%s](%s)", link, link)
})
return wrappedText
}
func removePlaceholders(text string) string {
re := regexp.MustCompile(`ELEMENTPLACEHOLDER\d+|CODEBLOCKPLACEHOLDER\d+|INLINECODEPLACEHOLDER\d+|LINKPLACEHOLDER\d`)
result := re.ReplaceAllString(text, "")
return result
}
func addPlaceholders(text string, elements map[string]string) (string, map[string]string) {
codeBlockRegex := regexp.MustCompile("```(?s:.*?)```")
inlineCodeRegex := regexp.MustCompile("`[^`]+`")
linkRegex := regexp.MustCompile(`\[[^\]]+\]\([^\)]+\)`)
elementsRegex := regexp.MustCompile(fmt.Sprintf("%s|%s|%s", codeBlockRegex.String(), inlineCodeRegex.String(), linkRegex.String()))
matches := elementsRegex.FindAllString(text, -1)
for i, match := range matches {
placeholder := fmt.Sprintf("ELEMENTPLACEHOLDER%d", len(elements)+i)
// Определяем тип элемента и добавляем соответствующий маркер
switch {
case codeBlockRegex.MatchString(match):
placeholder = fmt.Sprintf("CODEBLOCKPLACEHOLDER%d", len(elements)+i)
case inlineCodeRegex.MatchString(match):
placeholder = fmt.Sprintf("INLINECODEPLACEHOLDER%d", len(elements)+i)
case linkRegex.MatchString(match):
placeholder = fmt.Sprintf("LINKPLACEHOLDER%d", len(elements)+i)
}
elements[placeholder] = match
text = strings.Replace(text, match, placeholder, 1)
}
return text, elements
}
func processPlaceholders(md string, elements map[string]string) string {
for placeholder, element := range elements {
if strings.HasPrefix(placeholder, "LINKPLACEHOLDER") {
parts := strings.SplitN(element, "](", 2)
linkText := parts[0][1:]
linkURL := parts[1][:len(parts[1])-1]
linkText = escapeTelegramMarkdownV2(linkText)
element = "[" + linkText + "](" + linkURL + ")"
}
if strings.HasPrefix(placeholder, "CODEBLOCKPLACEHOLDER") {
re := regexp.MustCompile("(?s)```([a-zA-Z]*)\\n(.*?)\\n```")
element = re.ReplaceAllStringFunc(element, func(block string) string {
matches := re.FindStringSubmatch(block)
if len(matches) > 2 {
language := matches[1]
return fmt.Sprintf("```%s\n%s\n```", language, escapeTelegramMarkdownV2CodeBlocks(matches[2]))
}
return block
})
}
if strings.HasPrefix(placeholder, "INLINECODEPLACEHOLDER") {
re := regexp.MustCompile("`([^`]+)`")
element = re.ReplaceAllStringFunc(element, func(block string) string {
matches := re.FindStringSubmatch(block)
if len(matches) > 1 {
return fmt.Sprintf("`%s`", escapeTelegramMarkdownV2CodeBlocks(matches[1]))
}
return block
})
}
md = strings.Replace(md, placeholder, element, 1)
}
return md
}
func processStyles(md string) string {
replacements := []struct {
regex *regexp.Regexp
replacement string
}{
// bold text
{regexp.MustCompile(`\\\*\\\*(.*?)\\\*\\\*`), `*$1*`},
// italic text
{regexp.MustCompile(`\\\*(.*?)\\\*`), `_${1}_`},
{regexp.MustCompile(`\\_(.*?)\\_`), `_${1}_`},
// strikethrough text
{regexp.MustCompile(`\\~\\~(.*?)\\~\\~`), `~$1~`},
{regexp.MustCompile(`\\~(.*?)\\~`), `~$1~`},
}
for _, r := range replacements {
md = r.regex.ReplaceAllString(md, r.replacement)
}
return md
}
func ConvertMarkdownToTelegramMarkdownV2(md string) string {
elements := make(map[string]string)
md = removePlaceholders(md)
md, elements = addPlaceholders(md, elements)
md = wrapLinksInMarkdown(md)
md, elements = addPlaceholders(md, elements)
md = escapeTelegramMarkdownV2(md)
md = processStyles(md)
md = processPlaceholders(md, elements)
return md
}