fix telegram markdown rendering for multiline and code placeholders

This commit is contained in:
Henrique Costa 2026-02-12 18:50:15 -03:00
parent f660143ddd
commit 7dc16235db
2 changed files with 58 additions and 5 deletions

View file

@ -418,9 +418,9 @@ func markdownToTelegramHTML(text string) string {
inlineCodes := extractInlineCodes(text) inlineCodes := extractInlineCodes(text)
text = inlineCodes.text text = inlineCodes.text
text = regexp.MustCompile(`^#{1,6}\s+(.+)$`).ReplaceAllString(text, "$1") text = regexp.MustCompile(`(?m)^#{1,6}\s+(.+)$`).ReplaceAllString(text, "$1")
text = regexp.MustCompile(`^>\s*(.*)$`).ReplaceAllString(text, "$1") text = regexp.MustCompile(`(?m)^>\s*(.*)$`).ReplaceAllString(text, "$1")
text = escapeHTML(text) text = escapeHTML(text)
@ -441,7 +441,9 @@ func markdownToTelegramHTML(text string) string {
text = regexp.MustCompile(`~~(.+?)~~`).ReplaceAllString(text, "<s>$1</s>") text = regexp.MustCompile(`~~(.+?)~~`).ReplaceAllString(text, "<s>$1</s>")
text = regexp.MustCompile(`^[-*]\s+`).ReplaceAllString(text, "• ") text = regexp.MustCompile(`(?m)^[-*]\s+`).ReplaceAllString(text, "• ")
text = regexp.MustCompile(`(?m)^\d+\.\s+`).ReplaceAllString(text, "• ")
for i, code := range inlineCodes.codes { for i, code := range inlineCodes.codes {
escaped := escapeHTML(code) escaped := escapeHTML(code)
@ -470,8 +472,11 @@ func extractCodeBlocks(text string) codeBlockMatch {
codes = append(codes, match[1]) codes = append(codes, match[1])
} }
idx := 0
text = re.ReplaceAllStringFunc(text, func(m string) string { text = re.ReplaceAllStringFunc(text, func(m string) string {
return fmt.Sprintf("\x00CB%d\x00", len(codes)-1) placeholder := fmt.Sprintf("\x00CB%d\x00", idx)
idx++
return placeholder
}) })
return codeBlockMatch{text: text, codes: codes} return codeBlockMatch{text: text, codes: codes}
@ -491,8 +496,11 @@ func extractInlineCodes(text string) inlineCodeMatch {
codes = append(codes, match[1]) codes = append(codes, match[1])
} }
idx := 0
text = re.ReplaceAllStringFunc(text, func(m string) string { text = re.ReplaceAllStringFunc(text, func(m string) string {
return fmt.Sprintf("\x00IC%d\x00", len(codes)-1) placeholder := fmt.Sprintf("\x00IC%d\x00", idx)
idx++
return placeholder
}) })
return inlineCodeMatch{text: text, codes: codes} return inlineCodeMatch{text: text, codes: codes}

View file

@ -0,0 +1,45 @@
package channels
import (
"strings"
"testing"
)
func TestMarkdownToTelegramHTML_MultilineFormatting(t *testing.T) {
input := "## Titulo\n- item um\n- item dois\n1. item tres\n> citação"
got := markdownToTelegramHTML(input)
if strings.Contains(got, "##") {
t.Fatalf("heading marker should be removed, got: %q", got)
}
if !strings.Contains(got, "• item um") || !strings.Contains(got, "• item dois") || !strings.Contains(got, "• item tres") {
t.Fatalf("list markers should be normalized, got: %q", got)
}
if strings.Contains(got, "> citação") {
t.Fatalf("blockquote marker should be removed, got: %q", got)
}
}
func TestMarkdownToTelegramHTML_MultipleCodeBlocks(t *testing.T) {
input := "```go\nfmt.Println(1)\n```\ntexto\n```js\nconsole.log(2)\n```"
got := markdownToTelegramHTML(input)
if strings.Count(got, "<pre><code>") != 2 {
t.Fatalf("expected 2 code blocks, got: %q", got)
}
if !strings.Contains(got, "fmt.Println(1)") || !strings.Contains(got, "console.log(2)") {
t.Fatalf("missing code block contents, got: %q", got)
}
}
func TestMarkdownToTelegramHTML_MultipleInlineCodes(t *testing.T) {
input := "use `foo` and `bar`"
got := markdownToTelegramHTML(input)
if strings.Count(got, "<code>") != 2 {
t.Fatalf("expected 2 inline code tags, got: %q", got)
}
if !strings.Contains(got, "<code>foo</code>") || !strings.Contains(got, "<code>bar</code>") {
t.Fatalf("missing inline code contents, got: %q", got)
}
}