2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-05-18 21:48:20 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
import LinkifyIt from 'linkify-it';
|
2018-05-18 21:48:20 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
import { RenderTextCallbackType } from '../../types/Util';
|
2021-06-30 17:00:02 +00:00
|
|
|
import { isLinkSneaky } from '../../types/LinkPreview';
|
|
|
|
import { splitByEmoji } from '../../util/emoji';
|
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
2018-05-18 21:48:20 +00:00
|
|
|
|
2020-09-29 18:37:21 +00:00
|
|
|
const linkify = LinkifyIt()
|
2021-09-17 15:14:42 +00:00
|
|
|
// This is all TLDs in place in 2010, according to [IANA's root zone database][0]
|
|
|
|
// except for those domains marked as [a test domain][1].
|
|
|
|
//
|
|
|
|
// Note that this only applies to "fuzzy" matches (`example.com`), not matches with
|
|
|
|
// protocols (`https://example.com`). See [this GitHub comment][2] for more.
|
|
|
|
//
|
|
|
|
// [0]: https://www.iana.org/domains/root/db
|
|
|
|
// [1]: https://www.iana.org/domains/reserved
|
|
|
|
// [2]: https://github.com/signalapp/Signal-Desktop/issues/4538#issuecomment-748368590
|
2020-09-29 18:37:21 +00:00
|
|
|
.tlds([
|
2021-09-17 15:14:42 +00:00
|
|
|
'ac',
|
|
|
|
'ad',
|
|
|
|
'ae',
|
2020-09-29 18:37:21 +00:00
|
|
|
'aero',
|
2021-09-17 15:14:42 +00:00
|
|
|
'af',
|
|
|
|
'ag',
|
|
|
|
'ai',
|
|
|
|
'al',
|
|
|
|
'am',
|
|
|
|
'an',
|
|
|
|
'ao',
|
|
|
|
'aq',
|
|
|
|
'ar',
|
|
|
|
'arpa',
|
|
|
|
'as',
|
2020-09-29 18:37:21 +00:00
|
|
|
'asia',
|
2021-09-17 15:14:42 +00:00
|
|
|
'at',
|
|
|
|
'au',
|
|
|
|
'aw',
|
|
|
|
'ax',
|
|
|
|
'az',
|
|
|
|
'ba',
|
|
|
|
'bb',
|
|
|
|
'bd',
|
|
|
|
'be',
|
|
|
|
'bf',
|
|
|
|
'bg',
|
|
|
|
'bh',
|
|
|
|
'bi',
|
2020-09-29 18:37:21 +00:00
|
|
|
'biz',
|
2021-09-17 15:14:42 +00:00
|
|
|
'bj',
|
|
|
|
'bl',
|
|
|
|
'bm',
|
|
|
|
'bn',
|
|
|
|
'bo',
|
|
|
|
'bq',
|
|
|
|
'br',
|
|
|
|
'bs',
|
|
|
|
'bt',
|
|
|
|
'bv',
|
|
|
|
'bw',
|
|
|
|
'by',
|
|
|
|
'bz',
|
|
|
|
'ca',
|
2020-09-29 18:37:21 +00:00
|
|
|
'cat',
|
2021-09-17 15:14:42 +00:00
|
|
|
'cc',
|
|
|
|
'cd',
|
|
|
|
'cf',
|
|
|
|
'cg',
|
|
|
|
'ch',
|
|
|
|
'ci',
|
|
|
|
'ck',
|
|
|
|
'cl',
|
|
|
|
'cm',
|
|
|
|
'cn',
|
|
|
|
'co',
|
2020-09-29 18:37:21 +00:00
|
|
|
'com',
|
|
|
|
'coop',
|
2021-09-17 15:14:42 +00:00
|
|
|
'cr',
|
|
|
|
'cu',
|
|
|
|
'cv',
|
|
|
|
'cw',
|
|
|
|
'cx',
|
|
|
|
'cy',
|
|
|
|
'cz',
|
|
|
|
'de',
|
|
|
|
'dj',
|
|
|
|
'dk',
|
|
|
|
'dm',
|
|
|
|
'do',
|
|
|
|
'dz',
|
|
|
|
'ec',
|
2020-09-29 18:37:21 +00:00
|
|
|
'edu',
|
2021-09-17 15:14:42 +00:00
|
|
|
'ee',
|
|
|
|
'eg',
|
|
|
|
'er',
|
|
|
|
'es',
|
|
|
|
'et',
|
|
|
|
'eu',
|
|
|
|
'fi',
|
|
|
|
'fj',
|
|
|
|
'fk',
|
|
|
|
'fm',
|
|
|
|
'fo',
|
|
|
|
'fr',
|
|
|
|
'ga',
|
|
|
|
'gb',
|
|
|
|
'gd',
|
|
|
|
'ge',
|
|
|
|
'gf',
|
|
|
|
'gg',
|
|
|
|
'gh',
|
|
|
|
'gi',
|
|
|
|
'gl',
|
|
|
|
'gm',
|
|
|
|
'gn',
|
2020-09-29 18:37:21 +00:00
|
|
|
'gov',
|
2021-09-17 15:14:42 +00:00
|
|
|
'gp',
|
|
|
|
'gq',
|
|
|
|
'gr',
|
|
|
|
'gs',
|
|
|
|
'gt',
|
|
|
|
'gu',
|
|
|
|
'gw',
|
|
|
|
'gy',
|
|
|
|
'hk',
|
|
|
|
'hm',
|
|
|
|
'hn',
|
|
|
|
'hr',
|
|
|
|
'ht',
|
|
|
|
'hu',
|
|
|
|
'id',
|
|
|
|
'ie',
|
|
|
|
'il',
|
|
|
|
'im',
|
|
|
|
'in',
|
2020-09-29 18:37:21 +00:00
|
|
|
'info',
|
|
|
|
'int',
|
2021-09-17 15:14:42 +00:00
|
|
|
'io',
|
|
|
|
'iq',
|
|
|
|
'ir',
|
|
|
|
'is',
|
|
|
|
'it',
|
|
|
|
'je',
|
|
|
|
'jm',
|
|
|
|
'jo',
|
2020-09-29 18:37:21 +00:00
|
|
|
'jobs',
|
2021-09-17 15:14:42 +00:00
|
|
|
'jp',
|
|
|
|
'ke',
|
|
|
|
'kg',
|
|
|
|
'kh',
|
|
|
|
'ki',
|
|
|
|
'km',
|
|
|
|
'kn',
|
|
|
|
'kp',
|
|
|
|
'kr',
|
|
|
|
'kw',
|
|
|
|
'ky',
|
|
|
|
'kz',
|
|
|
|
'la',
|
|
|
|
'lb',
|
|
|
|
'lc',
|
|
|
|
'li',
|
|
|
|
'lk',
|
|
|
|
'lr',
|
|
|
|
'ls',
|
|
|
|
'lt',
|
|
|
|
'lu',
|
|
|
|
'lv',
|
|
|
|
'ly',
|
|
|
|
'ma',
|
|
|
|
'mc',
|
|
|
|
'md',
|
|
|
|
'me',
|
|
|
|
'mf',
|
|
|
|
'mg',
|
|
|
|
'mh',
|
2020-09-29 18:37:21 +00:00
|
|
|
'mil',
|
2021-09-17 15:14:42 +00:00
|
|
|
'mk',
|
|
|
|
'ml',
|
|
|
|
'mm',
|
|
|
|
'mn',
|
|
|
|
'mo',
|
2020-09-29 18:37:21 +00:00
|
|
|
'mobi',
|
2021-09-17 15:14:42 +00:00
|
|
|
'mp',
|
|
|
|
'mq',
|
|
|
|
'mr',
|
|
|
|
'ms',
|
|
|
|
'mt',
|
|
|
|
'mu',
|
2020-09-29 18:37:21 +00:00
|
|
|
'museum',
|
2021-09-17 15:14:42 +00:00
|
|
|
'mv',
|
|
|
|
'mw',
|
|
|
|
'mx',
|
|
|
|
'my',
|
|
|
|
'mz',
|
|
|
|
'na',
|
2020-09-29 18:37:21 +00:00
|
|
|
'name',
|
2021-09-17 15:14:42 +00:00
|
|
|
'nc',
|
|
|
|
'ne',
|
2020-09-29 18:37:21 +00:00
|
|
|
'net',
|
2021-09-17 15:14:42 +00:00
|
|
|
'nf',
|
|
|
|
'ng',
|
|
|
|
'ni',
|
|
|
|
'nl',
|
|
|
|
'no',
|
|
|
|
'np',
|
|
|
|
'nr',
|
|
|
|
'nu',
|
|
|
|
'nz',
|
|
|
|
'om',
|
2020-09-29 18:37:21 +00:00
|
|
|
'org',
|
2021-09-17 15:14:42 +00:00
|
|
|
'pa',
|
|
|
|
'pe',
|
|
|
|
'pf',
|
|
|
|
'pg',
|
|
|
|
'ph',
|
|
|
|
'pk',
|
|
|
|
'pl',
|
|
|
|
'pm',
|
|
|
|
'pn',
|
|
|
|
'pr',
|
2020-09-29 18:37:21 +00:00
|
|
|
'pro',
|
2021-09-17 15:14:42 +00:00
|
|
|
'ps',
|
|
|
|
'pt',
|
|
|
|
'pw',
|
|
|
|
'py',
|
|
|
|
'qa',
|
|
|
|
're',
|
|
|
|
'ro',
|
|
|
|
'rs',
|
|
|
|
'ru',
|
|
|
|
'rw',
|
|
|
|
'sa',
|
|
|
|
'sb',
|
|
|
|
'sc',
|
|
|
|
'sd',
|
|
|
|
'se',
|
|
|
|
'sg',
|
|
|
|
'sh',
|
|
|
|
'si',
|
|
|
|
'sj',
|
|
|
|
'sk',
|
|
|
|
'sl',
|
|
|
|
'sm',
|
|
|
|
'sn',
|
|
|
|
'so',
|
|
|
|
'sr',
|
|
|
|
'st',
|
|
|
|
'su',
|
|
|
|
'sv',
|
|
|
|
'sx',
|
|
|
|
'sy',
|
|
|
|
'sz',
|
|
|
|
'tc',
|
|
|
|
'td',
|
2020-09-29 18:37:21 +00:00
|
|
|
'tel',
|
2021-09-17 15:14:42 +00:00
|
|
|
'tf',
|
|
|
|
'tg',
|
|
|
|
'th',
|
|
|
|
'tj',
|
|
|
|
'tk',
|
|
|
|
'tl',
|
|
|
|
'tm',
|
|
|
|
'tn',
|
|
|
|
'to',
|
|
|
|
'tp',
|
|
|
|
'tr',
|
2020-09-29 18:37:21 +00:00
|
|
|
'travel',
|
2021-09-17 15:14:42 +00:00
|
|
|
'tt',
|
|
|
|
'tv',
|
|
|
|
'tw',
|
|
|
|
'tz',
|
|
|
|
'ua',
|
|
|
|
'ug',
|
|
|
|
'uk',
|
|
|
|
'um',
|
|
|
|
'us',
|
|
|
|
'uy',
|
|
|
|
'uz',
|
|
|
|
'va',
|
|
|
|
'vc',
|
|
|
|
've',
|
|
|
|
'vg',
|
|
|
|
'vi',
|
|
|
|
'vn',
|
|
|
|
'vu',
|
|
|
|
'wf',
|
|
|
|
'ws',
|
|
|
|
'中国',
|
|
|
|
'中國',
|
|
|
|
'ලංකා',
|
|
|
|
'香港',
|
|
|
|
'台湾',
|
|
|
|
'台灣',
|
|
|
|
'امارات',
|
|
|
|
'الاردن',
|
|
|
|
'السعودية',
|
|
|
|
'ไทย',
|
|
|
|
'рф',
|
|
|
|
'تونس',
|
|
|
|
'مصر',
|
|
|
|
'قطر',
|
|
|
|
'இலங்கை',
|
|
|
|
'فلسطين',
|
|
|
|
'ye',
|
|
|
|
'yt',
|
|
|
|
'za',
|
|
|
|
'zm',
|
|
|
|
'zw',
|
2020-09-29 18:37:21 +00:00
|
|
|
]);
|
2018-05-18 21:48:20 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type Props = {
|
2018-05-18 21:48:20 +00:00
|
|
|
text: string;
|
2021-02-04 22:30:21 +00:00
|
|
|
/** Allows you to customize how non-links are rendered. Simplest is just a <span>. */
|
2019-01-14 21:49:58 +00:00
|
|
|
renderNonLink?: RenderTextCallbackType;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-05-18 21:48:20 +00:00
|
|
|
|
|
|
|
const SUPPORTED_PROTOCOLS = /^(http|https):/i;
|
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
export class Linkify extends React.Component<Props> {
|
2018-05-18 21:48:20 +00:00
|
|
|
public static defaultProps: Partial<Props> = {
|
2018-06-27 20:53:49 +00:00
|
|
|
renderNonLink: ({ text }) => text,
|
2018-05-18 21:48:20 +00:00
|
|
|
};
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public render():
|
|
|
|
| JSX.Element
|
|
|
|
| string
|
|
|
|
| null
|
|
|
|
| Array<JSX.Element | string | null> {
|
2018-05-18 21:48:20 +00:00
|
|
|
const { text, renderNonLink } = this.props;
|
|
|
|
|
|
|
|
// We have to do this, because renderNonLink is not required in our Props object,
|
|
|
|
// but it is always provided via defaultProps.
|
|
|
|
if (!renderNonLink) {
|
2020-09-14 19:51:27 +00:00
|
|
|
return null;
|
2018-05-18 21:48:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 17:00:02 +00:00
|
|
|
const chunkData: Array<{
|
|
|
|
chunk: string;
|
2021-07-08 21:55:48 +00:00
|
|
|
matchData: ReadonlyArray<LinkifyIt.Match>;
|
2021-06-30 17:00:02 +00:00
|
|
|
}> = splitByEmoji(text).map(({ type, value: chunk }) => {
|
|
|
|
if (type === 'text') {
|
|
|
|
return { chunk, matchData: linkify.match(chunk) || [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'emoji') {
|
|
|
|
return { chunk, matchData: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
throw missingCaseError(type);
|
|
|
|
});
|
|
|
|
|
|
|
|
const results: Array<JSX.Element | string> = [];
|
|
|
|
let count = 1;
|
|
|
|
|
|
|
|
chunkData.forEach(({ chunk, matchData }) => {
|
|
|
|
if (matchData.length === 0) {
|
2021-06-30 19:25:02 +00:00
|
|
|
count += 1;
|
|
|
|
results.push(renderNonLink({ text: chunk, key: count }));
|
2021-06-30 17:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-18 21:48:20 +00:00
|
|
|
|
2021-07-27 23:39:57 +00:00
|
|
|
let chunkLastIndex = 0;
|
2021-06-30 17:00:02 +00:00
|
|
|
matchData.forEach(match => {
|
2021-07-27 23:39:57 +00:00
|
|
|
if (chunkLastIndex < match.index) {
|
|
|
|
const textWithNoLink = chunk.slice(chunkLastIndex, match.index);
|
2020-09-14 19:51:27 +00:00
|
|
|
count += 1;
|
|
|
|
results.push(renderNonLink({ text: textWithNoLink, key: count }));
|
2018-05-18 21:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const { url, text: originalText } = match;
|
2020-09-14 19:51:27 +00:00
|
|
|
count += 1;
|
2020-04-24 16:57:04 +00:00
|
|
|
if (SUPPORTED_PROTOCOLS.test(url) && !isLinkSneaky(url)) {
|
2018-05-18 21:48:20 +00:00
|
|
|
results.push(
|
2020-09-14 19:51:27 +00:00
|
|
|
<a key={count} href={url}>
|
2018-05-18 21:48:20 +00:00
|
|
|
{originalText}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
} else {
|
2020-09-14 19:51:27 +00:00
|
|
|
results.push(renderNonLink({ text: originalText, key: count }));
|
2018-05-18 21:48:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 23:39:57 +00:00
|
|
|
chunkLastIndex = match.lastIndex;
|
2021-06-30 17:00:02 +00:00
|
|
|
});
|
2018-05-18 21:48:20 +00:00
|
|
|
|
2021-07-27 23:39:57 +00:00
|
|
|
if (chunkLastIndex < chunk.length) {
|
2021-06-30 17:00:02 +00:00
|
|
|
count += 1;
|
2021-07-27 23:39:57 +00:00
|
|
|
results.push(
|
|
|
|
renderNonLink({
|
|
|
|
text: chunk.slice(chunkLastIndex),
|
|
|
|
key: count,
|
|
|
|
})
|
|
|
|
);
|
2021-06-30 17:00:02 +00:00
|
|
|
}
|
|
|
|
});
|
2018-05-18 21:48:20 +00:00
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|