Skip to content

Commit

Permalink
Use helpers from Contentful
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Jan 21, 2019
1 parent 1e4fc68 commit f9539db
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/rich-text-to-jsx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-use-before-define */
import React from 'react';
import { BLOCKS, INLINES, MARKS } from '@contentful/rich-text-types';
import { BLOCKS, INLINES, MARKS, helpers } from '@contentful/rich-text-types';

import cx from './lib/cx';
import get from './lib/get';
Expand All @@ -15,11 +15,6 @@ export const defaultOptions = {
createElement: React.createElement
};

const customBlockNodes = {
[BLOCKS.EMBEDDED_ENTRY]: true,
[BLOCKS.EMBEDDED_ASSET]: true
};

const tagMap = {
[BLOCKS.HEADING_1]: 'h1',
[BLOCKS.HEADING_2]: 'h2',
Expand All @@ -40,6 +35,10 @@ const tagMap = {
[MARKS.CODE]: 'code'
};

function isCustom(node) {
return !tagMap[node.nodeType];
}

export default function richTextToJsx(richText, options = {}) {
if (!richText) {
return null;
Expand All @@ -61,15 +60,11 @@ export function nodeToJsx(node = {}, options = {}, key) {
return unknownNodeToJsx(node, options, key);
}

const isTextNode = nodeType === 'text';

if (isTextNode) {
if (helpers.isText(node)) {
return textNodeToJsx(node, options, key);
}

const isCustomNode = !tagMap[nodeType];

if (isCustomNode) {
if (isCustom(node)) {
return customNodeToJsx(node, options, key);
}

Expand Down Expand Up @@ -123,8 +118,7 @@ export function customNodeToJsx(node, options, key) {

const elementOverrides = overrides[contentType];

const isBlockNode = customBlockNodes[nodeType];
const DefaultElement = isBlockNode ? BlockElement : InlineElement;
const DefaultElement = helpers.isBlock(node) ? BlockElement : InlineElement;
const element = getElement(nodeType, elementOverrides) || DefaultElement;

const props = getProps(nodeType, elementOverrides, {
Expand Down

0 comments on commit f9539db

Please sign in to comment.