-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLink.tsx
72 lines (66 loc) · 2.07 KB
/
Link.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { FC } from 'react';
import { Link as GatsbyLink } from 'gatsby';
import { isAbsoluteURL, DOCUMENTATION_URL } from '@app/utils';
export interface LinkProps {
children: string | React.ReactNode;
to: string;
className?: string;
activeClassName?: string;
partiallyActive?: boolean;
onClick?: (event: React.MouseEvent<HTMLAnchorElement>) => void;
}
// These links are considered SEO trusted, they should be opened in the new tab without "rel" attribute set
const TRUSTED_DOMAINS = [
'https://tdspora.ai',
'https://drill4j.github.io',
'https://healenium.io',
'https://merch.reportportal.io',
'https://console.cloud.google.com/marketplace/product/epam-mp-rp/reportportal?project=epam-mp-rp',
'https://reportportal.io',
'https://demo.reportportal.io',
DOCUMENTATION_URL,
];
// Since DOM elements <a> cannot receive activeClassName
// and partiallyActive, destructure the prop here and
// pass it only to GatsbyLink
export const Link: FC<LinkProps> = ({
children,
to = '',
activeClassName,
partiallyActive,
...other
}) => {
// Tailor the following test to your environment.
// This example assumes that any internal link (intended for Gatsby)
// will start with exactly one slash, and that anything else is external.
const isTrustedLink = TRUSTED_DOMAINS.some(domain => to.startsWith(domain as string));
const isInternal = !isTrustedLink && !isAbsoluteURL(to);
// Use Gatsby Link for internal links, and <a> for others
if (isInternal) {
return (
<GatsbyLink
to={to}
activeClassName={activeClassName}
partiallyActive={partiallyActive}
{...other}
>
{children}
</GatsbyLink>
);
}
// https://DOCUMENTATION_URL is the way to define DOCUMENTATION_URL in Contentful
const href = to.replace('https://DOCUMENTATION_URL', DOCUMENTATION_URL);
return (
// eslint-disable-next-line react/jsx-no-target-blank
<a
href={href}
target="_blank"
{...(!isTrustedLink && {
rel: 'noopener noreferrer',
})}
{...other}
>
{children}
</a>
);
};