Skip to content

Commit 36e28ff

Browse files
committed
fix(metadata): keep hyphens in heading slugs like the old doc tooling
#610 replaced the `--+` collapse with `/^(?!-)(?!-+$).*?(--+)/`, which also swallows everything before the first run of hyphens, so headings lost their prefix: `-c`, `--check` became `#-check` and `DEP0062: node --debug` became `#-debug`. nodejs.org/api serves those anchors today. The old tooling (getId in tools/doc/html.mjs) never collapsed or trimmed hyphens: it lowercased, dropped punctuation and turned spaces into hyphens, which is what github-slugger already does. Dropping the three hyphen rules matches its anchors for all 5809 headings in doc/api, where the current rules differ on 18 of them. That's the same drift #757 reported for `--` flags, which #810 fixed for that one case. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7e6bf44 commit 36e28ff

3 files changed

Lines changed: 25 additions & 28 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@doc-kit/core': patch
3+
---
4+
5+
Keep leading, trailing and repeated hyphens in heading slugs, so anchors match the old Node.js doc tooling again (`#-c---check` instead of `#-check`).

‎packages/core/src/generators/metadata/constants.mjs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ export const DOC_API_SLUGS_REPLACEMENTS = [
77
{ from: /node.js/i, to: 'nodejs' }, // Replace Node.js
88
{ from: /&/, to: '-and-' }, // Replace &
99
{ from: /[/,:;\\ ]/g, to: '-' }, // Replace /,:;\. and whitespace
10-
{ from: /^-(?!-|$)/g, to: '' }, // Remove a single leading hyphen
11-
{ from: /(?<!^-*)-+$/g, to: '' }, // Remove any trailing hyphens
12-
{ from: /^(?!-)(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens
1310
];
1411

1512
// Matches Node.js deprecation headings such as "DEP0001: Description".

‎packages/core/src/generators/metadata/utils/__tests__/slugger.test.mjs‎

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,36 @@ describe('slug', () => {
4646
});
4747
});
4848

49-
describe('leading hyphen removal', () => {
50-
it('removes a single leading hyphen', () => {
51-
assert.strictEqual(slug('-foo', identity), 'foo');
52-
});
53-
54-
it('preserves multiple leading hyphens', () => {
49+
describe('hyphens', () => {
50+
it('preserves leading hyphens', () => {
51+
assert.strictEqual(slug('-foo', identity), '-foo');
5552
assert.strictEqual(slug('--permission', identity), '--permission');
5653
});
5754

58-
it('preserves an all-hyphen string', () => {
59-
assert.strictEqual(slug('---', identity), '---');
60-
});
61-
});
62-
63-
describe('trailing hyphen removal', () => {
64-
it('removes a single trailing hyphen', () => {
65-
assert.strictEqual(slug('foo-', identity), 'foo');
66-
});
67-
68-
it('removes multiple trailing hyphens', () => {
69-
assert.strictEqual(slug('foo--', identity), 'foo');
55+
it('preserves trailing hyphens', () => {
56+
assert.strictEqual(slug('foo--', identity), 'foo--');
7057
});
71-
});
7258

73-
describe('consecutive hyphen replacement', () => {
74-
it('replaces from start of string up to and including double-hyphen with a single hyphen', () => {
75-
assert.strictEqual(slug('foo--bar', identity), '-bar');
59+
it('preserves consecutive hyphens', () => {
60+
assert.strictEqual(slug('foo--bar', identity), 'foo--bar');
7661
});
7762

78-
it('does not fire on an all-hyphen string', () => {
63+
it('preserves an all-hyphen string', () => {
7964
assert.strictEqual(slug('---', identity), '---');
8065
});
66+
});
8167

82-
it('does not collapse leading double hyphen flags', () => {
83-
assert.strictEqual(slug('--allow-fs-read', identity), '--allow-fs-read');
68+
describe('legacy Node.js anchors', () => {
69+
it('matches the anchors of the old doc tooling', () => {
70+
assert.strictEqual(slug('`-c`, `--check`'), '-c---check');
71+
assert.strictEqual(
72+
slug('DEP0147: `fs.rmdir(path, { recursive: true })`'),
73+
'dep0147-fsrmdirpath--recursive-true-'
74+
);
75+
assert.strictEqual(
76+
slug('Stability & Deprecation'),
77+
'stability--deprecation'
78+
);
8479
});
8580
});
8681

0 commit comments

Comments
 (0)