diff --git a/server/templaterenderer/issue_test.go b/server/templaterenderer/issue_test.go index 1d30416..0ac8931 100644 --- a/server/templaterenderer/issue_test.go +++ b/server/templaterenderer/issue_test.go @@ -100,4 +100,18 @@ func TestIssueTemplates(t *testing.T) { require.NoError(t, err) require.Equal(t, expected, actual) }) + + t.Run("RenderIssueCreatedEventNotificationForSubscribedChannels_WithImage", func(t *testing.T) { + // Test that images in issue descriptions are properly converted to markdown + // and don't leave orphan '>' blockquote characters + expected := "\n#### Bug with image attachment" + + "\n##### [\\[mattermost-plugin-bitbucket#2\\]](https://bitbucket.org/mattermost/mattermost-plugin-bitbucket/issues/2/bug-with-image)" + + "\n#new-issue by @testMmUser:" + + "\n>Here is a screenshot:\n" + + actual, err := tr.RenderIssueCreatedEventNotificationForSubscribedChannels(getTestIssueCreatedPayloadWithImage()) + + require.NoError(t, err) + require.Equal(t, expected, actual) + }) } diff --git a/server/templaterenderer/template.go b/server/templaterenderer/template.go index e48572b..bd7242c 100644 --- a/server/templaterenderer/template.go +++ b/server/templaterenderer/template.go @@ -113,7 +113,23 @@ func (tr *templateRenderer) init() { selection.SetText(bitbucketNickname) }) + // Convert img tags to markdown image syntax to preserve images in the output + // This fixes the issue where images in descriptions cause '>' to appear in posts + doc.Find("img").Each(func(i int, selection *goquery.Selection) { + src := selection.AttrOr("src", "") + alt := selection.AttrOr("alt", "image") + if src != "" { + // Replace the img tag with markdown image syntax as plain text + markdownImg := "" + selection.ReplaceWithHtml(markdownImg) + } else { + // Remove img tags without src to prevent empty lines + selection.Remove() + } + }) + // Text() returns only text, without HTML attributes or tags + // The markdown image syntax we inserted will be preserved since it's plain text return doc.Text() } diff --git a/server/templaterenderer/template_fixture_test.go b/server/templaterenderer/template_fixture_test.go index 99bc830..1f692b0 100644 --- a/server/templaterenderer/template_fixture_test.go +++ b/server/templaterenderer/template_fixture_test.go @@ -56,6 +56,16 @@ func getTestIssue() webhookpayload.Issue { return issue } +func getTestIssueWithImage() webhookpayload.Issue { + issue := webhookpayload.Issue{} + issue.ID = 2 + issue.Title = "Bug with image attachment" + issue.Content.HTML = "
Here is a screenshot:
