Skip to content

Commit

Permalink
Add a function to create a LinkMatcher based on a RegExp (#3972)
Browse files Browse the repository at this point in the history
  • Loading branch information
Karibash authored Mar 14, 2023
1 parent 5b25cc7 commit 2b611f4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
39 changes: 12 additions & 27 deletions packages/lexical-playground/src/plugins/AutoLinkPlugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,25 @@
*
*/

import {AutoLinkPlugin} from '@lexical/react/LexicalAutoLinkPlugin';
import {
AutoLinkPlugin,
createLinkMatcherWithRegExp,
} from '@lexical/react/LexicalAutoLinkPlugin';
import * as React from 'react';

const URL_MATCHER =
const URL_REGEX =
/((https?:\/\/(www\.)?)|(www\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/;

const EMAIL_MATCHER =
const EMAIL_REGEX =
/(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;

const MATCHERS = [
(text: string) => {
const match = URL_MATCHER.exec(text);
if (match === null) {
return null;
}
const fullMatch = match[0];
return {
index: match.index,
length: fullMatch.length,
text: fullMatch,
url: fullMatch.startsWith('http') ? fullMatch : `https://${fullMatch}`,
};
},
(text: string) => {
const match = EMAIL_MATCHER.exec(text);
return (
match && {
index: match.index,
length: match[0].length,
text: match[0],
url: `mailto:${match[0]}`,
}
);
},
createLinkMatcherWithRegExp(URL_REGEX, (text) => {
return text.startsWith('http') ? text : `https://${text}`;
}),
createLinkMatcherWithRegExp(EMAIL_REGEX, (text) => {
return `mailto:${text}`;
}),
];

export default function LexicalAutoLinkPlugin(): JSX.Element {
Expand Down
5 changes: 5 additions & 0 deletions packages/lexical-react/flow/LexicalAutoLinkPlugin.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ type LinkMatcherResult = {

export type LinkMatcher = (text: string) => LinkMatcherResult | null;

declare export function createLinkMatcherWithRegExp(
regExp: RegExp,
urlTransformer?: (text: string) => string,
): LinkMatcher;

declare export function AutoLinkPlugin(props: {
matchers: Array<LinkMatcher>,
onChange?: ChangeHandler,
Expand Down
16 changes: 16 additions & 0 deletions packages/lexical-react/src/LexicalAutoLinkPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ type LinkMatcherResult = {

export type LinkMatcher = (text: string) => LinkMatcherResult | null;

export function createLinkMatcherWithRegExp(
regExp: RegExp,
urlTransformer: (text: string) => string = (text) => text,
) {
return (text: string) => {
const match = regExp.exec(text);
if (match === null) return null;
return {
index: match.index,
length: match[0].length,
text: match[0],
url: urlTransformer(text),
};
};
}

function findFirstMatch(
text: string,
matchers: Array<LinkMatcher>,
Expand Down

2 comments on commit 2b611f4

@vercel
Copy link

@vercel vercel bot commented on 2b611f4 Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical.dev
lexicaljs.com
lexical-git-main-fbopensource.vercel.app
lexicaljs.org
lexical-fbopensource.vercel.app
www.lexical.dev

@vercel
Copy link

@vercel vercel bot commented on 2b611f4 Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground.vercel.app
lexical-playground-fbopensource.vercel.app
playground.lexical.dev
lexical-playground-git-main-fbopensource.vercel.app

Please sign in to comment.