]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/emoji.js
redo #4500 with customEmojis (#5016)
[mastodon.git] / app / javascript / mastodon / emoji.js
1 import { unicodeMapping } from './emojione_light';
2 import Trie from 'substring-trie';
3
4 const trie = new Trie(Object.keys(unicodeMapping));
5
6 const emojify = (str, customEmojis = {}) => {
7 let rtn = '';
8 for (;;) {
9 let match, i = 0, tag;
10 while (i < str.length && (tag = '<&:'.indexOf(str[i])) === -1 && !(match = trie.search(str.slice(i)))) {
11 i += str.codePointAt(i) < 65536 ? 1 : 2;
12 }
13 if (i === str.length)
14 break;
15 else if (tag >= 0) {
16 let tagend = str.indexOf('>;:'[tag], i + 1) + 1;
17 if (!tagend)
18 break;
19 if (str[i] === ':') {
20 const shortname = str.slice(i, tagend);
21 const lt = str.indexOf('<', i + 1);
22 if ((lt === -1 || lt >= tagend) && shortname in customEmojis) {
23 rtn += str.slice(0, i) + `<img draggable="false" class="emojione" alt="${shortname}" title="${shortname}" src="${customEmojis[shortname]}" />`;
24 str = str.slice(tagend);
25 } else {
26 rtn += str.slice(0, i + 1);
27 str = str.slice(i + 1);
28 }
29 } else {
30 rtn += str.slice(0, tagend);
31 str = str.slice(tagend);
32 }
33 } else {
34 const [filename, shortCode] = unicodeMapping[match];
35 rtn += str.slice(0, i) + `<img draggable="false" class="emojione" alt="${match}" title=":${shortCode}:" src="/emoji/${filename}.svg" />`;
36 str = str.slice(i + match.length);
37 }
38 }
39 return rtn + str;
40 };
41
42 export default emojify;
This page took 0.076021 seconds and 4 git commands to generate.