twemoji README

The project's README from github.com/twitter/twemoji. As one markdown file: README.md.

Twitter Emoji (Twemoji) Build Status

A simple library that provides standard Unicode emoji support across all platforms.

Twemoji v14.0 adheres to the Unicode 14.0 spec and supports the Emoji 14.0 spec. We do not support custom emoji.

The Twemoji library offers support for all Unicode-defined emoji which are recommended for general interchange (RGI).

Usage

CDN Support

The folks over at MaxCDN have graciously provided CDN support.

MaxCDN is shut down right now, so in the meanwhile use a different CDN or download the assets. (See Maxcdn has shut down, cdn not working anymore. · Issue #580 · twitter/twemoji).

Use the following in the <head> tag of your HTML document(s):

<script src="https://unpkg.com/twemoji@latest/dist/twemoji.min.js" crossorigin="anonymous"></script>

This guarantees that you will always use the latest version of the library.

If, instead, you'd like to include the latest version explicitly, you can add the following tag:

<script src="https://unpkg.com/[email protected]/dist/twemoji.min.js" integrity="sha384-eoGiwFCoIsUzdZGbvJ/7h/ICofqh5LolgoDnsgdbLptvnpK4+/swGDdkv3sb6bq+" crossorigin="anonymous"></script>

Download

If instead you want to download a specific version, please look at the gh-pages branch, where you will find the built assets for both our latest and older versions.

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.

Although there are two kinds of parsing supported by this utility, we recommend you use DOM parsing, explained below. Each type of parsing accepts a callback to generate an image source or an options object with parsing info.

The second kind of parsing is string parsing, explained in the legacy documentation here. This is unrecommended because this method does not sanitize the string or otherwise prevent malicious code from being executed; such sanitization is out of scope.

DOM parsing

If the first argument to twemoji.parse is an HTMLElement, generated image tags will replace emoji that are inside #text nodes only without compromising surrounding nodes or listeners, and completely avoiding the usage of innerHTML.

If security is a major concern, this parsing can be considered the safest option but with a slight performance penalty due to DOM operations that are inevitably costly.

var div = document.createElement('div');
div.textContent = 'I \u2764\uFE0F emoji!';
document.body.appendChild(div);

twemoji.parse(document.body);

var img = div.querySelector('img');

// note the div is preserved
img.parentNode === div; // true

img.src;        // https://twemoji.maxcdn.com/v/latest/72x72/2764.png
img.alt;        // \u2764\uFE0F
img.className;  // emoji
img.draggable;  // false

All other overloads described for string are available in exactly the same way for DOM parsing.

Object as parameter

Here's the list of properties accepted by the optional object that can be passed to the parse function.

  {
    callback: Function,   // default the common replacer
    attributes: Function, // default returns {}
    base: string,         // default MaxCDN
    ext: string,          // default ".png"
    className: string,    // default "emoji"
    size: string|number,  // default "72x72"
    folder: string        // in case it's specified
                          // it replaces .size info, if any
  }

callback

The function to invoke in order to generate image src(s).

By default it is a function like the following one:

function imageSourceGenerator(icon, options) {
  return ''.concat(
    options.base, // by default Twitter Inc. CDN
    options.size, // by default "72x72" string
    '/',
    icon,         // the found emoji as code point
    options.ext   // by default ".png"
  );
}

base

The default url is the same as twemoji.base, so if you modify the former, it will reflect as default for all parsed strings or nodes.

ext

The default image extension is the same as twemoji.ext which is ".png".

If you modify the former, it will reflect as default for all parsed strings or nodes.

className

The default class for each generated image is emoji. It is possible to specify a different one through this property.

size

The default asset size is the same as twemoji.size which is "72x72".

If you modify the former, it will reflect as default for all parsed strings or nodes.

folder

In case you don't want to specify a size for the image. It is possible to choose a folder, as in the case of SVG emoji.

twemoji.parse(genericNode, {
  folder: 'svg',
  ext: '.svg'
});

This will generate urls such https://twemoji.maxcdn.com/svg/2764.svg instead of using a specific size based image.

Utilities

Basic utilities / helpers to convert code points to JavaScript surrogates and vice versa.

twemoji.convert.fromCodePoint()

For a given HEX codepoint, returns UTF-16 surrogate pairs.

twemoji.convert.fromCodePoint('1f1e8');
 // "\ud83c\udde8"

twemoji.convert.toCodePoint()

For given UTF-16 surrogate pairs, returns the equivalent HEX codepoint.

 twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3');
 // "1f1e8-1f1f3"

 twemoji.convert.toCodePoint('\ud83c\udde8\ud83c\uddf3', '~');
 // "1f1e8~1f1f3"

Tips

Inline Styles

If you'd like to size the emoji according to the surrounding text, you can add the following CSS to your stylesheet:

img.emoji {
   height: 1em;
   width: 1em;
   margin: 0 .05em 0 .1em;
   vertical-align: -0.1em;
}

This will make sure emoji derive their width and height from the font-size of the text they're shown with. It also adds just a little bit of space before and after each emoji, and pulls them upwards a little bit for better optical alignment.

UTF-8 Character Set

To properly support emoji, the document character set must be set to UTF-8. This can be done by including the following meta tag in the document <head>

<meta charset="utf-8">

Exclude Characters (V1)

To exclude certain characters from being replaced by twemoji.js, call twemoji.parse() with a callback, returning false for the specific unicode icon. For example:

twemoji.parse(document.body, {
    callback: function(icon, options, variant) {
        switch ( icon ) {
            case 'a9':      // © copyright
            case 'ae':      // ® registered trademark
            case '2122':    // ™ trademark
                return false;
        }
        return ''.concat(options.base, options.size, '/', icon, options.ext);
    }
});

Legacy API (V1)

If you're still using our V1 API, you can read our legacy documentation here.

Contributing

The contributing documentation can be found here.

Attribution Requirements

As an open source project, attribution is critical from a legal, practical and motivational perspective in our opinion. The graphics are licensed under the CC-BY 4.0 which has a pretty good guide on best practices for attribution.

However, we consider the guide a bit onerous and as a project, will accept a mention in a project README or an 'About' section or footer on a website. In mobile applications, a common place would be in the Settings/About section (for example, see the mobile Twitter application Settings->About->Legal section). We would consider a mention in the HTML/JS source sufficient also.

Community Projects

Committers and Contributors

The goal of this project is to simply provide emoji for everyone. We definitely welcome improvements and fixes, but we may not merge every pull request suggested by the community due to the simple nature of the project.

The rules for contributing are available in the CONTRIBUTING.md file.

Thank you to all of our contributors.

License

Copyright 2019 Twitter, Inc and other contributors

Code licensed under the MIT License: http://opensource.org/licenses/MIT

Graphics licensed under CC-BY 4.0: https://creativecommons.org/licenses/by/4.0/

twemoji (twitter/twemoji)

Searching Database…

17.8K ·GitHub
这是什么?Twemoji 是 Twitter 开源的 emoji 库:它把标准的 Unicode emoji 替换成 Twitter 自家的一套,这样在各个平台上显示效果都一致。v14.0 遵循 Unicode 14.0 和 Emoji 14.0 规范。
它是字体、JavaScript 库,还是只是一堆图片?是一个小型 JavaScript 库加图片资源。调用 twemoji.parse() 会把页面或字符串里的 emoji 替换成指向 PNG 文件的标签(也支持 SVG)。
怎么把它加到我的网站上?在 HTML 里加上来自 CDN 的 twemoji 脚本标签,然后调用 twemoji.parse()。MaxCDN 已经关闭了,所以要换用其他 CDN,或者从 gh-pages 分支获取打包好的资源文件。
网页需要什么特殊设置才能正常显示 emoji 吗?只有一个要求:页面必须声明 UTF-8 字符集,通常是在 head 里加一个 meta charset 标签。
它能覆盖所有 emoji 吗,包括自定义的?不支持自定义 emoji。它支持 Unicode 14.0 规范中所有推荐通用交换(RGI)的 Unicode emoji。
真的可以免费使用吗,许可证上有没有什么限制?代码采用 MIT 许可证,但图形部分采用 CC-BY 4.0,要求注明来源 —— 团队接受在项目的 README、关于页面或网站页脚里简单提一句即可。
让 twemoji.parse 处理用户输入的文字安全吗?请使用 DOM 解析:文档说传入 HTMLElement 是最安全的方式,因为它完全不经过 innerHTML。直接解析普通字符串不会做任何过滤,也无法阻止恶意代码执行 —— 对用户输入做净化要靠你自己。
我能调整替换的效果吗 —— 比如尺寸、格式,或者跳过某些 emoji?可以。twemoji.parse() 支持一些选项:可以用回调函数生成图片 URL,或者返回 false 来排除特定的 emoji;此外还有基础 URL(base)、扩展名(默认 .png)、尺寸(默认 72x72)、文件夹(比如 SVG)和 CSS 类名(默认 'emoji')。
怎么让 emoji 和周围文字的大小一致?加上 Tips 部分提供的那段 CSS:这样每个 emoji 图片的宽高都会跟随文字的 font-size,还内置了少量间距和视觉对齐的微调。
Twemoji 能用在普通网页之外的地方吗?可以 —— README 里列出了社区项目,涵盖 Ruby、PHP、.NET、一个 Vue Twemoji 选择器、一个 iOS 框架、Roku、LaTeX,甚至还有给 Linux 和 OS X 用的 Twemoji 系统字体。
What are similar projects?mojs, rough, pdfkit, devicon, zdog, raphael, flag-icons, plotly.js
Creator
twitter
Size
822.3 MB
Stars
17.8K
Age
11y
Commits · 90 Days