@discordapp/twemoji

中文文档
17.0.3发布于 2026/11/10MIT AND CC-BY-4.0TypeScriptESM + CJS近 30 天下载 57.3万

A Unicode standard based way to implement emoji across all platforms.

原始使用文档
文档目录20 节
  1. Usage
  2. Install via NPM
  3. CDN Support
  4. Download
  5. API
  6. twemoji.parse( ... ) V1
  7. Object as parameter
  8. Utilities
  9. twemoji.convert.fromCodePoint()
  10. twemoji.convert.toCodePoint()
  11. Tips
  12. Inline Styles
  13. UTF-8 Character Set
  14. Exclude Characters (V1)
  15. Legacy API (V1)
  16. Contributing
  17. Attribution Requirements
  18. Community Projects
  19. Committers and Contributors
  20. License

Twitter Emoji (Twemoji)

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

Twemoji v17.0 adheres to the Unicode 17.0 spec and supports the Emoji 17.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

Install via NPM

Run the following inside of your already-initialized project:

npm install @twemoji/api

CDN Support

Default CDN support is provided via jsDelivr.

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

<script src="https://cdn.jsdelivr.net/npm/@twemoji/api@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://cdn.jsdelivr.net/npm/@twemoji/[email protected]/dist/twemoji.min.js" integrity="sha384-Y5xukbGJwykbHHkTbLJykYLcBPFxrwipTbEh0puxhkz9CZ90raTPGe2Ks4vCxsYU" 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://cdn.jsdelivr.net/gh/jdecked/twemoji@latest/assets/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 jsDelivr
    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 jsDelivr
    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://cdn.jsdelivr.net/gh/jdecked/twemoji@latest/assets/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

  • Justine De Caires (ex-Twitter)
  • Jason Sofonia (ex-Twitter)
  • Bryan Haggerty (ex-Twitter)
  • Nathan Downs (ex-Twitter)
  • Tom Wuttke (ex-Twitter)
  • Andrea Giammarchi (ex-Twitter)
  • Joen Asmussen (WordPress)
  • Marcus Kazmierczak (WordPress)
  • Kevin VQ Dam (ex-Discord)
  • Gica Tam (Discord)
  • Ben Olson (Discord)

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

See the LICENSE and LICENSE-GRAPHICS files for full license texts.

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/

版本与发布记录

@discordapp/twemoji 共发布 14 个版本,首次发布于 2019/11/06;过去 12 个月发布了 1 个版本。

版本发布日期距今
17.0.3latest2026/11/102 天前
16.0.12025/07/011.4 年前
15.1.02024/08/152 年前
15.0.32024/03/292 年前
15.0.22023/12/052 年前
14.1.22023/03/293 年前

关于 @discordapp/twemoji 的常见问题

@discordapp/twemoji 是什么?

A Unicode standard based way to implement emoji across all platforms.

如何安装 @discordapp/twemoji?

在项目目录运行 npm install @discordapp/twemoji,或使用 pnpm add / yarn add / bun add。

@discordapp/twemoji 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@discordapp/twemoji 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@discordapp/twemoji 可以商用吗?

@discordapp/twemoji 使用 MIT AND CC-BY-4.0 许可。声明的许可为 MIT AND CC-BY-4.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@discordapp/twemoji 的下载量有多少?

根据 npm 官方下载统计,@discordapp/twemoji 在 2026/10/13 – 2026/11/11 这 30 天内被下载 572,609 次,日均约 19,087 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@discordapp/twemoji 还在维护吗?

最新版本 17.0.3 发布于 2026/11/10(2 天前);至今共发布 14 个版本,其中过去 12 个月发布了 1 个;首次发布于 2019/11/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。