Skip to content

fix(metadata): preserve -- prefix in anchor IDs for CLI flags#758

Open
sujalgoel wants to merge 2 commits intonodejs:mainfrom
sujalgoel:fix/anchor-double-hyphen-prefix
Open

fix(metadata): preserve -- prefix in anchor IDs for CLI flags#758
sujalgoel wants to merge 2 commits intonodejs:mainfrom
sujalgoel:fix/anchor-double-hyphen-prefix

Conversation

@sujalgoel
Copy link
Copy Markdown
Contributor

Fixes #757.

In v25, CLI flag headings like --permission produce anchor #permission
instead of #--permission, breaking links that worked in v24.

Two regexes in DOC_API_SLUGS_REPLACEMENTS caused this. The leading
hyphen rule (^-+(?!-*$)) stripped all leading hyphens unconditionally.
The consecutive hyphen rule (^(?!-+$).*?(--+)) also fired on anything
starting with --.

Changes in constants.mjs:

  • Leading hyphen: ^-+(?!-*$) -> ^-(?=[^-]). Strips only a single
    - not followed by another, so --permission passes through.
  • Consecutive hyphens: added [^-] after the start anchor so the rule
    skips ---prefixed slugs.

Changes in slugger.test.mjs:

  • Updated --foo expectation from 'foo' to '--foo'
  • Added tests for --permission and --allow-fs-read

Test plan

  • node --run test
  • node --run lint
  • node --run format

Two regexes in DOC_API_SLUGS_REPLACEMENTS were stripping -- from the
start of slugs, turning --permission into permission and breaking
existing links.

Fixes nodejs#757
@sujalgoel sujalgoel requested a review from a team as a code owner April 9, 2026 05:54
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 9, 2026

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

Project Deployment Actions Updated (UTC)
api-docs-tooling Ready Ready Preview Apr 10, 2026 2:29pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 9, 2026

PR Summary

Low Risk
Low risk, localized change to slug-generation regexes, but it can affect existing anchor IDs/links for headings that start with hyphens.

Overview
Fixes anchor/slug generation so headings that represent CLI flags (e.g. --permission) keep their -- prefix instead of being stripped.

This updates the DOC_API_SLUGS_REPLACEMENTS regexes to only remove a single leading hyphen and to avoid the consecutive-hyphen rewrite at the start of ---prefixed slugs, and updates slugger tests to assert the new behavior.

Reviewed by Cursor Bugbot for commit a15edca. Bugbot is set up for automated code reviews on this repo. Configure here.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.43%. Comparing base (2765300) to head (a15edca).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #758   +/-   ##
=======================================
  Coverage   78.42%   78.43%           
=======================================
  Files         157      157           
  Lines       13959    13963    +4     
  Branches     1152     1152           
=======================================
+ Hits        10948    10952    +4     
  Misses       3006     3006           
  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
Copy Markdown
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.

Just make sure existing links aren't broken

Comment on lines +83 to +92
describe('cli flag anchor preservation', () => {
it('preserves -- prefix for CLI flags', () => {
assert.strictEqual(slug('--permission', identity), '--permission');
});

it('preserves -- prefix for multi-word CLI flags', () => {
assert.strictEqual(slug('--allow-fs-read', identity), '--allow-fs-read');
});
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Redundant

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed, thanks!

@sujalgoel
Copy link
Copy Markdown
Contributor Author

Just make sure existing links aren't broken

Should be fine. The docs never updated their links to match v25's output (the issue even calls this out), so #--permission style links were already broken... This puts the anchors back to where they were in v24

@regseb
Copy link
Copy Markdown

regseb commented Apr 10, 2026

On this preview page, the heading -C condition, --conditions=condition has the anchor -conditionscondition. The title is cut off at --.


You can use Link Checker to check links and anchors in Web pages or full Web sites. For example, with preview CLI:

Broken fragments:

{ from: /^-(?=[^-])/g, to: '' }, // Remove a single leading hyphen (preserves -- prefix for CLI flags)
{ from: /(?<!^-*)-+$/g, to: '' }, // Remove any trailing hyphens
{ from: /^(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens
{ from: /^(?!-+$)[^-].*?(--+)/g, to: '-' }, // Replace multiple consecutive hyphens (not at start)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This regex looks very odd, unless there is some magic going on, this is replacing all of [^-].*?(--+) with -?

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.

[v25] Anchors without -- prefix

4 participants