闭社主体 forked from https://github.com/tootsuite/mastodon
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.

35 lines
1.2 KiB

  1. import emojione from 'emojione';
  2. const toImage = str => shortnameToImage(unicodeToImage(str));
  3. const unicodeToImage = str => {
  4. const mappedUnicode = emojione.mapUnicodeToShort();
  5. return str.replace(emojione.regUnicode, unicodeChar => {
  6. if (typeof unicodeChar === 'undefined' || unicodeChar === '' || !(unicodeChar in emojione.jsEscapeMap)) {
  7. return unicodeChar;
  8. }
  9. const unicode = emojione.jsEscapeMap[unicodeChar];
  10. const short = mappedUnicode[unicode];
  11. const filename = emojione.emojioneList[short].fname;
  12. const alt = emojione.convert(unicode.toUpperCase());
  13. return `<img draggable="false" class="emojione" alt="${alt}" src="/emoji/${filename}.svg" />`;
  14. });
  15. };
  16. const shortnameToImage = str => str.replace(emojione.regShortNames, shortname => {
  17. if (typeof shortname === 'undefined' || shortname === '' || !(shortname in emojione.emojioneList)) {
  18. return shortname;
  19. }
  20. const unicode = emojione.emojioneList[shortname].unicode[emojione.emojioneList[shortname].unicode.length - 1];
  21. const alt = emojione.convert(unicode.toUpperCase());
  22. return `<img draggable="false" class="emojione" alt="${alt}" src="/emoji/${unicode}.svg" />`;
  23. });
  24. export default function emojify(text) {
  25. return toImage(text);
  26. };