1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00

Refactor and improve emoji sanitization function

The emoji sanitization function has been refactored for better handling of emoji variations. New types have been imported from 'emoji-mart', allowing for a more precise type assignment for the emoji input and output. Bound checking operations have been adjusted to better accommodate and handle custom emojis and skin variations.
This commit is contained in:
taichi221228 2024-04-24 10:04:33 +09:00
parent 3c7ccb2e62
commit 2236a16d55

View file

@ -1,6 +1,12 @@
// This code is largely borrowed from:
// https://github.com/missive/emoji-mart/blob/5f2ffcc/src/utils/index.js
import type {
BaseEmoji,
CustomEmoji,
EmojiSkin,
PickerProps,
} from 'emoji-mart';
import type { Emoji } from 'emoji-mart/dist-es/utils/data';
import * as data from './emoji_mart_data_light';
@ -46,22 +52,27 @@ function unifiedToNative(unified: Emoji['unified']) {
return String.fromCodePoint(...codePoints);
}
/* eslint-disable */
// @ts-expect-error
function sanitize(emoji) {
let {
name,
short_names,
function sanitize(
emoji: BaseEmoji &
CustomEmoji &
Pick<Emoji, 'skin_variations'> &
Pick<PickerProps, 'custom'> & { skin_tone?: EmojiSkin },
):
| BaseEmoji
| (Omit<CustomEmoji, 'short_names'> & Pick<PickerProps, 'custom'>) {
const {
name = '',
short_names = [],
skin_tone,
skin_variations,
emoticons,
unified,
emoticons = [],
unified = '',
custom,
imageUrl,
} = emoji,
id = emoji.id || short_names[0],
colons = `:${id}:`;
} = emoji;
const id = emoji.id || short_names[0];
let colons = `:${id}:`;
if (custom) {
return {
@ -84,11 +95,13 @@ function sanitize(emoji) {
colons,
emoticons,
unified: unified.toLowerCase(),
skin: skin_tone || (skin_variations ? 1 : null),
skin: skin_tone ?? (skin_variations ? 1 : null),
native: unifiedToNative(unified),
};
}
/* eslint-disable */
function getSanitizedData() {
// @ts-expect-error
return sanitize(getData(...arguments));