]>
cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/emoji.js
1 import { unicodeMapping
} from './emojione_light';
2 import Trie
from 'substring-trie';
4 const trie
= new Trie(Object
.keys(unicodeMapping
));
6 const emojify
= (str
, customEmojis
= {}) => {
10 while (i
< str
.length
&& (tag
= '<&'.indexOf(str
[i
])) === -1 && str
[i
] !== ':' && !(match
= trie
.search(str
.slice(i
)))) {
11 i
+= str
.codePointAt(i
) < 65536 ? 1 : 2;
16 const tagend
= str
.indexOf('>;'[tag
], i
+ 1) + 1;
19 rtn
+= str
.slice(0, tagend
);
20 str
= str
.slice(tagend
);
21 } else if (str
[i
] === ':') {
23 // if replacing :shortname: succeed, exit this block with "continue"
24 const closeColon
= str
.indexOf(':', i
+ 1) + 1;
25 if (!closeColon
) throw null; // no pair of ':'
26 const lt
= str
.indexOf('<', i
+ 1);
27 if (!(lt
=== -1 || lt
>= closeColon
)) throw null; // tag appeared before closing ':'
28 const shortname
= str
.slice(i
, closeColon
);
29 if (shortname
in customEmojis
) {
30 rtn
+= str
.slice(0, i
) + `<img draggable="false" class="emojione" alt="${shortname}" title="${shortname}" src="${customEmojis[shortname]}" />`;
31 str
= str
.slice(closeColon
);
35 // replacing :shortname: failed
36 rtn
+= str
.slice(0, i
+ 1);
37 str
= str
.slice(i
+ 1);
39 const [filename
, shortCode
] = unicodeMapping
[match
];
40 rtn
+= str
.slice(0, i
) + `<img draggable="false" class="emojione" alt="${match}" title=":${shortCode}:" src="/emoji/${filename}.svg" />`;
41 str
= str
.slice(i
+ match
.length
);
47 export default emojify
;
This page took 0.198953 seconds and 4 git commands to generate.