Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function assetNodeToJsx(node, options, key) {
const { data, content, nodeType } = node;
const { overrides, createElement } = options;
const mimeType = get(data, 'target.fields.file.contentType', '');
const mimeTypeGroup = mimeType.split('/')[0];
if (!mimeTypeGroup) {
return unknownNodeToJsx(node, options, key);
}
const elementOverrides = overrides[nodeType];
const BlockAsset = assetElementMap[mimeTypeGroup];
const DefaultAsset = helpers.isBlock(node) ? BlockAsset : AssetLink;
const element = getElement(mimeTypeGroup, elementOverrides) || DefaultAsset;
if (!element) {
return unknownNodeToJsx(node, options, key);
}
const props = getProps(nodeType, elementOverrides, {
...data.target,
key
});
const children = isEmpty(content)
? undefined
: nodeListToJsx(content, options);
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;
}, '');
}
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;
}, '');
}
export function entryNodeToJsx(node, options, key) {
const { data, content, nodeType } = node;
const { overrides, createElement } = options;
const contentType = get(data, 'target.sys.contentType.sys.id');
if (!contentType) {
return unknownNodeToJsx(node, options, key);
}
const elementOverrides = overrides[nodeType];
const DefaultElement = helpers.isBlock(node) ? BlockElement : InlineElement;
const element = getElement(contentType, elementOverrides) || DefaultElement;
const props = getProps(nodeType, elementOverrides, {
...data.target,
key
});
const children = isEmpty(content)
? undefined
: nodeListToJsx(content, options);
return createElement(element, props, children);
}