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)
}
}
marks: [],
value: '',
nodeType: 'text',
data: {},
},
],
nodeType: inlineType,
},
{
marks: [],
value: '',
nodeType: 'text',
data: {},
},
],
nodeType: BLOCKS.PARAGRAPH,
},
],
data: {},
nodeType: BLOCKS.DOCUMENT,
} as Document;
}
export default function(entry: Object) {
return {
nodeType: BLOCKS.DOCUMENT,
data: {},
content: [
{
nodeType: BLOCKS.EMBEDDED_ENTRY,
content: [],
data: {
target: entry,
},
},
],
} as Document;
}
import { Document, BLOCKS } from '@contentful/rich-text-types';
export default {
nodeType: BLOCKS.DOCUMENT,
data: {},
content: [
{
nodeType: 'UNRECOGNIZED_TYPE' as BLOCKS,
data: {},
content: [
{
nodeType: 'text',
value: 'Hello world!',
marks: [],
data: {},
},
],
},
],
} as Document;
export default function(entry: Object) {
return {
nodeType: BLOCKS.DOCUMENT,
data: {},
content: [
{
nodeType: BLOCKS.EMBEDDED_ENTRY,
content: [],
data: {
target: entry,
},
},
],
} as Document;
}
},
],
nodeType: inlineType,
},
{
marks: [],
value: '',
nodeType: 'text',
data: {},
},
],
nodeType: BLOCKS.PARAGRAPH,
},
],
data: {},
nodeType: BLOCKS.DOCUMENT,
} as Document;
}
export default function(heading: string) {
return {
nodeType: BLOCKS.DOCUMENT,
data: {},
content: [
{
nodeType: heading,
data: {},
content: [
{
nodeType: 'text',
value: 'hello world',
marks: [],
data: {},
},
],
},
],
} as Document;
it('should return an HTML tag if no override matches', () => {
const type = BLOCKS.PARAGRAPH;
const overrides = { foo: { component: Override } };
const actual = RichTextService.getElement(type, overrides);
expect(actual).toBe('p');
});
it('should return an override by the node type', () => {
const type = BLOCKS.PARAGRAPH;
const overrides = { [BLOCKS.PARAGRAPH]: 'foo' };
const actual = RichTextService.getOverride(type, overrides);
expect(actual).toBe('foo');
});
});