forked from CharlesMangwa/react-native-simple-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (67 loc) · 2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* @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,
rules: Object,
whitelist: Array,
blacklist: Array,
}
type DefaultProps = Props & {
children: string,
}
class Markdown extends Component {
props: Props
static defaultProps: DefaultProps = {
styles: styles,
children: '',
rules: {},
whitelist: [],
blacklist: [],
}
/** Post processes rules to strip out unwanted styling options
* while keeping the default 'paragraph' and 'text' rules
*/
postProcessRules(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) => {
const mergedStyles = Object.assign(styles, this.props.styles)
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)
}
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