Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new recognition patterns. #65

Open
willysoriaguzman opened this issue Jun 7, 2018 · 7 comments
Open

Adding new recognition patterns. #65

willysoriaguzman opened this issue Jun 7, 2018 · 7 comments

Comments

@willysoriaguzman
Copy link

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?

@AlexandrDobrovolskiy
Copy link

the same issue, need to recognize telegram contacts

@ro1414
Copy link

ro1414 commented Mar 30, 2020

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

@ranihorev
Copy link

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

@hmittal1
Copy link

hmittal1 commented Feb 4, 2021

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

@iShaVas
Copy link

iShaVas commented Nov 20, 2021

@hmittal1 Was you able to archive that?

@dodoto
Copy link

dodoto commented Jul 14, 2022

Maybe too late, I solved by nested Linkify component

@hmittal1
Copy link

@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},
};
});
}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants
@ro1414 @iShaVas @hmittal1 @willysoriaguzman @ranihorev @AlexandrDobrovolskiy @dodoto and others