Skip to content

Commit ed05ebc

Browse files
authored
Merge pull request #1723 from GetStream/mads/username-with-period
fix: don't parse @user.name as URLs
2 parents 2c18a2a + 2ccb577 commit ed05ebc

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

package/src/components/Message/MessageSimple/utils/parseLinks.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,10 @@ getstream.io
108108

109109
expect(result).toHaveLength(0);
110110
});
111+
112+
it.each([['@user'], ['@user.name']])('does not parse %p as a URL', (input) => {
113+
const result = parseLinksFromText(input);
114+
115+
expect(result).toHaveLength(0);
116+
});
111117
});

package/src/components/Message/MessageSimple/utils/parseLinks.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@ interface Link {
4343
* */
4444
const removeMarkdownLinksFromText = (input: string) => input.replace(/\[[\w\s]+\]\(.*\)/g, '');
4545

46+
/**
47+
* Hermes doesn't support lookbehind, so this is done separately to avoid
48+
* parsing user names as links.
49+
* */
50+
const removeUserNamesFromText = (input: string) => input.replace(/^@\w+\.?\w/, '');
51+
4652
export const parseLinksFromText = (input: string): Link[] => {
4753
let matches;
4854

49-
const inputWithoutMarkdownLinks = removeMarkdownLinksFromText(input);
55+
const strippedInput = [removeMarkdownLinksFromText, removeUserNamesFromText].reduce(
56+
(acc, fn) => fn(acc),
57+
input,
58+
);
5059

5160
const results: Link[] = [];
5261

@@ -61,7 +70,7 @@ export const parseLinksFromText = (input: string): Link[] => {
6170
* to avoid overlapping matches being duplicated in the output.
6271
* */
6372
const linkRegex = new RegExp(`${fqdnLinkPattern}|${schemePrefixedLinkPattern}`, 'gi');
64-
while ((matches = linkRegex.exec(inputWithoutMarkdownLinks)) !== null) {
73+
while ((matches = linkRegex.exec(strippedInput)) !== null) {
6574
const [
6675
raw,
6776
fqdnScheme = '',

0 commit comments

Comments
 (0)