forked from andangrd/react-native-markdown-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (68 loc) · 2.16 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
import React, {Component} from 'react';
import {View, ViewPropTypes} from 'react-native';
import {merge, isEqual, isArray} from 'lodash';
import PropTypes from 'prop-types';
import SimpleMarkdown from 'simple-markdown';
import styles from './styles';
class Markdown extends Component {
constructor(props) {
super(props);
if (props.enableLightBox && !props.navigator) {
throw new Error('props.navigator must be specified when enabling lightbox');
}
this.updateLocalData();
}
updateLocalData() {
const opts = {
enableLightBox: this.props.enableLightBox,
navigator: this.props.navigator,
imageParam: this.props.imageParam,
onLink: this.props.onLink,
bgImage: this.props.bgImage,
onImageOpen: this.props.onImageOpen,
onImageClose: this.props.onImageClose,
rules: this.props.rules
};
const mergedStyles = merge({}, styles, this.props.styles);
var rules = require('./rules')(mergedStyles, opts);
rules = merge({}, SimpleMarkdown.defaultRules, rules, opts.rules);
const parser = SimpleMarkdown.parserFor(rules);
this.parse = function (source) {
const blockSource = source + '\n\n';
return parser(blockSource, {inline: false});
};
this.renderer = SimpleMarkdown.outputFor(rules, 'react');
}
componentDidMount() {
if (this.props.onLoad) {
this.props.onLoad();
}
}
shouldComponentUpdate(nextProps, nextState) {
return !isEqual(nextProps.children, this.props.children) || !isEqual(nextProps.styles, this.props.styles);
}
render() {
const child = isArray(this.props.children)
? this.props.children.join('')
: this.props.children;
this.updateLocalData();
const tree = this.parse(child);
return <View style={[styles.view, this.props.styles.view]}>{this.renderer(tree)}</View>
}
}
Markdown.propTypes = {
enableLightBox: PropTypes.bool,
onLink: PropTypes.func,
onImageOpen: PropTypes.func,
onImageClose: PropTypes.func,
onLoad: PropTypes.func,
styles: PropTypes.shape({
view: ViewPropTypes.style,
}),
rules: PropTypes.object,
};
Markdown.defaultProps = {
styles: styles,
rules: {}
}
export default Markdown;