import {element} from 'deku';
import Paragraph from './components/paragraph';
import setupBlockquote from './components/blockquote';
import Header from './components/header';
import startswith from 'lodash.startswith';

const isText = type => type === 'paragraph' || startswith(type, 'header');

const hasContent = ({children}) =>
  children.some(child => child.type !== 'linebreak' &&
    (child.content && child.content.trim()) || child.mark);

const setup = ({Embed}) => {
  const blockList = items => items.map(renderItem);
  const Blockquote = setupBlockquote({blockList});
  const renderItem = item => {
    const {type, children} = item;
    if (isText(type) && !hasContent(item)) {
      return '';
    }

    if (type === 'paragraph') {
      return <Paragraph items={children} />;
    }

    if (type === 'header1') {
      return <Header type={1} items={children} />;
    }

    if (type === 'header2') {
      return <Header type={2} items={children} />;
    }

    if (type === 'header3') {
      return <Header type={3} items={children} />;
    }

    if (type === 'header4') {
      return <Header type={4} items={children} />;
    }

    if (type === 'header5') {
      return <Header type={5} items={children} />;
    }

    if (type === 'header6') {
      return <Header type={6} items={children} />;
    }

    if (type === 'blockquote') {
      return <Blockquote items={children} />;
    }

    if (type === 'embed') {
      return Embed.test(item) ? <Embed {...item} /> : '';
    }

    return '';
  };

  return blockList;
};

export default setup;

import {element} from 'deku';
import Paragraph from './components/paragraph';
import setupBlockquote from './components/blockquote';
import Header from './components/header';
import startswith from 'lodash.startswith';

const isText = type => type === 'paragraph' || startswith(type, 'header');

const hasContent = ({children}) =>
  children.some(child => child.type !== 'linebreak' &&
    (child.content && child.content.trim()) || child.mark);

const setup = ({Embed}) => {
  const types = {
    blockquote: ({children}) => <Blockquote items={children} />,
    embed: item => Embed.test(item) ? <Embed {...item} /> : '',
    header1: ({children}) => <Header level={1} items={children} />,
    header2: ({children}) => <Header level={2} items={children} />,
    header3: ({children}) => <Header level={3} items={children} />,
    header4: ({children}) => <Header level={4} items={children} />,
    header5: ({children}) => <Header level={5} items={children} />,
    header6: ({children}) => <Header level={6} items={children} />,
    paragraph: ({children}) => <Paragraph items={children} />
  };

  const blockList = items => items.map(renderItem);
  const Blockquote = setupBlockquote({blockList});
  const renderItem = item => {
    const {type} = item;
    const typeFn = types[type];
    if (!typeFn || (isText(type) && !hasContent(item))) {
      return '';
    }

    return typeFn(item);
  };

  return blockList;
};

export default setup;

remove complex method