2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
2019-04-08 12:35:28 +00:00
|
|
|
import { Text, Image } from 'react-native';
|
2017-12-02 13:19:58 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { emojify } from 'react-emojione';
|
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>
|
2018-09-25 19:28:42 +00:00
|
|
|
const formatText = text => text.replace(
|
|
|
|
new RegExp('(?:<|<)((?:https|http):\\/\\/[^\\|]+)\\|(.+?)(?=>|>)(?:>|>)', 'gm'),
|
|
|
|
(match, url, title) => `[${ title }](${ url })`
|
|
|
|
);
|
2018-05-07 20:41:36 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
export default class Markdown extends React.Component {
|
|
|
|
shouldComponentUpdate(nextProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { msg } = this.props;
|
|
|
|
return nextProps.msg !== msg;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
render() {
|
|
|
|
const {
|
2019-04-24 18:36:29 +00:00
|
|
|
msg, customEmojis, style, rules, baseUrl, username, edited, numberOfLines
|
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);
|
2019-04-18 12:43:31 +00:00
|
|
|
if (m) {
|
|
|
|
m = emojify(m, { output: 'unicode' });
|
|
|
|
}
|
2018-07-20 19:54:46 +00:00
|
|
|
m = m.replace(/^\[([^\]]*)\]\(([^)]*)\)/, '').trim();
|
2019-04-24 18:36:29 +00:00
|
|
|
if (numberOfLines > 0) {
|
|
|
|
m = m.replace(/[\n]+/g, '\n').trim();
|
|
|
|
}
|
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-09-11 16:32:52 +00:00
|
|
|
paragraph: (node, children) => (
|
2018-09-25 19:28:42 +00:00
|
|
|
// eslint-disable-next-line
|
2019-04-24 18:36:29 +00:00
|
|
|
<Text key={node.key} style={styles.paragraph} numberOfLines={numberOfLines}>
|
2018-09-25 19:28:42 +00:00
|
|
|
{children}
|
|
|
|
{edited ? <Text style={styles.edited}> (edited)</Text> : null}
|
2018-05-29 17:09:20 +00:00
|
|
|
</Text>
|
|
|
|
),
|
2018-09-11 16:32:52 +00:00
|
|
|
mention: (node) => {
|
|
|
|
const { content, key } = node;
|
|
|
|
let mentionStyle = styles.mention;
|
|
|
|
if (content === 'all' || content === 'here') {
|
|
|
|
mentionStyle = {
|
|
|
|
...mentionStyle,
|
|
|
|
...styles.mentionAll
|
|
|
|
};
|
|
|
|
} else if (content === username) {
|
|
|
|
mentionStyle = {
|
|
|
|
...mentionStyle,
|
|
|
|
...styles.mentionLoggedUser
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return (
|
2018-09-19 14:18:32 +00:00
|
|
|
<Text style={mentionStyle} key={key}>
|
2018-09-11 16:32:52 +00:00
|
|
|
{content}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
},
|
2018-05-29 17:09:20 +00:00
|
|
|
hashtag: node => (
|
2018-09-19 14:18:32 +00:00
|
|
|
<Text key={node.key} style={styles.mention}>
|
2018-09-11 16:32:52 +00:00
|
|
|
#{node.content}
|
2018-05-29 17:09:20 +00:00
|
|
|
</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-09-11 16:32:52 +00:00
|
|
|
return <CustomEmoji key={node.key} baseUrl={baseUrl} 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-08-10 17:26:36 +00:00
|
|
|
hardbreak: () => null,
|
|
|
|
blocklink: () => null,
|
2018-08-01 19:35:06 +00:00
|
|
|
image: node => (
|
|
|
|
<Image key={node.key} style={styles.inlineImage} source={{ uri: node.attributes.src }} />
|
|
|
|
),
|
2018-05-29 17:09:20 +00:00
|
|
|
...rules
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
paragraph: styles.paragraph,
|
2019-04-08 12:35:28 +00:00
|
|
|
text: styles.text,
|
|
|
|
codeInline: styles.codeInline,
|
|
|
|
codeBlock: styles.codeBlock,
|
|
|
|
link: styles.link,
|
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-09-11 16:32:52 +00:00
|
|
|
username: PropTypes.string.isRequired,
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
|
|
|
customEmojis: PropTypes.object.isRequired,
|
2018-05-29 17:09:20 +00:00
|
|
|
style: PropTypes.any,
|
2018-09-11 16:32:52 +00:00
|
|
|
rules: PropTypes.object,
|
2019-04-24 18:36:29 +00:00
|
|
|
edited: PropTypes.bool,
|
|
|
|
numberOfLines: PropTypes.number
|
2017-12-14 18:12:38 +00:00
|
|
|
};
|