Skip to content

Commit 079de38

Browse files
committed
fix(metadata): preserve -- prefix in anchor IDs for CLI flags
Two regexes in DOC_API_SLUGS_REPLACEMENTS were stripping -- from the start of slugs, turning --permission into permission and breaking existing links. Fixes #757
1 parent 2765300 commit 079de38

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/generators/metadata/constants.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export const DOC_API_SLUGS_REPLACEMENTS = [
77
{ from: /node.js/i, to: 'nodejs' }, // Replace Node.js
88
{ from: /&/, to: '-and-' }, // Replace &
99
{ from: /[/,:;\\ ]/g, to: '-' }, // Replace /,:;\. and whitespace
10-
{ from: /^-+(?!-*$)/g, to: '' }, // Remove any leading hyphens
10+
{ from: /^-(?=[^-])/g, to: '' }, // Remove a single leading hyphen (preserves -- prefix for CLI flags)
1111
{ from: /(?<!^-*)-+$/g, to: '' }, // Remove any trailing hyphens
12-
{ from: /^(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens
12+
{ from: /^(?!-+$)[^-].*?(--+)/g, to: '-' }, // Replace multiple consecutive hyphens (not at start)
1313
];
1414

1515
// These are regular expressions used to determine if a given Markdown heading

src/generators/metadata/utils/__tests__/slugger.test.mjs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe('slug', () => {
5151
assert.strictEqual(slug('-foo', identity), 'foo');
5252
});
5353

54-
it('removes multiple leading hyphens', () => {
55-
assert.strictEqual(slug('--foo', identity), 'foo');
54+
it('preserves double leading hyphens (CLI flag prefix)', () => {
55+
assert.strictEqual(slug('--foo', identity), '--foo');
5656
});
5757

5858
it('preserves an all-hyphen string', () => {
@@ -80,6 +80,16 @@ describe('slug', () => {
8080
});
8181
});
8282

83+
describe('cli flag anchor preservation', () => {
84+
it('preserves -- prefix for CLI flags', () => {
85+
assert.strictEqual(slug('--permission', identity), '--permission');
86+
});
87+
88+
it('preserves -- prefix for multi-word CLI flags', () => {
89+
assert.strictEqual(slug('--allow-fs-read', identity), '--allow-fs-read');
90+
});
91+
});
92+
8393
describe('integration with github-slugger', () => {
8494
it('lowercases and hyphenates a plain title', () => {
8595
assert.strictEqual(slug('Hello World'), 'hello-world');

0 commit comments

Comments
 (0)