@cf-wasm/og
Version:
Generate Open Graph Images dynamically from HTML/CSS without a browser.
1 lines • 5.44 kB
Source Map (JSON)
{"version":3,"file":"html-to-react.cjs","names":["Fragment"],"sources":["../src/html-to-react.ts"],"sourcesContent":["import {\n\tconvertHtmlToReact,\n\ttype ParserOptions as HTMLToReactParserOptions,\n} from '@hedgedoc/html-to-react';\nimport {\n\tcreateElement,\n\tFragment,\n\tisValidElement,\n\ttype ReactElement,\n\ttype ReactNode,\n} from 'react';\n\nexport interface ParserOptions extends HTMLToReactParserOptions {\n\ttailwind?: boolean | 'data' | 'class';\n}\n\nclass Transformer {\n\tprivate options: ParserOptions & { tailwind: boolean | 'data' | 'class' };\n\n\tconstructor(options: ParserOptions = {}) {\n\t\tthis.options = { tailwind: false, ...options };\n\t}\n\n\ttransform(html: string): ReactElement {\n\t\treturn this.wrapper(convertHtmlToReact(html));\n\t}\n\n\twrapper(children: (ReactElement | string | null)[]): ReactElement {\n\t\tif (children.length === 1 && isValidElement(children[0])) {\n\t\t\treturn this.element(children[0]);\n\t\t}\n\n\t\treturn this.fragment(children);\n\t}\n\n\telement(element: ReactElement) {\n\t\treturn createElement(\n\t\t\telement.type,\n\t\t\ttypeof element.props === 'object' && element.props\n\t\t\t\t? this.props(element.props)\n\t\t\t\t: {},\n\t\t);\n\t}\n\n\tfragment(children: ReactNode): ReactElement {\n\t\tconst transformed = this.children(children);\n\t\treturn createElement(\n\t\t\tFragment,\n\t\t\ttypeof transformed !== 'undefined' ? { children: transformed } : {},\n\t\t);\n\t}\n\n\tprops(input: {\n\t\tchildren?: ReactNode;\n\t\tclassName?: unknown;\n\t\ttw?: unknown;\n\t\t'data-tw'?: unknown;\n\t}) {\n\t\tconst props = { ...input };\n\n\t\tif ('children' in props) {\n\t\t\tconst transformed = this.children(props.children);\n\t\t\tif (typeof transformed === 'undefined') {\n\t\t\t\tdelete props.children;\n\t\t\t} else {\n\t\t\t\tprops.children = transformed;\n\t\t\t}\n\t\t}\n\n\t\tif (this.options.tailwind === true || this.options.tailwind === 'class') {\n\t\t\tif ('className' in props && typeof props.className === 'string') {\n\t\t\t\tprops.tw = props.className;\n\t\t\t}\n\t\t} else if (this.options.tailwind === 'data') {\n\t\t\tif ('data-tw' in props && typeof props['data-tw'] === 'string') {\n\t\t\t\tprops.tw = props['data-tw'];\n\t\t\t}\n\t\t}\n\n\t\treturn props;\n\t}\n\n\tchildren(children: ReactNode): ReactNode {\n\t\tif (\n\t\t\tchildren === null ||\n\t\t\ttypeof children === 'undefined' ||\n\t\t\ttypeof children === 'boolean'\n\t\t) {\n\t\t\treturn undefined;\n\t\t}\n\t\tif (isValidElement(children)) {\n\t\t\treturn this.element(children);\n\t\t}\n\t\tif (Array.isArray(children)) {\n\t\t\tconst filtered: ReactNode[] = [];\n\n\t\t\tfor (const child of children) {\n\t\t\t\tif (\n\t\t\t\t\tchild === null ||\n\t\t\t\t\ttypeof child === 'undefined' ||\n\t\t\t\t\ttypeof child === 'boolean'\n\t\t\t\t) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (isValidElement(child)) {\n\t\t\t\t\tfiltered.push(this.element(child));\n\t\t\t\t} else {\n\t\t\t\t\tfiltered.push(child);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (filtered.length === 0) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tif (filtered.length === 1) {\n\t\t\t\treturn filtered[0];\n\t\t\t}\n\t\t\treturn filtered;\n\t\t}\n\t\treturn children;\n\t}\n}\n\n/**\n * A helper function to parse html string to a {@link ReactElement} like object\n *\n * @param html The html string to parse\n * @param options Options\n *\n * @returns The {@link ReactElement}\n */\nexport const htmlToReact = (\n\thtml: string,\n\toptions: ParserOptions = {},\n): ReactElement => {\n\tif (typeof html !== 'string') {\n\t\tthrow new TypeError('Argument 1 must be of type string');\n\t}\n\tif (html.trim().length === 0) {\n\t\tthrow new TypeError('Blank html string cannot be parsed');\n\t}\n\n\treturn new Transformer(options).transform(html);\n};\n\nexport { ht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