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/short-deprecation-links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-core/doc-kit': patch
---

Use short `DEP` codes for deprecation heading anchors.
4 changes: 3 additions & 1 deletion src/generators/legacy-html/utils/buildContent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const buildHeading = ({ data, children, depth }, index, parent, legacySlug) => {
// to be converted into Rehype nodes
...children,
// Legacy anchor alias to preserve old external links
createElement('span', createElement(`a#${legacySlug}`)),
...(legacySlug === data.slug
? []
: [createElement('span', createElement(`a#${legacySlug}`))]),
Comment on lines +32 to +34

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, no?

// Creates the element that references the link to the heading
// (The `#` anchor on the right of each Heading section)
createElement(
Expand Down
3 changes: 3 additions & 0 deletions src/generators/metadata/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const DOC_API_SLUGS_REPLACEMENTS = [
{ from: /^(?!-)(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens
];

// Matches Node.js deprecation headings such as "DEP0001: Description".
export const DEPRECATION_HEADING_REGEX = /^(DEP\d+):/;

// These are regular expressions used to determine if a given Markdown heading
// is a specific type of API Doc entry (e.g., Event, Class, Method, etc)
// and to extract the inner content of said Heading to be used as the API doc entry name
Expand Down
32 changes: 32 additions & 0 deletions src/generators/metadata/utils/__tests__/parse.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@ describe('parseApiDoc', () => {
});
});

describe('deprecation headings', () => {
it('uses the DEP code as the slug on the deprecations page', () => {
const tree = u('root', [
h('DEP0001: http.OutgoingMessage.prototype.flush'),
]);
const [entry] = parseApiDoc({ path: '/deprecations', tree }, typeMap);

assert.strictEqual(entry.heading.data.slug, 'DEP0001');
});

it('deduplicates repeated DEP code slugs', () => {
const tree = u('root', [
h('DEP0001: first deprecated API'),
h('DEP0001: second deprecated API'),
]);
const entries = parseApiDoc({ path: '/deprecations', tree }, typeMap);

assert.strictEqual(entries[0].heading.data.slug, 'DEP0001');
assert.strictEqual(entries[1].heading.data.slug, 'DEP0001-1');
});

it('uses normal slugs for DEP headings outside the deprecations page', () => {
const tree = u('root', [h('DEP0190: some section heading')]);
const [entry] = parseApiDoc({ path, tree }, typeMap);

assert.strictEqual(
entry.heading.data.slug,
'dep0190-some-section-heading'
);
});
});

describe('YAML metadata', () => {
it('extracts added_in', () => {
const tree = u('root', [h('fs'), yaml('added: v0.1.0')]);
Expand Down
21 changes: 19 additions & 2 deletions src/generators/metadata/utils/parse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
import { UNIST } from '../../../utils/queries/index.mjs';
import { getRemark as remark } from '../../../utils/remark.mjs';
import { relative } from '../../../utils/url.mjs';
import { IGNORE_STABILITY_STEMS } from '../constants.mjs';
import {
DEPRECATION_HEADING_REGEX,
IGNORE_STABILITY_STEMS,
} from '../constants.mjs';
import { resolveTypeAnnotations } from './resolveTypes.mjs';

/**
Expand All @@ -44,6 +47,20 @@ export const parseApiDoc = ({ path, tree, mdx = false }, typeMap) => {
// Slug the API (We use a non-class slugger, since we are fairly certain that `path` is unique)
const api = slug(path.slice(1).replace(sep, '-'));

/**
* Creates a stable slug for a heading in the current API document.
* @param {string} text Heading text to slug.
* @returns {string} The generated heading slug.
*/
const getHeadingSlug = text => {
const deprecationHeading =
api === 'deprecations' && DEPRECATION_HEADING_REGEX.exec(text);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
api === 'deprecations' && DEPRECATION_HEADING_REGEX.exec(text);
DEPRECATION_HEADING_REGEX.exec(text);


return deprecationHeading
? nodeSlugger.slug(deprecationHeading[1]).toUpperCase()
: nodeSlugger.slug(text);
Comment on lines +59 to +61

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does nodeSlugger need to run on deprecationHeading[1]? Isn't that our slug itself?

};

// Get all Markdown Footnote definitions from the tree
const markdownDefinitions = selectAll('definition', tree);

Expand Down Expand Up @@ -96,7 +113,7 @@ export const parseApiDoc = ({ path, tree, mdx = false }, typeMap) => {
});

// Generate slug and update heading data
metadata.heading.data.slug = nodeSlugger.slug(metadata.heading.data.text);
metadata.heading.data.slug = getHeadingSlug(metadata.heading.data.text);

// Find the next heading to determine section boundaries
const nextHeadingNode =
Expand Down