docusaurus: onBrokenAnchors reports named anchor tags as broken links
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Prerequisites
- I’m using the latest version of Docusaurus.
- I have tried the
npm run clearoryarn clearcommand. - I have tried
rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages. - I have tried creating a repro with https://new.docusaurus.io.
- I have read the console error message carefully (if applicable).
Description
When you use named anchors (E.g., <a name="anchor">Some Text</a> or self-closing ) in a doc page, onBrokenAnchors reports it as a broken link.
Reproducible demo
No response
Steps to reproduce
- In
docusaurus.config.js, setonBrokenAnchorstoWarn(if not using the default setting). - Edit a doc page.
- Add a named anchor anywhere on the page. E.g.,
<a name="anchor">Some Text</a> - Add a link to the named anchor, e.g.,
[Link Text](#anchor) - Save the page.
- Build the site (
yarn build).
Expected behavior
No console feedback. onBrokenAnchors ignores the arbitrary named anchor because it’s only supposed to work for anchors declared with the Heading component.
Actual behavior
onBrokenAnchors reports the named anchor as a broken anchor. Example:
[WARNING] Docusaurus found broken anchors!
Please check the pages of your site in the list below, and make sure you don't reference any anchor that does not exist.
Note: it's possible to ignore broken anchors with the 'onBrokenAnchors' Docusaurus configuration, and let the build pass.
Exhaustive list of all broken anchors found:
- Broken anchor on source page path = /user-types-and-permissions:
-> linking to #anchor (resolved as: /user-types-and-permissions#anchor)
Note that on the rendered page, the anchor works.
Your environment
- Public source code:
- Public site URL: (issue encountered locally during build)
- Docusaurus version used: 3.1.1
- Environment name and version (e.g. Chrome 89, Node.js 16.4): Version 120.0.6099.129 (Official Build) (arm64)
- Operating system and version (e.g. Ubuntu 20.04.2 LTS): macOS Ventura 13.4.1 (22F82)
Self-service
- I’d be willing to fix this bug myself.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 3
- Comments: 22 (3 by maintainers)
Commits related to this issue
- refactor: fix "broken link anchors" warnings See: https://github.com/facebook/docusaurus/issues/9808 — committed to xen0n/areweloongyet by xen0n 2 years ago
- Rfecator Jellyfin Vue anchor usage The anchor was working properly, but it triggered Docusauru's warnings. Workaround source: https://github.com/facebook/docusaurus/issues/9808#issuecomment-19214002... — committed to jellyfin/jellyfin.org by ferferga 2 years ago
- Refactor Jellyfin Vue anchor usage The anchor was working properly, but it triggered Docusaurus' warnings. Workaround source: https://github.com/facebook/docusaurus/issues/9808#issuecomment-19214002... — committed to jellyfin/jellyfin.org by ferferga 2 years ago
- Refactor Jellyfin Vue anchor usage The anchor was working properly, but it triggered Docusaurus' warnings. Workaround source: https://github.com/facebook/docusaurus/issues/9808#issuecomment-19214002... — committed to jellyfin/jellyfin.org by ferferga 2 years ago
So, all h1s are not anchorable? That seems counterintuitive.
I understand not allowing anchors for the title and possibly the first h1, but all h1s? h1s aren’t for titling documents, they are for formatting and navigation.
More specifically, markdown’s headings are based off of headings in word documents. All top-level h1 headings in Microsoft Word are “anchored” and can be navigated to with hyperlinks in the word document. There’s no reason that h1s should be treated different here.
I do agree that anchoring the first h1 is pointless, but it is also harmless.
I came across this issue because we include a bunch of generated content in our docs which includes a few anchors like this:
The tool that generates the content has a PR open to align to HTML5 spec, but in the meantime I made a quick little rehype plugin that will swap all
nameattributes toidinstead. This is definitely a preference but it helped remove all the errors and still works as intended.Here is the code if anyone also has this issue:
rehypeNameToId.ts:docusaurus.config.ts:I’d suggest opening a different issue/discussion for the case of h1 headings in particular.
Let’s keep this issue clean related to
onBrokenAnchorsreporting#anchoras a broken link for<a id="anchor"/>.I’ll hide all the h1-related discussions above.
This is for general typesetting. It has nothing to do with web page structure. You can find numerous resources:
<h1>should be functionally equivalent to the page’s<title>, only displayed to the user. Everything else comes as<h2>.OOC, are other headings anchorable by default? Don’t they all require
ids?Also, “should multiple
h1s be allowed” has been a long flame war on MDN, but the opinion of the writers is also that there’s no legitimate use case of multipleh1s.@anonhostpi
It’s usually a best practice to only have one h1 per page
Oh really? Can you back this up with reference material from an SEO and accessibility authority? For example from Google?
We are not a text editor, and don’t particularly plan to inspire from them.
And remember h1 are not anchorable by default on the web. If you don’t like that, then maybe propose to browser vendors to make all headings anchorable by default?
If it prevents you from introducing an antipattern to your own site, we can even consider this restriction as a feature.
The good news is that Docusaurus is flexible enough to let you plug your own opinions if you don’t agree with our defaults.
@theme/Headingto alter all our theme headings@theme/MDXComponents/Headingto alter all# Markdown Titleof your site, or provide a customh1in the MDXComponents map.<h1>or<MyCustomH1Heading/>and implement whatever you wantIt’s quite difficult to see what problem your are facing with the current information, perhaps we could help you more if you try to recreate your error with https://docusaurus.io/docs/playground
The feature basically collects each link in a page and then try to resolve the link with or without anchor and if nothing is found its considered as a broken link / anchor
I don’t know how is your
Velaptor.SizeU.mdpage but#widthshould refer to a markdown heading@nijikokun a warning does not prevent your site from building, and yes this is configurable as explained in the docs and release notes: https://docusaurus.io/blog/releases/3.1#broken-anchors-checker
onBrokenAnchors: "ignore"Disabling it is not a big deal, it just reverts to what we had in v3.0-
But it would be better to fix the broken anchors and help us figure out when we report false positives (if it’s a different case from the one above)
That would be great!
I understand. That’s not what I want. I would much rather the arbitrary anchors get checked along with the heading anchors. It’s just that right now,
<a>anchors return false errors because they’re not supported, which makes the broken anchors check noisy.I really appreciate your taking the time to consider this, BTW.