contentful-rich-text-html-parser
Version:
Convert any HTML to the Contentful Rich Text format
75 lines • 3.69 kB
JavaScript
import { convertTagToBlock, convertTagToChildren, convertTagToHyperlink, convertTagToMark, convertTextNodeToText, } from "./converters.js";
import { parseHtml } from "./parseHtml.js";
import { processConvertedNodesFromTopLevel } from "./processConvertedNodesFromTopLevel.js";
import { createDocumentNode, getAsList, isNotNull } from "./utils.js";
const DEFAULT_TAG_CONVERTERS = {
h1: convertTagToBlock,
h2: convertTagToBlock,
h3: convertTagToBlock,
h4: convertTagToBlock,
h5: convertTagToBlock,
h6: convertTagToBlock,
hr: convertTagToBlock,
li: convertTagToBlock,
ol: convertTagToBlock,
p: convertTagToBlock,
blockquote: convertTagToBlock,
table: convertTagToBlock,
td: convertTagToBlock,
th: convertTagToBlock,
tr: convertTagToBlock,
ul: convertTagToBlock,
b: convertTagToMark,
strong: convertTagToMark,
pre: convertTagToMark,
i: convertTagToMark,
em: convertTagToMark,
sub: convertTagToMark,
sup: convertTagToMark,
u: convertTagToMark,
a: convertTagToHyperlink,
};
const mapHtmlNodeToRichTextNode = (node, marks, options) => {
var _a;
const { convertText, convertTag, defaultTagConverter } = options;
const mapChildren = (node, mark) => {
const newMarks = mark ? getAsList(mark) : [];
const allMarks = newMarks.concat(marks);
if (node.type === "element") {
return node.children.flatMap((child) => mapHtmlNodeToRichTextNode(child, allMarks, options));
}
return getAsList(mapHtmlNodeToRichTextNode(node, allMarks, options));
};
const next = mapChildren;
if (node.type === "text") {
return convertText(node, marks);
}
const tagConverter = (_a = convertTag[node.tagName]) !== null && _a !== void 0 ? _a : defaultTagConverter;
const convertedNode = tagConverter(node, next);
return convertedNode;
};
export const htmlStringToDocument = (htmlString, options = {}) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
const optionsWithDefaults = {
convertTag: Object.assign(Object.assign({}, DEFAULT_TAG_CONVERTERS), options.convertTag),
defaultTagConverter: (_a = options.defaultTagConverter) !== null && _a !== void 0 ? _a : convertTagToChildren,
convertText: (_b = options.convertText) !== null && _b !== void 0 ? _b : convertTextNodeToText,
parserOptions: {
handleWhitespaceNodes: (_d = (_c = options === null || options === void 0 ? void 0 : options.parserOptions) === null || _c === void 0 ? void 0 : _c.handleWhitespaceNodes) !== null && _d !== void 0 ? _d : "preserve",
},
postProcessing: {
handleTopLevelInlines: (_f = (_e = options === null || options === void 0 ? void 0 : options.postProcessing) === null || _e === void 0 ? void 0 : _e.handleTopLevelInlines) !== null && _f !== void 0 ? _f : "preserve",
handleTopLevelText: (_h = (_g = options === null || options === void 0 ? void 0 : options.postProcessing) === null || _g === void 0 ? void 0 : _g.handleTopLevelText) !== null && _h !== void 0 ? _h : "preserve",
},
};
const parserOptions = {
ignoreWhiteSpace: optionsWithDefaults.parserOptions.handleWhitespaceNodes == "remove",
};
const parsedHtml = parseHtml(htmlString, parserOptions);
const richTextNodes = parsedHtml.flatMap((node) => mapHtmlNodeToRichTextNode(node, [], optionsWithDefaults));
const processedRichTextNodes = richTextNodes
.map((node) => processConvertedNodesFromTopLevel(node, optionsWithDefaults))
.filter(isNotNull);
return createDocumentNode(processedRichTextNodes);
};
//# sourceMappingURL=htmlStringToDocument.js.map