Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function nodeToHtmlString(node: CommonNode, { renderNode, renderMark }: Options): string {
if (helpers.isText(node)) {
const nodeValue = escape(node.value);
if (node.marks.length > 0) {
return node.marks.reduce((value: string, mark: Mark) => {
if (!renderMark[mark.type]) {
return value;
}
return renderMark[mark.type](value);
}, nodeValue);
}
return nodeValue;
} else {
const nextNode: Next = nodes => nodeListToHtmlString(nodes, { renderMark, renderNode });
if (!node.nodeType || !renderNode[node.nodeType]) {
// TODO: Figure what to return when passed an unrecognized node.
return '';
const renderNode = (node, key, next) => {
const nodeRenderer = next.node
if (helpers.isText(node)) {
// We're at final tip of node branch, can render text.
const markerRender = next.mark
return nodeRenderer.text(node, key, markerRender)
} else {
const nextNode = nodes => renderNodeList(nodes, key, next)
if (!nodeRenderer) {
return <div>{`${key} ;lost nodeRenderer`}</div>
}
if (!node.nodeType || !nodeRenderer[node.nodeType]) {
// TODO: Figure what to return when passed an unrecognized node.
return '(Unrecognized node type)'
}
return nodeRenderer[node.nodeType](node, key, nextNode)
}
}
export function nodeToReactComponent(node: CommonNode, options: Options): ReactNode {
const { renderNode, renderMark, renderText } = options;
if (helpers.isText(node)) {
return node.marks.reduce((value: ReactNode, mark: Mark): ReactNode => {
if (!renderMark[mark.type]) {
return value;
}
return renderMark[mark.type](value);
}, renderText ? renderText(node.value) : node.value);
} else {
const children: ReactNode = nodeListToReactComponents(node.content, options);
if (!node.nodeType || !renderNode[node.nodeType]) {
return <>{children};
}
return renderNode[node.nodeType](node, children);
}
}
export function nodeToJsx(node = {}, options = {}, key) {
const { nodeType } = node;
if (!nodeType) {
return unknownNodeToJsx(node, options, key);
}
if (helpers.isText(node)) {
return textNodeToJsx(node, options, key);
}
if (isEntryNode(node)) {
return entryNodeToJsx(node, options, key);
}
if (isAssetNode(node)) {
return assetNodeToJsx(node, options, key);
}
return parentNodeToJsx(node, options, key);
}
return (rootNode as Block).content.reduce((acc: string, node: Node, i: number): string => {
let nodeTextValue: string;
if (helpers.isText(node)) {
nodeTextValue = node.value;
} else if (helpers.isBlock(node) || helpers.isInline(node)) {
nodeTextValue = documentToPlainTextString(node, blockDivisor);
if (!nodeTextValue.length) {
return acc;
}
}
const nextNode = rootNode.content[i + 1];
const isNextNodeBlock = nextNode && helpers.isBlock(nextNode);
const divisor = isNextNodeBlock ? blockDivisor : '';
return acc + nodeTextValue + divisor;
}, '');
}