AutoLinkPlugin.tsx (1098B)
1 import { AutoLinkPlugin, LinkMatcher } from "@lexical/react/LexicalAutoLinkPlugin"; 2 3 const URL_MATCHER = /((https?:\/\/(www\.)?)|(www\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/; 4 5 const EMAIL_MATCHER = /(([^<>()[\]\\.,;:\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,}))/; 6 7 const MATCHERS: LinkMatcher[] = [ 8 (text) => { 9 const match = URL_MATCHER.exec(text); 10 return ( 11 match && { 12 index: match.index, 13 length: match[0].length, 14 text: match[0], 15 url: match[0] 16 } 17 ); 18 }, 19 (text) => { 20 const match = EMAIL_MATCHER.exec(text); 21 return ( 22 match && { 23 index: match.index, 24 length: match[0].length, 25 text: match[0], 26 url: `mailto:${match[0]}` 27 } 28 ); 29 } 30 ]; 31 32 export default function PlaygroundAutoLinkPlugin() { 33 return <AutoLinkPlugin matchers={MATCHERS} />; 34 }