diff --git a/ts/test-node/types/LinkPreview_test.ts b/ts/test-node/types/LinkPreview_test.ts index d8a28877490..302325dd161 100644 --- a/ts/test-node/types/LinkPreview_test.ts +++ b/ts/test-node/types/LinkPreview_test.ts @@ -116,6 +116,24 @@ describe('Link previews', () => { assert.deepEqual(expected, actual); }); + it('returns all links after emojis with spaces in between, if a caretLocation is provided', () => { + const text = '😎 https://github.com/signalapp/Signal-Desktop'; + + const expected = ['https://github.com/signalapp/Signal-Desktop']; + + const actual = findLinks(text, 45); + assert.deepEqual(expected, actual); + }); + + it('returns all links after a complex emoji, if a caretLocation is provided', () => { + const text = '👨‍👩‍👦 https://github.com/signalapp/Signal-Desktop'; + + const expected = ['https://github.com/signalapp/Signal-Desktop']; + + const actual = findLinks(text, 45); + assert.deepEqual(expected, actual); + }); + it('includes all links if cursor is not in a link', () => { const text = 'Check out this link: https://github.com/signalapp/Signal-Desktop\nAnd this one too: https://github.com/signalapp/Signal-Android'; diff --git a/ts/types/LinkPreview.ts b/ts/types/LinkPreview.ts index 4c3b199e639..9c6c4e66e29 100644 --- a/ts/types/LinkPreview.ts +++ b/ts/types/LinkPreview.ts @@ -10,6 +10,7 @@ import LinkifyIt from 'linkify-it'; import { maybeParseUrl } from '../util/url.js'; import { replaceEmojiWithSpaces } from '../util/emoji.js'; +import { count } from '../util/grapheme.js'; import type { AttachmentWithHydratedData } from './Attachment.js'; import { @@ -167,7 +168,7 @@ export function findLinks(text: string, caretLocation?: number): Array { } const haveCaretLocation = isNumber(caretLocation); - const textLength = text ? text.length : 0; + const textLength = count(text || ''); const matches = linkify.match(text ? replaceEmojiWithSpaces(text) : '') || []; return compact(