-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- _Set as [Draft PR](https://github.blog/2019-02-14-introducing-draft-pull-requests/) if it's not ready to be merged_. [PR best practices Reference](https://blog.codeminer42.com/on-writing-a-great-pull-request-37c60ce6f31d/) --> ## Description - add a function to apply whatsapp markdown - use this function within the multichannel to apply the default markdown in case the provider is WhatsApp <!-- - Must be clear and concise (2-3 lines). - Don't make reviewers think. The description should explain what has been implemented or what it's used for. If a pull request is not descriptive, people will be lazy or not willing to spend much time on it. - Be explicit with the names (don't abbreviate and don't use acronyms that can lead to misleading understanding). - If you consider it appropriate, include the steps to try the new features. --> ## Context <!-- - What problem is trying to solve this pull request? - What are the reasons or business goals of this implementation? - Can I provide visual resources or links to understand better the situation? --> ## Approach taken / Explain the design This function will apply bold when text is placed between `**text**` or `__text__` This function will apply italics when text is enclosed in `*text*` or `_text_` This function will apply bold and italics when text is placed between `**_text_**` For the option `__*text*__` I first normalise to `**_text_**` <!-- - Explain what the code does. - If it's a complex solution, try to provide a sketch. --> ## To document / Usage example <!-- - How this is used? - If possible, provide a snippet of code with a usage example. --> ## Testing The pull request... - has unit tests - has integration tests - doesn't need tests because... **[provide a description]**
- Loading branch information
Showing
7 changed files
with
946 additions
and
623 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/botonic-react/src/components/multichannel/whatsapp/markdown.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const MARKDOWN_BOLD_OPTION_1 = '**' | ||
const MARKDOWN_BOLD_OPTION_2 = '__' | ||
const MARKDOWN_WHATSAPP_BOLD = '*' | ||
|
||
const MARKDOWN_ITALIC_OPTION_1 = '*' | ||
const MARKDOWN_WHATSAPP_ITALIC = '_' | ||
|
||
const MARKDOWN_BOLD_OR_ITALIC_REGEX = /(\*\*|__)(.*?)\1|(\*|_)(.*?)\3/g | ||
|
||
const MARKDOWN_NORMALIZED_BOLD_ITALIC_OPEN = '**_' | ||
const MARKDOWN_NORMALIZED_BOLD_ITALIC_CLOSE = '_**' | ||
|
||
const MARKDOWN_BOLD_AND_ITALIC_OPTION1 = /(_\*\*)(.*?)(\*\*_)/g | ||
const MARKDOWN_BOLD_AND_ITALIC_OPTION2 = /(\*__)(.*?)(__\*)/g | ||
const MARKDOWN_BOLD_AND_ITALIC_OPTION3 = /(__\*)(.*?)(\*__)/g | ||
|
||
export function whatsappMarkdown(text: string) { | ||
const textNormalized = normalizeMarkdown(text) | ||
const matches = textNormalized.match(MARKDOWN_BOLD_OR_ITALIC_REGEX) | ||
if (matches) { | ||
const matchesResult = matches.map(match => { | ||
if (match.startsWith(MARKDOWN_BOLD_OPTION_1)) { | ||
return replaceAllOccurrences( | ||
match, | ||
MARKDOWN_BOLD_OPTION_1, | ||
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( | ||
textWhatsapp, | ||
matches[i], | ||
matchesResult[i] | ||
) | ||
} | ||
return textWhatsapp | ||
} | ||
return text | ||
} | ||
|
||
function normalizeMarkdown(text: string) { | ||
text = replaceBoldItalic(text, MARKDOWN_BOLD_AND_ITALIC_OPTION1) | ||
text = replaceBoldItalic(text, MARKDOWN_BOLD_AND_ITALIC_OPTION2) | ||
text = replaceBoldItalic(text, MARKDOWN_BOLD_AND_ITALIC_OPTION3) | ||
return text | ||
} | ||
|
||
function replaceBoldItalic(text: string, regex: RegExp) { | ||
return text.replace( | ||
regex, | ||
( | ||
match: string, | ||
markdownOpen: string, | ||
textInsideMarkdown: string, | ||
markdownClose: string | ||
) => { | ||
if (match.startsWith(markdownOpen) && match.endsWith(markdownClose)) { | ||
return `${MARKDOWN_NORMALIZED_BOLD_ITALIC_OPEN}${textInsideMarkdown}${MARKDOWN_NORMALIZED_BOLD_ITALIC_CLOSE}` | ||
} | ||
return match | ||
} | ||
) | ||
} | ||
|
||
function replaceAllOccurrences( | ||
text: string, | ||
searchValue: string, | ||
replaceValue: string | ||
) { | ||
return text.split(searchValue).join(replaceValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters