From 37ceeeb76a1c4bbff6bd608aaa2097c050d3dd26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Thu, 25 Jan 2024 18:18:11 +0100 Subject: [PATCH] fix(core): links with target "_blank" should no be checked by the broken link checker (#9788) --- .../docusaurus/src/client/exports/Link.tsx | 7 ++++- .../_pages tests/markdown-tests-mdx.mdx | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus/src/client/exports/Link.tsx b/packages/docusaurus/src/client/exports/Link.tsx index 3c02a025a81a..0e67536f885e 100644 --- a/packages/docusaurus/src/client/exports/Link.tsx +++ b/packages/docusaurus/src/client/exports/Link.tsx @@ -143,8 +143,13 @@ function Link( // It is simple local anchor link targeting current page? const isAnchorLink = targetLink?.startsWith('#') ?? false; + // See also RR logic: + // https://github.com/remix-run/react-router/blob/v5/packages/react-router-dom/modules/Link.js#L47 + const hasInternalTarget = !props.target || props.target === '_self'; + // Should we use a regular tag instead of React-Router Link component? - const isRegularHtmlLink = !targetLink || !isInternal || isAnchorLink; + const isRegularHtmlLink = + !targetLink || !isInternal || !hasInternalTarget || isAnchorLink; if (!noBrokenLinkCheck && (isAnchorLink || !isRegularHtmlLink)) { brokenLinks.collectLink(targetLink!); diff --git a/website/_dogfooding/_pages tests/markdown-tests-mdx.mdx b/website/_dogfooding/_pages tests/markdown-tests-mdx.mdx index 037a1f9e6458..feaf146f059d 100644 --- a/website/_dogfooding/_pages tests/markdown-tests-mdx.mdx +++ b/website/_dogfooding/_pages tests/markdown-tests-mdx.mdx @@ -361,6 +361,37 @@ See [#3309](https://github.com/facebook/docusaurus/issues/3309) - [pathname://../dogfooding/javadoc/index.html](pathname://../dogfooding/javadoc/index.html) +### Linking to non-SPA page with Link component + +See [#9758](https://github.com/facebook/docusaurus/issues/9758), these external urls should not be reported by the broken links checker: + +```mdx-code-block +import Link from '@docusaurus/Link'; + +export function TestLink({noCheck, ...props}) { + return ( + + {(noCheck ? '❌' : '✅') + + ' ' + + (props.to ?? props.href) + + (props.target ? ` (target=${props.target})` : '')} + + ); +} +``` + +- +- +- +- + +These links are broken (try to single click on them) and should be reported. We need to explicitly disable the broken link checker for them: + +- +- +- +- + ### Linking to JSON - [./script.js](./_script.js)