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
26 // inline appends one line of Markdown with its inline marks as entities. A
27 // backslash before an ASCII punctuation character makes the character plain
29 func (m *TelegramMarkdownText) inline(line string) {
32 for index < len(line) {
33 if line[index] == '\\' && index+1 < len(line) && strings.IndexByte(markdownPunctuation, line[index+1]) >= 0 {
34 m.add(line[plain:index])
42 span, found := markdownMarkSpan(line, index)
48 m.add(line[plain:index])
51 if span.kind == "code" {
54 m.inline(span.content)
57 m.entity(TelegramEntity{
60 Length: m.units - start,
72 func (m *TelegramMarkdownText) add(text string) {
73 m.text.WriteString(text)
75 m.units += telegramUTF16Units(text)
78 // entity keeps entity, unless it is empty, because Telegram refuses an entity
80 func (m *TelegramMarkdownText) entity(entity TelegramEntity) {
81 if entity.Length > 0 {
82 m.entities = append(m.entities, entity)
86 // telegramUTF16Units returns the number of UTF-16 code units of text.
87 func telegramUTF16Units(text string) int {
89 for _, char := range text {
99 // telegramMarkdownEntities converts the Markdown of a reply to plain text and
100 // Telegram message entities. It knows bold (** and __), italic (* and _),
101 // strikethrough (~~), inline code, fenced code blocks with an optional
102 // language, links [text](url), and ATX headings, which become bold. Lists,
103 // quotes, and tables stay as their text. A mark that does not close stays as
104 // text with no entity, and code holds no entity.
106 // Inline marks do not cross a line break, so a mark that does not close cannot
107 // change the rest of the reply.
108 func telegramMarkdownEntities(markdown string) (string, []TelegramEntity) {
109 var out TelegramMarkdownText
111 lines := strings.Split(markdown, "\n")
112 for index := 0; index < len(lines); index++ {
118 language, fence := markdownCodeFence(line)
119 end := markdownClosingFence(lines, index, fence)
120 heading := markdownHeadingText(line)
124 out.add(strings.Join(lines[index+1:end], "\n"))
125 out.entity(TelegramEntity{
128 Length: out.units - start,
136 out.entity(TelegramEntity{
139 Length: out.units - start,
146 slices.SortStableFunc(out.entities, func(a, b TelegramEntity) int {
147 if a.Offset != b.Offset {
148 return a.Offset - b.Offset
151 return b.Length - a.Length
154 return out.text.String(), out.entities
157 // telegramEscapeTags writes each & of markdown outside inline code and fenced
158 // code blocks as & and each < as <. Rich Markdown reads raw HTML and
159 // HTML entities in prose, and it does not read a backslash before < as an
160 // escape. With the entities, it shows a tag name and an entity that the model
161 // wrote in prose as text.
162 func telegramEscapeTags(markdown string) string {
163 return telegramRewriteProse(markdown, telegramTagEscaper.Replace)
166 // telegramRewriteProse returns markdown with rewrite applied to each part of a
167 // line outside inline code and fenced code blocks. A code block that does not
168 // close runs to the end, as in Markdown.
169 func telegramRewriteProse(markdown string, rewrite func(string) string) string {
170 var out strings.Builder
172 lines := strings.Split(markdown, "\n")
173 for index := 0; index < len(lines); index++ {
175 out.WriteString("\n")
179 _, fence := markdownCodeFence(line)
181 telegramRewriteLineProse(&out, line, rewrite)
186 end := markdownClosingFence(lines, index, fence)
191 out.WriteString(strings.Join(lines[index:end+1], "\n"))
199 // telegramRewriteLineProse writes line to out with rewrite applied to each part
200 // outside inline code. A backtick after a backslash opens no code. A run of
201 // backticks that opens no inline code stays as text whole, as in Markdown.
202 func telegramRewriteLineProse(out *strings.Builder, line string, rewrite func(string) string) {
204 for index := 0; index < len(line); {
207 if index+1 < len(line) && strings.IndexByte(markdownPunctuation, line[index+1]) >= 0 {
212 end := index + markdownBackticks(line[index:])
213 span, found := markdownCodeSpan(line, index)
218 out.WriteString(rewrite(line[prose:index]))
219 out.WriteString(line[index:end])
230 out.WriteString(rewrite(line[prose:]))
233 // telegramUnescapeTags gives back the Markdown that telegramEscapeTags escaped.
234 // It is exact, because after the escape each & outside code starts & or
235 // <. It also works on a part of telegramSplitRich, because a part has the
236 // same code as the text that it comes from.
237 func telegramUnescapeTags(markdown string) string {
238 return telegramRewriteProse(markdown, telegramTagUnescaper.Replace)