Skip to content

Commit 0d8152e

Browse files
committed
fix: preserve underscores in slug generation for anchor link compatibility
- Remove underscore (_) from the special-character replacement regex in DOC_API_SLUGS_REPLACEMENTS so that names like __dirname and __filename keep their underscores in generated IDs. Previously: __dirname → --dirname → dirname (broken anchor #__dirname) Now: __dirname → __dirname (matches anchor #__dirname correctly) - Update slugger tests to reflect new behavior - Add new 'underscore preservation' test suite covering: - __dirname - __filename - child_process (internal underscores) - mixed underscore + other special chars Fixes: broken anchor links for identifiers with leading/internal underscores
1 parent 27de0e7 commit 0d8152e

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

src/generators/metadata/constants.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const IGNORE_STABILITY_STEMS = ['documentation'];
66
export const DOC_API_SLUGS_REPLACEMENTS = [
77
{ from: /node.js/i, to: 'nodejs' }, // Replace Node.js
88
{ from: /&/, to: '-and-' }, // Replace &
9-
{ from: /[/_,:;\\ ]/g, to: '-' }, // Replace /_,:;\. and whitespace
9+
{ from: /[/,:;\\ ]/g, to: '-' }, // Replace /,:;\ and whitespace (underscores are preserved)
1010
{ from: /^-+(?!-*$)/g, to: '' }, // Remove any leading hyphens
1111
{ from: /(?<!^-*)-+$/g, to: '' }, // Remove any trailing hyphens
1212
{ from: /^(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ describe('slug', () => {
2525
});
2626

2727
describe('special character to hyphen replacement', () => {
28-
it('replaces underscores with hyphens', () => {
29-
assert.strictEqual(slug('foo_bar', identity), 'foo-bar');
28+
it('preserves underscores (does not replace with hyphens)', () => {
29+
assert.strictEqual(slug('foo_bar', identity), 'foo_bar');
3030
});
3131

3232
it('replaces forward slashes with hyphens', () => {
@@ -46,6 +46,24 @@ describe('slug', () => {
4646
});
4747
});
4848

49+
describe('underscore preservation (anchor link compatibility)', () => {
50+
it('preserves leading underscores so __dirname slug matches #__dirname anchor', () => {
51+
assert.strictEqual(slug('__dirname', identity), '__dirname');
52+
});
53+
54+
it('preserves leading underscores so __filename slug matches #__filename anchor', () => {
55+
assert.strictEqual(slug('__filename', identity), '__filename');
56+
});
57+
58+
it('preserves underscores within names', () => {
59+
assert.strictEqual(slug('child_process', identity), 'child_process');
60+
});
61+
62+
it('preserves mixed underscores and other characters', () => {
63+
assert.strictEqual(slug('foo_bar:baz', identity), 'foo_bar-baz');
64+
});
65+
});
66+
4967
describe('leading hyphen removal', () => {
5068
it('removes a single leading hyphen', () => {
5169
assert.strictEqual(slug('-foo', identity), 'foo');
@@ -85,13 +103,21 @@ describe('slug', () => {
85103
assert.strictEqual(slug('Hello World'), 'hello-world');
86104
});
87105

88-
it('converts underscored names to hyphenated slugs', () => {
89-
assert.strictEqual(slug('child_process'), 'child-process');
106+
it('preserves underscores in names (no hyphenation)', () => {
107+
assert.strictEqual(slug('child_process'), 'child_process');
90108
});
91109

92110
it('handles titles with no special characters', () => {
93111
assert.strictEqual(slug('stability index'), 'stability-index');
94112
});
113+
114+
it('generates correct slug for __dirname', () => {
115+
assert.strictEqual(slug('__dirname'), '__dirname');
116+
});
117+
118+
it('generates correct slug for __filename', () => {
119+
assert.strictEqual(slug('__filename'), '__filename');
120+
});
95121
});
96122
});
97123

0 commit comments

Comments
 (0)