Spaces:
Runtime error
Runtime error
File size: 1,252 Bytes
4327358 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
export function MarkdownToWhatsApp(text: string): string {
if (!text) {
return '';
}
return (
text
// Triple-backtick blocks → WhatsApp monospace blocks (same syntax)
.replace(/```([\s\S]*?)```/g, '```$1```')
// Italic **first**, but only single‐star or single‐underscore
// (?<!\*)\*(?!\*) = a '*' not part of '**'
// (?<!_)_(?!_) = a '_' not part of '__'
.replace(
/(?<!\*)\*(?!\*)(.*?)\*(?!\*)|(?<!_)_(?!_)(.*?)_(?!_)/g,
(_m, a, b) => `_${a || b}_`,
)
// Bold: **bold** → *bold*
.replace(/\*\*(.*?)\*\*/g, '*$1*')
// Strikethrough: ~~strike~~ → ~strike~
.replace(/~~(.*?)~~/g, '~$1~')
// Links: [text](url) → text (url)
.replace(/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g, '$1 ($2)')
// Lists: -, +, * → * item
.replace(/^[-+*] (.*)/gm, '* $1')
);
}
export function WhatsappToMarkdown(text: string): string {
if (!text) {
return '';
}
return (
text
// Bold: *bold* → **bold**
.replace(/\*(.*?)\*/g, '**$1**')
// Strikethrough: ~strike~ → ~~strike~~
.replace(/~(.*?)~/g, '~~$1~~')
// Italic: _italic_ → *italic*
.replace(/_(.*?)_/g, '*$1*')
);
}
|