diff --git a/shared/utils/emoji.ts b/shared/utils/emoji.ts index b617309226..1a647236f8 100644 --- a/shared/utils/emoji.ts +++ b/shared/utils/emoji.ts @@ -1,5 +1,5 @@ // copied from https://github.com/markdown-it/markdown-it-emoji/blob/master/lib/data/full.mjs -const emojis = { +const emojis: Record = { '100': '💯', '1234': '🔢', 'grinning': '😀', @@ -1905,15 +1905,16 @@ const emojis = { 'wales': '🏴󠁧󠁢󠁷󠁬󠁳󠁿', } -const emojisKeysRegex = new RegExp( - Object.keys(emojis) - .map(key => `:${key}:`) - .join('|'), - 'g', -) -export function convertToEmoji(text: string): string { - return text.replace(emojisKeysRegex, match => { - const key = match.slice(1, -1) as keyof typeof emojis - return emojis[key] || match - }) +export function convertToEmoji(html: string): string { + return html.replace( + /(][\s\S]*?<\/code>|][\s\S]*?<\/pre>)|(:[\w+-]+:)/gi, + (match, codeBlock: string | undefined, shortcode: string | undefined) => { + if (codeBlock) return codeBlock + if (shortcode) { + const key = shortcode.slice(1, -1) + return emojis[key] ?? shortcode + } + return match + }, + ) }