forked from andangrd/react-native-markdown-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (60 loc) · 1.98 KB
/
index.js
File metadata and controls
72 lines (60 loc) · 1.98 KB
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
import React, { Component } from 'react';
import { View } 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 {
componentWillMount() {
if (this.props.enableLightBox && !this.props.navigator) {
throw new Error('props.navigator must be specified when enabling lightbox');
}
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,
};
const mergedStyles = merge({}, styles, this.props.styles);
var rules = require('./rules')(mergedStyles, opts);
rules = merge({}, SimpleMarkdown.defaultRules, rules);
const parser = SimpleMarkdown.parserFor(rules);
this.parse = function(source) {
const blockSource = source + '\n\n';
return parser(blockSource, {inline: false});
};
this.renderer = SimpleMarkdown.reactFor(SimpleMarkdown.ruleOutput(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>
}
}
Markdown.propTypes = {
enableLightBox: PropTypes.bool,
onLink: PropTypes.func,
onImageOpen: PropTypes.func,
onImageClose: PropTypes.func,
onLoad: PropTypes.func,
styles: PropTypes.shape({
view: PropTypes.any,
}),
};
Markdown.defaultProps = {
styles: styles
}
export default Markdown;