Skip to content

Commit a69d73f

Browse files
fix: parsing the links with unconventional tlds (#2241)
1 parent b55d561 commit a69d73f

File tree

4 files changed

+14
-45
lines changed

4 files changed

+14
-45
lines changed

package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"dayjs": "1.10.5",
7373
"file-loader": "6.2.0",
7474
"i18next": "20.2.4",
75-
"linkify-it": "^4.0.1",
75+
"linkifyjs": "^4.1.1",
7676
"lodash-es": "4.17.21",
7777
"metro-react-native-babel-preset": "0.66.2",
7878
"mime-types": "^2.1.34",

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

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ describe('parseLinksFromText', () => {
1414
'https://localhost/with/path?and=query#fragment',
1515
'https://localhost/with/path?and=query#fragment',
1616
],
17-
['reactnative.stream', undefined],
17+
['reactnative.dev', 'http://reactnative.dev'],
18+
['hinge.health/schedule-with-a-coach', 'http://hinge.health/schedule-with-a-coach'],
1819
[
1920
'https://zh.wikipedia.org/wiki/挪威牛油危機',
2021
'https://zh.wikipedia.org/wiki/%E6%8C%AA%E5%A8%81%E7%89%9B%E6%B2%B9%E5%8D%B1%E6%A9%9F',
@@ -87,29 +88,4 @@ describe('parseLinksFromText', () => {
8788
raw: input,
8889
});
8990
});
90-
it('doest not report invalid tlds as urls', () => {
91-
const input = `
92-
%
93-
% Not links
94-
%
95-
example.invalid
96-
example.invalid/
97-
http://.example.com
98-
http://-example.com
99-
hppt://example.com
100-
example.coma
101-
-example.coma
102-
`;
103-
const result = parseLinksFromText(input);
104-
expect(result).toHaveLength(0);
105-
});
106-
it('does not parse a decimal number as a URL', () => {
107-
const input = '123.456';
108-
const result = parseLinksFromText(input);
109-
expect(result).toHaveLength(0);
110-
});
111-
it.each([['@user'], ['@user.name']])('does not parse %p as a URL', (input) => {
112-
const result = parseLinksFromText(input);
113-
expect(result).toHaveLength(0);
114-
});
11591
});

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Linkify from 'linkify-it';
1+
import { find } from 'linkifyjs';
22

33
interface LinkInfo {
44
encodedUrl: string;
@@ -23,13 +23,13 @@ export const parseLinksFromText = (input: string): LinkInfo[] => {
2323
input,
2424
);
2525

26-
const linkify = Linkify();
27-
const matches = linkify.match(strippedInput) ?? [];
26+
const links = find(strippedInput, 'url');
27+
const emails = find(strippedInput, 'email');
2828

29-
const result: LinkInfo[] = matches.map((match) => {
30-
const { raw, url } = match;
31-
return { encodedUrl: encodeURI(url), raw };
32-
});
29+
const result: LinkInfo[] = [...links, ...emails].map(({ href, value }) => ({
30+
encodedUrl: encodeURI(href),
31+
raw: value,
32+
}));
3333

3434
return result;
3535
};

package/yarn.lock

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6998,12 +6998,10 @@ lines-and-columns@^1.1.6:
69986998
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
69996999
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==
70007000

7001-
linkify-it@^4.0.1:
7002-
version "4.0.1"
7003-
resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-4.0.1.tgz#01f1d5e508190d06669982ba31a7d9f56a5751ec"
7004-
integrity sha512-C7bfi1UZmoj8+PQx22XyeXCuBlokoyWQL5pWSP+EI6nzRylyThouddufc2c1NDIcP9k5agmN9fLpA7VNJfIiqw==
7005-
dependencies:
7006-
uc.micro "^1.0.1"
7001+
linkifyjs@^4.1.1:
7002+
version "4.1.1"
7003+
resolved "https://registry.yarnpkg.com/linkifyjs/-/linkifyjs-4.1.1.tgz#73d427e3bbaaf4ca8e71c589ad4ffda11a9a5fde"
7004+
integrity sha512-zFN/CTVmbcVef+WaDXT63dNzzkfRBKT1j464NJQkV7iSgJU0sLBus9W0HBwnXK13/hf168pbrx/V/bjEHOXNHA==
70077005

70087006
loader-runner@^2.4.0:
70097007
version "2.4.0"
@@ -10036,11 +10034,6 @@ [email protected]:
1003610034
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324"
1003710035
integrity sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==
1003810036

10039-
uc.micro@^1.0.1:
10040-
version "1.0.6"
10041-
resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac"
10042-
integrity sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==
10043-
1004410037
uglify-es@^3.1.9:
1004510038
version "3.3.9"
1004610039
resolved "https://registry.yarnpkg.com/uglify-es/-/uglify-es-3.3.9.tgz#0c1c4f0700bed8dbc124cdb304d2592ca203e677"

0 commit comments

Comments
 (0)