forked from CharlesMangwa/react-native-simple-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 1.35 KB
/
index.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* @flow */
import React, { Component } from 'react'
import { View } from 'react-native'
import SimpleMarkdown from 'simple-markdown'
import _ from 'lodash'
import initialRules from './rules'
import styles from './styles'
type Props = {
styles: any,
children?: string,
}
type DefaultProps = Props & {
children: string,
}
class Markdown extends Component {
props: Props
static defaultProps: DefaultProps = {
styles: styles,
children: '',
}
renderContent = (children: string) => {
const mergedStyles = Object.assign(styles, this.props.styles)
const rules = _.merge({}, SimpleMarkdown.defaultRules, initialRules(mergedStyles))
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)
}
shouldComponentUpdate(nextProps, nextState) {
if (
this.props.children === nextProps.children &&
this.props.styles === nextProps.styles
) {
return false
}
return true
}
render() {
return (
<View style={[styles.view, this.props.styles.view]}>
{this.renderContent(this.props.children)}
</View>
)
}
}
export default Markdown