forked from mientjan/react-native-markdown-renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (74 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
81
82
83
84
85
86
87
/**
* Base Markdown component
* @author Mient-jan Stelling
*/
import React, { Component, PropTypes } from 'react';
import { View } from 'react-native';
import { parser, stringToTokens, tokensToAST } from './lib/parser';
import defaultRenderFunctions from './lib/defaultRenderFunctions';
import AstRenderer from './lib/AstRenderer';
import MarkdownIt from 'markdown-it';
import PluginContainer from "./lib/PluginContainer";
import blockPlugin from "./lib/blockPlugin";
/**
*
*/
export { defaultRenderFunctions, AstRenderer, parser, stringToTokens, tokensToAST, MarkdownIt, PluginContainer, blockPlugin };
export default class Markdown extends Component {
/**
* Definition of the prop types
*/
static propTypes = {
children: PropTypes.node.isRequired,
renderer: PropTypes.instanceOf(AstRenderer),
plugins: PropTypes.arrayOf(PropTypes.instanceOf(PluginContainer)),
};
/**
* Default Props
*/
static defaultProps = {
renderer: new AstRenderer(defaultRenderFunctions),
plugins: [],
};
copy = '';
/**
* Only when the copy changes will the markdown render again.
* @param nextProps
* @param nextState
* @return {boolean}
*/
shouldComponentUpdate(nextProps, nextState) {
const copy = nextProps.children instanceof Array
? nextProps.children.join('')
: nextProps.children;
if (copy !== this.copy) {
this.copy = copy;
return true;
}
return false;
}
componentWillMount() {
if(this.props.plugins && this.props.plugins.length > 0 && !this.md)
{
let md = MarkdownIt();
this.props.plugins.forEach(plugin => {
md = md.use.apply(md, plugin.toArray());
});
this.md = md;
}
}
getCopyFromProps() {
return this.props.children instanceof Array
? this.props.children.join('')
: this.props.children;
}
/**
*
* @return {View}
*/
render() {
const copy = (this.copy = this.getCopyFromProps());
const { renderer } = this.props;
return parser(copy, renderer, this.md);
}
}