pleroma/app/soapbox/features/emoji/mapping.ts

82 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-07-04 16:07:04 -07:00
import data, { EmojiData } from './data';
2022-07-04 13:30:35 -07:00
2022-07-07 15:33:55 -07:00
const stripLeadingZeros = /^0+/;
function replaceAll(str: string, find: string, replace: string) {
return str.replace(new RegExp(find, 'g'), replace);
}
2022-07-04 16:07:04 -07:00
interface UnicodeMap {
2022-07-04 13:30:35 -07:00
[s: string]: {
unified: string,
shortcode: string,
}
}
/*
* Twemoji strips their hex codes from unicode codepoints to make it look "pretty"
* - leading 0s are removed
* - fe0f is removed unless it has 200d
* - fe0f is NOT removed for 1f441-fe0f-200d-1f5e8-fe0f even though it has a 200d
*
* this is all wrong
*/
2022-07-07 16:37:59 -07:00
const blacklist = {
'1f441-fe0f-200d-1f5e8-fe0f': true,
};
const tweaks = {
'👁‍🗨️': ['1f441-200d-1f5e8', 'eye-in-speech-bubble'],
'#⃣': ['23-20e3', 'hash'],
'*⃣': ['2a-20e3', 'keycap_star'],
'0⃣': ['30-20e3', 'zero'],
'1⃣': ['31-20e3', 'one'],
'2⃣': ['32-20e3', 'two'],
'3⃣': ['33-20e3', 'three'],
'4⃣': ['34-20e3', 'four'],
'5⃣': ['35-20e3', 'five'],
'6⃣': ['36-20e3', 'six'],
'7⃣': ['37-20e3', 'seven'],
'8⃣': ['38-20e3', 'eight'],
'9⃣': ['39-20e3', 'nine'],
'🏳‍🌈': ['1f3f3-fe0f-200d-1f308', 'rainbow-flag'],
'🏳‍⚧️': ['1f3f3-fe0f-200d-26a7-fe0f', 'transgender_flag'],
'🏳‍⚧': ['1f3f3-fe0f-200d-26a7-fe0f', 'transgender_flag'],
2022-07-07 17:52:29 -07:00
'✴︎': ['2734', 'eight_pointed_black_star'],
2022-07-07 16:37:59 -07:00
};
2022-07-04 16:07:04 -07:00
export const generateMappings = (data: EmojiData): UnicodeMap => {
2022-07-04 13:30:35 -07:00
const result = {};
const emojis = Object.values(data.emojis ?? {});
for (const value of emojis) {
// @ts-ignore
for (const item of value.skins) {
const { unified, native } = item;
2022-07-07 15:33:55 -07:00
const stripped = unified.replace(stripLeadingZeros, '');
2022-07-07 16:37:59 -07:00
if (unified.includes('200d') && !(unified in blacklist)) {
2022-07-07 15:33:55 -07:00
// @ts-ignore
result[native] = { unified: stripped, shortcode: value.id };
} else {
2022-07-07 17:52:29 -07:00
const twemojiCode = replaceAll(stripped, '-fe0f', '').replace('fe0e', '');
2022-07-04 13:30:35 -07:00
2022-07-07 15:33:55 -07:00
// @ts-ignore
result[native] = { unified: twemojiCode, shortcode: value.id };
}
2022-07-04 13:30:35 -07:00
}
}
2022-07-07 16:37:59 -07:00
for (const [key, value] of Object.entries(tweaks)) {
// @ts-ignore
result[key] = { unified: value[0], shortcode: value[1] };
}
2022-07-04 13:30:35 -07:00
return result;
};
2022-07-04 16:07:04 -07:00
const unicodeMapping = generateMappings(data);
2022-07-04 13:30:35 -07:00
export default unicodeMapping;