2
2
3
3
import { u as createTree } from 'unist-builder' ;
4
4
import { valueToEstree } from 'estree-util-value-to-estree' ;
5
+ import { AST_NODES } from '../constants.mjs' ;
5
6
6
7
/**
7
8
* Creates an MDX JSX element with support for complex attribute values.
@@ -18,54 +19,59 @@ export const createJSXElement = (
18
19
name ,
19
20
{ inline = true , children = [ ] , ...attributes } = { }
20
21
) => {
21
- // Process children: convert string to text node or use array as is
22
+ // Convert string children to text node or use array directly
22
23
const processedChildren =
23
24
typeof children === 'string'
24
25
? [ createTree ( 'text' , { value : children } ) ]
25
- : ( children ?? [ ] ) ;
26
+ : children ;
27
+
28
+ const elementType = inline
29
+ ? AST_NODES . MDX . JSX_INLINE_ELEMENT
30
+ : AST_NODES . MDX . JSX_BLOCK_ELEMENT ;
26
31
27
- // Create attribute nodes, handling complex objects and primitive values differently
28
32
const attrs = Object . entries ( attributes ) . map ( ( [ key , value ] ) =>
29
33
createAttributeNode ( key , value )
30
34
) ;
31
35
32
- // Create and return the appropriate JSX element type
33
- return createTree ( inline ? 'mdxJsxTextElement' : 'mdxJsxFlowElement' , {
36
+ return createTree ( elementType , {
34
37
name,
35
38
attributes : attrs ,
36
39
children : processedChildren ,
37
40
} ) ;
38
41
} ;
39
42
40
43
/**
41
- * Creates an MDX JSX attribute node from the input .
44
+ * Creates an MDX JSX attribute node based on the value type .
42
45
*
43
46
* @param {string } name - The attribute name
44
- * @param {any } value - The attribute value (can be any valid JS value)
47
+ * @param {any } value - The attribute value
45
48
* @returns {import('unist').Node } The MDX JSX attribute node
46
49
*/
47
50
function createAttributeNode ( name , value ) {
48
- // For objects and arrays, create expression nodes to preserve structure
51
+ // Use expression for objects and arrays
49
52
if ( value !== null && typeof value === 'object' ) {
50
- return createTree ( 'mdxJsxAttribute' , {
53
+ return createTree ( AST_NODES . MDX . JSX_ATTRIBUTE , {
51
54
name,
52
- value : createTree ( 'mdxJsxAttributeValueExpression' , {
55
+ value : createTree ( AST_NODES . MDX . JSX_ATTRIBUTE_EXPRESSION , {
53
56
data : {
54
57
estree : {
55
- type : 'Program' ,
58
+ type : AST_NODES . ESTREE . PROGRAM ,
56
59
body : [
57
60
{
58
- type : 'ExpressionStatement' ,
61
+ type : AST_NODES . ESTREE . EXPRESSION_STATEMENT ,
59
62
expression : valueToEstree ( value ) ,
60
63
} ,
61
64
] ,
62
- sourceType : 'module' ,
63
65
} ,
64
66
} ,
65
67
} ) ,
66
68
} ) ;
67
69
}
68
70
69
- // For primitives, use simple string conversion
70
- return createTree ( 'mdxJsxAttribute' , { name, value : String ( value ) } ) ;
71
+ // For primitives, use simple string conversion.
72
+ // If undefined, pass nothing.
73
+ return createTree ( AST_NODES . MDX . JSX_ATTRIBUTE , {
74
+ name,
75
+ value : value === undefined ? value : String ( value ) ,
76
+ } ) ;
71
77
}
0 commit comments