]>
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 assetHost
= process
.env
.CDN_HOST
|| '';
8 const emojify
= (str
, customEmojis
= {}) => {
11 let match
, i
= 0, tag
;
12 while (i
< str
.length
&& (tag
= '<&'.indexOf(str
[i
])) === -1 && str
[i
] !== ':' && !(match
= trie
.search(str
.slice(i
)))) {
13 i
+= str
.codePointAt(i
) < 65536 ? 1 : 2;
18 const tagend
= str
.indexOf('>;'[tag
], i
+ 1) + 1;
21 rtn
+= str
.slice(0, tagend
);
22 str
= str
.slice(tagend
);
23 } else if (str
[i
] === ':') {
25 // if replacing :shortname: succeed, exit this block with "continue"
26 const closeColon
= str
.indexOf(':', i
+ 1) + 1;
27 if (!closeColon
) throw null; // no pair of ':'
28 const lt
= str
.indexOf('<', i
+ 1);
29 if (!(lt
=== -1 || lt
>= closeColon
)) throw null; // tag appeared before closing ':'
30 const shortname
= str
.slice(i
, closeColon
);
31 if (shortname
in customEmojis
) {
32 rtn
+= str
.slice(0, i
) + `<img draggable="false" class="emojione" alt="${shortname}" title="${shortname}" src="${customEmojis[shortname]}" />`;
33 str
= str
.slice(closeColon
);
37 // replacing :shortname: failed
38 rtn
+= str
.slice(0, i
+ 1);
39 str
= str
.slice(i
+ 1);
41 const [filename
, shortCode
] = unicodeMapping
[match
];
42 rtn
+= str
.slice(0, i
) + `<img draggable="false" class="emojione" alt="${match}" title=":${shortCode}:" src="${assetHost}/emoji/${filename}.svg" />`;
43 str
= str
.slice(i
+ match
.length
);
49 export default emojify
;
51 export const buildCustomEmojis
= customEmojis
=> {
54 customEmojis
.forEach(emoji
=> {
55 const shortcode
= emoji
.get('shortcode');
56 const url
= emoji
.get('url');
57 const name
= shortcode
.replace(':', '');
This page took 0.094551 seconds and 4 git commands to generate.