at main 1.3 kB view raw
1function renderNode(nodeName, attributes) { 2 let {children, ...attrs} = attributes 3 4 // It's a <text> node 5 if (nodeName === "text") { 6 // Join all children into one string 7 if (Array.isArray(children)) { 8 attrs["content"] = children.map(x => String(x)).join("") 9 children = [] 10 } else { 11 attrs["content"] = String(children) 12 children = [] 13 } 14 } else { 15 if (!children) { 16 children = [] 17 } else if (!Array.isArray(children)) { 18 children = [children] 19 } 20 } 21 22 return { 23 type: "node", 24 nodeName, 25 children, 26 attrs, 27 } 28} 29 30export function jsx(nodeName, attributes) { 31 // Set default weight 32 let { 33 weight, 34 size, 35 margin, 36 padding, 37 cornerRadius, 38 fontSize, 39 ...attrs 40 } = attributes 41 42 const style = { 43 weight: weight || 1, 44 size: size || 0, 45 margin: margin || 0, 46 padding: padding || 0, 47 cornerRadius: cornerRadius || 0, 48 fontSize: fontSize || 0, 49 } 50 51 // Text node 52 if (typeof nodeName === "string") { 53 return { 54 ...renderNode(nodeName, attrs), 55 style, 56 } 57 } 58 59 // Module node 60 return { 61 type: "module", 62 module: nodeName, 63 style, 64 attrs, 65 } 66} 67 68export function jsxs(nodeName, attributes) { 69 return jsx(nodeName, attributes) 70}