What is twemoji-parser?
The twemoji-parser npm package is a utility for parsing and processing emoji characters in text using the Twemoji library. It allows you to identify, extract, and manipulate emoji characters in strings, making it useful for applications that need to handle emoji-rich content.
What are twemoji-parser's main functionalities?
Parsing Emojis
This feature allows you to parse a string and identify all the emoji characters within it. The `parse` method returns an array of objects, each representing an emoji found in the text.
const twemojiParser = require('twemoji-parser');
const text = 'Hello 🌍!';
const parsed = twemojiParser.parse(text);
console.log(parsed);
Converting Emojis to HTML
This feature allows you to convert parsed emoji characters into their corresponding HTML code points. This can be useful for rendering emojis in web applications.
const twemojiParser = require('twemoji-parser');
const text = 'Hello 🌍!';
const parsed = twemojiParser.parse(text);
const html = parsed.map(emoji => twemojiParser.toCodePoint(emoji.text)).join('');
console.log(html);
Extracting Emoji URLs
This feature allows you to extract URLs for the emoji images from the parsed text. The URLs point to the Twemoji CDN, where the emoji images are hosted.
const twemojiParser = require('twemoji-parser');
const text = 'Hello 🌍!';
const parsed = twemojiParser.parse(text);
const urls = parsed.map(emoji => `https://twemoji.maxcdn.com/v/latest/72x72/${twemojiParser.toCodePoint(emoji.text)}.png`);
console.log(urls);
Other packages similar to twemoji-parser
emoji-regex
The emoji-regex package provides a regular expression to match all emoji symbols and sequences. Unlike twemoji-parser, it focuses solely on matching emojis in text without additional parsing or conversion functionalities.
node-emoji
The node-emoji package allows you to easily replace emoji codes in a string with the corresponding emoji characters and vice versa. It also provides a list of all available emojis. While it offers some parsing capabilities, it does not provide the same level of detail or conversion to HTML as twemoji-parser.
emojione
The emojione package provides a comprehensive set of tools for working with emojis, including parsing, converting to HTML, and rendering. It is similar to twemoji-parser but uses the EmojiOne library for its emoji set.
Twemoji Parser
A simple library for identifying emoji entities within a string in order to render them as Twemoji.
For example, this parser is used within the rendering flow for Tweets and other text on mobile.twitter.com
Setup
Add twemoji-parser
as a dependency to your project:
yarn add twemoji-parser
Or, to work directly in this repo, clone it and run yarn install
from the repo root.
Usage
The tests are intended to serve as a more exhaustive source of documentation, but the general idea is that the parser takes a string and returns an array of the emoji entities it finds:
import { parse } from 'twemoji-parser';
const entities = parse('I 🧡 Twemoji! 🥳');
Authors
- Nathan Downs <ndowns [at] twitter [dot] com>
Follow @TwitterOSS on Twitter for updates.
Contributing
We feel that a welcoming community is important and we ask that you follow Twitter's
Open Source Code of Conduct
in all interactions with the community.
Support
Create a new issue on GitHub.
Security Issues?
Please report sensitive security issues via Twitter's bug-bounty program (https://hackerone.com/twitter) rather than GitHub.
License
MIT https://github.com/twitter/twemoji-parser/blob/master/LICENSE.md