Skip to content

Commit

Permalink
[blog] Fix handling of markdown links (#35628)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Dec 30, 2022
1 parent dd2848e commit d6a6c96
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
4 changes: 4 additions & 0 deletions docs/src/BrandingCssVarsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import CssBaseline from '@mui/material/CssBaseline';
import { NextNProgressBar } from 'docs/src/modules/components/AppFrame';
import { getDesignTokens, getThemedComponents } from 'docs/src/modules/brandingTheme';
import SkipLink from 'docs/src/modules/components/SkipLink';
import MarkdownLinks from 'docs/src/modules/components/MarkdownLinks';

declare module '@mui/material/styles' {
interface PaletteOptions {
Expand Down Expand Up @@ -54,6 +56,8 @@ export default function BrandingCssVarsProvider({ children }: { children: React.
<CssVarsProvider theme={theme} defaultMode="system" disableTransitionOnChange>
<NextNProgressBar />
<CssBaseline />
<SkipLink />
<MarkdownLinks />
{children}
</CssVarsProvider>
);
Expand Down
2 changes: 2 additions & 0 deletions docs/src/BrandingProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CssBaseline from '@mui/material/CssBaseline';
import { brandingDarkTheme, brandingLightTheme } from 'docs/src/modules/brandingTheme';
import { NextNProgressBar } from 'docs/src/modules/components/AppFrame';
import SkipLink from 'docs/src/modules/components/SkipLink';
import MarkdownLinks from 'docs/src/modules/components/MarkdownLinks';

interface BrandingProviderProps {
children: React.ReactNode;
Expand All @@ -23,6 +24,7 @@ export default function BrandingProvider(props: BrandingProviderProps) {
{modeProp ? null : <NextNProgressBar />}
{modeProp ? null : <CssBaseline />}
{modeProp ? null : <SkipLink />}
{modeProp ? null : <MarkdownLinks />}
{children}
</ThemeProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/AppFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export default function AppFrame(props) {
</Box>
</NextLink>
<GrowingDiv />
<Stack direction="row" spacing={1.3}>
<Stack direction="row" spacing="10px">
<AppFrameBanner />
<DeferredAppSearch />
<Tooltip title={t('appFrame.github')} enterDelay={300}>
Expand Down
4 changes: 2 additions & 2 deletions docs/src/modules/components/AppNavDrawerItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import KeyboardArrowRightRoundedIcon from '@mui/icons-material/KeyboardArrowRigh
import { alpha, styled } from '@mui/material/styles';
import Collapse from '@mui/material/Collapse';
import Chip from '@mui/material/Chip';
import { openLinkInNewTab } from 'docs/src/modules/components/MarkdownLinks';
import { shoudHandleLinkClick } from 'docs/src/modules/components/MarkdownLinks';
import Link from 'docs/src/modules/components/Link';
import ArticleRoundedIcon from '@mui/icons-material/ArticleRounded';
import ToggleOffRoundedIcon from '@mui/icons-material/ToggleOffRounded';
Expand Down Expand Up @@ -217,7 +217,7 @@ export default function AppNavDrawerItem(props) {
const [open, setOpen] = React.useState(openImmediately);
const handleClick = (event) => {
// Ignore the action if opening the link in a new tab
if (openLinkInNewTab(event)) {
if (shoudHandleLinkClick(event)) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions docs/src/modules/components/AppTableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Typography from '@mui/material/Typography';
import NoSsr from '@mui/material/NoSsr';
import Link from 'docs/src/modules/components/Link';
import { useTranslate } from 'docs/src/modules/utils/i18n';
import { openLinkInNewTab } from 'docs/src/modules/components/MarkdownLinks';
import { shoudHandleLinkClick } from 'docs/src/modules/components/MarkdownLinks';
import TableOfContentsBanner from 'docs/src/components/banner/TableOfContentsBanner';

const Nav = styled('nav')(({ theme }) => ({
Expand Down Expand Up @@ -177,7 +177,7 @@ export default function AppTableOfContents(props) {

const handleClick = (hash) => (event) => {
// Ignore click for new tab/new window behavior
if (openLinkInNewTab(event)) {
if (shoudHandleLinkClick(event)) {
return;
}

Expand Down
26 changes: 11 additions & 15 deletions docs/src/modules/components/MarkdownLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import Router from 'next/router';
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';

export function openLinkInNewTab(event) {
export function shoudHandleLinkClick(event) {
if (
event.defaultPrevented ||
event.button !== 0 || // ignore everything but left-click
Expand All @@ -16,18 +16,6 @@ export function openLinkInNewTab(event) {
return false;
}

export function handleEvent(event, as) {
// Ignore click for new tab/new window behavior
if (openLinkInNewTab(event)) {
return;
}

event.preventDefault();

const canonicalPathname = pathnameToLanguage(as).canonicalPathname;
Router.push(canonicalPathname, as);
}

/**
* @param {MouseEvent} event
*/
Expand All @@ -37,7 +25,7 @@ function handleClick(event) {
activeElement = activeElement.parentElement;
}

// Ignore non link clicks
// Ignore non internal link clicks
if (
activeElement === null ||
activeElement.nodeName !== 'A' ||
Expand All @@ -48,7 +36,15 @@ function handleClick(event) {
return;
}

handleEvent(event, activeElement.getAttribute('href'));
// Ignore click meant for native link handling, e.g. open in new tab
if (shoudHandleLinkClick(event)) {
return;
}

event.preventDefault();
const as = activeElement.getAttribute('href');
const canonicalPathname = pathnameToLanguage(as).canonicalPathname;
Router.push(canonicalPathname, as);
}

export default function MarkdownLinks() {
Expand Down

0 comments on commit d6a6c96

Please sign in to comment.