diff --git a/.changeset/legacy-hyphen-anchors.md b/.changeset/legacy-hyphen-anchors.md new file mode 100644 index 000000000..bb9b37036 --- /dev/null +++ b/.changeset/legacy-hyphen-anchors.md @@ -0,0 +1,5 @@ +--- +'@doc-kit/core': patch +--- + +Keep leading, trailing and repeated hyphens in heading slugs, so anchors match the old Node.js doc tooling again (`#-c---check` instead of `#-check`). diff --git a/packages/core/src/generators/metadata/constants.mjs b/packages/core/src/generators/metadata/constants.mjs index 7e7541472..2143396b4 100644 --- a/packages/core/src/generators/metadata/constants.mjs +++ b/packages/core/src/generators/metadata/constants.mjs @@ -7,9 +7,6 @@ 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: '' }, // Remove a single leading hyphen - { from: /(? { }); }); - describe('leading hyphen removal', () => { - it('removes a single leading hyphen', () => { - assert.strictEqual(slug('-foo', identity), 'foo'); - }); - - it('preserves multiple leading hyphens', () => { + describe('hyphens', () => { + it('preserves leading hyphens', () => { + assert.strictEqual(slug('-foo', identity), '-foo'); assert.strictEqual(slug('--permission', identity), '--permission'); }); - it('preserves an all-hyphen string', () => { - assert.strictEqual(slug('---', identity), '---'); - }); - }); - - describe('trailing hyphen removal', () => { - it('removes a single trailing hyphen', () => { - assert.strictEqual(slug('foo-', identity), 'foo'); - }); - - it('removes multiple trailing hyphens', () => { - assert.strictEqual(slug('foo--', identity), 'foo'); + it('preserves trailing hyphens', () => { + assert.strictEqual(slug('foo--', identity), 'foo--'); }); - }); - describe('consecutive hyphen replacement', () => { - it('replaces from start of string up to and including double-hyphen with a single hyphen', () => { - assert.strictEqual(slug('foo--bar', identity), '-bar'); + it('preserves consecutive hyphens', () => { + assert.strictEqual(slug('foo--bar', identity), 'foo--bar'); }); - it('does not fire on an all-hyphen string', () => { + it('preserves an all-hyphen string', () => { assert.strictEqual(slug('---', identity), '---'); }); + }); - it('does not collapse leading double hyphen flags', () => { - assert.strictEqual(slug('--allow-fs-read', identity), '--allow-fs-read'); + describe('legacy Node.js anchors', () => { + it('matches the anchors of the old doc tooling', () => { + assert.strictEqual(slug('`-c`, `--check`'), '-c---check'); + assert.strictEqual( + slug('DEP0147: `fs.rmdir(path, { recursive: true })`'), + 'dep0147-fsrmdirpath--recursive-true-' + ); + assert.strictEqual( + slug('Stability & Deprecation'), + 'stability--deprecation' + ); }); });