Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/generators/metadata/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const IGNORE_STABILITY_STEMS = ['documentation'];
export const DOC_API_SLUGS_REPLACEMENTS = [
{ from: /node.js/i, to: 'nodejs' }, // Replace Node.js
{ from: /&/, to: '-and-' }, // Replace &
{ from: /[/_,:;\\ ]/g, to: '-' }, // Replace /_,:;\. and whitespace
{ from: /[/,:;\\ ]/g, to: '-' }, // Replace /,:;\ and whitespace (underscores are preserved)
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.

Suggested change
{ from: /[/,:;\\ ]/g, to: '-' }, // Replace /,:;\ and whitespace (underscores are preserved)
{ from: /[/,:;\\ ]/g, to: '-' }, // Replace /,:;\ and whitespace.

{ from: /^-+(?!-*$)/g, to: '' }, // Remove any leading hyphens
{ from: /(?<!^-*)-+$/g, to: '' }, // Remove any trailing hyphens
{ from: /^(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens
Expand Down
34 changes: 30 additions & 4 deletions src/generators/metadata/utils/__tests__/slugger.test.mjs
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.

Can you remove redundant tests?

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('slug', () => {
});

describe('special character to hyphen replacement', () => {
it('replaces underscores with hyphens', () => {
assert.strictEqual(slug('foo_bar', identity), 'foo-bar');
it('preserves underscores (does not replace with hyphens)', () => {
assert.strictEqual(slug('foo_bar', identity), 'foo_bar');
});
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 breaks existing behavior, are we 100% sure no existing links break post this PR

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The fix was originally targeted at __dirname / __filename where the old behavior caused a two-step destruction: __dirname → --dirname → dirname (leading hyphens stripped), making the anchor completely unreachable.
However, you're correct that removing _ from the replacement entirely is too broad — names like child_process previously generated the slug child-process, and if existing documentation already links to #child-process, those links would silently break after this change.I'd like to propose a more targeted fix instead of removing _ from the replacement globally, we could handle the specific case where leading underscores get converted to leading hyphens and then stripped. For example, only preserve underscores that are at the start of the identifier.
I'll update the PR with a narrower approach and verify against existing cross-references in the docs before pushing. Thanks for the review!


it('replaces forward slashes with hyphens', () => {
Expand All @@ -46,6 +46,24 @@ describe('slug', () => {
});
});

describe('underscore preservation (anchor link compatibility)', () => {
it('preserves leading underscores so __dirname slug matches #__dirname anchor', () => {
assert.strictEqual(slug('__dirname', identity), '__dirname');
});

it('preserves leading underscores so __filename slug matches #__filename anchor', () => {
assert.strictEqual(slug('__filename', identity), '__filename');
});

it('preserves underscores within names', () => {
assert.strictEqual(slug('child_process', identity), 'child_process');
});

it('preserves mixed underscores and other characters', () => {
assert.strictEqual(slug('foo_bar:baz', identity), 'foo_bar-baz');
});
});

describe('leading hyphen removal', () => {
it('removes a single leading hyphen', () => {
assert.strictEqual(slug('-foo', identity), 'foo');
Expand Down Expand Up @@ -85,13 +103,21 @@ describe('slug', () => {
assert.strictEqual(slug('Hello World'), 'hello-world');
});

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

it('handles titles with no special characters', () => {
assert.strictEqual(slug('stability index'), 'stability-index');
});

it('generates correct slug for __dirname', () => {
assert.strictEqual(slug('__dirname'), '__dirname');
});

it('generates correct slug for __filename', () => {
assert.strictEqual(slug('__filename'), '__filename');
});
});
});

Expand Down
Loading