@wordpress/block-library
Version:
Block library for the WordPress editor.
8 lines (7 loc) • 2.59 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/utils/html-renderer.js"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\nimport parse, { attributesToProps, domToReact } from 'html-react-parser';\n\n/**\n * WordPress dependencies\n */\nimport { safeHTML } from '@wordpress/dom';\n\n/**\n * Renders HTML content as React elements with optional wrapper props.\n *\n * @param {Object} props - The props for the component.\n * @param {Object} props.wrapperProps - The props to merge with the root element.\n * className and style are merged with the parsed HTML attributes.\n * @param {string} props.html - The HTML content to render.\n * @return {JSX.Element} The rendered React elements.\n */\nconst HtmlRenderer = ( { wrapperProps = {}, html = '' } ) => {\n\tconst options = {\n\t\treplace: ( { name, type, attribs, parent, children } ) => {\n\t\t\tif ( type === 'tag' && name ) {\n\t\t\t\tconst parsedProps = attributesToProps( attribs || {} );\n\t\t\t\tconst TagName = name;\n\t\t\t\tif ( ! parent ) {\n\t\t\t\t\tconst mergedProps = {\n\t\t\t\t\t\t...parsedProps,\n\t\t\t\t\t\t...wrapperProps,\n\t\t\t\t\t\tclassName: clsx(\n\t\t\t\t\t\t\tparsedProps.className,\n\t\t\t\t\t\t\twrapperProps.className\n\t\t\t\t\t\t),\n\t\t\t\t\t\tstyle: {\n\t\t\t\t\t\t\t...( parsedProps.style || {} ),\n\t\t\t\t\t\t\t...( wrapperProps.style || {} ),\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<TagName { ...mergedProps }>\n\t\t\t\t\t\t\t{ domToReact( children, options ) }\n\t\t\t\t\t\t</TagName>\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t};\n\n\tconst sanitizedContent = safeHTML( html );\n\tconst parsedContent = parse( sanitizedContent, options );\n\n\treturn parsedContent;\n};\n\nexport default HtmlRenderer;\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAiB;AACjB,+BAAqD;AAKrD,iBAAyB;AA+BnB;AApBN,IAAM,eAAe,CAAE,EAAE,eAAe,CAAC,GAAG,OAAO,GAAG,MAAO;AAC5D,QAAM,UAAU;AAAA,IACf,SAAS,CAAE,EAAE,MAAM,MAAM,SAAS,QAAQ,SAAS,MAAO;AACzD,UAAK,SAAS,SAAS,MAAO;AAC7B,cAAM,kBAAc,4CAAmB,WAAW,CAAC,CAAE;AACrD,cAAM,UAAU;AAChB,YAAK,CAAE,QAAS;AACf,gBAAM,cAAc;AAAA,YACnB,GAAG;AAAA,YACH,GAAG;AAAA,YACH,eAAW,YAAAA;AAAA,cACV,YAAY;AAAA,cACZ,aAAa;AAAA,YACd;AAAA,YACA,OAAO;AAAA,cACN,GAAK,YAAY,SAAS,CAAC;AAAA,cAC3B,GAAK,aAAa,SAAS,CAAC;AAAA,YAC7B;AAAA,UACD;AACA,iBACC,4CAAC,WAAU,GAAG,aACX,mDAAY,UAAU,OAAQ,GACjC;AAAA,QAEF;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAEA,QAAM,uBAAmB,qBAAU,IAAK;AACxC,QAAM,oBAAgB,yBAAAC,SAAO,kBAAkB,OAAQ;AAEvD,SAAO;AACR;AAEA,IAAO,wBAAQ;",
"names": ["clsx", "parse"]
}