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
5 changes: 5 additions & 0 deletions .changeset/legacy-hyphen-anchors.md
Original file line number Diff line number Diff line change
@@ -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`).
3 changes: 0 additions & 3 deletions packages/core/src/generators/metadata/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: /(?<!^-*)-+$/g, to: '' }, // Remove any trailing hyphens
{ from: /^(?!-)(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens
];

// Matches Node.js deprecation headings such as "DEP0001: Description".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,36 @@ describe('slug', () => {
});
});

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'
);
});
});

Expand Down
Loading