Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/generators/metadata/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ 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
{ from: /(?<=[^_])_+/g, to: '-' }, // Replace internal/trailing underscores with a hyphen
{ from: /^-+(?!-*$)/g, to: '' }, // Remove any leading hyphens
{ from: /(?<!^-*)-+$/g, to: '' }, // Remove any trailing hyphens
{ from: /^(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens
Expand Down
22 changes: 20 additions & 2 deletions src/generators/metadata/utils/__tests__/slugger.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('slug', () => {
});

describe('special character to hyphen replacement', () => {
it('replaces underscores with hyphens', () => {
it('replaces underscores with hyphens (except leading)', () => {
assert.strictEqual(slug('foo_bar', identity), 'foo-bar');
});

Expand All @@ -46,6 +46,16 @@ 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');
});
});

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

it('converts underscored names to hyphenated slugs', () => {
it('converts internal underscored names to hyphenated slugs', () => {
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 (preserves leading underscores)', () => {
assert.strictEqual(slug('__dirname'), '__dirname');
});

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

Expand Down
Loading