What is markdown-it-emoji?
The markdown-it-emoji package is a plugin for the markdown-it Markdown parser that allows you to easily include emoji in your Markdown documents. It converts emoji shortcodes (like :smile:) into their corresponding Unicode emoji characters.
What are markdown-it-emoji's main functionalities?
Basic Emoji Conversion
This feature allows you to convert emoji shortcodes into their corresponding Unicode emoji characters. By using the markdown-it-emoji plugin with markdown-it, you can easily include emojis in your Markdown content.
const md = require('markdown-it')();
const emoji = require('markdown-it-emoji');
md.use(emoji);
const result = md.render('I :heart: Markdown!');
console.log(result); // Outputs: <p>I ❤️ Markdown!</p>
Custom Emoji Definitions
This feature allows you to define custom emoji shortcodes and their corresponding Unicode characters. You can extend or override the default emoji definitions provided by the plugin.
const md = require('markdown-it')();
const emoji = require('markdown-it-emoji');
md.use(emoji, {
defs: {
smile: '😃',
custom_emoji: '🚀'
}
});
const result = md.render('I :smile: using :custom_emoji:!');
console.log(result); // Outputs: <p>I 😃 using 🚀!</p>
Custom Emoji Rendering
This feature allows you to customize the rendering of emojis. You can define how the emojis should be rendered in the final HTML output, such as wrapping them in custom HTML tags or adding CSS classes.
const md = require('markdown-it')();
const emoji = require('markdown-it-emoji');
md.use(emoji, {
shortcuts: {
angry: [':angry:', '>:(']
},
renderer: function (token, idx) {
return '<span class="emoji emoji-' + token[idx].markup + '">' + token[idx].content + '</span>';
}
});
const result = md.render('I am >:( right now!');
console.log(result); // Outputs: <p>I am <span class="emoji emoji-angry">😠</span> right now!</p>
Other packages similar to markdown-it-emoji
emojione
The emojione package provides a comprehensive set of emoji images and a JavaScript library for converting emoji shortcodes, Unicode characters, and ASCII emoticons into emoji images. Compared to markdown-it-emoji, emojione offers more customization options for rendering emojis as images rather than Unicode characters.
emoji-dictionary
The emoji-dictionary package provides a simple way to look up emoji characters by their names or shortcodes. It is more focused on providing a dictionary of emojis rather than integrating with Markdown parsing, making it a more lightweight option compared to markdown-it-emoji.
markdown-it
The markdown-it package is a Markdown parser that can be extended with plugins like markdown-it-emoji. While markdown-it itself does not provide emoji support out of the box, it serves as the foundation for adding such functionality through plugins.
markdown-it-emoji
Plugin for markdown-it markdown parser, adding emoji & emoticon syntax support.
v1.+ requires markdown-it
v4.+, see changelog.
Two versions:
- Full (default), with all github supported emojies.
- Light, with only well supported unicode emojies and reduced size.
Also supports emoticons shortcuts like :)
, :-(
, and other. See the full list in link above.
Install
node.js, browser:
npm install markdown-it-emoji --save
bower install markdown-it-emoji --save
Use
init
var md = require('markdown-it')();
var emoji = require('markdown-it-emoji');
md.use(emoji [, options]);
Options are not mantatory:
- defs (Object) - rewrite available emojies definitions
- example:
{ name1: char1, name2: char2, ... }
- enabled (Array) - disable all emojies except whitelisted
- shortcuts (Object) - rewrite default shortcuts
- example:
{ "smile": [ ":)", ":-)" ], "laughing": ":D" }
Differences in browser. If you load script directly into the page, without
package system, module will add itself globally with name markdownitEmoji
.
Then init will look a bit different:
var md = window.markdownit().use(window.markdownitEmoji);
change output
By default, emojies are rendered as appropriate unicode chars. But you can change
renderer function as you wish.
Render as span blocks (for example, to use custom iconic font):
md.renderer.rules.emoji = function(token, idx) {
return '<span class="emoji emoji_' + token[idx].markup + '"></span>';
};
Or use twemoji:
var twemoji = require('twemoji')
md.renderer.rules.emoji = function(token, idx) {
return twemoji.parse(token[idx].content);
};
NB 1. Read twemoji docs!
May be you need more options to change image size & type.
NB 2. For twemoji you can like to fit image height to line height with this
style:
.emoji {
height: 1.2em;
}
License
MIT