All files Markdown.js

76.92% Statements 10/13
60% Branches 3/5
80% Functions 4/5
75% Lines 9/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44            31x   1x 46x 46x 46x 46x           33x                                     13x 13x          
import React, {Component} from 'react';
import {Parser} from 'commonmark';
import shallowEqual from 'shallowequal';
import {getChildren} from './util';
import COMPONENT_NAMES from './component-names';
 
const defaultCustomizer = (render, children) => children.map(render);
 
const render = (components, customizer = defaultCustomizer, node) => {
  const NodeComponent = components[COMPONENT_NAMES[node.type]];
  const {info, level, listType, listStart, title, destination} = node;
  const children = [...getChildren(node)];
  return (
    <NodeComponent {...{info, level, listType, listStart, title, destination}}>
      {!children.length
        ? node.literal
        : customizer(
          (child, i) =>
            React.cloneElement(render(components, customizer, child), {
              key: i,
            }),
          children,
        )}
    </NodeComponent>
  );
};
 
class Markdown extends Component {
  parser = new Parser();
 
  shouldComponentUpdate(nextProps) {
    const {components, ...rest} = this.props;
    const {components: nextComponents, ...nextRest} = nextProps;
    return !shallowEqual(rest, nextRest) || !shallowEqual(components, nextComponents);
  }
 
  render() {
    const {components, customizer, children} = this.props;
    return render(components, customizer, this.parser.parse(children));
  }
}
 
export default Markdown;