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

fix(core): links with target "_blank" should no be checked by the broken link checker #9788

Merged
merged 1 commit into from
Jan 25, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/docusaurus/src/client/exports/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a> 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!);
Expand Down
31 changes: 31 additions & 0 deletions website/_dogfooding/_pages tests/markdown-tests-mdx.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Link {...props} data-noBrokenLinkCheck={noCheck}>
{(noCheck ? '❌' : '✅') +
' ' +
(props.to ?? props.href) +
(props.target ? ` (target=${props.target})` : '')}
</Link>
);
}
```

- <TestLink to="pathname:///dogfooding/javadoc#goodlink1" />
- <TestLink href="pathname:///dogfooding/javadoc#goodlink2" />
- <TestLink to="/dogfooding/javadoc#goodlink3" target="_blank" />
- <TestLink href="/dogfooding/javadoc#goodlink4" target="_blank" />

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:

- <TestLink to="/dogfooding/javadoc#badlink1" noCheck />
- <TestLink href="/dogfooding/javadoc#badlink2" noCheck />
- <TestLink to="/dogfooding/javadoc#badlink3" target="_self" noCheck />
- <TestLink href="/dogfooding/javadoc#badlink4" target="_self" noCheck />

### Linking to JSON

- [./script.js](./_script.js)
Expand Down