/* eslint-disable react/display-name */
import { documentToPlainTextString } from '@contentful/rich-text-plain-text-renderer';
import { documentToReactComponents } from '@contentful/rich-text-react-renderer';
import {
AssetLinkBlock,
BLOCKS,
Document,
Heading1,
Heading2,
Heading3,
Heading4,
Hyperlink,
INLINES,
MARKS,
OrderedList,
Text,
UnorderedList,
} from '@contentful/rich-text-types';
import React, { ReactNode } from 'react';
import { CMS } from '../constants';
import { renderShortcode } from '../services/cms';
import { ArticleCallout } from './article/ArticleCallout';
const Bold = ({ children }) => (
{children}
);
const Paragraph = ({ children }) => (
{children}
);
const options = {
renderMark: {
[MARKS.BOLD]: text => {text},
},
renderNode: {
[BLOCKS.PARAGRAPH]: (node, children) => {
// Grab shortcodes
// const children;
const plaintext = documentToPlainTextString(node);
const isShortcode = CMS.SHORTCODE_REGEX.test(plaintext);
return isShortcode ? (
renderShortcode(plaintext)
) : (
{children}
);
},
[BLOCKS.HEADING_1]: (node: Heading1) => {
const content = (node.content[0] as Text)?.value;
return (
{content}
);
},
[BLOCKS.HEADING_2]: (node: Heading2) => {
const content = (node.content[0] as Text)?.value;
return (
{content}
);
},
[BLOCKS.HEADING_3]: (node: Heading3) => {
const content = (node.content[0] as Text)?.value;
return (
{content}
);
},
[BLOCKS.HEADING_4]: (node: Heading4) => {
const content = (node.content[0] as Text)?.value;
return (
{content}
);
},
[BLOCKS.QUOTE]: (_node, children: ReactNode) => {
return (
);
},
[BLOCKS.EMBEDDED_ASSET]: (node: AssetLinkBlock) => {
const link = (node.data.target as any).fields.file.url.replace(
'//',
'http://',
);
return (
);
},
[BLOCKS.UL_LIST]: (node: UnorderedList, children: JSX.Element[]) => {
return (
{children.map(item => (
- {item}
))}
);
},
[BLOCKS.OL_LIST]: (node: OrderedList, children: JSX.Element[]) => {
return (
{children.map(item => (
- {item}
))}
);
},
[INLINES.HYPERLINK]: (node: Hyperlink) => {
const { content } = node;
return (
{content[0].value}
);
},
},
};
interface Props {
body: Document;
}
// Renders a rich text body
export function RichBody({ body }: Props) {
const RichBody = documentToReactComponents(body, options);
return {RichBody}
;
}