Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

botonic-react: messenger markdown not applied #BLT-1152 #2930

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,15 @@ export const MultichannelText = props => {
return (
<>
{texts &&
texts.map((e, i) => (
texts.map((message, i) => (
Iru89 marked this conversation as resolved.
Show resolved Hide resolved
<Text key={i} {...propsWithoutChildren}>
{e}
{whatsappMarkdown(text)}
</Text>
))}
<Text {...propsLastText}>{propsLastText.children}</Text>
<Text {...propsLastText}>
{/* TODO: Review how render buttons and replies in Facebook with markdown */}
{whatsappMarkdown(propsLastText.children)}
</Text>
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -26,22 +31,26 @@ export function whatsappMarkdown(text: string) {
MARKDOWN_WHATSAPP_BOLD
)
}

if (match.startsWith(MARKDOWN_BOLD_OPTION_2)) {
return replaceAllOccurrences(
match,
MARKDOWN_BOLD_OPTION_2,
MARKDOWN_WHATSAPP_BOLD
)
}

if (match.startsWith(MARKDOWN_ITALIC_OPTION_1)) {
return replaceAllOccurrences(
match,
MARKDOWN_ITALIC_OPTION_1,
MARKDOWN_WHATSAPP_ITALIC
)
}

return match
})

let textWhatsapp = textNormalized
for (let i = 0; i < matches.length; i++) {
textWhatsapp = replaceAllOccurrences(
Expand All @@ -50,8 +59,10 @@ export function whatsappMarkdown(text: string) {
matchesResult[i]
)
}

return textWhatsapp
}

return text
}

Expand Down Expand Up @@ -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')
Iru89 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading