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

MDX: Don't transform http:// links #26488

Merged
merged 3 commits into from
Apr 19, 2024
Merged
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
72 changes: 35 additions & 37 deletions code/ui/blocks/src/blocks/mdx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,45 +96,43 @@ export const AnchorMdx: FC<PropsWithChildren<AnchorMdxProps>> = (props) => {
const { href, target, children, ...rest } = props;
const context = useContext(DocsContext);

if (href) {
// Enable scrolling for in-page anchors.
if (href.startsWith('#')) {
return <AnchorInPage hash={href}>{children}</AnchorInPage>;
}

// Links to other pages of SB should use the base URL of the top level iframe instead of the base URL of the preview iframe.
if (target !== '_blank' && !href.startsWith('https://')) {
return (
<A
href={href}
onClick={(event: MouseEvent<HTMLAnchorElement>) => {
// Cmd/Ctrl/Shift/Alt + Click should trigger default browser behaviour. Same applies to non-left clicks
const LEFT_BUTTON = 0;
const isLeftClick =
event.button === LEFT_BUTTON &&
!event.altKey &&
!event.ctrlKey &&
!event.metaKey &&
!event.shiftKey;

if (isLeftClick) {
event.preventDefault();
// use the A element's href, which has been modified for
// local paths without a `?path=` query param prefix
navigate(context, event.currentTarget.getAttribute('href'));
}
}}
target={target}
{...rest}
>
{children}
</A>
);
}
// links to external locations don't need any modifications.
if (!href || target === '_blank' || /^https?:\/\//.test(href)) {
return <A {...props} />;
}

// External URL dont need any modification.
return <A {...props} />;
// Enable scrolling for in-page anchors.
if (href.startsWith('#')) {
return <AnchorInPage hash={href}>{children}</AnchorInPage>;
}

// Links to other pages of SB should use the base URL of the top level iframe instead of the base URL of the preview iframe.
return (
<A
href={href}
onClick={(event: MouseEvent<HTMLAnchorElement>) => {
// Cmd/Ctrl/Shift/Alt + Click should trigger default browser behaviour. Same applies to non-left clicks
const LEFT_BUTTON = 0;
const isLeftClick =
event.button === LEFT_BUTTON &&
!event.altKey &&
!event.ctrlKey &&
!event.metaKey &&
!event.shiftKey;

if (isLeftClick) {
event.preventDefault();
// use the A element's href, which has been modified for
// local paths without a `?path=` query param prefix
navigate(context, event.currentTarget.getAttribute('href'));
}
}}
target={target}
{...rest}
>
{children}
</A>
);
};

const SUPPORTED_MDX_HEADERS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as const;
Expand Down