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

[docs] Fix crash on Safari because of unsupported lookahead feature #30345

Merged
merged 7 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
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
24 changes: 11 additions & 13 deletions docs/src/modules/components/AppSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Link from 'docs/src/modules/components/Link';
import { useTranslate, useUserLanguage } from 'docs/src/modules/utils/i18n';
import useLazyCSS from 'docs/src/modules/utils/useLazyCSS';
import { useRouter } from 'next/router';
import FEATURE_TOGGLE from 'docs/src/featureToggle';
import replaceUrl from 'docs/src/modules/utils/replaceUrl';

const SearchButton = styled('button')(({ theme }) => {
return {
Expand Down Expand Up @@ -296,25 +296,23 @@ export default function AppSearch() {
// `url` contains the domain
// but we want to link to the current domain e.g. deploy-preview-1--material-ui.netlify.app
const parseUrl = document.createElement('a');
parseUrl.href = item.url;

let hash = parseUrl.hash;
let pathname = parseUrl.pathname;

if (['lvl2', 'lvl3'].includes(item.type)) {
// remove '#heading-' from `href` url so that the link targets <span class="anchor-link"> inside <h2> or <h3>
// this will make the title appear under the Header
parseUrl.href = item.url.replace('#heading-', '#');
} else {
parseUrl.href = item.url;
hash = hash.replace('#heading-', '#');
}

// TODO: remove this logic once the migration to new structure is done.
if (FEATURE_TOGGLE.enable_product_scope) {
parseUrl.href = parseUrl.href.replace(
/(?<!material\/)(getting-started|components|api|customization|guides|discover-more)(\/[^/]+\/)/,
`material/$1$2`,
);
}
// This logic covers us during the ~60 minutes that it takes Algolia to run a crawl and update its index.
// It also allows us to have a search bar that works in dev mode while the new structure is not pushed to production.
pathname = replaceUrl(pathname, router.asPath);

const { canonicalAs, canonicalPathname } = pathnameToLanguage(
`${parseUrl.pathname}${parseUrl.hash}`,
);
const { canonicalAs, canonicalPathname } = pathnameToLanguage(`${pathname}${hash}`);

return {
...item,
Expand Down
31 changes: 10 additions & 21 deletions docs/src/modules/utils/i18n.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import * as React from 'react';
import FEATURE_TOGGLE from 'docs/src/featureToggle';
import { useRouter } from 'next/router';
import replaceMarkdownLinks from 'docs/src/modules/utils/replaceMarkdownLinks';

function mapTranslations(req) {
const translations = {};
Expand Down Expand Up @@ -62,28 +63,16 @@ export function useSetUserLanguage() {
const warnedOnce = {};

export function useTranslate() {
const router = useRouter();
const userLanguage = useUserLanguage();

return React.useMemo(
() =>
function translate(key, options = {}) {
function prefixMaterial(translation) {
if (typeof translation === 'string' && FEATURE_TOGGLE.enable_product_scope) {
let prefixed = translation;
[
'/getting-started',
'/components',
'/api-docs',
'/customization',
'/guides',
'/discover-more',
].forEach((pathname) => {
prefixed = prefixed.replace(
new RegExp(`href="${pathname}`, 'g'),
`href="/material${pathname}`,
);
});
return prefixed;
// TODO: remove this logic once the migration to new structure is done.
function pointToNewHref(translation) {
if (typeof translation === 'string') {
return replaceMarkdownLinks(translation, router.asPath);
}
return translation;
}
Expand All @@ -104,11 +93,11 @@ export function useTranslate() {
console.error(`Missing translation for ${fullKey}`);
warnedOnce[fullKey] = true;
}
return prefixMaterial(getPath(translations.en, key));
return pointToNewHref(getPath(translations.en, key));
}

return prefixMaterial(translation);
return pointToNewHref(translation);
},
[userLanguage],
[userLanguage, router.asPath],
);
}
237 changes: 237 additions & 0 deletions docs/src/modules/utils/replaceMarkdownLinks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
import { expect } from 'chai';
import replaceMarkdownLinks, {
replaceMaterialLinks,
replaceAPILinks,
replaceComponentLinks,
} from './replaceMarkdownLinks';

describe('replaceMarkdownLinks', () => {
it('replace material related links', () => {
expect(
replaceMaterialLinks(`
<ul>
<a href="/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>
<a href="/customization/theme-components/#default-props">default props</a>
<a data-no-markdown-link="true" tabindex="0" href="/getting-started/usage/">Get started</a>
<li><a href="/discover-more/related-projects/">Tree view</a></li>
</ul>
`),
).to.equal(`
<ul>
<a href="/material/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>
<a href="/material/customization/theme-components/#default-props">default props</a>
<a data-no-markdown-link="true" tabindex="0" href="/material/getting-started/usage/">Get started</a>
<li><a href="/material/discover-more/related-projects/">Tree view</a></li>
</ul>
`);
});

it('should not change if links have been updated', () => {
expect(
replaceMaterialLinks(`
<ul>
<a href="/material/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>
<a href="/material/customization/theme-components/#default-props">default props</a>
<a data-no-markdown-link="true" tabindex="0" href="/material/getting-started/usage/">Get started</a>
<li><a href="/material/discover-more/related-projects/">Tree view</a></li>
</ul>
`),
).to.equal(`
<ul>
<a href="/material/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>
<a href="/material/customization/theme-components/#default-props">default props</a>
<a data-no-markdown-link="true" tabindex="0" href="/material/getting-started/usage/">Get started</a>
<li><a href="/material/discover-more/related-projects/">Tree view</a></li>
</ul>
`);
});

it('replace correct component links', () => {
expect(
replaceComponentLinks(`
<ul>
<li><a href="/components/button-group/">Button Group</a></li>
<li><a href="/components/buttons/">Buttons</a></li>
<li><a href="/components/tree-view/">Tree view</a></li>
<li><a href="/components/data-grid/demo/">Demo</a></li>
</ul>
`),
).to.equal(`
<ul>
<li><a href="/material/react-button-group/">Button Group</a></li>
<li><a href="/material/react-buttons/">Buttons</a></li>
<li><a href="/material/react-tree-view/">Tree view</a></li>
<li><a href="/x/react-data-grid/demo/">Demo</a></li>
</ul>
`);
});

it('should do nothing if the components have updated', () => {
expect(
replaceComponentLinks(`
<ul>
<li><a href="/material/react-button-group/">Button Group</a></li>
<li><a href="/material/react-buttons/">Buttons</a></li>
<li><a href="/material/react-tree-view/">Tree view</a></li>
<li><a href="/x/react-data-grid/demo/">Demo</a></li>
</ul>
`),
).to.equal(`
<ul>
<li><a href="/material/react-button-group/">Button Group</a></li>
<li><a href="/material/react-buttons/">Buttons</a></li>
<li><a href="/material/react-tree-view/">Tree view</a></li>
<li><a href="/x/react-data-grid/demo/">Demo</a></li>
</ul>
`);
});

it('replace correct API links', () => {
expect(
replaceAPILinks(`
<h2 id="heading-api"><span class="anchor-link" id="api"></span>API<a aria-labelledby="heading-api" class="anchor-link-style" href="#api" tabindex="-1"><svg><use xlink:href="#anchor-link-icon" /></svg></a></h2><ul>
<li><a href="/api/button/"><code>&lt;Button /&gt;</code></a></li>
<li><a href="/api/button-base/"><code>&lt;ButtonBase /&gt;</code></a></li>
<li><a href="/api/button-unstyled/"><code>&lt;ButtonUnstyled /&gt;</code></a></li>
<li><a href="/api/icon-button/"><code>&lt;IconButton /&gt;</code></a></li>
<li><a href="/api/loading-button/"><code>&lt;LoadingButton /&gt;</code></a></li>
<li><a href="/api/data-grid/data-grid/">DataGrid</a></li>
<li><a href="/api/data-grid/data-grid-pro/">DataGridPro</a></li>
<li><a href="/styles/api/">Styles</a></li>
<li><a href="/system/basics/">System</a></li>
</ul>
`),
).to.equal(`
<h2 id="heading-api"><span class="anchor-link" id="api"></span>API<a aria-labelledby="heading-api" class="anchor-link-style" href="#api" tabindex="-1"><svg><use xlink:href="#anchor-link-icon" /></svg></a></h2><ul>
<li><a href="/material/api/mui-material/button/"><code>&lt;Button /&gt;</code></a></li>
<li><a href="/material/api/mui-material/button-base/"><code>&lt;ButtonBase /&gt;</code></a></li>
<li><a href="/material/api/mui-base/button-unstyled/"><code>&lt;ButtonUnstyled /&gt;</code></a></li>
<li><a href="/material/api/mui-material/icon-button/"><code>&lt;IconButton /&gt;</code></a></li>
<li><a href="/material/api/mui-lab/loading-button/"><code>&lt;LoadingButton /&gt;</code></a></li>
<li><a href="/x/api/mui-data-grid/data-grid/">DataGrid</a></li>
<li><a href="/x/api/mui-data-grid/data-grid-pro/">DataGridPro</a></li>
<li><a href="/styles/api/">Styles</a></li>
<li><a href="/system/basics/">System</a></li>
</ul>
`);
});

it('should do nothing if the APIs have updated', () => {
expect(
replaceAPILinks(`
<h2 id="heading-api"><span class="anchor-link" id="api"></span>API<a aria-labelledby="heading-api" class="anchor-link-style" href="#api" tabindex="-1"><svg><use xlink:href="#anchor-link-icon" /></svg></a></h2><ul>
<li><a href="/material/api/mui-material/button/"><code>&lt;Button /&gt;</code></a></li>
<li><a href="/material/api/mui-material/button-base/"><code>&lt;ButtonBase /&gt;</code></a></li>
<li><a href="/material/api/mui-base/button-unstyled/"><code>&lt;ButtonUnstyled /&gt;</code></a></li>
<li><a href="/material/api/mui-material/icon-button/"><code>&lt;IconButton /&gt;</code></a></li>
<li><a href="/material/api/mui-lab/loading-button/"><code>&lt;LoadingButton /&gt;</code></a></li>
</ul>
`),
).to.equal(`
<h2 id="heading-api"><span class="anchor-link" id="api"></span>API<a aria-labelledby="heading-api" class="anchor-link-style" href="#api" tabindex="-1"><svg><use xlink:href="#anchor-link-icon" /></svg></a></h2><ul>
<li><a href="/material/api/mui-material/button/"><code>&lt;Button /&gt;</code></a></li>
<li><a href="/material/api/mui-material/button-base/"><code>&lt;ButtonBase /&gt;</code></a></li>
<li><a href="/material/api/mui-base/button-unstyled/"><code>&lt;ButtonUnstyled /&gt;</code></a></li>
<li><a href="/material/api/mui-material/icon-button/"><code>&lt;IconButton /&gt;</code></a></li>
<li><a href="/material/api/mui-lab/loading-button/"><code>&lt;LoadingButton /&gt;</code></a></li>
</ul>
`);
});

it('only replace links for new routes (/material/* & /x/*)', () => {
expect(
replaceMarkdownLinks(
`
<ul>
<li><a href="/components/button-group/">Button Group</a></li>
<li><a href="/components/buttons/">Buttons</a></li>
<li><a href="/components/tree-view/">Tree view</a></li>
<li><a href="/components/data-grid/demo/">Demo</a></li>
<li><a href="/api/button/"><code>&lt;Button /&gt;</code></a></li>
<li><a href="/api/button-base/"><code>&lt;ButtonBase /&gt;</code></a></li>
<li><a href="/api/button-unstyled/"><code>&lt;ButtonUnstyled /&gt;</code></a></li>
<li><a href="/api/icon-button/"><code>&lt;IconButton /&gt;</code></a></li>
<li><a href="/api/loading-button/"><code>&lt;LoadingButton /&gt;</code></a></li>
<li><a href="/api/data-grid/data-grid/">DataGrid</a></li>
<li><a href="/api/data-grid/data-grid-pro/">DataGridPro</a></li>
<li><a href="/styles/api/">Styles</a></li>
<li><a href="/system/basics/">System</a></li>
<a href="/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>
<a href="/customization/theme-components/#default-props">default props</a>
<a data-no-markdown-link="true" tabindex="0" href="/getting-started/usage/">Get started</a>
<li><a href="/discover-more/related-projects/">Tree view</a></li>
</ul>
`,
'/material/react-buttons',
),
).to.equal(`
<ul>
<li><a href="/material/react-button-group/">Button Group</a></li>
<li><a href="/material/react-buttons/">Buttons</a></li>
<li><a href="/material/react-tree-view/">Tree view</a></li>
<li><a href="/x/react-data-grid/demo/">Demo</a></li>
<li><a href="/material/api/mui-material/button/"><code>&lt;Button /&gt;</code></a></li>
<li><a href="/material/api/mui-material/button-base/"><code>&lt;ButtonBase /&gt;</code></a></li>
<li><a href="/material/api/mui-base/button-unstyled/"><code>&lt;ButtonUnstyled /&gt;</code></a></li>
<li><a href="/material/api/mui-material/icon-button/"><code>&lt;IconButton /&gt;</code></a></li>
<li><a href="/material/api/mui-lab/loading-button/"><code>&lt;LoadingButton /&gt;</code></a></li>
<li><a href="/x/api/mui-data-grid/data-grid/">DataGrid</a></li>
<li><a href="/x/api/mui-data-grid/data-grid-pro/">DataGridPro</a></li>
<li><a href="/styles/api/">Styles</a></li>
<li><a href="/system/basics/">System</a></li>
<a href="/material/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>
<a href="/material/customization/theme-components/#default-props">default props</a>
<a data-no-markdown-link="true" tabindex="0" href="/material/getting-started/usage/">Get started</a>
<li><a href="/material/discover-more/related-projects/">Tree view</a></li>
</ul>
`);
});

it('should work with json', () => {
const json = {
importDifference:
'You can learn about the difference by <a href="/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>.',
};
expect(replaceMarkdownLinks(JSON.stringify(json), '/material/api/')).to.equal(
'{"importDifference":"You can learn about the difference by <a href=\\"/material/guides/minimizing-bundle-size/\\">reading this guide on minimizing bundle size</a>."}',
);
const json2 = {
styleOverrides:
'The name <code>{{componentStyles.name}}</code> can be used when providing <a href="/customization/theme-components/#default-props">default props</a> or <a href="/customization/theme-components/#global-style-overrides">style overrides</a> in the theme.',
};
expect(replaceMarkdownLinks(JSON.stringify(json2), '/material/api/')).to.equal(
'{"styleOverrides":"The name <code>{{componentStyles.name}}</code> can be used when providing <a href=\\"/material/customization/theme-components/#default-props\\">default props</a> or <a href=\\"/material/customization/theme-components/#global-style-overrides\\">style overrides</a> in the theme."}',
);
});

it('does nothing for old routes', () => {
expect(
replaceMarkdownLinks(
`
<ul>
<li><a href="/components/button-group/">Button Group</a></li>
<li><a href="/components/buttons/">Buttons</a></li>
<li><a href="/components/tree-view/">Tree view</a></li>
<li><a href="/components/data-grid/demo/">Demo</a></li>
<a href="/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>
<a href="/customization/theme-components/#default-props">default props</a>
<a data-no-markdown-link="true" tabindex="0" href="/getting-started/usage/">Get started</a>
<li><a href="/discover-more/related-projects/">Tree view</a></li>
</ul>
`,
'/components/buttons/',
),
).to.equal(`
<ul>
<li><a href="/components/button-group/">Button Group</a></li>
<li><a href="/components/buttons/">Buttons</a></li>
<li><a href="/components/tree-view/">Tree view</a></li>
<li><a href="/components/data-grid/demo/">Demo</a></li>
<a href="/guides/minimizing-bundle-size/">reading this guide on minimizing bundle size</a>
<a href="/customization/theme-components/#default-props">default props</a>
<a data-no-markdown-link="true" tabindex="0" href="/getting-started/usage/">Get started</a>
<li><a href="/discover-more/related-projects/">Tree view</a></li>
</ul>
`);
});
});
33 changes: 33 additions & 0 deletions docs/src/modules/utils/replaceMarkdownLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const replaceMaterialLinks = (markdown: string) => {
return markdown.replace(
/href=(\\*?)"\/(guides|customization|getting-started|discover-more)\/([^"]*)"/gm,
'href=$1"/material/$2/$3"',
);
};

export const replaceComponentLinks = (markdown: string) => {
return markdown
.replace(/href=(\\*?)"\/components\/data-grid([^"]*)"/gm, 'href=$1"/x/react-data-grid$2"')
.replace(/href=(\\*?)"\/components\/([^"]+)"/gm, 'href=$1"/material/react-$2"');
};

export const replaceAPILinks = (markdown: string) => {
return markdown
.replace(/href=(\\*?)"\/api\/data-grid([^"]*)"/gm, 'href=$1"/x/api/mui-data-grid$2"')
.replace(
/href=(\\*?)"\/api\/(loading-button|tab-list|tab-panel|date-picker|date-time-picker|time-picker|calendar-picker|calendar-picker-skeleton|desktop-picker|mobile-date-picker|month-picker|pickers-day|static-date-picker|year-picker|masonry|timeline|timeline-connector|timeline-content|timeline-dot|timeline-item|timeline-opposite-content|timeline-separator|unstable-trap-focus|tree-item|tree-view)([^"]*)"/gm,
'href=$1"/material/api/mui-lab/$2$3"',
)
.replace(
/href=(\\*?)"\/api\/([^"-]+-unstyled)([^"]*)"/gm,
'href=$1"/material/api/mui-base/$2$3"',
)
.replace(/href=(\\*?)"\/api\/([^"]*)"/gm, 'href=$1"/material/api/mui-material/$2"');
};

export default function replaceMarkdownLinks(markdown: string, asPath: string) {
if (asPath.startsWith('/material/') || asPath.startsWith('/x/')) {
return replaceMaterialLinks(replaceAPILinks(replaceComponentLinks(markdown)));
}
return markdown;
}
Loading