2018-11-20 09:01:23 +01:00
|
|
|
import { getEmojiRegex } from './emojiRegex'
|
|
|
|
|
|
|
|
// replace emoji in HTML with something else, safely skipping HTML tags
|
|
|
|
export function replaceEmoji (string, replacer) {
|
|
|
|
let output = ''
|
|
|
|
let leftAngleBracketIdx = string.indexOf('<')
|
|
|
|
let currentIdx = 0
|
|
|
|
let emojiRegex = getEmojiRegex()
|
|
|
|
|
|
|
|
function safeReplacer (substring) {
|
2018-11-20 18:42:49 +01:00
|
|
|
// emoji regex matches digits and pound sign https://git.io/fpl6J
|
2018-11-24 06:32:57 +01:00
|
|
|
if (substring.match(/^[0-9#*™®]+$/)) {
|
2018-11-20 09:01:23 +01:00
|
|
|
return substring
|
|
|
|
}
|
|
|
|
return replacer(substring)
|
|
|
|
}
|
|
|
|
|
|
|
|
while (leftAngleBracketIdx !== -1) {
|
|
|
|
let substring = string.substring(currentIdx, leftAngleBracketIdx)
|
|
|
|
|
|
|
|
output += substring.replace(emojiRegex, safeReplacer)
|
|
|
|
|
|
|
|
let rightAngleBracketIdx = string.indexOf('>', leftAngleBracketIdx + 1)
|
|
|
|
if (rightAngleBracketIdx === -1) { // broken HTML, abort
|
|
|
|
output += string.substring(leftAngleBracketIdx, string.length)
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
output += string.substring(leftAngleBracketIdx, rightAngleBracketIdx) + '>'
|
|
|
|
currentIdx = rightAngleBracketIdx + 1
|
|
|
|
leftAngleBracketIdx = string.indexOf('<', currentIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
output += string.substring(currentIdx, string.length).replace(emojiRegex, safeReplacer)
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|