diff --git a/src/apps/chatwoot/messages/to/chatwoot/PixMessage.test.ts b/src/apps/chatwoot/messages/to/chatwoot/PixMessage.test.ts index aa760e42..d8472808 100644 --- a/src/apps/chatwoot/messages/to/chatwoot/PixMessage.test.ts +++ b/src/apps/chatwoot/messages/to/chatwoot/PixMessage.test.ts @@ -108,15 +108,15 @@ describe('PixMessage - pt-BR', () => { const result = converter.convert(payload, protoMessage); // Assert - const expected = `💳 ****PIX - Copia e Cola**** + const expected = `💳 **PIX - Copia e Cola** -****Merchant Name Here**** +**Merchant Name Here** -****Chave PIX:**** +5000000000000 -****Tipo:**** PHONE -****Referência:**** 4TB11111111 +**Chave PIX:** +5000000000000 +**Tipo:** PHONE +**Referência:** 4TB11111111 -💳 ****PIX Copia e Cola enviado****`; +💳 **PIX Copia e Cola enviado**`; expect(protoMessage).not.toBeNull(); expect(result).not.toBeNull(); @@ -227,16 +227,16 @@ describe('PixMessage - pt-BR', () => { const result = converter.convert(payload, protoMessage); // Assert - const expected = `💳 ****PIX - Copia e Cola**** + const expected = `💳 **PIX - Copia e Cola** -****Merchant Name Here**** +**Merchant Name Here** -****Chave PIX:**** +5000000000000 -****Tipo:**** PHONE -****Valor:**** R$\u00A025,12 -****Referência:**** 4TB11111111 +**Chave PIX:** +5000000000000 +**Tipo:** PHONE +**Valor:** R$\u00A025,12 +**Referência:** 4TB11111111 -💳 ****PIX Copia e Cola enviado****`; +💳 **PIX Copia e Cola enviado**`; expect(protoMessage).not.toBeNull(); expect(result).not.toBeNull(); diff --git a/src/apps/chatwoot/messages/to/chatwoot/utils/markdown.test.ts b/src/apps/chatwoot/messages/to/chatwoot/utils/markdown.test.ts index 9f63c562..b6c90352 100644 --- a/src/apps/chatwoot/messages/to/chatwoot/utils/markdown.test.ts +++ b/src/apps/chatwoot/messages/to/chatwoot/utils/markdown.test.ts @@ -53,4 +53,40 @@ describe('WhatsappToMarkdown', () => { const expected = 'URL: https://example.com/test_page e texto *em itálico*'; expect(WhatsappToMarkdown(input)).toBe(expected); }); + + it('keeps already bold markdown intact', () => { + const input = '**PIX - Copia e Cola**'; + expect(WhatsappToMarkdown(input)).toBe(input); + }); + + it('keeps already strikethrough markdown intact', () => { + const input = '~~removed~~'; + expect(WhatsappToMarkdown(input)).toBe(input); + }); + + it('keeps underscores inside identifiers intact', () => { + const input = 'api key: abc_test_key'; + expect(WhatsappToMarkdown(input)).toBe(input); + }); + + it('does not convert underscores inside words', () => { + const input = 'Instance name: customer_prod_instance'; + expect(WhatsappToMarkdown(input)).toBe(input); + }); + + it('does not convert when markers are stuck to word characters', () => { + const input = 'My name is*Rafael* and email is test_user@example.com'; + expect(WhatsappToMarkdown(input)).toBe(input); + }); + + it('keeps formatting when markers touch punctuation boundaries', () => { + const input = 'Hello *Rafael*, welcome to _WAHA_!'; + const expected = 'Hello **Rafael**, welcome to *WAHA*!'; + expect(WhatsappToMarkdown(input)).toBe(expected); + }); + + it('ignores markers with inner spaces next to the marker', () => { + const input = '* text* and _ text_ should stay the same'; + expect(WhatsappToMarkdown(input)).toBe(input); + }); }); diff --git a/src/apps/chatwoot/messages/to/chatwoot/utils/markdown.ts b/src/apps/chatwoot/messages/to/chatwoot/utils/markdown.ts index a59cafed..0f1beafb 100644 --- a/src/apps/chatwoot/messages/to/chatwoot/utils/markdown.ts +++ b/src/apps/chatwoot/messages/to/chatwoot/utils/markdown.ts @@ -1,5 +1,18 @@ import { UrlProtection } from '@waha/apps/chatwoot/messages/to/markdown'; +const boldPattern = new RegExp( + `(?