contentful-rich-text-html-parser
Version:
Convert any HTML to the Contentful Rich Text format
48 lines • 1.6 kB
JavaScript
import { parseFragment } from "parse5";
import { isNotNull, isWhiteSpace } from "./utils.js";
const isChildNodeComment = (childNode) => {
return childNode.nodeName === "#comment";
};
const isChildNodeTextNode = (childNode) => {
return childNode.nodeName === "#text";
};
const isChildNodeTemplate = (childNode) => {
return childNode.nodeName === "template";
};
const isChildNodeDocumentType = (childNode) => {
return childNode.nodeName === "#documentType";
};
const isTextNodePureWhiteSpace = (textNode) => {
return isWhiteSpace(textNode.value);
};
const mapChildNodeToHtmlNode = (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((c) => mapChildNodeToHtmlNode(c, options))
.filter(isNotNull),
attrs: Object.fromEntries(childNode.attrs.map((attr) => [attr.name, attr.value])),
};
};
export const parseHtml = (htmlString, options) => {
const parsedHtml = parseFragment(htmlString);
return parsedHtml.childNodes
.map((node) => mapChildNodeToHtmlNode(node, options))
.filter(isNotNull);
};
//# sourceMappingURL=parseHtml.js.map