Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Url Regex #150

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions client/messenger/components/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -39,6 +39,10 @@ class Message extends React.Component<Props> {
);
}

formatText(text: string) {
return urlify(text).replace(/&nbsp;/g, " ");
}

renderContent() {
const { messengerAppData, attachments, color, user, content } = this.props;
const messageClasses = classNames("erxes-message", {
Expand All @@ -57,7 +61,9 @@ class Message extends React.Component<Props> {
return (
<div style={messageBackground} className={messageClasses}>
{hasAttachment ? <Attachment attachment={attachments[0]} /> : null}
<span dangerouslySetInnerHTML={{ __html: xss(content) }} />
<span
dangerouslySetInnerHTML={{ __html: this.formatText(xss(content)) }}
/>
</div>
);
}
Expand Down
35 changes: 35 additions & 0 deletions client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,38 @@ export const striptags = (htmlString: string) => {
_text = _text.replace(/\</g, "&lt;").replace(/\>/g, "&gt;");
return _text;
};

export const urlify = (text: string) => {
const rancherRegex = /<a[\s]+([^>]+)>((?:.(?!<\/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 '<a target="_blank" href="' + href + '">' + rancherText + "</a>";
});
};