Skip to content

Commit 02848c2

Browse files
update from feedback
Co-Authored-By: Aviv Keller <[email protected]>
1 parent 5b5f601 commit 02848c2

File tree

2 files changed

+55
-45
lines changed

2 files changed

+55
-45
lines changed

src/generators/jsx-ast/utils/__tests__/transformHeadingNode.test.mjs renamed to src/generators/jsx-ast/utils/__tests__/buildContent.test.mjs

File renamed without changes.

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

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,58 @@ export const transformStabilityNode = (node, index, parent) => {
163163
return [SKIP];
164164
};
165165

166+
/**
167+
* Maps deprecation type text to AlertBox level
168+
*
169+
* @param {string} typeText - The deprecation type text
170+
* @returns {string} The corresponding AlertBox level
171+
*/
172+
const getLevelFromDeprecationType = typeText => {
173+
const normalized = String(typeText || '')
174+
.trim()
175+
.toLowerCase()
176+
.replace(/[.,]/g, ' ')
177+
.split(/\s+/)
178+
.filter(Boolean);
179+
180+
if (
181+
normalized.includes('documentation') ||
182+
normalized.includes('compilation')
183+
) {
184+
return 'info';
185+
} else if (
186+
normalized.includes('runtime') ||
187+
normalized.includes('application')
188+
) {
189+
return 'warning';
190+
} else {
191+
return 'danger';
192+
}
193+
};
194+
195+
/**
196+
*
197+
* @param nodes
198+
*/
199+
const getTextValue = nodes =>
200+
(nodes || [])
201+
.map(node => {
202+
if (!node) {
203+
return '';
204+
}
205+
if (typeof node.value === 'string') {
206+
return node.value;
207+
}
208+
if (typeof node.alt === 'string') {
209+
return node.alt;
210+
}
211+
if (Array.isArray(node.children)) {
212+
return getTextValue(node.children);
213+
}
214+
return '';
215+
})
216+
.join('');
217+
166218
/**
167219
* Transforms a heading node by injecting metadata, source links, and signatures.
168220
* @param {ApiDocMetadataEntry} entry - The API metadata entry
@@ -187,54 +239,12 @@ export const transformHeadingNode = (entry, remark, node, index, parent) => {
187239
undefined,
188240
{ textHandling: { boundaries: 'preserve' } }
189241
).node.children;
190-
// Helper to recursively extract all text from a node and its children
191-
/**
192-
*
193-
* @param node
194-
*/
195-
function extractText(node) {
196-
if (!node) {
197-
return '';
198-
}
199-
if (typeof node.value === 'string') {
200-
return node.value;
201-
}
202-
if (Array.isArray(node.children)) {
203-
return node.children.map(extractText).join('');
204-
}
205-
return '';
206-
}
207-
208-
// Derive plain text for type matching
209-
const typeText = sliced.map(extractText).join('').trim().toLowerCase();
210-
211-
// Map user-facing deprecation types to AlertBox levels
212-
// documentation / compilation -> blue (`info`)
213-
// runtime / application -> orange (`warning`)
214-
// fallback -> danger (red)
215-
let level = 'danger';
216-
217-
// Use stricter matching to avoid false positives (e.g., "compilation" inside "End-of-Life")
218-
const normalizedTypeText = typeText
219-
.replace(/[.,]/g, ' ')
220-
.split(/\s+/)
221-
.filter(Boolean);
222-
223-
if (
224-
normalizedTypeText.includes('documentation') ||
225-
normalizedTypeText.includes('compilation')
226-
) {
227-
level = 'info';
228-
} else if (
229-
normalizedTypeText.includes('runtime') ||
230-
normalizedTypeText.includes('application')
231-
) {
232-
level = 'warning';
233-
}
242+
243+
const typeText = getTextValue(sliced);
234244

235245
parent.children[index + 1] = createJSXElement(JSX_IMPORTS.AlertBox.name, {
236246
children: sliced,
237-
level,
247+
level: getLevelFromDeprecationType(typeText),
238248
title: 'Type',
239249
});
240250
}

0 commit comments

Comments
 (0)