docusaurus: broken anchor false positive?

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

With the new version 3.1 I get broken anchor errors, although the links work just fine. I don’t see any issue in the anchor link.

Example: On the page https://sweetcode.com/blog/are-all-payment-gateways-created-equal/ I have one link linking to the pricing section with an anchor that looks like this /plugins/pmw/#pricing-section (it also throws and error when I remove the trailing slash like this: /plugins/pmw#pricing-section

The full link with the anchor looks like this: https://sweetcode.com/plugins/pmw/#pricing-section

You’ll find the link in the document by searching for Pro version of the Pixel Manager.

Here’s the error output:

Broken anchor on source page path = /blog/are-all-payment-gateways-created-equal:
     -> linking to /plugins/pmw#pricing-section

Reproducible demo

No response

Steps to reproduce

https://sweetcode.com/blog/are-all-payment-gateways-created-equal/

Expected behavior

No broken anchor error thrown on this link: https://sweetcode.com/plugins/pmw/#pricing-section

Actual behavior

A broken anchor error is thrown.

Broken anchor on source page path = /blog/are-all-payment-gateways-created-equal:
     -> linking to /plugins/pmw#pricing-section

Your environment

Self-service

  • I’d be willing to fix this bug myself.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 17 (4 by maintainers)

Most upvoted comments

@slorber

I haven’t swizzled any components… But I have forked the remark-toc plugin in order to fix an issue with TOC not properly generating when importing other mdx files with headers of their own.

I’ve come to the conclusion to wait for 3.2 which is supposed to add some support for this case (https://github.com/facebook/docusaurus/issues/3915#issuecomment-1900862004) and see if the anchor links warnings dissapears.

Thanks for the response 👍

@polly-bold for us updating these packages to the versions used in the sandbox fixed it (not sure which one specifically, but when we only updated @docusaurus... it wasn’t working)

"@docusaurus/core": "^3.2.0",
"@docusaurus/preset-classic": "^3.2.0",
"@mdx-js/react": "^3.0.0",
"clsx": "^2.0.0",
"prism-react-renderer": "^2.3.0",

We had to change the way we use the prism themes on docusaurus.config.js but it’s minimal, and you can see how the new version works on the sandbox.

This is working as intended, cf release blog post: https://docusaurus.io/blog/releases/3.1#broken-anchors-checker

You are creating your own anchors, and fall under this case where you need to tell Docusaurus about the anchors:

CleanShot 2024-01-09 at 12 25 53@2x

Use the broken links API:

useBrokenLinks().collectAnchor("pricing-section")

Alternatively, you can also add onBrokenAnchors: "ignore" to your config: this will not warn you about such false positives.


Note that I’m not sure having multiple h1 elements on a page like you did is a good semantic html practice.