You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
889 B

  1. import { unicodeMapping } from './emojione_light';
  2. import Trie from 'substring-trie';
  3. const trie = new Trie(Object.keys(unicodeMapping));
  4. const emojify = str => {
  5. let rtn = '';
  6. for (;;) {
  7. let match, i = 0;
  8. while (i < str.length && str[i] !== '<' && !(match = trie.search(str.slice(i)))) {
  9. i += str.codePointAt(i) < 65536 ? 1 : 2;
  10. }
  11. if (i === str.length)
  12. break;
  13. else if (str[i] === '<') {
  14. let tagend = str.indexOf('>', i + 1) + 1;
  15. if (!tagend)
  16. break;
  17. rtn += str.slice(0, tagend);
  18. str = str.slice(tagend);
  19. } else {
  20. const [filename, shortCode] = unicodeMapping[match];
  21. rtn += str.slice(0, i) + `<img draggable="false" class="emojione" alt="${match}" title=":${shortCode}:" src="/emoji/${filename}.svg" />`;
  22. str = str.slice(i + match.length);
  23. }
  24. }
  25. return rtn + str;
  26. };
  27. export default emojify;