docusaurus: onBrokenAnchors reports named anchor tags as broken links

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I’m using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and 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

  1. In docusaurus.config.js, set onBrokenAnchors to Warn (if not using the default setting).
  2. Edit a doc page.
  3. Add a named anchor anywhere on the page. E.g., <a name="anchor">Some Text</a>
  4. Add a link to the named anchor, e.g., [Link Text](#anchor)
  5. Save the page.
  6. 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

Most upvoted comments

First, we don’t create anchors for docs titles / top-level headings. You can just link to the page directly because that title should rather be at the top.

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’d suggest opening a different issue/discussion for the case of h1 headings in particular.

Let’s keep this issue clean related to onBrokenAnchors reporting #anchor as 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>.

And remember h1 are not anchorable by default on the web.

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 multiple h1s.

@anonhostpi

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?

It’s usually a best practice to only have one h1 per page

h1s aren’t for titling documents, they are for formatting and navigation.

Oh really? Can you back this up with reference material from an SEO and accessibility authority? For example from Google?

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.

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?

I do agree that anchoring the first h1 is pointless, _but it is also harmless.

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.

  • swizzle @theme/Heading to alter all our theme headings
  • swizzle @theme/MDXComponents/Heading to alter all # Markdown Title of your site, or provide a custom h1 in the MDXComponents map.
  • use JSX <h1> or <MyCustomH1Heading/> and implement whatever you want

I am having the same issue but I am using MD and MDX files. I guess I just don’t understand why this would be an issue when using markdown files because they get transpiled by docusaurus. Wouldn’t docusaurus properly convert a markdown link to the required HTML so it “is not a broken anchor”?

For example, I have the markdown link [Width](./Velaptor.SizeU.md#width) with other variations that I have tried, but I always get the warning when building.

Here are the variations I have tried with no good results:

  1. Width
  2. Width
  3. Width
  4. Width
  5. Width
  6. Width
  7. Width

Of course, this could be maybe understanding what a “broken anchor” means. Even though I get the warning, the anchor works locally with pnpm start, pnpm run serve, and in production.

So I guess I do need an explanation of what “broken anchor” means?

It’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.md page but #widthshould refer to a markdown heading

// hello.md
# heading

a link to my [title](#heading)
// world.md
# heading

a link to my [hello heading](./hello#heading) // also should work depending on the path
a link to my [hello heading](hello#heading) // should work

@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)

We’ll try to add support for <div id="anchor"/>, <a id="anchor"/> etc in Markdown files.

That would be great!

For a given page, we either validate or not, but there’s no middle ground. What you ask for basically means disabling the broken anchor check

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.