Better emoji support in linkify/previews

This commit is contained in:
Fedor Indutny 2021-06-30 10:00:02 -07:00 committed by GitHub
parent 65ad608aa7
commit 773aa9af19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 337 additions and 260 deletions

36
ts/util/emoji.ts Normal file
View file

@ -0,0 +1,36 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-restricted-syntax */
import emojiRegex from 'emoji-regex/es2015/RGI_Emoji';
import { assert } from './assert';
const REGEXP = emojiRegex();
export function replaceEmojiWithSpaces(value: string): string {
return value.replace(REGEXP, ' ');
}
export type SplitElement = Readonly<{
type: 'emoji' | 'text';
value: string;
}>;
export function splitByEmoji(value: string): ReadonlyArray<SplitElement> {
const emojis = value.matchAll(REGEXP);
const result: Array<SplitElement> = [];
let lastIndex = 0;
for (const match of emojis) {
result.push({ type: 'text', value: value.slice(lastIndex, match.index) });
result.push({ type: 'emoji', value: match[0] });
assert(match.index !== undefined, '`matchAll` should provide indices');
lastIndex = match.index + match[0].length;
}
result.push({ type: 'text', value: value.slice(lastIndex) });
return result;
}