Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open all external links in a new tab #870

Merged
merged 11 commits into from
Apr 2, 2024
2 changes: 1 addition & 1 deletion app/scripts/components/common/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ function CardComponent(props: CardComponentProps) {

const isExternalLink = linkTo.match(/^https?:\/\//);
const linkProps = isExternalLink
? { href: linkTo, onClick: onLinkClick }
? { href: linkTo, target: "_blank", rel: "noopener noreferrer", onClick: onLinkClick }
: { as: Link, to: linkTo, onClick: onLinkClick };

return (
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/components/common/mdx-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
LazyEmbed
} from '$components/common/blocks/lazy-components';
import { NotebookConnectCalloutBlock } from '$components/common/notebook-connect';
import SmartLink from '$components/common/smart-link';
import SmartLink, { CustomLink } from '$components/common/smart-link';

function MdxContent(props) {
const pageMdx = useMdxPageLoader(props.loader);
Expand All @@ -44,6 +44,7 @@ function MdxContent(props) {
CompareImage: LazyCompareImage,
NotebookConnectCallout: NotebookConnectCalloutBlock,
Link: SmartLink,
a: CustomLink,
Table: LazyTable,
Embed: LazyEmbed
}}
Expand Down
19 changes: 18 additions & 1 deletion app/scripts/components/common/smart-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,25 @@ export default function SmartLink(props: SmartLinkProps) {
const { to, ...rest } = props;

return /^https?:\/\//.test(to) ? (
<a href={to} {...rest} />
<a target='_blank' rel='noopener noreferrer' href={to} {...rest} />
) : (
<Link to={to} {...rest} />
);
}

interface CustomLinkProps {
href: string;
}

/**
* For links defined as markdown, this component will open the external link in a new tab.
*/
export function CustomLink(props: CustomLinkProps) {
const { href, ...rest } = props;

return /^https?:\/\//.test(href) ? (
<a target='_blank' rel='noopener noreferrer' href={href} {...rest} />
) : (
<Link to={href} {...rest} />
);
sandrahoang686 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading