The code powering m.abunchtell.com https://m.abunchtell.com
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

37 рядки
1.3 KiB

  1. import { unicodeMapping } from './emojione_light';
  2. import Trie from 'substring-trie';
  3. const trie = new Trie(Object.keys(unicodeMapping));
  4. const excluded = ['™', '©', '®'];
  5. function emojify(str) {
  6. // This walks through the string from start to end, ignoring any tags (<p>, <br>, etc.)
  7. // and replacing valid unicode strings
  8. // that _aren't_ within tags with an <img> version.
  9. // The goal is to be the same as an emojione.regUnicode replacement, but faster.
  10. let i = -1;
  11. let insideTag = false;
  12. let match;
  13. while (++i < str.length) {
  14. const char = str.charAt(i);
  15. if (insideTag && char === '>') {
  16. insideTag = false;
  17. } else if (char === '<') {
  18. insideTag = true;
  19. } else if (!insideTag && (match = trie.search(str.substring(i)))) {
  20. const unicodeStr = match;
  21. if (unicodeStr in unicodeMapping && excluded.indexOf(unicodeStr) === -1) {
  22. const [filename, shortCode] = unicodeMapping[unicodeStr];
  23. const alt = unicodeStr;
  24. const replacement = `<img draggable="false" class="emojione" alt="${alt}" title=":${shortCode}:" src="/emoji/${filename}.svg" />`;
  25. str = str.substring(0, i) + replacement + str.substring(i + unicodeStr.length);
  26. i += (replacement.length - unicodeStr.length); // jump ahead the length we've added to the string
  27. }
  28. }
  29. }
  30. return str;
  31. }
  32. export default emojify;