diff --git a/client/messenger/components/Message.tsx b/client/messenger/components/Message.tsx index a744de1e..bfc694f2 100755 --- a/client/messenger/components/Message.tsx +++ b/client/messenger/components/Message.tsx @@ -4,7 +4,7 @@ import * as React from "react"; import * as xss from "xss"; import { defaultAvatar } from "../../icons/Icons"; import { IUser } from "../../types"; -import { readFile } from "../../utils"; +import { readFile, urlify } from "../../utils"; import { Attachment, User } from "../components/common"; import { IAttachment, IMessengerAppData } from "../types"; @@ -39,6 +39,10 @@ class Message extends React.Component { ); } + formatText(text: string) { + return urlify(text).replace(/ /g, " "); + } + renderContent() { const { messengerAppData, attachments, color, user, content } = this.props; const messageClasses = classNames("erxes-message", { @@ -57,7 +61,9 @@ class Message extends React.Component { return (
{hasAttachment ? : null} - +
); } diff --git a/client/utils.ts b/client/utils.ts index 9c5d497e..2d2e5017 100644 --- a/client/utils.ts +++ b/client/utils.ts @@ -298,3 +298,38 @@ export const striptags = (htmlString: string) => { _text = _text.replace(/\/g, ">"); return _text; }; + +export const urlify = (text: string) => { + const rancherRegex = /]+)>((?:.(?!<\/a\\>))*.)<\/a>/g; + + text = text.replace(rancherRegex, (...args: any[]) => { + const href = args[1].substring(6, args[1].length - 1); + const rancherText = args[2]; + + if (href === rancherText) { + return href; + } + + return `${href}&rancherText=${rancherText}`; + }); + + const urlRegex = /(((https?:\/\/)|(www\.))[-a-zA-Z0-9@:%._~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_.~#?&-a-zA-Z0-9//=]*))/g; + + return text.replace(urlRegex, (url: string) => { + let rancherText = url; + let href = url; + + const index = url.indexOf("&rancherText="); + + if (index !== -1) { + rancherText = url.substring(index + 13); + href = url.substring(0, index); + } + + if (!url.includes("http")) { + href = `http://${url}`; + } + + return '' + rancherText + ""; + }); +};