This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
forked from devongovett/apple-color-emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (43 loc) 路 1.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var path = require('path');
exports.imageDir = path.join(__dirname, 'images');
exports.basePath = exports.imageDir;
exports.regex = require('./regex');
// Detect native browser support for emoji
exports.nativeSupport = (function() {
if (typeof document === 'undefined')
return false;
var canvas = document.createElement('canvas');
if (!canvas.getContext)
return false;
var ctx = canvas.getContext('2d');
ctx.textBaseline = 'top';
ctx.font = '32px sans-serif';
ctx.fillText('馃槂', 0, 0);
return ctx.getImageData(16, 16, 1, 1).data[0] !== 0;
})();
function getImage(str) {
// strip unicode variation selectors
str = str.replace(/[\ufe00-\ufe0f\u200d]/g, '');
var name = [];
for (var i = 0; i < str.length; i++)
name.push(('0000' + str.charCodeAt(i).toString(16)).slice(-4));
return exports.basePath.replace(/\/$/, '') + '/' + name.join('-') + '.png';
}
// make a regex to test whether an entire string is an emoji
var testRegex = new RegExp('^(' + exports.regex.toString().slice(1, -2) + ')$');
// Get the image path for the given emoji string
exports.getImage = function(chars) {
if (!testRegex.test(chars))
return null;
return getImage(chars);
};
// Replaces occurrences of emoji in the string with <img> tags
// if there is no native support for emoji in the current environment
exports.replace = function(string) {
string = '' + string;
if (exports.nativeSupport)
return string;
return string.replace(exports.regex, function(c) {
return '<img class="emoji" src="' + getImage(c) + '" alt="' + c + '">';
});
};