2021-06-01 19:19:38 +00:00
|
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
|
import mathiasBynensEmojiRegex from 'emoji-regex';
|
|
|
|
|
import {
|
|
|
|
|
fromCodepointToUnicode,
|
|
|
|
|
fromHexcodeToCodepoint,
|
|
|
|
|
fromUnicodeToHexcode,
|
|
|
|
|
} from 'emojibase';
|
|
|
|
|
import emojibaseEmojiRegex from 'emojibase-regex/emoji';
|
|
|
|
|
import SHORTCODE_REGEX from 'emojibase-regex/shortcode';
|
2021-04-20 08:52:57 +00:00
|
|
|
|
import type { RenovateConfig } from '../config/types';
|
2021-06-01 19:19:38 +00:00
|
|
|
|
import dataFiles from '../data-files.generated';
|
|
|
|
|
import { regEx } from './regex';
|
2019-09-25 09:40:16 +00:00
|
|
|
|
|
2021-05-02 17:59:36 +00:00
|
|
|
|
let unicodeEmoji = true;
|
2019-09-25 09:40:16 +00:00
|
|
|
|
|
2021-06-01 19:19:38 +00:00
|
|
|
|
let mappingsInitialized = false;
|
|
|
|
|
const shortCodesByHex = new Map<string, string>();
|
|
|
|
|
const hexCodesByShort = new Map<string, string>();
|
|
|
|
|
|
|
|
|
|
function lazyInitMappings(): void {
|
|
|
|
|
if (!mappingsInitialized) {
|
|
|
|
|
const table: Record<string, string | string[]> = JSON.parse(
|
|
|
|
|
dataFiles.get('node_modules/emojibase-data/en/shortcodes/github.json')
|
|
|
|
|
);
|
|
|
|
|
for (const [hex, val] of Object.entries(table)) {
|
|
|
|
|
const shortCodes: string[] = is.array<string>(val) ? val : [val];
|
|
|
|
|
shortCodesByHex.set(hex, `:${shortCodes[0]}:`);
|
|
|
|
|
shortCodes.forEach((shortCode) => {
|
|
|
|
|
hexCodesByShort.set(`:${shortCode}:`, hex);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
mappingsInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 07:43:24 +00:00
|
|
|
|
export function setEmojiConfig(_config: RenovateConfig): void {
|
2019-09-25 09:40:16 +00:00
|
|
|
|
unicodeEmoji = _config.unicodeEmoji;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 19:19:38 +00:00
|
|
|
|
const shortCodeRegex = regEx(SHORTCODE_REGEX.source, 'g');
|
|
|
|
|
|
2019-09-25 09:40:16 +00:00
|
|
|
|
export function emojify(text: string): string {
|
2021-06-01 19:19:38 +00:00
|
|
|
|
if (!unicodeEmoji) {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
lazyInitMappings();
|
|
|
|
|
return text.replace(shortCodeRegex, (shortCode) => {
|
|
|
|
|
const hexCode = hexCodesByShort.get(shortCode);
|
|
|
|
|
return hexCode
|
|
|
|
|
? fromCodepointToUnicode(fromHexcodeToCodepoint(hexCode))
|
|
|
|
|
: shortCode;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const emojiRegexSrc = [emojibaseEmojiRegex, mathiasBynensEmojiRegex()].map(
|
|
|
|
|
({ source }) => source
|
|
|
|
|
);
|
2021-10-27 14:37:11 +00:00
|
|
|
|
const emojiRegex = new RegExp(`(?:${emojiRegexSrc.join('|')})`, 'g'); // TODO #12070
|
2021-06-01 19:19:38 +00:00
|
|
|
|
const excludedModifiers = new Set([
|
|
|
|
|
'20E3',
|
|
|
|
|
'200D',
|
|
|
|
|
'FE0E',
|
|
|
|
|
'FE0F',
|
|
|
|
|
'1F3FB',
|
|
|
|
|
'1F3FC',
|
|
|
|
|
'1F3FD',
|
|
|
|
|
'1F3FE',
|
|
|
|
|
'1F3FF',
|
|
|
|
|
'1F9B0',
|
|
|
|
|
'1F9B1',
|
|
|
|
|
'1F9B2',
|
|
|
|
|
'1F9B3',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
export function stripHexCode(input: string): string {
|
|
|
|
|
return input
|
|
|
|
|
.split('-')
|
|
|
|
|
.filter((modifier) => !excludedModifiers.has(modifier))
|
|
|
|
|
.join('-');
|
2019-09-25 09:40:16 +00:00
|
|
|
|
}
|
2021-04-14 09:03:08 +00:00
|
|
|
|
|
|
|
|
|
export function unemojify(text: string): string {
|
2021-06-01 19:19:38 +00:00
|
|
|
|
if (unicodeEmoji) {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
lazyInitMappings();
|
|
|
|
|
return text.replace(emojiRegex, (emoji) => {
|
|
|
|
|
const hexCode = stripHexCode(fromUnicodeToHexcode(emoji));
|
|
|
|
|
const shortCode = shortCodesByHex.get(hexCode);
|
|
|
|
|
return shortCode || '<27>';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stripEmoji(emoji: string): string {
|
|
|
|
|
const hexCode = stripHexCode(fromUnicodeToHexcode(emoji));
|
|
|
|
|
const codePoint = fromHexcodeToCodepoint(hexCode);
|
|
|
|
|
const result = fromCodepointToUnicode(codePoint);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function stripEmojis(input: string): string {
|
|
|
|
|
return input.replace(emojiRegex, stripEmoji);
|
2021-04-14 09:03:08 +00:00
|
|
|
|
}
|