2019-03-15 00:44:49 -04:00
|
|
|
import {
|
|
|
|
aliases,
|
2020-12-01 13:31:26 -05:00
|
|
|
emojis,
|
2019-03-21 04:11:33 -04:00
|
|
|
replacements,
|
2019-03-15 00:44:49 -04:00
|
|
|
searchAliases,
|
2019-03-21 04:11:33 -04:00
|
|
|
tonableEmojis,
|
|
|
|
translations,
|
2019-03-15 00:44:49 -04:00
|
|
|
} from "pretty-text/emoji/data";
|
|
|
|
import { IMAGE_VERSION } from "pretty-text/emoji/version";
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
const extendedEmoji = {};
|
|
|
|
|
2020-03-30 14:16:10 -04:00
|
|
|
export function registerEmoji(code, url, group) {
|
2019-03-13 08:02:56 -04:00
|
|
|
code = code.toLowerCase();
|
2020-03-30 14:16:10 -04:00
|
|
|
extendedEmoji[code] = { url, group };
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function extendedEmojiList() {
|
|
|
|
return extendedEmoji;
|
|
|
|
}
|
|
|
|
|
|
|
|
const emojiHash = {};
|
|
|
|
|
2021-04-22 02:43:06 -04:00
|
|
|
// https://github.com/mathiasbynens/emoji-regex/blob/main/text.js
|
|
|
|
export const emojiReplacementRegex =
|
|
|
|
"\uD83C\uDFF4\uDB40\uDC67\uDB40\uDC62(?:\uDB40\uDC77\uDB40\uDC6C\uDB40\uDC73|\uDB40\uDC73\uDB40\uDC63\uDB40\uDC74|\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67)\uDB40\uDC7F|(?:\uD83E\uDDD1\uD83C\uDFFF\u200D\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFF\u200D\uD83E\uDD1D\u200D(?:\uD83D[\uDC68\uDC69]))(?:\uD83C[\uDFFB-\uDFFE])|(?:\uD83E\uDDD1\uD83C\uDFFE\u200D\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFE\u200D\uD83E\uDD1D\u200D(?:\uD83D[\uDC68\uDC69]))(?:\uD83C[\uDFFB-\uDFFD\uDFFF])|(?:\uD83E\uDDD1\uD83C\uDFFD\u200D\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFD\u200D\uD83E\uDD1D\u200D(?:\uD83D[\uDC68\uDC69]))(?:\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])|(?:\uD83E\uDDD1\uD83C\uDFFC\u200D\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFC\u200D\uD83E\uDD1D\u200D(?:\uD83D[\uDC68\uDC69]))(?:\uD83C[\uDFFB\uDFFD-\uDFFF])|(?:\uD83E\uDDD1\uD83C\uDFFB\u200D\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D)?\uD83E\uDDD1|\uD83D\uDC69\uD83C\uDFFB\u200D\uD83E\uDD1D\u200D(?:\uD83D[\uDC68\uDC69]))(?:\uD83C[\uDFFC-\uDFFF])|\uD83D\uDC68(?:\uD83C\uDFFB(?:\u200D(?:\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFF])|\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFF]))|\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFC-\uDFFF])|[\u2695\u2696\u2708]\uFE0F|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD]))?|(?:\uD83C[\uDFFC-\uDFFF])\u200D\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFF])|\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFF]))|\u200D(?:\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D)?\uD83D\uDC68|(?:\uD83D[\uDC68\uDC69])\u200D(?:\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67]))|\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67])|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFF\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFE])|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFE\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB-\uDFFD\uDFFF])|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFD\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB\uDFFC\uDFFE\uDFFF])|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFC\u200D(?:\uD83E\uDD1D\u200D\uD83D\uDC68(?:\uD83C[\uDFFB\uDFFD-\uDFFF])|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|(?:\uD83C\uDFFF\u200D[\u2695\u2696\u2708]|\uD83C\uDFFE\u200D[\u2695\u2696\u2708]|\uD83C\uDFFD\u200D[\u2695\u2696\u2708]|\uD83C\uDFFC\u200D[\u2695\u2696\u2708]|\u200D[\u2695\u2696\u2708])\uFE0F|\u200D(?:(?:\uD83D[\uDC68\uDC69])\u200D(?:\uD83D[\uDC66\uDC67])|\uD83D[\uDC66\uDC67])|\uD83C\uDFFF|\uD83C\uDFFE|\uD83C\uDFFD|\uD83C\uDFFC)?|(?:\uD83D\uDC69(?:\uD83C\uDFFB\u200D\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D(?:\uD83D[\uDC68\uDC69])|\uD83D[\uDC68\uDC69])|(?:\uD83C[\uDFFC-\uDFFF])\u200D\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D(?:\uD83D[\uDC68\uDC69])|\uD83D[\uDC68\uDC69]))|\uD83E\uDDD1(?:\uD83C[\uDFFB-\uDFFF])\u200D\uD83E\uDD1D\u200D\uD83E\uDDD1)(?:\uD83C[\uDFFB-\uDFFF])|\uD83D\uDC69\u200D\uD83D\uDC69\u200D(?:\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67]))|\uD83D\uDC69(?:\u200D(?:\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D(?:\uD83D[\uDC68\uDC69])|\uD83D[\uDC68\uDC69])|\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])|\uD83C\uDFFF\u200D(?:\uD83C[\uDF3E\uDF73\uDF7C\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]|\uD83E[\uDDAF-\uDDB3\uDDBC\uDDBD])
|
2019-12-03 11:32:33 -05:00
|
|
|
|
2021-04-22 02:43:06 -04:00
|
|
|
function textEmojiRegex(inlineEmoji) {
|
|
|
|
return inlineEmoji ? /:[^\s:]+(?::t\d)?:?/g : /\B:[^\s:]+(?::t\d)?:?\B/g;
|
2019-12-03 11:32:33 -05:00
|
|
|
}
|
2019-03-28 17:31:27 -04:00
|
|
|
|
2019-03-13 08:02:56 -04:00
|
|
|
// add all default emojis
|
2019-03-15 00:44:49 -04:00
|
|
|
emojis.forEach((code) => (emojiHash[code] = true));
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
// and their aliases
|
|
|
|
const aliasHash = {};
|
|
|
|
Object.keys(aliases).forEach((name) => {
|
2019-03-15 00:44:49 -04:00
|
|
|
aliases[name].forEach((alias) => (aliasHash[alias] = name));
|
2019-03-13 08:02:56 -04:00
|
|
|
});
|
|
|
|
|
2019-12-03 11:32:33 -05:00
|
|
|
function isReplacableInlineEmoji(string, index, inlineEmoji) {
|
2020-09-22 10:28:28 -04:00
|
|
|
if (inlineEmoji) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
// index depends on regex; when `inlineEmoji` is false, the regex starts
|
|
|
|
// with a `\B` character, so there's no need to subtract from the index
|
2020-05-07 10:25:23 -04:00
|
|
|
const beforeEmoji = string.slice(0, index);
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
beforeEmoji.length === 0 ||
|
|
|
|
/(?:\s|[>.,\/#!$%^&*;:{}=\-_`~()])$/.test(beforeEmoji) ||
|
2021-04-22 02:43:06 -04:00
|
|
|
new RegExp(`(?:${emojiReplacementRegex})$`, "g").test(beforeEmoji)
|
2019-12-03 11:32:33 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-13 08:02:56 -04:00
|
|
|
export function performEmojiUnescape(string, opts) {
|
2020-03-30 14:16:10 -04:00
|
|
|
if (!string) {
|
2019-03-15 00:44:49 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
|
2020-05-27 14:11:52 -04:00
|
|
|
const allTranslations = Object.assign(
|
|
|
|
{},
|
|
|
|
translations,
|
|
|
|
opts.customEmojiTranslation || {}
|
|
|
|
);
|
2019-12-03 11:32:33 -05:00
|
|
|
|
2021-04-22 02:43:06 -04:00
|
|
|
const replacementFunction = (m, index) => {
|
2020-05-27 14:11:52 -04:00
|
|
|
const isEmoticon = opts.enableEmojiShortcuts && !!allTranslations[m];
|
2019-03-21 04:11:33 -04:00
|
|
|
const isUnicodeEmoticon = !!replacements[m];
|
|
|
|
let emojiVal;
|
2021-04-22 02:43:06 -04:00
|
|
|
|
2019-03-21 04:11:33 -04:00
|
|
|
if (isEmoticon) {
|
2020-05-27 14:11:52 -04:00
|
|
|
emojiVal = allTranslations[m];
|
2019-03-21 04:11:33 -04:00
|
|
|
} else if (isUnicodeEmoticon) {
|
|
|
|
emojiVal = replacements[m];
|
|
|
|
} else {
|
|
|
|
emojiVal = m.slice(1, m.length - 1);
|
|
|
|
}
|
|
|
|
const hasEndingColon = m.lastIndexOf(":") === m.length - 1;
|
|
|
|
const url = buildEmojiUrl(emojiVal, opts);
|
2020-08-24 08:20:51 -04:00
|
|
|
let classes = isCustomEmoji(emojiVal, opts)
|
2019-03-21 04:11:33 -04:00
|
|
|
? "emoji emoji-custom"
|
|
|
|
: "emoji";
|
|
|
|
|
2020-08-24 08:20:51 -04:00
|
|
|
if (opts.class) {
|
|
|
|
classes = `${classes} ${opts.class}`;
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:32:33 -05:00
|
|
|
const isReplacable =
|
|
|
|
(isEmoticon || hasEndingColon || isUnicodeEmoticon) &&
|
2021-04-22 02:43:06 -04:00
|
|
|
isReplacableInlineEmoji(string, index, opts.inlineEmoji);
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
return url && isReplacable
|
2020-09-28 07:23:46 -04:00
|
|
|
? `<img width="20" height="20" src='${url}' ${
|
|
|
|
opts.skipTitle ? "" : `title='${emojiVal}'`
|
|
|
|
} ${
|
2020-08-24 08:20:51 -04:00
|
|
|
opts.lazy ? "loading='lazy' " : ""
|
|
|
|
}alt='${emojiVal}' class='${classes}'>`
|
2019-03-21 04:11:33 -04:00
|
|
|
: m;
|
2021-04-22 02:43:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return string
|
|
|
|
.replace(new RegExp(emojiReplacementRegex, "g"), replacementFunction)
|
|
|
|
.replace(textEmojiRegex(opts.inlineEmoji), replacementFunction);
|
2019-03-21 04:11:33 -04:00
|
|
|
}
|
|
|
|
|
2019-05-21 10:56:51 -04:00
|
|
|
export function performEmojiEscape(string, opts) {
|
2020-05-27 14:11:52 -04:00
|
|
|
const allTranslations = Object.assign(
|
|
|
|
{},
|
|
|
|
translations,
|
|
|
|
opts.customEmojiTranslation || {}
|
|
|
|
);
|
2019-12-03 11:32:33 -05:00
|
|
|
|
2021-04-22 02:43:06 -04:00
|
|
|
const replacementFunction = (m, index) => {
|
|
|
|
if (isReplacableInlineEmoji(string, index, opts.inlineEmoji)) {
|
2020-05-27 14:11:52 -04:00
|
|
|
if (!!allTranslations[m]) {
|
|
|
|
return opts.emojiShortcuts ? `:${allTranslations[m]}:` : m;
|
2019-12-03 11:32:33 -05:00
|
|
|
} else if (!!replacements[m]) {
|
|
|
|
return `:${replacements[m]}:`;
|
|
|
|
}
|
2019-03-21 04:11:33 -04:00
|
|
|
}
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
return m;
|
2021-04-22 02:43:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return string
|
|
|
|
.replace(new RegExp(emojiReplacementRegex, "g"), replacementFunction)
|
|
|
|
.replace(textEmojiRegex(opts.inlineEmoji), replacementFunction);
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isCustomEmoji(code, opts) {
|
|
|
|
code = code.toLowerCase();
|
2020-09-22 10:28:28 -04:00
|
|
|
if (extendedEmoji.hasOwnProperty(code)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (opts && opts.customEmoji && opts.customEmoji.hasOwnProperty(code)) {
|
2019-03-15 00:44:49 -04:00
|
|
|
return true;
|
2020-09-22 10:28:28 -04:00
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildEmojiUrl(code, opts) {
|
|
|
|
let url;
|
|
|
|
code = String(code).toLowerCase();
|
|
|
|
if (extendedEmoji.hasOwnProperty(code)) {
|
2020-03-30 14:16:10 -04:00
|
|
|
url = extendedEmoji[code].url;
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts && opts.customEmoji && opts.customEmoji[code]) {
|
2020-03-30 14:16:10 -04:00
|
|
|
url = opts.customEmoji[code].url || opts.customEmoji[code];
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const noToneMatch = code.match(/([^:]+):?/);
|
2021-03-02 14:04:16 -05:00
|
|
|
|
|
|
|
let emojiBasePath = "/images/emoji";
|
|
|
|
if (opts.emojiCDNUrl) {
|
|
|
|
emojiBasePath = opts.emojiCDNUrl;
|
|
|
|
}
|
|
|
|
|
2019-03-15 00:44:49 -04:00
|
|
|
if (
|
|
|
|
noToneMatch &&
|
|
|
|
!url &&
|
|
|
|
(emojiHash.hasOwnProperty(noToneMatch[1]) ||
|
|
|
|
aliasHash.hasOwnProperty(noToneMatch[1]))
|
|
|
|
) {
|
|
|
|
url = opts.getURL(
|
2021-03-02 14:04:16 -05:00
|
|
|
`${emojiBasePath}/${opts.emojiSet}/${code.replace(/:t/, "/")}.png`
|
2019-03-15 00:44:49 -04:00
|
|
|
);
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
url = url + "?v=" + IMAGE_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function emojiExists(code) {
|
|
|
|
code = code.toLowerCase();
|
2019-03-15 00:44:49 -04:00
|
|
|
return !!(
|
|
|
|
extendedEmoji.hasOwnProperty(code) ||
|
|
|
|
emojiHash.hasOwnProperty(code) ||
|
|
|
|
aliasHash.hasOwnProperty(code)
|
|
|
|
);
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
let toSearch;
|
|
|
|
export function emojiSearch(term, options) {
|
|
|
|
const maxResults = (options && options["maxResults"]) || -1;
|
2021-02-01 11:36:35 -05:00
|
|
|
const diversity = options && options.diversity;
|
2019-03-15 00:44:49 -04:00
|
|
|
if (maxResults === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
|
2019-03-15 00:44:49 -04:00
|
|
|
toSearch =
|
|
|
|
toSearch ||
|
2020-09-01 14:45:44 -04:00
|
|
|
[
|
|
|
|
...Object.keys(emojiHash),
|
|
|
|
...Object.keys(extendedEmoji),
|
|
|
|
...Object.keys(aliasHash),
|
|
|
|
].sort();
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
function addResult(t) {
|
|
|
|
const val = aliasHash[t] || t;
|
|
|
|
if (results.indexOf(val) === -1) {
|
2021-02-01 11:36:35 -05:00
|
|
|
if (diversity && diversity > 1 && isSkinTonableEmoji(val)) {
|
|
|
|
results.push(`${val}:t${diversity}`);
|
|
|
|
} else {
|
|
|
|
results.push(val);
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if term matches from beginning
|
2019-03-15 00:44:49 -04:00
|
|
|
for (let i = 0; i < toSearch.length; i++) {
|
2019-03-13 08:02:56 -04:00
|
|
|
const item = toSearch[i];
|
2020-09-22 10:28:28 -04:00
|
|
|
if (item.indexOf(term) === 0) {
|
|
|
|
addResult(item);
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (searchAliases[term]) {
|
|
|
|
results.push.apply(results, searchAliases[term]);
|
|
|
|
}
|
|
|
|
|
2019-03-15 00:44:49 -04:00
|
|
|
for (let i = 0; i < toSearch.length; i++) {
|
2019-03-13 08:02:56 -04:00
|
|
|
const item = toSearch[i];
|
2020-09-22 10:28:28 -04:00
|
|
|
if (item.indexOf(term) > 0) {
|
|
|
|
addResult(item);
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (maxResults === -1) {
|
|
|
|
return results;
|
|
|
|
} else {
|
|
|
|
return results.slice(0, maxResults);
|
|
|
|
}
|
2019-03-15 00:44:49 -04:00
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
export function isSkinTonableEmoji(term) {
|
2020-09-01 16:47:22 -04:00
|
|
|
const match = term.split(":").filter(Boolean)[0];
|
2019-03-13 08:02:56 -04:00
|
|
|
if (match) {
|
|
|
|
return tonableEmojis.indexOf(match) !== -1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|