UNPKG

@cf-wasm/og

Version:

Generate Open Graph Images dynamically from HTML/CSS without a browser.

78 lines (76 loc) 2.88 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/html-to-react/parser.ts var parser_exports = {}; __export(parser_exports, { element: () => element, fragment: () => fragment, htmlToReact: () => htmlToReact, props: () => props, wrapper: () => wrapper }); module.exports = __toCommonJS(parser_exports); var import_html_to_react = require("@hedgedoc/html-to-react"); var import_react = require("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) => (0, import_react.createElement)(element2.type, typeof element2.props === "object" && element2.props ? props(element2.props) : {}); var fragment = (children) => (0, import_react.createElement)(import_react.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((0, import_html_to_react.convertHtmlToReact)(html, options)); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { element, fragment, htmlToReact, props, wrapper });