|
| 1 | +import data from "@emoji-mart/data/sets/15/native.json" with { type: "json" }; |
| 2 | + |
| 3 | +import type { CustomEmoji } from "./remarkCustomEmoji"; |
| 4 | + |
| 5 | +type EmojiMartData = { |
| 6 | + emojis?: Record< |
| 7 | + string, |
| 8 | + { |
| 9 | + skins?: Array<{ native?: string }>; |
| 10 | + } |
| 11 | + >; |
| 12 | +}; |
| 13 | + |
| 14 | +let nativeEmojiSet: Set<string> | null = null; |
| 15 | + |
| 16 | +function buildNativeEmojiSet(): Set<string> { |
| 17 | + const set = new Set<string>(); |
| 18 | + const emojis = (data as EmojiMartData).emojis ?? {}; |
| 19 | + for (const emoji of Object.values(emojis)) { |
| 20 | + for (const skin of emoji.skins ?? []) { |
| 21 | + if (skin.native) { |
| 22 | + set.add(skin.native); |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + return set; |
| 27 | +} |
| 28 | + |
| 29 | +function isNativeEmojiCluster(cluster: string): boolean { |
| 30 | + nativeEmojiSet ??= buildNativeEmojiSet(); |
| 31 | + return ( |
| 32 | + nativeEmojiSet.has(cluster) || /\p{Extended_Pictographic}/u.test(cluster) |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +function readGrapheme(text: string, start: number): string { |
| 37 | + const firstCodePoint = text.codePointAt(start); |
| 38 | + if (firstCodePoint === undefined) { |
| 39 | + return ""; |
| 40 | + } |
| 41 | + |
| 42 | + let end = start + (firstCodePoint > 0xffff ? 2 : 1); |
| 43 | + |
| 44 | + const nextCodePoint = text.codePointAt(end); |
| 45 | + if ( |
| 46 | + isRegionalIndicator(firstCodePoint) && |
| 47 | + nextCodePoint !== undefined && |
| 48 | + isRegionalIndicator(nextCodePoint) |
| 49 | + ) { |
| 50 | + return text.slice(start, end + (nextCodePoint > 0xffff ? 2 : 1)); |
| 51 | + } |
| 52 | + |
| 53 | + while (end < text.length) { |
| 54 | + const codePoint = text.codePointAt(end); |
| 55 | + if (codePoint === undefined) { |
| 56 | + break; |
| 57 | + } |
| 58 | + |
| 59 | + if ( |
| 60 | + codePoint === 0xfe0f || |
| 61 | + codePoint === 0x200d || |
| 62 | + codePoint === 0x20e3 || |
| 63 | + isEmojiModifier(codePoint) || |
| 64 | + isEmojiTag(codePoint) |
| 65 | + ) { |
| 66 | + end += codePoint > 0xffff ? 2 : 1; |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + if (text.codePointAt(end - 1) === 0x200d) { |
| 71 | + end += codePoint > 0xffff ? 2 : 1; |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + return text.slice(start, end); |
| 79 | +} |
| 80 | + |
| 81 | +function isEmojiModifier(codePoint: number): boolean { |
| 82 | + return codePoint >= 0x1f3fb && codePoint <= 0x1f3ff; |
| 83 | +} |
| 84 | + |
| 85 | +function isRegionalIndicator(codePoint: number): boolean { |
| 86 | + return codePoint >= 0x1f1e6 && codePoint <= 0x1f1ff; |
| 87 | +} |
| 88 | + |
| 89 | +function isEmojiTag(codePoint: number): boolean { |
| 90 | + return codePoint >= 0xe0020 && codePoint <= 0xe007f; |
| 91 | +} |
| 92 | + |
| 93 | +export function isEmojiOnlyMessage( |
| 94 | + content: string, |
| 95 | + customEmoji: CustomEmoji[] = [], |
| 96 | +): boolean { |
| 97 | + const trimmed = content.trim(); |
| 98 | + if (!trimmed) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + const shortcodeSet = new Set( |
| 103 | + customEmoji.map((emoji) => emoji.shortcode.toLowerCase()), |
| 104 | + ); |
| 105 | + let sawEmoji = false; |
| 106 | + |
| 107 | + for (let index = 0; index < trimmed.length; ) { |
| 108 | + const char = trimmed[index]; |
| 109 | + |
| 110 | + if (/\s/u.test(char)) { |
| 111 | + index += char.length; |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + if (char === ":") { |
| 116 | + const end = trimmed.indexOf(":", index + 1); |
| 117 | + if (end > index + 1) { |
| 118 | + const shortcode = trimmed.slice(index + 1, end).toLowerCase(); |
| 119 | + if (shortcodeSet.has(shortcode)) { |
| 120 | + sawEmoji = true; |
| 121 | + index = end + 1; |
| 122 | + continue; |
| 123 | + } |
| 124 | + } |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + const cluster = readGrapheme(trimmed, index); |
| 129 | + if (!isNativeEmojiCluster(cluster)) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + sawEmoji = true; |
| 134 | + index += cluster.length; |
| 135 | + } |
| 136 | + |
| 137 | + return sawEmoji; |
| 138 | +} |
0 commit comments