Skip to content

feat: restore legacy anchor alias to preserve old links#699

Open
shivxmsharma wants to merge 1 commit intonodejs:mainfrom
shivxmsharma:feat/restore-legacy-anchor-alias
Open

feat: restore legacy anchor alias to preserve old links#699
shivxmsharma wants to merge 1 commit intonodejs:mainfrom
shivxmsharma:feat/restore-legacy-anchor-alias

Conversation

@shivxmsharma
Copy link
Contributor

Fixes: #697

Description

Restores the legacy anchor alias for headings to preserve old documentation links.

The old Node.js doc generator (tools/doc/html.mjs) produced two anchor IDs per heading — a modern GitHub-style slug (e.g., #file-system) and a legacy underscore-based slug prefixed with the filename (e.g., #fs_file_system). External sites like Express and many others still link to these legacy anchors, and since doc-kit dropped them, those links silently break.

This PR ports the old getLegacyId algorithm into doc-kit and renders a hidden <a> element with the legacy ID alongside every heading, so both old and new anchor links resolve correctly.

Changes

  • src/generators/metadata/utils/slugger.mjs — Added getLegacySlug() function implementing the old underscore-based slug algorithm, and added a legacySlug() method to createNodeSlugger with counter-based deduplication.
  • src/generators/metadata/types.d.ts — Added legacySlug: string field to HeadingData interface.
  • src/generators/metadata/utils/parse.mjs — Generate legacySlug for every heading during metadata parsing.
  • src/generators/legacy-html/utils/buildContent.mjs — Render a hidden legacy anchor <a> element in the legacy HTML output.
  • src/generators/jsx-ast/utils/buildContent.mjs — Render a hidden legacy anchor <a> element in the JSX/AST (web) output.
  • src/generators/metadata/utils/__tests__/slugger.test.mjs — Added tests for getLegacySlug and createNodeSlugger.legacySlug.

Validation

  • All 349 existing tests pass (npm test).
  • Lint and formatting checks pass (npm run lint, npm run format:check).
  • Locally generated HTML output for a sample fs.md file confirms both anchors are present:
    • <a id=fs_file_system></a> (legacy) alongside <a class=mark id=file-system> (modern)
    • <a id=fs_fs_readfile_path></a> (legacy) alongside <a class=mark id=fsreadfilepath> (modern)

Related Issues

Fixes #697

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run node --run test and all tests passed.
  • I have check code formatting with node --run format & node --run lint.
  • I've covered new added functionality with unit tests if necessary.

@shivxmsharma shivxmsharma requested a review from a team as a code owner March 23, 2026 08:48
@vercel
Copy link

vercel bot commented Mar 23, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
api-docs-tooling Ready Ready Preview Mar 25, 2026 6:35pm

Request Review

@codecov
Copy link

codecov bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 87.73585% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.51%. Comparing base (d9697c7) to head (8e5017f).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/generators/legacy-html/utils/buildContent.mjs 0.00% 13 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #699      +/-   ##
==========================================
+ Coverage   75.44%   75.51%   +0.07%     
==========================================
  Files         148      150       +2     
  Lines       13572    13690     +118     
  Branches     1023     1035      +12     
==========================================
+ Hits        10239    10338      +99     
- Misses       3328     3347      +19     
  Partials        5        5              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any and all legacy slug generation should go in the legacy generator, not the metadata generator

Comment on lines +137 to +138
// Legacy anchor alias to preserve old external links
createElement('a', { id: legacySlug }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this in the new generator

@shivxmsharma
Copy link
Contributor Author

@avivkeller I've updated the PR to remove the legacy logic from the metadata/jsx-ast generators completely. All the legacy slug generation and its tests are now strictly isolated within the legacy-html generator as requested.

Let me know if there's anything else that needs adjusting!

Comment on lines +233 to +242
// Legacy slug counter tracking for deduplication
const legacyIdCounters = { __proto__: null };
/** Deduplicates legacy slugs by appending an incremented counter */
const legacyDeduplicateFn = id => {
if (legacyIdCounters[id] !== undefined) {
return `${id}_${++legacyIdCounters[id]}`;
}
legacyIdCounters[id] = 0;
return id;
};
Copy link
Member

@avivkeller avivkeller Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Legacy slug counter tracking for deduplication
const legacyIdCounters = { __proto__: null };
/** Deduplicates legacy slugs by appending an incremented counter */
const legacyDeduplicateFn = id => {
if (legacyIdCounters[id] !== undefined) {
return `${id}_${++legacyIdCounters[id]}`;
}
legacyIdCounters[id] = 0;
return id;
};
/** Deduplicates legacy slugs by appending an incremented counter */
const legacyDeduplicator = (counters = {}) => id => {
counters[id] ??= 0;
return `${id}_${counters[id]++}`;
};

and move this into slugger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored this and moved the deduplicator logic entirely into createLegacySlugger() inside slugger.mjs, so that it tracks its own state automatically. Let me know what you think!

Comment on lines +21 to +27
const deduplicate = id => {
if (legacyIdCounters[id] !== undefined) {
return `${id}_${++legacyIdCounters[id]}`;
}
legacyIdCounters[id] = 0;
return id;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the implementation I showed you for this inner function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented your closure approach! I just made one tiny tweak: returning just id (instead of id_0) on the first pass so that the legacy backward compatibility remains 100% perfectly intact. It's updated and pushed up!

// Parses the Heading nodes into Heading elements
visit(content, UNIST.isHeading, buildHeading);
visit(content, UNIST.isHeading, (node, index, parent) =>
buildHeading(node, index, parent, entry.api, legacySlugger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create the slug here and pass it to the function, rather than passing the slug creator only to be used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated it to generate the slug inside the visit loop and pass the string directly into buildHeading. Pushed the update!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restore the anchor alias to preserve old links

2 participants