Skip to content

Commit

Permalink
Merge pull request #26488 from storybookjs/jeppe/allow-http-docs-exte…
Browse files Browse the repository at this point in the history
…rnal-links

MDX: Don't transform `http://` links
(cherry picked from commit c12ae69)
  • Loading branch information
JReinhold authored and storybook-bot committed May 1, 2024
1 parent 3c598bd commit 5c6e6af
Showing 1 changed file with 35 additions and 37 deletions.
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

0 comments on commit 5c6e6af

Please sign in to comment.