fix(core): recognize arbitrary HTML anchors in broken anchor checker (AI-assisted) - #12430
Open
Sanjays2402 wants to merge 1 commit into
Open
Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
…(AI-assisted) The onBrokenAnchors checker only knew about anchors explicitly collected through the useBrokenLinks() API (e.g. by the <Heading/> theme component), so valid anchors such as <div id="anchor"/> or legacy <a name="anchor"/> in MDX/React pages were wrongly reported as broken anchors. During SSG, anchor targets (any element id attribute, plus legacy <a name>) are now extracted from the rendered HTML page - the same source of truth browsers use for fragment navigation - and added to the collected anchors. Fixes facebook#9808
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9808
Problem
The
onBrokenAnchorschecker only knew about anchors explicitly collected through theuseBrokenLinks()API (e.g. by the@theme/Headingcomponent). Valid anchors written directly in MDX/React pages — such as<div id="anchor"/>,<h3 id="anchor">, or legacy<a name="anchor"/>— were never collected, so links targeting them were wrongly reported as broken anchors duringdocusaurus build, even though the anchors work fine in the browser.Fix
During SSG (
packages/docusaurus/src/client/serverEntry.tsx), anchor targets are now extracted from the fully rendered HTML page — the same source of truth browsers use for fragment navigation — and added to the collected anchors:extractHtmlAnchors()util (packages/docusaurus/src/client/extractHtmlAnchors.ts) collects theidattribute of any element plus legacy<a name="...">anchors, with HTML-entity decoding, deduplication, and guards against false positives (data-id, text content, non-<a>nameattributes)website/docs/api/docusaurus.config.js.mdx):onBrokenAnchorsno longer claims anchors must be "declared with theHeadingcomponent"This matches the direction discussed in the issue (automatically supporting
<div id="anchor"/>,<a id="anchor"/>, etc. in Markdown files, not just headings).Test plan
packages/docusaurus/src/client/__tests__/extractHtmlAnchors.test.ts: 9 tests pass (vitest) — arbitrary element ids, legacy<a name>, mixed pages, dedup, empty anchors, false-positive guards (data-id,aria-labelledby, text content), HTML-entity decodingtsc --noEmit --strict(with repo flagsnoUncheckedIndexedAccess,erasableSyntaxOnly): cleanoxfmt --list-differenton changed TS files: clean<a name="anchor">+ link#anchor— before the fix the checker's anchor-matching logic reports#anchoras broken (reproduces the issue); after adding the HTML-extracted anchors,#anchorand#custom-anchorresolve while a genuinely missing#missinganchor is still reported as brokenyarn build/ jest suite not run here (deps not installed in this environment); CI will cover it(AI-assisted)