diff --git a/__tests__/__snapshots__/Storyshots.test.js.snap b/__tests__/__snapshots__/Storyshots.test.js.snap index 7d0605ac7..5a163f692 100644 --- a/__tests__/__snapshots__/Storyshots.test.js.snap +++ b/__tests__/__snapshots__/Storyshots.test.js.snap @@ -11188,6 +11188,104 @@ exports[`Storyshots Markdown list Markdown 1`] = ` /> + + + 🤙 + + + + + + ひ + + text.replace( ); const emojiRanges = [ - '\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]', // unicode emoji from https://www.regextester.com/106421 + emojiRegex(), ':.{1,40}:', // custom emoji ' |\n' // allow spaces and line breaks ].join('|'); +const emojisRegex = new RegExp(emojiRanges, 'g'); + const removeSpaces = str => str && str.replace(/\s/g, ''); -const removeAllEmoji = str => str.replace(new RegExp(emojiRanges, 'g'), ''); +const removeAllEmoji = str => str.replace(emojisRegex, ''); + +const emojiCount = str => str?.match(emojisRegex, '')?.length; const isOnlyEmoji = (str) => { str = removeSpaces(str); - return !removeAllEmoji(str).length; -}; - -const removeOneEmoji = str => str.replace(new RegExp(emojiRanges), ''); - -const emojiCount = (str) => { - str = removeSpaces(str); - let oldLength = 0; - let counter = 0; - - while (oldLength !== str.length) { - oldLength = str.length; - str = removeOneEmoji(str); - if (oldLength !== str.length) { - counter += 1; - } - } - - return counter; + return !removeAllEmoji(str).length && emojiCount(str) <= 3; }; const parser = new Parser(); @@ -395,7 +382,7 @@ class Markdown extends PureComponent { let ast = parser.parse(m); ast = mergeTextNodes(ast); - this.isMessageContainsOnlyEmoji = isOnlyEmoji(m) && emojiCount(m) <= 3; + this.isMessageContainsOnlyEmoji = isOnlyEmoji(m); this.editedMessage(ast); return this.renderer.render(ast); } diff --git a/package.json b/package.json index bcd353755..e86b7afac 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "commonmark-react-renderer": "git+https://github.com/RocketChat/commonmark-react-renderer.git", "deep-equal": "2.0.3", "ejson": "2.2.0", + "emoji-regex": "^9.2.0", "eslint-config-airbnb": "^18.1.0", "expo-apple-authentication": "^2.2.1", "expo-av": "8.2.1", diff --git a/storybook/stories/Markdown.js b/storybook/stories/Markdown.js index c24d1ca9b..f3ab49bc8 100644 --- a/storybook/stories/Markdown.js +++ b/storybook/stories/Markdown.js @@ -171,6 +171,13 @@ export default ({ theme }) => { getCustomEmoji={getCustomEmoji} baseUrl={baseUrl} /> + {/* Emojis + non-latin characters */} + diff --git a/yarn.lock b/yarn.lock index 9cb699507..60478c768 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6105,6 +6105,11 @@ emoji-regex@^9.0.0: resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-9.0.0.tgz#48a2309cc8a1d2e9d23bc6a67c39b63032e76ea4" integrity sha512-6p1NII1Vm62wni/VR/cUMauVQoxmLVb9csqQlvLz+hO2gk8U2UYDfXHQSUYIBKmZwAKz867IDqG7B+u0mj+M6w== +emoji-regex@^9.2.0: + version "9.2.0" + resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-9.2.0.tgz#a26da8e832b16a9753309f25e35e3c0efb9a066a" + integrity sha512-DNc3KFPK18bPdElMJnf/Pkv5TXhxFU3YFDEuGLDRtPmV4rkmCjBkCSEp22u6rBHdSN9Vlp/GK7k98prmE1Jgug== + emojis-list@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"