diff --git a/pkg/channels/telegram.go b/pkg/channels/telegram.go index 3ad4818c3..a18fd1ed8 100644 --- a/pkg/channels/telegram.go +++ b/pkg/channels/telegram.go @@ -418,9 +418,9 @@ func markdownToTelegramHTML(text string) string { inlineCodes := extractInlineCodes(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) @@ -441,7 +441,9 @@ func markdownToTelegramHTML(text string) string { text = regexp.MustCompile(`~~(.+?)~~`).ReplaceAllString(text, "$1") - 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 { escaped := escapeHTML(code) @@ -470,8 +472,11 @@ func extractCodeBlocks(text string) codeBlockMatch { codes = append(codes, match[1]) } + idx := 0 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} @@ -491,8 +496,11 @@ func extractInlineCodes(text string) inlineCodeMatch { codes = append(codes, match[1]) } + idx := 0 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} diff --git a/pkg/channels/telegram_test.go b/pkg/channels/telegram_test.go new file mode 100644 index 000000000..158e56496 --- /dev/null +++ b/pkg/channels/telegram_test.go @@ -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, "
") != 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, "") != 2 {
+		t.Fatalf("expected 2 inline code tags, got: %q", got)
+	}
+	if !strings.Contains(got, "foo") || !strings.Contains(got, "bar") {
+		t.Fatalf("missing inline code contents, got: %q", got)
+	}
+}