jsx-md
Version:
Generate markdown files with a React-like syntax.
49 lines • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createElement = void 0;
/**
* @packageDocumentation
* @internal
*/
const model_1 = require("./model");
function createElement(typeOrComponent, attributes, ...children) {
if (typeOrComponent === model_1.MdFragmentType) {
return createFragmentElement(children);
}
if (typeOrComponent === model_1.MdAwaitType) {
// Throws error if is it not exactly one children.
if (children.length !== 1) {
throw new TypeError(`Received ${children.length} promises, expected a single promise.`);
}
return createPromiseElement(children[0]);
}
if (typeof typeOrComponent === "function") {
return createComponentElement(typeOrComponent, attributes, children);
}
throw new TypeError(`Unsupported lower-case element '${typeOrComponent}' encountered, please make sure all your components start with an upper-case letter and are functions.`);
}
exports.createElement = createElement;
function createFragmentElement(children) {
return {
type: model_1.MdFragmentType,
props: {
children: children.flat(),
},
key: null,
};
}
function createPromiseElement(children) {
return {
type: model_1.MdAwaitType,
props: { children },
key: null,
};
}
function createComponentElement(component, attributes, children) {
return {
type: component,
props: Object.assign(Object.assign({}, (attributes !== null && attributes !== void 0 ? attributes : {})), { children: children.length <= 1 ? children[0] : children }),
key: null,
};
}
//# sourceMappingURL=createElement.js.map