redo #4500 with customEmojis (#5016)

th-downstream
MIYAGI Hikaru 7 years ago committed by Eugen Rochko
parent 816175a1fb
commit 1fa17e1a43

@ -4,47 +4,39 @@ import Trie from 'substring-trie';
const trie = new Trie(Object.keys(unicodeMapping)); const trie = new Trie(Object.keys(unicodeMapping));
const emojify = (str, customEmojis = {}) => { const emojify = (str, customEmojis = {}) => {
// This walks through the string from start to end, ignoring any tags (<p>, <br>, etc.) let rtn = '';
// and replacing valid unicode strings for (;;) {
// that _aren't_ within tags with an <img> version. let match, i = 0, tag;
// The goal is to be the same as an emojione.regUnicode replacement, but faster. while (i < str.length && (tag = '<&:'.indexOf(str[i])) === -1 && !(match = trie.search(str.slice(i)))) {
let i = -1; i += str.codePointAt(i) < 65536 ? 1 : 2;
let insideTag = false; }
let insideShortname = false; if (i === str.length)
let shortnameStartIndex = -1; break;
let match; else if (tag >= 0) {
while (++i < str.length) { let tagend = str.indexOf('>;:'[tag], i + 1) + 1;
const char = str.charAt(i); if (!tagend)
if (insideShortname && char === ':') { break;
const shortname = str.substring(shortnameStartIndex, i + 1); if (str[i] === ':') {
if (shortname in customEmojis) { const shortname = str.slice(i, tagend);
const replacement = `<img draggable="false" class="emojione" alt="${shortname}" title="${shortname}" src="${customEmojis[shortname]}" />`; const lt = str.indexOf('<', i + 1);
str = str.substring(0, shortnameStartIndex) + replacement + str.substring(i + 1); if ((lt === -1 || lt >= tagend) && shortname in customEmojis) {
i += (replacement.length - shortname.length - 1); // jump ahead the length we've added to the string rtn += str.slice(0, i) + `<img draggable="false" class="emojione" alt="${shortname}" title="${shortname}" src="${customEmojis[shortname]}" />`;
str = str.slice(tagend);
} else {
rtn += str.slice(0, i + 1);
str = str.slice(i + 1);
}
} else { } else {
i--; rtn += str.slice(0, tagend);
} str = str.slice(tagend);
insideShortname = false;
} else if (insideTag && char === '>') {
insideTag = false;
} else if (char === '<') {
insideTag = true;
insideShortname = false;
} else if (!insideTag && char === ':') {
insideShortname = true;
shortnameStartIndex = i;
} else if (!insideTag && (match = trie.search(str.substring(i)))) {
const unicodeStr = match;
if (unicodeStr in unicodeMapping) {
const [filename, shortCode] = unicodeMapping[unicodeStr];
const alt = unicodeStr;
const replacement = `<img draggable="false" class="emojione" alt="${alt}" title=":${shortCode}:" src="/emoji/${filename}.svg" />`;
str = str.substring(0, i) + replacement + str.substring(i + unicodeStr.length);
i += (replacement.length - unicodeStr.length); // jump ahead the length we've added to the string
} }
} else {
const [filename, shortCode] = unicodeMapping[match];
rtn += str.slice(0, i) + `<img draggable="false" class="emojione" alt="${match}" title=":${shortCode}:" src="/emoji/${filename}.svg" />`;
str = str.slice(i + match.length);
} }
} }
return str; return rtn + str;
}; };
export default emojify; export default emojify;

Loading…
Cancel
Save