emoji.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import anchorme from "anchorme";
  2. import twemoji from "twemoji";
  3. const emoji = [];
  4. function parser(text) {
  5. var decodeText = anchorme({
  6. input: text,
  7. options: {
  8. attributes: {
  9. target: "_blank",
  10. }
  11. }
  12. });
  13. // (text.match(/\[[\w\s\u4E00-\u9FCC\u3400-\u4DB5\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\ud840-\ud868\udc00-\udfff\ud869[\udc00-\uded6\udf00-\udfff\ud86a-\ud86c\udc00-\udfff\ud86d[\udc00-\udf34\udf40-\udfff\ud86e\udc00-\udc1d]+\]/g) || []).map(e => {
  14. // var className = getEmojiClassName(e);
  15. //
  16. // if (!className) {
  17. // // Invalid emoji
  18. // return;
  19. // }
  20. // text = decodeText = text.split(`${e}`).join(`<a target="_blank" class="${className}"></a>`);
  21. // });
  22. let emoji = twemoji.parse(decodeText);
  23. // emoji = emoji.replace(/src="https:\/\/twemoji\.maxcdn\.com\/v\/[0-9.]+\//g, 'src="' + twemoji_base_url)
  24. return emoji;
  25. }
  26. function normalize(text = '') {
  27. var matchs = text.match(/<span class="emoji emoji[0-9a-fA-F]+"><\/span>/g) || [];
  28. var decodeText = text;
  29. try {
  30. matchs.map(e => {
  31. // Decode utf16 to emoji
  32. var emojiCode = e.match(/emoji([0-9a-fA-F]+)/)[1].substr(0, 5);
  33. var emoji = String.fromCodePoint(parseInt(emojiCode, 16));
  34. text = decodeText = text.split(e).join(emoji);
  35. });
  36. } catch (ex) {
  37. console.error('Failed decode %s: %o', text, ex);
  38. }
  39. return decodeText;
  40. }
  41. export {emoji, parser, normalize};