UNPKG

contentful-rich-text-html-parser

Version:
52 lines 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHtml = void 0; var parse5_1 = require("parse5"); var utils_js_1 = require("./utils.js"); var isChildNodeComment = function (childNode) { return childNode.nodeName === "#comment"; }; var isChildNodeTextNode = function (childNode) { return childNode.nodeName === "#text"; }; var isChildNodeTemplate = function (childNode) { return childNode.nodeName === "template"; }; var isChildNodeDocumentType = function (childNode) { return childNode.nodeName === "#documentType"; }; var isTextNodePureWhiteSpace = function (textNode) { return (0, utils_js_1.isWhiteSpace)(textNode.value); }; var mapChildNodeToHtmlNode = function (childNode, options) { if (isChildNodeComment(childNode) || isChildNodeDocumentType(childNode) || isChildNodeTemplate(childNode)) { return null; } if (isChildNodeTextNode(childNode)) { if (options.ignoreWhiteSpace && isTextNodePureWhiteSpace(childNode)) { return null; } return { type: "text", value: childNode.value, }; } return { type: "element", tagName: childNode.tagName, children: childNode.childNodes .map(function (c) { return mapChildNodeToHtmlNode(c, options); }) .filter(utils_js_1.isNotNull), attrs: Object.fromEntries(childNode.attrs.map(function (attr) { return [attr.name, attr.value]; })), }; }; var parseHtml = function (htmlString, options) { var parsedHtml = (0, parse5_1.parseFragment)(htmlString); return parsedHtml.childNodes .map(function (node) { return mapChildNodeToHtmlNode(node, options); }) .filter(utils_js_1.isNotNull); }; exports.parseHtml = parseHtml; //# sourceMappingURL=parseHtml.js.map