From 0410385e8c888cd9073b0e868db8cf2b5f5a0288 Mon Sep 17 00:00:00 2001 From: Oriol Raventos Date: Mon, 4 Nov 2024 17:29:40 +0100 Subject: [PATCH 1/2] feat(botonic-react): remove markdown links --- .../multichannel/whatsapp/markdown.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/botonic-react/src/components/multichannel/whatsapp/markdown.ts b/packages/botonic-react/src/components/multichannel/whatsapp/markdown.ts index eae1752673..b14b07ff70 100644 --- a/packages/botonic-react/src/components/multichannel/whatsapp/markdown.ts +++ b/packages/botonic-react/src/components/multichannel/whatsapp/markdown.ts @@ -15,6 +15,11 @@ const MARKDOWN_BOLD_AND_ITALIC_OPTION2 = /(\*__)(.*?)(__\*)/g const MARKDOWN_BOLD_AND_ITALIC_OPTION3 = /(__\*)(.*?)(\*__)/g export function whatsappMarkdown(text: string) { + const textWithItalicAndBold = replaceItalicAndBold(text) + return replaceMarkdownLinks(textWithItalicAndBold) +} + +export function replaceItalicAndBold(text: string) { const textNormalized = normalizeMarkdown(text) const matches = textNormalized.match(MARKDOWN_BOLD_OR_ITALIC_REGEX) if (matches) { @@ -26,6 +31,7 @@ export function whatsappMarkdown(text: string) { MARKDOWN_WHATSAPP_BOLD ) } + if (match.startsWith(MARKDOWN_BOLD_OPTION_2)) { return replaceAllOccurrences( match, @@ -33,6 +39,7 @@ export function whatsappMarkdown(text: string) { MARKDOWN_WHATSAPP_BOLD ) } + if (match.startsWith(MARKDOWN_ITALIC_OPTION_1)) { return replaceAllOccurrences( match, @@ -40,8 +47,10 @@ export function whatsappMarkdown(text: string) { MARKDOWN_WHATSAPP_ITALIC ) } + return match }) + let textWhatsapp = textNormalized for (let i = 0; i < matches.length; i++) { textWhatsapp = replaceAllOccurrences( @@ -50,8 +59,10 @@ export function whatsappMarkdown(text: string) { matchesResult[i] ) } + return textWhatsapp } + return text } @@ -86,3 +97,9 @@ function replaceAllOccurrences( ) { return text.split(searchValue).join(replaceValue) } + +const REGEX_MARKDOWN_LINK = /\[([^\]]+)\]\(([^)]+)\)/g + +function replaceMarkdownLinks(markdown: string) { + return markdown.replace(REGEX_MARKDOWN_LINK, '$1: $2') +} From 8106777ea05d7b6ef249e752a3d766217bb5ec3e Mon Sep 17 00:00:00 2001 From: Oriol Raventos Date: Mon, 4 Nov 2024 17:30:19 +0100 Subject: [PATCH 2/2] refactor(botonic-react): apply whatsapp markdown in facebook --- .../src/components/multichannel/multichannel-text.jsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/botonic-react/src/components/multichannel/multichannel-text.jsx b/packages/botonic-react/src/components/multichannel/multichannel-text.jsx index ffb09725c6..66fd56bf75 100644 --- a/packages/botonic-react/src/components/multichannel/multichannel-text.jsx +++ b/packages/botonic-react/src/components/multichannel/multichannel-text.jsx @@ -287,12 +287,15 @@ export const MultichannelText = props => { return ( <> {texts && - texts.map((e, i) => ( + texts.map((message, i) => ( - {e} + {whatsappMarkdown(text)} ))} - {propsLastText.children} + + {/* TODO: Review how render buttons and replies in Facebook with markdown */} + {whatsappMarkdown(propsLastText.children)} + ) }