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
feat(core): make broken link checker detect broken anchors - add onBrokenAnchors
config
#9528
Conversation
✅ [V2]
To edit notification comments on pull requests, go to your Netlify site configuration. |
⚡️ Lighthouse report for the deploy preview of this PR
|
I don't particularly like this solution, because many other things can create anchors without using the Heading component. Since we only run the checker after a build anyway, why don't we read the HTML file and find all IDs? On a related note @slorber do you know why our broken link checker uses the Link component to find valid links and the SPA routes data to find valid targets, instead of using the build output as the source-of-truth? Here's the checker I built for MDN: https://github.com/jc-verse/mdn-checker/blob/master/packages/mdn-checker/src/rules/anchor-links.ts |
I understand the concern, although most achors will usually be created through the heading component. For remaining cases we'll expose a core API to report usage of links and anchors. The API is not designed yet but ideally users should be able to create their own link/anchor componennts and have it work nicely with the broken link checker.
We could also do that to check links too, not just anchors. IMHO users appreciate the fact that we provide an integrated solution for link checking. And I have good hope to someday be able to do some fancy things with the graph we collect, and have heuristics to report most broken links in dev mode thanks to those new APIs and a bit of caching.
That's an initial implementation detail, but it turns out it wasn't a bad one IMHO since it works nicely and is quite robust. And this gives the opportunity to have something more opinionated in the future. We probably only want to double check html files emitted through the Docusaurus SPA. Not 100% sure it's our responsibility to report broken links on Let's also not forget that regular non-SPA links are real HTML navigations and you can't know the redirects set up at the CDN/server level: this can lead to false positives. For these reasons I'd like for now to pursue in the current direction. I wish it didn't require introducing a new core API for it, but it's not too bad IMHO. |
Okay, that makes sense to me. I have an existing use case with a remark plugin that emits DLs with |
Great I don't think we'll expose a core React component but only a hook. Now you can decide to create your own component that uses the hook and emit it if it makes things simpler to integrate for your use-case |
onBrokenAnchors
config
…rokenAnchors` config (#9528) Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
I don't think it's a big issue, but this feature broken relative links to the https://github.com/johnnyreilly/blog.johnnyreilly.com/actions/runs/7426096653/job/20209108231?pr=809 I'm tackling this by making them fully qualified links instead: https://github.com/johnnyreilly/blog.johnnyreilly.com/pull/809/files#diff-78b51ee0667cbd2ab45d9da97c4128aee41826b6a32cf6662a70810068269939 As I say, I don't think it's a big issue but I thought I should mention it! |
Ah yes this is code we removed recently that used to look for existing static files on the build folder. If it's a markdown link, you can also use |
It's a markdown link - I wasn't aware of the Hard coding to the main website isn't actually a problem for me in this case - it's fine. Just thought I'd share in case it wasn't intentional. |
Hey @slorber , sorry if it is explained somewhere and I've missed it, but what is the way to make the broken anchor checker to work with custom (non-header) anchors in markdown? I.e. I use |
@birdofpreyru this falls under the case where you need to use the https://docusaurus.io/blog/releases/3.1#broken-anchors-checker The example shows a header, but it doesn't necessarily have to be a header. You can just encapsulate this API in any component that create anchors, in your case: export default MyCustomAnchor({id}) {
const brokenLinks = useBrokenLinks();
brokenLinks.collectAnchor(id);
return <a id={id}></a>
}
When you use One thing we could do is to map Eventually we could still enhance our link component and allow you to write |
Thanks @slorber ,
That clears my primary doubt regarding
That would be awesome, to have a standard out-of-the-box way to anchor. I mean, I can achieve on my own admonitions, code blocks, and what not; but it is a way more convenient they just come ready to use :) |
@birdofpreyru I tried to improve things in #9732 It's now possible to use the import Link from '@docusaurus/Link';
<Link id="my-anchor-id"/> Note that I still recommend you add it the Link component to the MDXComponents map, to avoid having to import it everywhre. I tried to see if it was possible to support collecting anchors for arbitrary I think there could be a way through a rehype plugin, but it's a bit complicated technically so I'd rather wait a bit for feedback to see if this is really necessary. |
Motivation
Previously it could only report broken paths.
siteConfig.onBrokenAnchors
option, defaults towarn
for v3.x retro-compatibility, no breaking change.useBrokenLinks()
core hook to collect page links and anchors (for advanced cases and plugin authors only)Fix #3321
Fix #9057
Test Plan
unit tests + dogfood
Test links
Preview: https://deploy-preview-9528--docusaurus-2.netlify.app/
Docs: