2021-06-01 19:19:38 +00:00
|
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
|
import mathiasBynensEmojiRegex from 'emoji-regex';
|
|
|
|
|
import {
|
|
|
|
|
fromCodepointToUnicode,
|
|
|
|
|
fromHexcodeToCodepoint,
|
|
|
|
|
fromUnicodeToHexcode,
|
|
|
|
|
} from 'emojibase';
|
2021-12-22 08:37:47 +00:00
|
|
|
|
import emojibaseEmojiRegex from 'emojibase-regex/emoji.js';
|
|
|
|
|
import SHORTCODE_REGEX from 'emojibase-regex/shortcode.js';
|
2023-03-09 06:31:16 +00:00
|
|
|
|
import { z } from 'zod';
|
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';
|
2023-03-09 06:31:16 +00:00
|
|
|
|
import { logger } from '../logger';
|
2021-06-01 19:19:38 +00:00
|
|
|
|
import { regEx } from './regex';
|
2023-08-21 15:29:41 +00:00
|
|
|
|
import { Json } from './schema-utils';
|
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>();
|
|
|
|
|
|
2023-08-21 15:29:41 +00:00
|
|
|
|
const EmojiShortcodesSchema = Json.pipe(
|
2023-11-07 15:50:29 +00:00
|
|
|
|
z.record(z.string(), z.union([z.string(), z.array(z.string())])),
|
2023-03-09 06:31:16 +00:00
|
|
|
|
);
|
|
|
|
|
|
2021-06-01 19:19:38 +00:00
|
|
|
|
function lazyInitMappings(): void {
|
|
|
|
|
if (!mappingsInitialized) {
|
2023-03-09 06:31:16 +00:00
|
|
|
|
const result = EmojiShortcodesSchema.safeParse(
|
2023-11-07 15:50:29 +00:00
|
|
|
|
dataFiles.get('node_modules/emojibase-data/en/shortcodes/github.json')!,
|
2021-06-01 19:19:38 +00:00
|
|
|
|
);
|
2023-03-09 06:31:16 +00:00
|
|
|
|
// istanbul ignore if: not easily testable
|
|
|
|
|
if (!result.success) {
|
|
|
|
|
logger.warn({ error: result.error }, 'Unable to parse emoji shortcodes');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (const [hex, val] of Object.entries(result.data)) {
|
|
|
|
|
const shortCodes = is.array(val) ? val : [val];
|
2021-06-01 19:19:38 +00:00
|
|
|
|
shortCodesByHex.set(hex, `:${shortCodes[0]}:`);
|
|
|
|
|
shortCodes.forEach((shortCode) => {
|
|
|
|
|
hexCodesByShort.set(`:${shortCode}:`, hex);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
mappingsInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 09:32:27 +00:00
|
|
|
|
export function setEmojiConfig(config: RenovateConfig): void {
|
|
|
|
|
unicodeEmoji = !!config.unicodeEmoji;
|
2019-09-25 09:40:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
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(
|
2023-11-07 15:50:29 +00:00
|
|
|
|
({ source }) => source,
|
2021-06-01 19:19:38 +00:00
|
|
|
|
);
|
2021-12-06 15:05:37 +00:00
|
|
|
|
const emojiRegex = new RegExp(`(?:${emojiRegexSrc.join('|')})`, 'g'); // TODO #12875 cannot figure it out
|
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);
|
2022-01-12 07:28:26 +00:00
|
|
|
|
return shortCode ?? '<27>';
|
2021-06-01 19:19:38 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stripEmoji(emoji: string): string {
|
|
|
|
|
const hexCode = stripHexCode(fromUnicodeToHexcode(emoji));
|
2022-10-08 06:05:54 +00:00
|
|
|
|
// `hexCode` could be empty if `emoji` is a modifier character that isn't
|
|
|
|
|
// modifying anything
|
|
|
|
|
if (hexCode) {
|
|
|
|
|
const codePoint = fromHexcodeToCodepoint(hexCode);
|
|
|
|
|
return fromCodepointToUnicode(codePoint);
|
|
|
|
|
}
|
|
|
|
|
return '';
|
2021-06-01 19:19:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function stripEmojis(input: string): string {
|
|
|
|
|
return input.replace(emojiRegex, stripEmoji);
|
2021-04-14 09:03:08 +00:00
|
|
|
|
}
|