2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
2018-02-16 18:34:25 +00:00
|
|
|
import { Text, StyleSheet, ViewPropTypes } from 'react-native';
|
2017-12-02 13:19:58 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import EasyMarkdown from 'react-native-easy-markdown'; // eslint-disable-line
|
2017-12-14 18:12:38 +00:00
|
|
|
import SimpleMarkdown from 'simple-markdown';
|
2017-12-02 13:19:58 +00:00
|
|
|
import { emojify } from 'react-emojione';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
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';
|
2017-12-14 18:12:38 +00:00
|
|
|
|
|
|
|
const BlockCode = ({ node, state }) => (
|
|
|
|
<Text
|
|
|
|
key={state.key}
|
2018-01-16 18:48:05 +00:00
|
|
|
style={styles.codeStyle}
|
2017-12-14 18:12:38 +00:00
|
|
|
>
|
|
|
|
{node.content}
|
|
|
|
</Text>
|
|
|
|
);
|
2018-01-09 17:12:55 +00:00
|
|
|
const mentionStyle = { color: '#13679a' };
|
2017-12-14 18:12:38 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const defaultRules = {
|
|
|
|
username: {
|
|
|
|
order: -1,
|
|
|
|
match: SimpleMarkdown.inlineRegex(/^@[0-9a-zA-Z-_.]+/),
|
|
|
|
parse: capture => ({ content: capture[0] }),
|
|
|
|
react: (node, output, state) => ({
|
|
|
|
type: 'custom',
|
|
|
|
key: state.key,
|
|
|
|
props: {
|
|
|
|
children: (
|
|
|
|
<Text
|
|
|
|
key={state.key}
|
|
|
|
style={mentionStyle}
|
|
|
|
onPress={() => alert('Username')}
|
|
|
|
>
|
|
|
|
{node.content}
|
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
heading: {
|
|
|
|
order: -2,
|
|
|
|
match: SimpleMarkdown.inlineRegex(/^#[0-9a-zA-Z-_.]+/),
|
|
|
|
parse: capture => ({ content: capture[0] }),
|
|
|
|
react: (node, output, state) => ({
|
|
|
|
type: 'custom',
|
|
|
|
key: state.key,
|
|
|
|
props: {
|
|
|
|
children: (
|
|
|
|
<Text
|
|
|
|
key={state.key}
|
|
|
|
style={mentionStyle}
|
|
|
|
onPress={() => alert('Room')}
|
|
|
|
>
|
|
|
|
{node.content}
|
|
|
|
</Text>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
fence: {
|
|
|
|
order: -3,
|
|
|
|
match: SimpleMarkdown.blockRegex(/^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n *)+\n/),
|
|
|
|
parse: capture => ({
|
|
|
|
lang: capture[2] || undefined,
|
|
|
|
content: capture[3]
|
|
|
|
}),
|
|
|
|
react: (node, output, state) => ({
|
|
|
|
type: 'custom',
|
|
|
|
key: state.key,
|
|
|
|
props: {
|
|
|
|
children: (
|
|
|
|
<BlockCode key={state.key} node={node} state={state} />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
blockCode: {
|
|
|
|
order: -4,
|
|
|
|
match: SimpleMarkdown.blockRegex(/^(```)\s*([\s\S]*?[^`])\s*\1(?!```)/),
|
|
|
|
parse: capture => ({ content: capture[2] }),
|
|
|
|
react: (node, output, state) => ({
|
|
|
|
type: 'custom',
|
|
|
|
key: state.key,
|
|
|
|
props: {
|
|
|
|
children: (
|
|
|
|
<BlockCode key={state.key} node={node} state={state} />
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
2018-01-16 18:48:05 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const codeStyle = StyleSheet.flatten(styles.codeStyle);
|
|
|
|
|
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 {
|
|
|
|
msg, customEmojis = {}, style, markdownStyle, customRules, renderInline
|
|
|
|
} = 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-01-16 18:48:05 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const s = StyleSheet.flatten(style);
|
|
|
|
return (
|
|
|
|
<EasyMarkdown
|
|
|
|
style={{ marginBottom: 0, ...s }}
|
|
|
|
markdownStyles={{ code: codeStyle, ...markdownStyle }}
|
|
|
|
rules={{
|
|
|
|
customEmoji: {
|
|
|
|
order: -5,
|
|
|
|
match: SimpleMarkdown.inlineRegex(/^:([0-9a-zA-Z-_.]+):/),
|
|
|
|
parse: capture => ({ content: capture }),
|
|
|
|
react: (node, output, state) => {
|
|
|
|
const element = {
|
|
|
|
type: 'custom',
|
|
|
|
key: state.key,
|
|
|
|
props: {
|
|
|
|
children: <Text key={state.key}>{node.content[0]}</Text>
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const content = node.content[1];
|
|
|
|
const emojiExtension = customEmojis[content];
|
|
|
|
if (emojiExtension) {
|
|
|
|
const emoji = { extension: emojiExtension, content };
|
|
|
|
element.props.children = (
|
|
|
|
<CustomEmoji key={state.key} style={styles.customEmoji} emoji={emoji} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...defaultRules,
|
|
|
|
...customRules
|
|
|
|
}}
|
|
|
|
renderInline={renderInline}
|
|
|
|
>{m}
|
|
|
|
</EasyMarkdown>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
// eslint-disable-next-line react/no-typos
|
|
|
|
style: ViewPropTypes.style,
|
|
|
|
markdownStyle: PropTypes.object,
|
|
|
|
customRules: PropTypes.object,
|
|
|
|
renderInline: PropTypes.bool
|
2017-12-02 13:19:58 +00:00
|
|
|
};
|
|
|
|
|
2017-12-14 18:12:38 +00:00
|
|
|
BlockCode.propTypes = {
|
|
|
|
node: PropTypes.object,
|
|
|
|
state: PropTypes.object
|
|
|
|
};
|