diff --git a/index.js b/index.js index 83fdf08..16a61c9 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ /* @flow */ -import React, { PureComponent } from 'react' +import React, { Component } from 'react' import { View } from 'react-native' import SimpleMarkdown from 'simple-markdown' import _ from 'lodash' @@ -12,58 +12,67 @@ type Props = { children?: string, rules: Object, whitelist: Array, - blacklist: Array, + blacklist: Array } type DefaultProps = Props & { - children: string, + children: string } -class Markdown extends PureComponent { +class Markdown extends Component { static defaultProps = { styles: initialStyles, children: '', rules: {}, whitelist: [], - blacklist: [], + blacklist: [] } /** Post processes rules to strip out unwanted styling options * while keeping the default 'paragraph' and 'text' rules */ _postProcessRules = (preRules: Array): Array => { - const defaultRules = ['paragraph', 'text'] - if (this.props.whitelist.length) { - return _.pick(preRules, _.concat(this.props.whitelist, defaultRules)) - } - else if (this.props.blacklist.length) { - return _.omit(preRules, _.pullAll(this.props.blacklist, defaultRules)) - } - else { - return preRules - } + const defaultRules = ['paragraph', 'text'] + if (this.props.whitelist.length) { + return _.pick(preRules, _.concat(this.props.whitelist, defaultRules)) + } else if (this.props.blacklist.length) { + return _.omit(preRules, _.pullAll(this.props.blacklist, defaultRules)) + } else { + return preRules + } } _renderContent = (children: string): React$Element => { try { const mergedStyles = Object.assign(initialStyles, this.props.styles) - const rules = this._postProcessRules(_.merge({}, SimpleMarkdown.defaultRules, initialRules(mergedStyles), this.props.rules)) + const rules = this._postProcessRules( + _.merge( + {}, + SimpleMarkdown.defaultRules, + initialRules(mergedStyles), + this.props.rules + ) + ) const child = Array.isArray(this.props.children) ? this.props.children.join('') : this.props.children const blockSource = child + '\n\n' - const tree = SimpleMarkdown.parserFor(rules)(blockSource, { inline: false }) - return SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'))(tree) - } catch(errors) { + const tree = SimpleMarkdown.parserFor(rules)(blockSource, { + inline: false + }) + return SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(rules, 'react'))( + tree + ) + } catch (errors) { this.props.errorHandler - ? this.props.errorHandler(errors,children) + ? this.props.errorHandler(errors, children) : console.error(errors) } } - shouldComponentUpdate = (nextProps: Props): boolean => ( - this.props.children !== nextProps.children || this.props.styles !== nextProps.styles - ) + shouldComponentUpdate = (nextProps: Props): boolean => + this.props.children !== nextProps.children || + this.props.styles !== nextProps.styles render() { return ( @@ -72,7 +81,6 @@ class Markdown extends PureComponent { ) } - } export default Markdown