react-linkify icon indicating copy to clipboard operation
react-linkify copied to clipboard

Adding new recognition patterns.

Open willysoriaguzman opened this issue 7 years ago • 7 comments

I was reading this library uses linkify-it: (https://github.com/markdown-it/linkify-it), so I was wondering where should I add a new pattern to recognize for example phone numbers using react typescipt.

Is there any example to achieve this or similar behaviour?

willysoriaguzman avatar Jun 07 '18 22:06 willysoriaguzman

the same issue, need to recognize telegram contacts

AlexandrDobrovolskiy avatar Sep 07 '18 20:09 AlexandrDobrovolskiy

Any thoughts here? @willysoriaguzman did you find a solution?

ro1414 avatar Mar 30 '20 16:03 ro1414

You can do that by overriding the matchDecorator. For example, this decorator detects arxiv IDs:

<Linkify
  matchDecorator={value => {
    const matches = value.matchAll(/\d{4}\.\d{4,5}/g);
    return [...matches].map(match => {
      return {
        schema: '',
        index: match.index || 0,
        lastIndex: (match.index || 0) + match[0].length,
        text: match[0],
        url: `<your URL>`,
      };
    });
  }}
>{text}</Linkify

ranihorev avatar Nov 09 '20 02:11 ranihorev

Able to linkify phone numbers but urls stop getting linkify after this change. can you tell me how can show both urls and phone numbers

hmittal1 avatar Feb 04 '21 12:02 hmittal1

@hmittal1 Was you able to archive that?

iShaVas avatar Nov 20 '21 09:11 iShaVas

Maybe too late, I solved by nested Linkify component

dodoto avatar Jul 14 '22 08:07 dodoto

@iShaVas sorry i missed your comment. yes, i have modified the regex to cover aaaall valid scenarios.

export const linkifyRegex = /(+?\d[\s\-\d]{6,17}\d)|((https?://(?:www.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^\s]{2,}|www.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^\s]{2,}|https?://(?:www.|(?!www))[a-zA-Z0-9]+.[^\s]{2,}|www.[a-zA-Z0-9]+.[^\s]{2,}|[a-zA-Z]+.com[^\s]{0,}|[a-zA-Z]+.[a-zA-Z]{2,4}/[^\s]{2,}))/g; export const phoneNumberRegex = /^+?\d[\s\-\d]{6,17}\d/;

matchDecorator={value => { const matches = value.matchAll(linkifyRegex); return [...matches].map(match => { let url = match[0]; // Prepend 'tel:' string on open phone // number linkify url into dailer app. if (phoneNumberRegex.test(url)) { url = tel: ${url}; } return { schema: '', index: match.index || 0, lastIndex: (match.index || 0) + match[0].length, text: match[0], url: ${url}, }; }); }}

hmittal1 avatar Jul 20 '22 07:07 hmittal1