Skip to content

Commit c204205

Browse files
committed
Added ability for mdxJSX elements to not be stripped out, and also added the ability for attributes whitelisted on those elements to not be stripped
1 parent 7f30d9e commit c204205

File tree

1 file changed

+84
-4
lines changed

1 file changed

+84
-4
lines changed

lib/index.js

+84-4
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,62 @@ function transform(state, node) {
288288
return text(state, unsafe)
289289
}
290290

291+
case 'mdxJsxFlowElement': {
292+
// Map the attributes array into name/value pairs
293+
const attributesMap = {}
294+
if (Array.isArray(unsafe.attributes)) {
295+
unsafe.attributes.forEach((attr) => {
296+
if (
297+
attr &&
298+
typeof attr === 'object' &&
299+
'name' in attr &&
300+
'value' in attr
301+
) {
302+
attributesMap[attr.name] = attr.value
303+
}
304+
})
305+
}
306+
307+
const mdxProps = {
308+
...unsafe,
309+
tagName: unsafe.name,
310+
properties: {
311+
...unsafe.properties,
312+
...attributesMap // Spread the mapped attributes directly into properties
313+
}
314+
}
315+
316+
return element(state, mdxProps)
317+
}
318+
319+
case 'mdxJsxTextElement': {
320+
// Handle text elements the same way
321+
const attributesMap = {}
322+
if (Array.isArray(unsafe.attributes)) {
323+
unsafe.attributes.forEach((attr) => {
324+
if (
325+
attr &&
326+
typeof attr === 'object' &&
327+
'name' in attr &&
328+
'value' in attr
329+
) {
330+
attributesMap[attr.name] = attr.value
331+
}
332+
})
333+
}
334+
335+
const mdxProps = {
336+
...unsafe,
337+
tagName: unsafe.name,
338+
properties: {
339+
...unsafe.properties,
340+
...attributesMap
341+
}
342+
}
343+
344+
return element(state, mdxProps)
345+
}
346+
291347
default:
292348
}
293349
}
@@ -503,15 +559,18 @@ function properties(state, properties) {
503559
: undefined
504560
const defaults =
505561
attributes && own.call(attributes, '*') ? attributes['*'] : undefined
562+
563+
// Handle both traditional properties and MDX attributes
506564
const properties_ =
507-
/** @type {Readonly<Record<string, Readonly<unknown>>>} */ (
508-
properties && typeof properties === 'object' ? properties : {}
509-
)
565+
properties && typeof properties === 'object' ? properties : {}
566+
const mdxAttributes = properties_?.attributes || {} // Add support for MDX attributes
567+
510568
/** @type {Properties} */
511569
const result = {}
512570
/** @type {string} */
513571
let key
514572

573+
// Process traditional properties
515574
for (key in properties_) {
516575
if (own.call(properties_, key)) {
517576
const unsafe = properties_[key]
@@ -532,9 +591,30 @@ function properties(state, properties) {
532591
}
533592
}
534593

594+
// Process MDX attributes
595+
for (key in mdxAttributes) {
596+
if (own.call(mdxAttributes, key)) {
597+
const unsafe = mdxAttributes[key]
598+
let safe = propertyValue(
599+
state,
600+
findDefinition(specific, key),
601+
key,
602+
unsafe
603+
)
604+
605+
if (safe === null || safe === undefined) {
606+
safe = propertyValue(state, findDefinition(defaults, key), key, unsafe)
607+
}
608+
609+
if (safe !== null && safe !== undefined) {
610+
result[key] = safe
611+
}
612+
}
613+
}
614+
615+
// Handle required properties
535616
if (required && own.call(required, tagName)) {
536617
const properties = required[tagName]
537-
538618
for (key in properties) {
539619
if (own.call(properties, key) && !own.call(result, key)) {
540620
result[key] = properties[key]

0 commit comments

Comments
 (0)