From 4bb0bfa7ebdf8a4ad5c82bb2e34a5defabde4ba3 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Wed, 27 Nov 2019 17:53:14 -0300 Subject: [PATCH] [IMPROVEMENT] Markdown performance when identifying emoji only content (#1422) --- .../__snapshots__/Storyshots.test.js.snap | 20 ++++--------------- app/containers/markdown/index.js | 15 ++++++++++---- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/__tests__/__snapshots__/Storyshots.test.js.snap b/__tests__/__snapshots__/Storyshots.test.js.snap index 3009165ff..36fe6c521 100644 --- a/__tests__/__snapshots__/Storyshots.test.js.snap +++ b/__tests__/__snapshots__/Storyshots.test.js.snap @@ -6012,13 +6012,7 @@ exports[`Storyshots Message list 1`] = ` "fontWeight": "400", }, Array [ - Object { - "backgroundColor": "transparent", - "color": "#2F343D", - "fontFamily": "System", - "fontSize": 30, - "fontWeight": "400", - }, + Object {}, Object { "alignItems": "flex-start", "flexDirection": "row", @@ -6040,8 +6034,8 @@ exports[`Storyshots Message list 1`] = ` "overflow": "hidden", }, Object { - "height": 30, - "width": 30, + "height": 20, + "width": 20, }, ] } @@ -6077,13 +6071,7 @@ exports[`Storyshots Message list 1`] = ` "fontWeight": "400", }, Array [ - Object { - "backgroundColor": "transparent", - "color": "#2F343D", - "fontFamily": "System", - "fontSize": 30, - "fontWeight": "400", - }, + Object {}, Object { "alignItems": "flex-start", "flexDirection": "row", diff --git a/app/containers/markdown/index.js b/app/containers/markdown/index.js index 4282f96b3..2c6372154 100644 --- a/app/containers/markdown/index.js +++ b/app/containers/markdown/index.js @@ -3,7 +3,7 @@ import { Text, Image } from 'react-native'; import { Parser, Node } from 'commonmark'; import Renderer from 'commonmark-react-renderer'; import PropTypes from 'prop-types'; -import { toShort, shortnameToUnicode } from 'emoji-toolkit'; +import { shortnameToUnicode } from 'emoji-toolkit'; import I18n from '../../i18n'; @@ -32,13 +32,19 @@ const emojiRanges = [ ' |\n' // allow spaces and line breaks ].join('|'); +const removeSpaces = str => str && str.replace(/\s/g, ''); + const removeAllEmoji = str => str.replace(new RegExp(emojiRanges, 'g'), ''); -const isOnlyEmoji = str => !removeAllEmoji(str).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; @@ -322,6 +328,8 @@ export default class Markdown extends PureComponent { if (preview) { m = m.split('\n').reduce((lines, line) => `${ lines } ${ line }`, ''); + const ast = this.parser.parse(m); + return this.renderer.render(ast); } if (!useMarkdown && !preview) { @@ -329,8 +337,7 @@ export default class Markdown extends PureComponent { } const ast = this.parser.parse(m); - const encodedEmojis = toShort(m); - this.isMessageContainsOnlyEmoji = isOnlyEmoji(encodedEmojis) && emojiCount(encodedEmojis) <= 3; + this.isMessageContainsOnlyEmoji = isOnlyEmoji(m) && emojiCount(m) <= 3; this.editedMessage(ast);