hastx
Version:
JSX Transform emitting Rehype syntax trees (HAST)
69 lines • 1.93 kB
JavaScript
export function jsx(type, props, key) {
if (typeof type === "string") {
let tagName = type;
let { children, ...properties } = props;
let className = properties.class ? { className: properties.class } : null;
if (tagName === "template") {
return {
type: "element",
tagName,
properties: { ...properties, ...className },
children: [],
content: {
type: "root",
children: read(children),
},
};
}
else {
return {
type: "element",
tagName,
properties: { ...properties, ...className },
children: read(children).filter((child) => child.type !== "doctype"),
};
}
}
else {
return {
type: "root",
children: read(type({ ...props, ...(key ? { key } : {}) })),
};
}
}
export const jsxs = jsx;
export const jsxDEV = jsx;
export function Fragment(props) {
let { children = [] } = props;
return {
type: "root",
children: read(children),
};
}
function read(children) {
switch (typeof children) {
case "undefined":
case "boolean":
return [];
case "number":
case "string":
return [{
type: "text",
value: String(children),
}];
default:
if (children === null) {
return [];
}
else if (Array.isArray(children)) {
return children.flatMap(read);
}
else if (children.type === "root") {
return children.children;
}
else {
return [children];
}
}
}
//# sourceMappingURL=jsx-runtime.js.map