import React from 'react'; import { Text, Image } from 'react-native'; import PropTypes from 'prop-types'; import { emojify } from 'react-emojione'; import MarkdownRenderer, { PluginContainer } from 'react-native-markdown-renderer'; import MarkdownFlowdock from 'markdown-it-flowdock'; import styles from './styles'; import CustomEmoji from '../EmojiPicker/CustomEmoji'; import MarkdownEmojiPlugin from './MarkdownEmojiPlugin'; import I18n from '../../i18n'; const EmojiPlugin = new PluginContainer(MarkdownEmojiPlugin); const MentionsPlugin = new PluginContainer(MarkdownFlowdock); const plugins = [EmojiPlugin, MentionsPlugin]; // Support const formatText = text => text.replace( new RegExp('(?:<|<)((?:https|http):\\/\\/[^\\|]+)\\|(.+?)(?=>|>)(?:>|>)', 'gm'), (match, url, title) => `[${ title }](${ url })` ); const Markdown = React.memo(({ msg, style, rules, baseUrl, username, isEdited, numberOfLines, mentions, channels, getCustomEmoji, useMarkdown = true }) => { if (!msg) { return null; } let m = formatText(msg); if (m) { m = emojify(m, { output: 'unicode' }); } m = m.replace(/^\[([^\]]*)\]\(([^)]*)\)/, '').trim(); if (numberOfLines > 0) { m = m.replace(/[\n]+/g, '\n').trim(); } if (!useMarkdown) { return {m}; } return ( ( {children} {isEdited ? ({I18n.t('edited')}) : null} ), 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 }; } if (mentions && mentions.length && mentions.findIndex(mention => mention.username === content) !== -1) { return (  {content}  ); } return `@${ content }`; }, hashtag: (node) => { const { content, key } = node; if (channels && channels.length && channels.findIndex(channel => channel.name === content) !== -1) { return (  #{content}  ); } return `#${ content }`; }, emoji: (node) => { if (node.children && node.children.length && node.children[0].content) { const { content } = node.children[0]; const emoji = getCustomEmoji && getCustomEmoji(content); if (emoji) { return ; } return :{content}:; } return null; }, hardbreak: () => null, blocklink: () => null, image: node => ( ), ...rules }} style={{ paragraph: styles.paragraph, text: styles.text, codeInline: styles.codeInline, codeBlock: styles.codeBlock, link: styles.link, ...style }} plugins={plugins} >{m} ); }, (prevProps, nextProps) => prevProps.msg === nextProps.msg); Markdown.propTypes = { msg: PropTypes.string, username: PropTypes.string, baseUrl: PropTypes.string, style: PropTypes.any, rules: PropTypes.object, isEdited: PropTypes.bool, numberOfLines: PropTypes.number, useMarkdown: PropTypes.bool, mentions: PropTypes.oneOfType([PropTypes.array, PropTypes.object]), channels: PropTypes.oneOfType([PropTypes.array, PropTypes.object]), getCustomEmoji: PropTypes.func }; Markdown.displayName = 'MessageMarkdown'; export default Markdown;