9 // telegramTagEscaper writes the characters that rich Markdown reads as
10 // HTML as HTML entities.
11 telegramTagEscaper = strings.NewReplacer("&", "&", "<", "<")
13 // telegramTagUnescaper reverses telegramTagEscaper.
14 telegramTagUnescaper = strings.NewReplacer("&", "&", "<", "<")
17 // TelegramMarkdownText is the plain text of a Markdown reply and its Telegram
18 // entities, while the conversion builds it. Units counts the UTF-16 code units
19 // of text, the unit of the entity offsets and lengths.
20 type TelegramMarkdownText struct {
23 entities []TelegramEntity
27 func (m *TelegramMarkdownText) add(text string) {
28 m.text.WriteString(text)
30 m.units += telegramUTF16Units(text)
33 // entity keeps entity, unless it is empty, because Telegram refuses an entity
35 func (m *TelegramMarkdownText) entity(entity TelegramEntity) {
36 if entity.Length > 0 {
37 m.entities = append(m.entities, entity)
41 // inline appends one line of Markdown with its inline marks as entities. A
42 // backslash before an ASCII punctuation character makes the character plain
44 func (m *TelegramMarkdownText) inline(line string) {
47 for index < len(line) {
48 if line[index] == '\\' && index+1 < len(line) && strings.IndexByte(markdownPunctuation, line[index+1]) >= 0 {
49 m.add(line[plain:index])
57 span, found := markdownMarkSpan(line, index)
63 m.add(line[plain:index])
66 if span.kind == "code" {
69 m.inline(span.content)
72 m.entity(TelegramEntity{
75 Length: m.units - start,
86 // telegramMarkdownEntities converts the Markdown of a reply to plain text and
87 // Telegram message entities. It knows bold (** and __), italic (* and _),
88 // strikethrough (~~), inline code, fenced code blocks with an optional
89 // language, links [text](url), and ATX headings, which become bold. Lists,
90 // quotes, and tables stay as their text. A mark that does not close stays as
91 // text with no entity, and code holds no entity.
93 // Inline marks do not cross a line break, so a mark that does not close cannot
94 // change the rest of the reply.
95 func telegramMarkdownEntities(markdown string) (string, []TelegramEntity) {
96 var out TelegramMarkdownText
98 lines := strings.Split(markdown, "\n")
99 for index := 0; index < len(lines); index++ {
105 language, fence := markdownCodeFence(line)
106 end := markdownClosingFence(lines, index, fence)
107 heading := markdownHeadingText(line)
111 out.add(strings.Join(lines[index+1:end], "\n"))
112 out.entity(TelegramEntity{
115 Length: out.units - start,
123 out.entity(TelegramEntity{
126 Length: out.units - start,
133 slices.SortStableFunc(out.entities, func(a, b TelegramEntity) int {
134 if a.Offset != b.Offset {
135 return a.Offset - b.Offset
138 return b.Length - a.Length
141 return out.text.String(), out.entities
144 // telegramEscapeTags writes each & of markdown outside inline code and fenced
145 // code blocks as & and each < as <. Rich Markdown reads raw HTML and
146 // HTML entities in prose, and it does not read a backslash before < as an
147 // escape. With the entities, it shows a tag name and an entity that the model
148 // wrote in prose as text.
149 func telegramEscapeTags(markdown string) string {
150 return telegramRewriteProse(markdown, telegramTagEscaper.Replace)
153 // telegramUnescapeTags gives back the Markdown that telegramEscapeTags escaped.
154 // It is exact, because after the escape each & outside code starts & or
155 // <. It also works on a part of telegramSplitRich, because a part has the
156 // same code as the text that it comes from.
157 func telegramUnescapeTags(markdown string) string {
158 return telegramRewriteProse(markdown, telegramTagUnescaper.Replace)
161 // telegramUTF16Units returns the number of UTF-16 code units of text.
162 func telegramUTF16Units(text string) int {
164 for _, char := range text {
174 // telegramRewriteProse returns markdown with rewrite applied to each part of a
175 // line outside inline code and fenced code blocks. A code block that does not
176 // close runs to the end, as in Markdown.
177 func telegramRewriteProse(markdown string, rewrite func(string) string) string {
178 var out strings.Builder
180 lines := strings.Split(markdown, "\n")
181 for index := 0; index < len(lines); index++ {
183 out.WriteString("\n")
187 _, fence := markdownCodeFence(line)
189 telegramRewriteLineProse(&out, line, rewrite)
194 end := markdownClosingFence(lines, index, fence)
199 out.WriteString(strings.Join(lines[index:end+1], "\n"))
207 // telegramRewriteLineProse writes line to out with rewrite applied to each part
208 // outside inline code. A backtick after a backslash opens no code. A run of
209 // backticks that opens no inline code stays as text whole, as in Markdown.
210 func telegramRewriteLineProse(out *strings.Builder, line string, rewrite func(string) string) {
212 for index := 0; index < len(line); {
215 if index+1 < len(line) && strings.IndexByte(markdownPunctuation, line[index+1]) >= 0 {
220 end := index + markdownBackticks(line[index:])
221 span, found := markdownCodeSpan(line, index)
226 out.WriteString(rewrite(line[prose:index]))
227 out.WriteString(line[index:end])
238 out.WriteString(rewrite(line[prose:]))