Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function(entry: Object) {
return {
nodeType: BLOCKS.DOCUMENT,
data: {},
content: [
{
nodeType: BLOCKS.EMBEDDED_ENTRY,
content: [],
data: {
target: entry,
},
},
],
} as Document;
}
it('should render an embedded entry block override', () => {
const overrides = {
[BLOCKS.EMBEDDED_ENTRY]: { page: Override }
};
const actual = RichTextService.entryNodeToJsx(embeddedEntryBlock, {
...options,
overrides
});
expect(actual).toMatchSnapshot();
});
],
},
{
nodeType: BLOCKS.PARAGRAPH,
data: {},
content: [
{
nodeType: 'text',
data: {},
marks: [],
value: 'The first chapter is free to read: ',
},
],
},
{
nodeType: BLOCKS.EMBEDDED_ENTRY,
data: {
target: {
sys: {
id: 'chapter-1',
type: 'Link',
linkType: 'Entry',
},
},
},
content: [
{
nodeType: 'text',
data: {},
marks: [],
value: '',
},
[BLOCKS.PARAGRAPH]: (node, children) => {
const filteredChildren = children.filter((item) => !!item);
if (filteredChildren.length === 1 && typeof filteredChildren[0] === 'object') {
return filteredChildren[0];
}
return <p>{children.filter((item) => !!item)}</p>;
},
[BLOCKS.EMBEDDED_ASSET]: (node) => {
const { url } = node.data.target.fields.file;
const { description, title } = node.data.target.fields;
return imageComponent({ src: url, description, title });
},
[BLOCKS.EMBEDDED_ENTRY]: (node) => {
if (_.get(node, 'data.target.sys.contentType.sys.id') === 'usefulReadings') {
const { bookList, list } = _.get(node, 'data.target.fields', null);
return (
<div>
<div>
<img src="/static/images/book_icon.png" alt="Book icon">
</div>
<div>
<h4>useful readings:</h4>
{(bookList || list) && documentToReactComponents(bookList || list)}
</div>
</div>
);
}
if (_.get(node, 'data.target.sys.contentType.sys.id') === 'mostSuitableFor') {
[BLOCKS.HEADING_6]: 'h6',
[BLOCKS.PARAGRAPH]: 'p',
[BLOCKS.UL_LIST]: 'ul',
[BLOCKS.OL_LIST]: 'ol',
[BLOCKS.LIST_ITEM]: 'li',
[BLOCKS.QUOTE]: 'blockquote',
[BLOCKS.HR]: 'hr',
[INLINES.HYPERLINK]: 'a',
[MARKS.BOLD]: 'strong',
[MARKS.ITALIC]: 'em',
[MARKS.UNDERLINE]: 'u',
[MARKS.CODE]: 'code'
};
const entryMap = {
[BLOCKS.EMBEDDED_ENTRY]: true,
[INLINES.ENTRY_HYPERLINK]: true,
[INLINES.EMBEDDED_ENTRY]: true
};
const assetMap = {
[BLOCKS.EMBEDDED_ASSET]: true,
[INLINES.ASSET_HYPERLINK]: true
};
function isEntryNode(node) {
return entryMap[node.nodeType];
}
function isAssetNode(node) {
return assetMap[node.nodeType];
}
};
export const embeddedEntryInline = {
data: {
target: entry
},
content: [],
nodeType: INLINES.EMBEDDED_ENTRY
};
export const embeddedEntryBlock = {
data: {
target: entry
},
content: [],
nodeType: BLOCKS.EMBEDDED_ENTRY
};
const defaultMarkRenderers = {
[MARKS.BOLD]: (text, key) => <strong>{text}</strong>,
[MARKS.ITALIC]: (text, key) => <em>{text}</em>,
[MARKS.UNDERLINE]: (text, key) => <u>{text}</u>,
[MARKS.CODE]: (text, key) => <code>{text}</code>
}
const defaultNodeRenderers = {
[BLOCKS.PARAGRAPH]: (node, key, next) => <p>{next(node.content, key, next)}</p>,
[BLOCKS.HEADING_1]: (node, key, next) => <h1>{next(node.content, key, next)}</h1>,
[BLOCKS.HEADING_2]: (node, key, next) => <h2>{next(node.content, key, next)}</h2>,
[BLOCKS.HEADING_3]: (node, key, next) => <h3>{next(node.content, key, next)}</h3>,
[BLOCKS.HEADING_4]: (node, key, next) => <h4>{next(node.content, key, next)}</h4>,
[BLOCKS.HEADING_5]: (node, key, next) => <h5>{next(node.content, key, next)}</h5>,
[BLOCKS.HEADING_6]: (node, key, next) => <h6>{next(node.content, key, next)}</h6>,
[BLOCKS.EMBEDDED_ENTRY]: (node, key, next) => <div>{next(node.content, key, next)}</div>,
[BLOCKS.UL_LIST]: (node, key, next) => <ul>{next(node.content, key, next)}</ul>,
[BLOCKS.OL_LIST]: (node, key, next) => <ol>{next(node.content, key, next)}</ol>,
[BLOCKS.LIST_ITEM]: (node, key, next) => <li>{next(node.content, key, next)}</li>,
[BLOCKS.QUOTE]: (node, key, next) => <blockquote>{next(node.content, key, next)}</blockquote>,
[BLOCKS.HR]: (node, key) => <hr>,
[INLINES.ASSET_HYPERLINK]: (node, key) => defaultInline(INLINES.ASSET_HYPERLINK, node, key),
[INLINES.ENTRY_HYPERLINK]: (node, key) => defaultInline(INLINES.ENTRY_HYPERLINK, node, key),
[INLINES.EMBEDDED_ENTRY]: (node, key) => defaultInline(INLINES.EMBEDDED_ENTRY, node, key),
[INLINES.HYPERLINK]: (node, key, next) => {
return (<a href="{node.data.uri}">{next(node.content, key, next)}</a>)
},
text: ({ marks, value }, key, markRenderer) => {
return marks.length ? (
marks.reduce((aggregate, mark, i) => markRenderer[mark.type](aggregate, `${key}-${i}`), value)
) : value
}
data: {},
marks: [],
value: '',
},
],
},
{
nodeType: BLOCKS.UL_LIST,
data: {},
content: [
{
nodeType: BLOCKS.LIST_ITEM,
data: {},
content: [
{
nodeType: BLOCKS.EMBEDDED_ENTRY,
data: {
target: {
sys: {
id: 'chapter-2',
type: 'Link',
linkType: 'Entry',
},
},
},
content: [
{
nodeType: 'text',
data: {},
marks: [],
value: '',
},
MARKS,
INLINES,
Block,
Inline,
helpers,
} from '@contentful/rich-text-types';
const defaultNodeRenderers: RenderNode = {
[BLOCKS.PARAGRAPH]: (node, next) => `<p>${next(node.content)}</p>`,
[BLOCKS.HEADING_1]: (node, next) => `<h1>${next(node.content)}</h1>`,
[BLOCKS.HEADING_2]: (node, next) => `<h2>${next(node.content)}</h2>`,
[BLOCKS.HEADING_3]: (node, next) => `<h3>${next(node.content)}</h3>`,
[BLOCKS.HEADING_4]: (node, next) => `<h4>${next(node.content)}</h4>`,
[BLOCKS.HEADING_5]: (node, next) => `<h5>${next(node.content)}</h5>`,
[BLOCKS.HEADING_6]: (node, next) => `<h6>${next(node.content)}</h6>`,
[BLOCKS.EMBEDDED_ENTRY]: (node, next) => `<div>${next(node.content)}</div>`,
[BLOCKS.UL_LIST]: (node, next) => `<ul>${next(node.content)}</ul>`,
[BLOCKS.OL_LIST]: (node, next) => `<ol>${next(node.content)}</ol>`,
[BLOCKS.LIST_ITEM]: (node, next) => `<li>${next(node.content)}</li>`,
[BLOCKS.QUOTE]: (node, next) => `<blockquote>${next(node.content)}</blockquote>`,
[BLOCKS.HR]: () => '<hr>',
[INLINES.ASSET_HYPERLINK]: node => defaultInline(INLINES.ASSET_HYPERLINK, node as Inline),
[INLINES.ENTRY_HYPERLINK]: node => defaultInline(INLINES.ENTRY_HYPERLINK, node as Inline),
[INLINES.EMBEDDED_ENTRY]: node => defaultInline(INLINES.EMBEDDED_ENTRY, node as Inline),
[INLINES.HYPERLINK]: (node, next) => `<a href="${node.data.uri}">${next(node.content)}</a>`,
};
const defaultMarkRenderers: RenderMark = {
[MARKS.BOLD]: text => `<b>${text}</b>`,
[MARKS.ITALIC]: text => `<i>${text}</i>`,
[MARKS.UNDERLINE]: text => `<u>${text}</u>`,
[MARKS.CODE]: text => `<code>${text}</code>`,