Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Latin characters in emoji suggestion #39805

Merged
merged 11 commits into from
May 30, 2024
4 changes: 2 additions & 2 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1731,8 +1731,8 @@ const CONST = {
// Extract attachment's source from the data's html string
ATTACHMENT_DATA: /(data-expensify-source|data-name)="([^"]+)"/g,

EMOJI_NAME: /:[\w+-]+:/g,
EMOJI_SUGGESTIONS: /:[a-zA-Z0-9_+-]{1,40}$/,
EMOJI_NAME: /:[\p{L}0-9_+-]+:/gu,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used when auto-completing the emoji (:emoji_name:).

EMOJI_SUGGESTIONS: /:[\p{L}0-9_+-]{1,40}$/u,
AFTER_FIRST_LINE_BREAK: /\n.*/g,
LINE_BREAK: /\r|\n/g,
CODE_2FA: /^\d{6}$/,
Expand Down
5 changes: 5 additions & 0 deletions src/libs/EmojiTrie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import emojis, {localeEmojis} from '@assets/emojis';
import type {Emoji, HeaderEmoji, PickerEmoji} from '@assets/emojis/types';
import CONST from '@src/CONST';
import Timing from './actions/Timing';
import StringUtils from './StringUtils';
import Trie from './Trie';

type EmojiMetaData = {
Expand Down Expand Up @@ -33,6 +34,10 @@ function addKeywordsToTrie(trie: Trie<EmojiMetaData>, keywords: string[], item:
keywords.forEach((keyword) => {
const keywordNode = trie.search(keyword);
if (!keywordNode) {
const keywordWithoutAccents = StringUtils.normalizeAccents(keyword);
if (keywordWithoutAccents !== keyword) {
trie.add(keywordWithoutAccents, {suggestions: [{code: item.code, types: item.types, name}]});
}
trie.add(keyword, {suggestions: [{code: item.code, types: item.types, name}]});
} else {
const suggestion = {code: item.code, types: item.types, name};
Expand Down
5 changes: 4 additions & 1 deletion src/libs/GetStyledTextArray.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Str from 'expensify-common/lib/str';
import StringUtils from './StringUtils';

type StyledText = {
text: string;
Expand All @@ -8,7 +9,9 @@ type StyledText = {
const getStyledTextArray = (name: string, prefix: string): StyledText[] => {
const texts = [];
const prefixLowercase = prefix.toLowerCase();
const prefixLocation = name.toLowerCase().search(Str.escapeForRegExp(prefixLowercase));
const prefixLocation = StringUtils.normalizeAccents(name)
.toLowerCase()
.search(Str.escapeForRegExp(StringUtils.normalizeAccents(prefixLowercase)));

if (prefixLocation === 0 && prefix.length === name.length) {
texts.push({text: name, isColored: true});
Expand Down
11 changes: 10 additions & 1 deletion src/libs/StringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ function removeInvisibleCharacters(value: string): string {
return result.trim();
}

/**
* Remove accents/diacritics
* @param text - The input string
tienifr marked this conversation as resolved.
Show resolved Hide resolved
* @returns The string with all accents/diacritics removed
*/
function normalizeAccents(text: string) {
return text.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}

/**
* Replace all CRLF with LF
* @param value - The input string
Expand All @@ -89,4 +98,4 @@ function getAcronym(string: string): string {
return acronym;
}

export default {sanitizeString, isEmptyString, removeInvisibleCharacters, normalizeCRLF, getAcronym};
export default {sanitizeString, isEmptyString, removeInvisibleCharacters, normalizeAccents, normalizeCRLF, getAcronym};
Loading