[IMPROVEMENT] Markdown performance when identifying emoji only content (#1422)

This commit is contained in:
Diego Mello 2019-11-27 17:53:14 -03:00 committed by GitHub
parent 02c6a674fe
commit 4bb0bfa7eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 20 deletions

View File

@ -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",

View File

@ -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);