Skip to content

Commit 6377774

Browse files
authored
Merge pull request #192 from el-garro/master
Fixed InsertTagsIntoText breaking emojis
2 parents 28dcef3 + c1b5cf4 commit 6377774

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

telegram/formatting.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ func ParseEntitiesToTags(entities []MessageEntity) []Tag {
246246

247247
func InsertTagsIntoText(text string, tags []Tag) string {
248248
utf16Text := utf16.Encode([]rune(text))
249-
var result strings.Builder
250249
openTags := make(map[int][]Tag)
251250
closeTags := make(map[int][]Tag)
252251

@@ -255,30 +254,36 @@ func InsertTagsIntoText(text string, tags []Tag) string {
255254
closeTags[int(tag.Offset+tag.Length)] = append(closeTags[int(tag.Offset+tag.Length)], tag)
256255
}
257256

257+
result := make([]uint16, len(utf16Text))
258258
for i := 0; i < len(utf16Text); i++ {
259259
if opening, exists := openTags[i]; exists {
260260
for _, tag := range opening {
261+
var utf16tag []uint16
262+
261263
if len(tag.Attrs) > 0 {
262264
attrStr := ""
263265
for k, v := range tag.Attrs {
264266
attrStr += fmt.Sprintf(" %s=\"%s\"", k, v)
265267
}
266-
result.WriteString(fmt.Sprintf("<%s%s>", tag.Type, attrStr))
268+
269+
utf16tag = utf16.Encode([]rune(fmt.Sprintf("<%s%s>", tag.Type, attrStr)))
267270
} else {
268-
result.WriteString(fmt.Sprintf("<%s>", tag.Type))
271+
utf16tag = utf16.Encode([]rune(fmt.Sprintf("<%s>", tag.Type)))
269272
}
273+
274+
result = append(result, utf16tag...)
270275
}
271276
}
272-
result.WriteString(string(utf16.Decode([]uint16{utf16Text[i]})))
277+
result = append(result, utf16Text[i])
273278

274279
if closing, exists := closeTags[i+1]; exists {
275280
for j := len(closing) - 1; j >= 0; j-- {
276-
result.WriteString(fmt.Sprintf("</%s>", closing[j].Type))
281+
utf16tag := utf16.Encode([]rune(fmt.Sprintf("</%s>", closing[j].Type)))
282+
result = append(result, utf16tag...)
277283
}
278284
}
279285
}
280-
281-
return result.String()
286+
return string(utf16.Decode(result))
282287
}
283288

284289
func ToMarkdown(htmlStr string) string {

0 commit comments

Comments
 (0)