redo #4500 with customEmojis (#5016)

main
MIYAGI Hikaru 2017-09-19 21:27:29 +00:00 committed by Eugen Rochko
parent 1de6c52545
commit 8e33fc29d7
1 changed files with 29 additions and 37 deletions

View File

@ -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 { } else {
i--; rtn += str.slice(0, i + 1);
str = str.slice(i + 1);
} }
insideShortname = false; } else {
} else if (insideTag && char === '>') { rtn += str.slice(0, tagend);
insideTag = false; str = str.slice(tagend);
} else if (char === '<') { }
insideTag = true; } else {
insideShortname = false; const [filename, shortCode] = unicodeMapping[match];
} else if (!insideTag && char === ':') { rtn += str.slice(0, i) + `<img draggable="false" class="emojione" alt="${match}" title=":${shortCode}:" src="/emoji/${filename}.svg" />`;
insideShortname = true; str = str.slice(i + match.length);
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
} }
} }
} return rtn + str;
return str;
}; };
export default emojify; export default emojify;