Skip to content

Commit 8b865f6

Browse files
committed
fix: use DEP code as slug for deprecation headings
Headings in deprecations.md use the form 'DEP0001: some description'. The old tooling generated the DEP code (e.g., 'DEP0001') as the anchor ID for these headings, producing stable links like #DEP0190. The new tooling was generating long slugs from the full heading text, breaking existing external links. When processing the 'deprecations' API doc, detect headings that match the DEP#### pattern and use just the code as the slug rather than the full text-derived slug. Fixes: #750
1 parent 2680e84 commit 8b865f6

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

src/generators/metadata/constants.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,9 @@ export const DOC_API_HEADING_TYPES = [
5959
// This regex is used to match basic TypeScript generic types (e.g., Promise<string>)
6060
export const TYPE_GENERIC_REGEX = /^([^<]+)<([^>]+)>$/;
6161

62+
// This regex matches headings in the deprecations API doc (e.g., "DEP0001: some title")
63+
// and captures the deprecation code (e.g., "DEP0001") as the first group
64+
export const DEPRECATION_HEADING_REGEX = /^(DEP\d+):/;
65+
6266
// This is the base URL of the Man7 documentation
6367
export const DOC_MAN_BASE_URL = 'http://man7.org/linux/man-pages/man';

src/generators/metadata/utils/__tests__/parse.test.mjs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,43 @@ describe('parseApiDoc', () => {
222222
});
223223
});
224224

225+
describe('deprecation heading slugs', () => {
226+
it('uses the DEP code as slug for headings in the deprecations doc', () => {
227+
const tree = u('root', [
228+
h('DEP0001: `http.OutgoingMessage.prototype.flush`', 3),
229+
]);
230+
const [entry] = parseApiDoc({ path: '/deprecations', tree }, typeMap);
231+
232+
assert.strictEqual(entry.heading.data.slug, 'DEP0001');
233+
});
234+
235+
it('uses the DEP code as slug regardless of the heading text that follows', () => {
236+
const tree = u('root', [
237+
h(
238+
'DEP0190: spawning .bat and .cmd files with child_process.spawn() with shell option',
239+
3
240+
),
241+
]);
242+
const [entry] = parseApiDoc({ path: '/deprecations', tree }, typeMap);
243+
244+
assert.strictEqual(entry.heading.data.slug, 'DEP0190');
245+
});
246+
247+
it('does not use the DEP code shortcut for non-deprecations docs', () => {
248+
const tree = u('root', [h('DEP0190: some section heading', 3)]);
249+
const [entry] = parseApiDoc({ path, tree }, typeMap);
250+
251+
assert.notStrictEqual(entry.heading.data.slug, 'DEP0190');
252+
});
253+
254+
it('uses normal slug for non-DEP headings in the deprecations doc', () => {
255+
const tree = u('root', [h('List of deprecated APIs', 2)]);
256+
const [entry] = parseApiDoc({ path: '/deprecations', tree }, typeMap);
257+
258+
assert.strictEqual(entry.heading.data.slug, 'list-of-deprecated-apis');
259+
});
260+
});
261+
225262
describe('document without headings', () => {
226263
it('produces one entry for content with no headings', () => {
227264
const tree = u('root', [

src/generators/metadata/utils/parse.mjs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import {
2222
import { UNIST } from '../../../utils/queries/index.mjs';
2323
import { getRemark as remark } from '../../../utils/remark.mjs';
2424
import { relative } from '../../../utils/url.mjs';
25-
import { IGNORE_STABILITY_STEMS } from '../constants.mjs';
25+
import {
26+
DEPRECATION_HEADING_REGEX,
27+
IGNORE_STABILITY_STEMS,
28+
} from '../constants.mjs';
2629

2730
/**
2831
* This generator generates a flattened list of metadata entries from a API doc
@@ -87,8 +90,16 @@ export const parseApiDoc = ({ path, tree }, typeMap) => {
8790
heading: headingNode,
8891
});
8992

90-
// Generate slug and update heading data
91-
metadata.heading.data.slug = nodeSlugger.slug(metadata.heading.data.text);
93+
// Generate slug and update heading data.
94+
// For the deprecations API doc, headings like "DEP0001: some title" use
95+
// just the deprecation code (e.g., "DEP0001") as the anchor to preserve
96+
// compatibility with existing external links.
97+
const depMatch =
98+
api === 'deprecations' &&
99+
DEPRECATION_HEADING_REGEX.exec(metadata.heading.data.text);
100+
metadata.heading.data.slug = depMatch
101+
? depMatch[1]
102+
: nodeSlugger.slug(metadata.heading.data.text);
92103

93104
// Find the next heading to determine section boundaries
94105
const nextHeadingNode =

0 commit comments

Comments
 (0)