@wordpress/blocks
Version:
Block API for WordPress.
52 lines (42 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.htmlToBlocks = htmlToBlocks;
var _factory = require("../factory");
var _parser = require("../parser");
var _getRawTransforms = require("./get-raw-transforms");
/**
* Internal dependencies
*/
/**
* Converts HTML directly to blocks. Looks for a matching transform for each
* top-level tag. The HTML should be filtered to not have any text between
* top-level tags and formatted in a way that blocks can handle the HTML.
*
* @param {string} html HTML to convert.
*
* @return {Array} An array of blocks.
*/
function htmlToBlocks(html) {
const doc = document.implementation.createHTMLDocument('');
doc.body.innerHTML = html;
return Array.from(doc.body.children).flatMap(node => {
const rawTransform = (0, _factory.findTransform)((0, _getRawTransforms.getRawTransforms)(), ({
isMatch
}) => isMatch(node));
if (!rawTransform) {
return (0, _factory.createBlock)( // Should not be hardcoded.
'core/html', (0, _parser.getBlockAttributes)('core/html', node.outerHTML));
}
const {
transform,
blockName
} = rawTransform;
if (transform) {
return transform(node);
}
return (0, _factory.createBlock)(blockName, (0, _parser.getBlockAttributes)(blockName, node.outerHTML));
});
}
//# sourceMappingURL=html-to-blocks.js.map