-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
58 lines (48 loc) · 1.62 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
import React, {Component} from 'react';
import {View} from 'react-native';
import {merge, isEqual, isArray} from 'lodash';
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');
}
const opts = {
enableLightBox: props.enableLightBox,
navigator: props.navigator,
imageParam: props.imageParam,
onLink: props.onLink,
bgImage: props.bgImage,
onImageOpen: props.onImageOpen,
onImageClose: props.onImageClose,
rules: props.rules
};
const mergedStyles = merge({}, styles, 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);
}
render() {
const child = isArray(this.props.children)
? this.props.children.join('')
: this.props.children;
const tree = this.parse(child);
return <View style={[styles.view, this.props.styles.view]}>{this.renderer(tree)}</View>
}
}
export default Markdown;