Fix: Fixing #2306 Links in team description is considered as a page. (#2329)

* Fix: Fixing #2306 Links in team description is considered as a page.

* Adding isValidURl util

* Checking falsy condition first
This commit is contained in:
Sachin Chaurasiya 2022-01-21 14:06:17 +05:30 committed by GitHub
parent 845b62d48d
commit cd65037ac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -14,8 +14,10 @@
import classNames from 'classnames';
import React, { useEffect, useState } from 'react';
import ReactMarkdown, { PluggableList } from 'react-markdown';
import { Link } from 'react-router-dom';
import rehypeRaw from 'rehype-raw';
import gfm from 'remark-gfm';
import { isValidUrl } from '../../../utils/CommonUtils';
/*eslint-disable */
const RichTextEditorPreviewer = ({
@ -51,6 +53,27 @@ const RichTextEditorPreviewer = ({
</ul>
);
},
a: ({ node, children, ...props }) => {
if (!isValidUrl(props.href as string)) {
return <span>{children}</span>;
} else {
let link = '';
const href = props.href;
if (
href?.indexOf('http://') == 0 ||
href?.indexOf('https://') == 0
) {
link = href;
} else {
link = `https://${href}`;
}
return (
<Link to={{ pathname: link }} target="_blank">
{children}
</Link>
);
}
},
}}
remarkPlugins={[gfm] as unknown as PluggableList | undefined}
rehypePlugins={[[rehypeRaw, { allowDangerousHtml: false }]]}

View File

@ -305,3 +305,12 @@ export const getImages = (imageUri: string) => {
return imagesObj;
};
export const isValidUrl = (href: string) => {
const regex = new RegExp(
// eslint-disable-next-line no-useless-escape
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g
);
return href.match(regex);
};