2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
2018-05-29 17:09:20 +00:00
|
|
|
import { Text, Platform } from 'react-native';
|
2017-12-02 13:19:58 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { emojify } from 'react-emojione';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-05-29 17:09:20 +00:00
|
|
|
import MarkdownRenderer, { PluginContainer } from 'react-native-markdown-renderer';
|
|
|
|
import MarkdownFlowdock from 'markdown-it-flowdock';
|
2018-01-16 18:48:05 +00:00
|
|
|
import styles from './styles';
|
2018-01-30 19:48:26 +00:00
|
|
|
import CustomEmoji from '../EmojiPicker/CustomEmoji';
|
2018-05-29 17:09:20 +00:00
|
|
|
import MarkdownEmojiPlugin from './MarkdownEmojiPlugin';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-07 20:41:36 +00:00
|
|
|
// Support <http://link|Text>
|
|
|
|
const formatText = text =>
|
|
|
|
text.replace(
|
|
|
|
new RegExp('(?:<|<)((?:https|http):\\/\\/[^\\|]+)\\|(.+?)(?=>|>)(?:>|>)', 'gm'),
|
|
|
|
(match, url, title) => `[${ title }](${ url })`
|
|
|
|
);
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
@connect(state => ({
|
|
|
|
customEmojis: state.customEmojis
|
|
|
|
}))
|
|
|
|
export default class Markdown extends React.Component {
|
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
return nextProps.msg !== this.props.msg;
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
const {
|
2018-05-29 17:09:20 +00:00
|
|
|
msg, customEmojis, style, rules
|
2018-04-24 19:34:03 +00:00
|
|
|
} = this.props;
|
|
|
|
if (!msg) {
|
|
|
|
return null;
|
2018-01-16 18:48:05 +00:00
|
|
|
}
|
2018-05-07 20:41:36 +00:00
|
|
|
let m = formatText(msg);
|
|
|
|
m = emojify(m, { output: 'unicode' });
|
2018-04-24 19:34:03 +00:00
|
|
|
return (
|
2018-05-29 17:09:20 +00:00
|
|
|
<MarkdownRenderer
|
2018-04-24 19:34:03 +00:00
|
|
|
rules={{
|
2018-05-29 17:09:20 +00:00
|
|
|
...Platform.OS === 'android' ? {} : {
|
|
|
|
paragraph: (node, children) => (
|
|
|
|
<Text key={node.key} style={styles.paragraph}>
|
|
|
|
{children}
|
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
mention: node => (
|
|
|
|
<Text key={node.key} onPress={() => alert(`Username @${ node.content }`)} style={styles.mention}>
|
|
|
|
@{node.content}
|
|
|
|
</Text>
|
|
|
|
),
|
|
|
|
hashtag: node => (
|
|
|
|
<Text key={node.key} onPress={() => alert(`Room #${ node.content }`)} style={styles.mention}>
|
|
|
|
#{node.content}
|
|
|
|
</Text>
|
|
|
|
),
|
|
|
|
emoji: (node) => {
|
2018-07-18 20:34:59 +00:00
|
|
|
if (node.children && node.children.length && node.children[0].content) {
|
|
|
|
const { content } = node.children[0];
|
2018-04-24 19:34:03 +00:00
|
|
|
const emojiExtension = customEmojis[content];
|
|
|
|
if (emojiExtension) {
|
|
|
|
const emoji = { extension: emojiExtension, content };
|
2018-05-29 17:09:20 +00:00
|
|
|
return <CustomEmoji key={node.key} style={styles.customEmoji} emoji={emoji} />;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-05-29 17:09:20 +00:00
|
|
|
return <Text key={node.key}>:{content}:</Text>;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-05-29 17:09:20 +00:00
|
|
|
return null;
|
|
|
|
},
|
2018-07-18 20:34:59 +00:00
|
|
|
blocklink: () => {},
|
2018-05-29 17:09:20 +00:00
|
|
|
...rules
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
paragraph: styles.paragraph,
|
|
|
|
codeInline: {
|
|
|
|
borderWidth: 1,
|
|
|
|
borderColor: '#CCCCCC',
|
|
|
|
backgroundColor: '#f5f5f5',
|
|
|
|
padding: 2,
|
|
|
|
borderRadius: 4
|
2018-04-24 19:34:03 +00:00
|
|
|
},
|
2018-05-29 17:09:20 +00:00
|
|
|
...style
|
2018-04-24 19:34:03 +00:00
|
|
|
}}
|
2018-05-29 17:09:20 +00:00
|
|
|
plugins={[
|
|
|
|
new PluginContainer(MarkdownFlowdock),
|
|
|
|
new PluginContainer(MarkdownEmojiPlugin)
|
|
|
|
]}
|
2018-04-24 19:34:03 +00:00
|
|
|
>{m}
|
2018-05-29 17:09:20 +00:00
|
|
|
</MarkdownRenderer>
|
2018-04-24 19:34:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-12-02 13:19:58 +00:00
|
|
|
|
|
|
|
Markdown.propTypes = {
|
2018-04-24 19:34:03 +00:00
|
|
|
msg: PropTypes.string,
|
2018-02-16 18:34:25 +00:00
|
|
|
customEmojis: PropTypes.object,
|
2018-05-29 17:09:20 +00:00
|
|
|
style: PropTypes.any,
|
|
|
|
rules: PropTypes.object
|
2017-12-14 18:12:38 +00:00
|
|
|
};
|