Skip to content

Commit 555a61e

Browse files
feat(web-generator): update deprecation alertbox
1 parent b2181a2 commit 555a61e

File tree

2 files changed

+108
-9
lines changed

2 files changed

+108
-9
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import { transformHeadingNode } from '../buildContent.mjs';
5+
6+
describe('transformHeadingNode (deprecation Type -> AlertBox level)', () => {
7+
const makeHeading = () => ({
8+
type: 'heading',
9+
depth: 3,
10+
data: { type: 'misc', slug: 's', text: 'Heading' },
11+
children: [{ type: 'text', value: 'Heading' }],
12+
});
13+
14+
const makeParent = typeText => ({
15+
children: [
16+
makeHeading(),
17+
{
18+
type: 'paragraph',
19+
children: [{ type: 'text', value: `Type: ${typeText}` }],
20+
},
21+
],
22+
});
23+
24+
it('maps documentation/compilation to info', () => {
25+
const entry = { api: 'deprecations' };
26+
const parent = makeParent('Documentation');
27+
const node = parent.children[0];
28+
29+
transformHeadingNode(entry, {}, node, 0, parent);
30+
31+
const alert = parent.children[1];
32+
assert.equal(alert.name, 'AlertBox');
33+
const levelAttr = alert.attributes.find(a => a.name === 'level');
34+
assert.ok(levelAttr);
35+
assert.equal(levelAttr.value, 'info');
36+
});
37+
38+
it('maps runtime/application to warning', () => {
39+
const entry = { api: 'deprecations' };
40+
const parent = makeParent('Runtime');
41+
const node = parent.children[0];
42+
43+
transformHeadingNode(entry, {}, node, 0, parent);
44+
45+
const alert = parent.children[1];
46+
assert.equal(alert.name, 'AlertBox');
47+
const levelAttr = alert.attributes.find(a => a.name === 'level');
48+
assert.ok(levelAttr);
49+
assert.equal(levelAttr.value, 'warning');
50+
});
51+
52+
it('falls back to danger for unknown types', () => {
53+
const entry = { api: 'deprecations' };
54+
const parent = makeParent('SomeOtherThing');
55+
const node = parent.children[0];
56+
57+
transformHeadingNode(entry, {}, node, 0, parent);
58+
59+
const alert = parent.children[1];
60+
assert.equal(alert.name, 'AlertBox');
61+
const levelAttr = alert.attributes.find(a => a.name === 'level');
62+
assert.ok(levelAttr);
63+
assert.equal(levelAttr.value, 'danger');
64+
});
65+
});

src/generators/jsx-ast/utils/buildContent.mjs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,50 @@ export const transformHeadingNode = (entry, remark, node, index, parent) => {
180180

181181
if (entry.api === 'deprecations' && node.depth === 3) {
182182
// On the 'deprecations.md' page, "Type: <XYZ>" turns into an AlertBox
183-
parent.children[index + 1] = createJSXElement(JSX_IMPORTS.AlertBox.name, {
184-
children: slice(
185-
parent.children[index + 1],
186-
TYPE_PREFIX_LENGTH,
187-
undefined,
188-
{
189-
textHandling: { boundaries: 'preserve' },
183+
// Extract the nodes representing the type text
184+
const sliced = slice(
185+
parent.children[index + 1],
186+
TYPE_PREFIX_LENGTH,
187+
undefined,
188+
{ textHandling: { boundaries: 'preserve' } }
189+
).node.children;
190+
191+
// Derive plain text for type matching
192+
const typeText = sliced
193+
.map(n => {
194+
if (n.type === 'text') {
195+
return n.value;
196+
}
197+
if (n.type === 'inlineCode') {
198+
return n.value;
199+
}
200+
if (n.type === 'link' && Array.isArray(n.children)) {
201+
return n.children.map(c => c.value || '').join('');
190202
}
191-
).node.children,
192-
level: 'danger',
203+
return '';
204+
})
205+
.join('')
206+
.trim()
207+
.toLowerCase();
208+
209+
// Map user-facing deprecation types to AlertBox levels
210+
// documentation / compilation -> blue (`info`)
211+
// runtime / application -> orange (`warning`)
212+
// fallback -> danger (red)
213+
let level = 'danger';
214+
215+
if (typeText.includes('documentation') || typeText.includes('compil')) {
216+
level = 'info';
217+
} else if (
218+
typeText.includes('runtime') ||
219+
typeText.includes('application')
220+
) {
221+
level = 'warning';
222+
}
223+
224+
parent.children[index + 1] = createJSXElement(JSX_IMPORTS.AlertBox.name, {
225+
children: sliced,
226+
level,
193227
title: 'Type',
194228
});
195229
}

0 commit comments

Comments
 (0)