javascript - How to replace emojis in html with twemoji? - Stack Overflow

In user-generated posts, I have many raw emojis like<p>I ❤ your post!<p> I'm wonderin

In user-generated posts, I have many raw emojis like

   <p>I ❤ your post!</p> 

I'm wondering what is the best way to replace these emojis with colorful emojis like:

 <p>I <i class="twa twa-heart">❤️</i> your post!</p>

I'd like to style the emojies with twemoji library.

You might ask why not let users insert the styled emojis at the first place while picking the emojis? The answer is that the interface's text editor is not WYSIWYG and the user posts are heavily stripped. So such option is ruled out and I'm looking for a solution that does this conversion at render time.

In user-generated posts, I have many raw emojis like

   <p>I ❤ your post!</p> 

I'm wondering what is the best way to replace these emojis with colorful emojis like:

 <p>I <i class="twa twa-heart">❤️</i> your post!</p>

I'd like to style the emojies with twemoji library.

You might ask why not let users insert the styled emojis at the first place while picking the emojis? The answer is that the interface's text editor is not WYSIWYG and the user posts are heavily stripped. So such option is ruled out and I'm looking for a solution that does this conversion at render time.

Share Improve this question edited May 30, 2019 at 14:00 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked May 30, 2019 at 13:53 KarlomKarlom 14.9k29 gold badges78 silver badges118 bronze badges 3
  • You'd just use replace. – Jack Bashford Commented May 30, 2019 at 13:54
  • @Jack Bashford. There are dozens of emojis to deal with. So I'm not sure what is the most suitable to parse and replace the whole page. – Karlom Commented May 30, 2019 at 13:55
  • The documentation on their front page in GitHub literally tells you how to do it: twemoji.parse(document.body); – Strelok Commented May 30, 2019 at 14:05
Add a ment  | 

3 Answers 3

Reset to default 4

Check out the docs for twemoji.... https://github./twitter/twemoji#api

API

Following are all the methods exposed in the twemoji namespace.

twemoji.parse( ... ) V1

This is the main parsing utility and has 3 overloads per parsing type.

There are mainly two kinds of parsing: string parsing and DOM parsing.

You can parse strings (parse their input).

I believe you're looking for smoething like this:

However, for already sanitized strings, this method can be considered safe enough. Please see DOM parsing if security is one of your major concerns.

twemoji.parse('I \u2764\uFE0F emoji!');

// will produce
/*
I <img
  class="emoji"
  draggable="false"
  alt="❤️"
  src="https://twemoji.maxcdn./36x36/2764.png"/> emoji!
*/

Use an object, where the key is the stripped-back emoji, and the value is the full HTML content. Iterate through each character in the string and check if it's a stripped-back emoji. If so, replace it. Finally, join it all together and write to the page.

const raw = "<p>I ❤ your post!</p>";
const emojis = {
  "❤": `<i class="twa twa-heart">❤️</i>`
};
const res = [...raw].map(e => emojis[e] || e).join("");
document.write(res);

If you don't need library:

var codePoints = Array.from(

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745039400a4607720.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信