@cf-wasm/og
Version:
Generate Open Graph Images dynamically from HTML/CSS without a browser.
49 lines (48 loc) • 1.7 kB
JavaScript
// src/html-to-react/parser.ts
import { convertHtmlToReact } from "@hedgedoc/html-to-react";
import { Fragment, createElement } from "react";
var props = (input) => {
const props2 = { ...input };
if ("children" in props2) {
const { children } = props2;
if (typeof children === "undefined" || children === null) {
delete props2.children;
} else if (typeof children === "string") {
props2.children = children;
} else if (Array.isArray(children)) {
const filtered = children.filter((child) => typeof child === "string" || Boolean(child)).map((child) => typeof child === "object" && child ? element(child) : child);
if (filtered.length === 0) {
delete props2.children;
} else if (filtered.length === 1 && typeof filtered[0] === "string") {
props2.children = filtered[0];
} else {
props2.children = filtered;
}
}
}
return props2;
};
var element = (element2) => createElement(element2.type, typeof element2.props === "object" && element2.props ? props(element2.props) : {});
var fragment = (children) => createElement(Fragment, { children });
var wrapper = (children) => {
if (children.length === 1 && typeof children[0] === "object" && children[0]) {
return element({ ...children[0], key: null });
}
return element(fragment(children));
};
var htmlToReact = (html, options) => {
if (typeof html !== "string") {
throw new TypeError("Argument 1 must be of type string");
}
if (html.trim().length === 0) {
throw new TypeError("Blank html string cannot be parsed");
}
return wrapper(convertHtmlToReact(html, options));
};
export {
element,
fragment,
htmlToReact,
props,
wrapper
};