Rocket.Chat.ReactNative/app/containers/markdown/index.tsx

327 lines
8.8 KiB
TypeScript
Raw Normal View History

import React, { useCallback, useEffect, useReducer, useRef } from 'react';
import { Image, StyleProp, Text, TextStyle } from 'react-native';
import { Parser } from 'commonmark';
import Renderer from 'commonmark-react-renderer';
[NEW] Support new message parser (#3313) * Add message parser to profile view and db * Add md to db * Remove changes to Xcode project * Remove message-parser lib and add enable message parser field to User model * Fix message parser * Remove admin enableMessageParserEarlyAdoption * Add NewMarkdown component * Remove NewMarkdown component and add specific components for new message parser * Add new parser components * Fix BigEmoji * Updated components and added more Code components * update components and add storybooks * Update Code component and add it to storybooks * Update Mention component * Minor tweaks * Add server message parser validation * Renamed folder, add @rocket.chat/message-parser, migrate some files to TypeScript * Migrate components to TypeScript and fix styling * Change interfaces and add TaskListComponent and styles * Fix new markdown and styles * Fix inlinecode * Stop using server setting * Use enableMessageParserEarlyAdoption on mapStateToProps * Remove React.FC * add link to bold, italic and strike * Update parser components * Fix missing components * Minor tweak * Fix lint and add getCustomEmojis * Fix customEmojis * Update emojis * Minor tweak * disconnect markdown from store * Use @rocket.chat/message-parser@0.30.0 * Fix link style * Unify lists and styles * Remove style prop * Use big emoji as a normal token * Remove unnecessary memo * Fix code styles * Update tests * Conditionally create renderer * Use Context instead of prop drill * Fix Link component * Fix plain text regression and update tests Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-10-20 16:32:58 +00:00
import { MarkdownAST } from '@rocket.chat/message-parser';
import MarkdownLink from './Link';
import MarkdownList from './List';
import MarkdownListItem from './ListItem';
import MarkdownAtMention from './AtMention';
import MarkdownHashtag from './Hashtag';
import MarkdownBlockQuote from './BlockQuote';
import MarkdownEmoji from './Emoji';
import MarkdownTable from './Table';
import MarkdownTableRow, { ITableRow } from './TableRow';
import MarkdownTableCell, { ITableCell } from './TableCell';
2020-02-28 16:18:03 +00:00
import mergeTextNodes from './mergeTextNodes';
import styles from './styles';
import { isValidURL } from '../../lib/methods/helpers';
[NEW] Support new message parser (#3313) * Add message parser to profile view and db * Add md to db * Remove changes to Xcode project * Remove message-parser lib and add enable message parser field to User model * Fix message parser * Remove admin enableMessageParserEarlyAdoption * Add NewMarkdown component * Remove NewMarkdown component and add specific components for new message parser * Add new parser components * Fix BigEmoji * Updated components and added more Code components * update components and add storybooks * Update Code component and add it to storybooks * Update Mention component * Minor tweaks * Add server message parser validation * Renamed folder, add @rocket.chat/message-parser, migrate some files to TypeScript * Migrate components to TypeScript and fix styling * Change interfaces and add TaskListComponent and styles * Fix new markdown and styles * Fix inlinecode * Stop using server setting * Use enableMessageParserEarlyAdoption on mapStateToProps * Remove React.FC * add link to bold, italic and strike * Update parser components * Fix missing components * Minor tweak * Fix lint and add getCustomEmojis * Fix customEmojis * Update emojis * Minor tweak * disconnect markdown from store * Use @rocket.chat/message-parser@0.30.0 * Fix link style * Unify lists and styles * Remove style prop * Use big emoji as a normal token * Remove unnecessary memo * Fix code styles * Update tests * Conditionally create renderer * Use Context instead of prop drill * Fix Link component * Fix plain text regression and update tests Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-10-20 16:32:58 +00:00
import NewMarkdown from './new';
import { formatText } from './formatText';
import { IUserMention, IUserChannel, TOnLinkPress } from './interfaces';
import { TGetCustomEmoji } from '../../definitions';
import { formatHyperlink } from './formatHyperlink';
import { useTheme } from '../../theme';
import { IRoomInfoParam } from '../../views/SearchMessagesView';
export { default as MarkdownPreview } from './Preview';
[NEW] Support new message parser (#3313) * Add message parser to profile view and db * Add md to db * Remove changes to Xcode project * Remove message-parser lib and add enable message parser field to User model * Fix message parser * Remove admin enableMessageParserEarlyAdoption * Add NewMarkdown component * Remove NewMarkdown component and add specific components for new message parser * Add new parser components * Fix BigEmoji * Updated components and added more Code components * update components and add storybooks * Update Code component and add it to storybooks * Update Mention component * Minor tweaks * Add server message parser validation * Renamed folder, add @rocket.chat/message-parser, migrate some files to TypeScript * Migrate components to TypeScript and fix styling * Change interfaces and add TaskListComponent and styles * Fix new markdown and styles * Fix inlinecode * Stop using server setting * Use enableMessageParserEarlyAdoption on mapStateToProps * Remove React.FC * add link to bold, italic and strike * Update parser components * Fix missing components * Minor tweak * Fix lint and add getCustomEmojis * Fix customEmojis * Update emojis * Minor tweak * disconnect markdown from store * Use @rocket.chat/message-parser@0.30.0 * Fix link style * Unify lists and styles * Remove style prop * Use big emoji as a normal token * Remove unnecessary memo * Fix code styles * Update tests * Conditionally create renderer * Use Context instead of prop drill * Fix Link component * Fix plain text regression and update tests Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-10-20 16:32:58 +00:00
export interface IMarkdownProps {
msg?: string | null;
md?: MarkdownAST;
mentions?: IUserMention[];
getCustomEmoji?: TGetCustomEmoji;
baseUrl?: string;
username?: string;
tmid?: string;
numberOfLines?: number;
customEmojis?: boolean;
useRealName?: boolean;
channels?: IUserChannel[];
enableMessageParser?: boolean;
navToRoomInfo?: (params: IRoomInfoParam) => void;
testID?: string;
style?: StyleProp<TextStyle>[];
onLinkPress?: TOnLinkPress;
}
type TLiteral = {
literal: string;
};
const emojiRanges = [
'\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]', // unicode emoji from https://www.regextester.com/106421
':.{1,40}:', // custom emoji
' |\n' // allow spaces and line breaks
].join('|');
const removeSpaces = (str: string) => str && str.replace(/\s/g, '');
const removeAllEmoji = (str: string) => str.replace(new RegExp(emojiRanges, 'g'), '');
const isOnlyEmoji = (str: string) => {
str = removeSpaces(str);
return !removeAllEmoji(str).length;
};
const removeOneEmoji = (str: string) => str.replace(new RegExp(emojiRanges), '');
const emojiCount = (str: string) => {
str = removeSpaces(str);
let oldLength = 0;
let counter = 0;
while (oldLength !== str.length) {
oldLength = str.length;
str = removeOneEmoji(str);
if (oldLength !== str.length) {
counter += 1;
}
}
return counter;
};
const parser = new Parser();
export const markdownTestID = 'markdown';
const Markdown = ({
msg,
md,
mentions,
getCustomEmoji,
baseUrl,
username,
tmid,
numberOfLines,
customEmojis,
useRealName,
channels,
enableMessageParser,
navToRoomInfo,
style,
onLinkPress
}: IMarkdownProps) => {
const { colors } = useTheme();
const renderer = useRef<any>();
const isMessageContainsOnlyEmoji = useRef(false);
const [, forceUpdate] = useReducer(x => x + 1, 0);
const isNewMarkdown = useCallback(() => !!enableMessageParser && !!md, [enableMessageParser, md]);
const createRenderer = useCallback(
() =>
new Renderer({
renderers: {
text: renderText,
emph: Renderer.forwardChildren,
strong: Renderer.forwardChildren,
del: Renderer.forwardChildren,
code: renderCodeInline,
link: renderLink,
image: renderImage,
atMention: renderAtMention,
emoji: renderEmoji,
hashtag: renderHashtag,
paragraph: renderParagraph,
heading: renderHeading,
codeBlock: renderCodeBlock,
blockQuote: renderBlockQuote,
list: renderList,
item: renderListItem,
hardBreak: renderBreak,
thematicBreak: renderBreak,
softBreak: renderBreak,
htmlBlock: renderText,
htmlInline: renderText,
table: renderTable,
table_row: renderTableRow,
table_cell: renderTableCell
},
renderParagraphsInLists: true
}),
[]
);
useEffect(() => {
if (!isNewMarkdown() && msg) {
renderer.current = createRenderer();
forceUpdate();
[NEW] Support new message parser (#3313) * Add message parser to profile view and db * Add md to db * Remove changes to Xcode project * Remove message-parser lib and add enable message parser field to User model * Fix message parser * Remove admin enableMessageParserEarlyAdoption * Add NewMarkdown component * Remove NewMarkdown component and add specific components for new message parser * Add new parser components * Fix BigEmoji * Updated components and added more Code components * update components and add storybooks * Update Code component and add it to storybooks * Update Mention component * Minor tweaks * Add server message parser validation * Renamed folder, add @rocket.chat/message-parser, migrate some files to TypeScript * Migrate components to TypeScript and fix styling * Change interfaces and add TaskListComponent and styles * Fix new markdown and styles * Fix inlinecode * Stop using server setting * Use enableMessageParserEarlyAdoption on mapStateToProps * Remove React.FC * add link to bold, italic and strike * Update parser components * Fix missing components * Minor tweak * Fix lint and add getCustomEmojis * Fix customEmojis * Update emojis * Minor tweak * disconnect markdown from store * Use @rocket.chat/message-parser@0.30.0 * Fix link style * Unify lists and styles * Remove style prop * Use big emoji as a normal token * Remove unnecessary memo * Fix code styles * Update tests * Conditionally create renderer * Use Context instead of prop drill * Fix Link component * Fix plain text regression and update tests Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-10-20 16:32:58 +00:00
}
}, [createRenderer, isNewMarkdown, msg]);
if (!msg) {
return null;
[NEW] Support new message parser (#3313) * Add message parser to profile view and db * Add md to db * Remove changes to Xcode project * Remove message-parser lib and add enable message parser field to User model * Fix message parser * Remove admin enableMessageParserEarlyAdoption * Add NewMarkdown component * Remove NewMarkdown component and add specific components for new message parser * Add new parser components * Fix BigEmoji * Updated components and added more Code components * update components and add storybooks * Update Code component and add it to storybooks * Update Mention component * Minor tweaks * Add server message parser validation * Renamed folder, add @rocket.chat/message-parser, migrate some files to TypeScript * Migrate components to TypeScript and fix styling * Change interfaces and add TaskListComponent and styles * Fix new markdown and styles * Fix inlinecode * Stop using server setting * Use enableMessageParserEarlyAdoption on mapStateToProps * Remove React.FC * add link to bold, italic and strike * Update parser components * Fix missing components * Minor tweak * Fix lint and add getCustomEmojis * Fix customEmojis * Update emojis * Minor tweak * disconnect markdown from store * Use @rocket.chat/message-parser@0.30.0 * Fix link style * Unify lists and styles * Remove style prop * Use big emoji as a normal token * Remove unnecessary memo * Fix code styles * Update tests * Conditionally create renderer * Use Context instead of prop drill * Fix Link component * Fix plain text regression and update tests Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-10-20 16:32:58 +00:00
}
const renderText = ({ context, literal }: { context: []; literal: string }) => {
const defaultStyle = [isMessageContainsOnlyEmoji.current ? styles.textBig : {}, ...context.map(type => styles[type])];
2019-12-04 16:39:53 +00:00
return (
<Text accessibilityLabel={literal} style={[styles.text, defaultStyle, ...(style || [])]} numberOfLines={numberOfLines}>
2019-12-04 16:39:53 +00:00
{literal}
</Text>
);
2019-10-02 12:41:51 +00:00
};
const renderCodeInline = ({ literal }: TLiteral) => (
<Text
testID={`${markdownTestID}-code-in-line`}
style={[
{
...styles.codeInline,
color: colors.bodyText,
backgroundColor: colors.bannerBackground,
borderColor: colors.bannerBackground
},
...(style || [])
]}>
{literal}
</Text>
);
const renderCodeBlock = ({ literal }: TLiteral) => (
<Text
testID={`${markdownTestID}-code-block`}
style={[
{
...styles.codeBlock,
color: colors.bodyText,
backgroundColor: colors.bannerBackground,
borderColor: colors.bannerBackground
},
...(style || [])
]}>
{literal}
</Text>
);
const renderBreak = () => <Text>{tmid ? ' ' : '\n'}</Text>;
const renderParagraph = ({ children }: { children: React.ReactElement[] }) => {
if (!children || children.length === 0) {
return null;
}
return (
<Text style={[styles.text, style, { color: colors.bodyText }]} numberOfLines={numberOfLines}>
2019-10-02 12:41:51 +00:00
{children}
</Text>
);
};
const renderLink = ({ children, href }: { children: React.ReactElement | null; href: string }) => (
<MarkdownLink link={href} onLinkPress={onLinkPress} testID={markdownTestID}>
{children}
</MarkdownLink>
);
const renderHashtag = ({ hashtag }: { hashtag: string }) => (
<MarkdownHashtag hashtag={hashtag} channels={channels} navToRoomInfo={navToRoomInfo} style={style} testID={markdownTestID} />
);
const renderAtMention = ({ mentionName }: { mentionName: string }) => (
<MarkdownAtMention
mentions={mentions}
mention={mentionName}
useRealName={useRealName}
username={username}
navToRoomInfo={navToRoomInfo}
style={style}
testID={markdownTestID}
/>
);
const renderEmoji = ({ literal }: TLiteral) => (
<MarkdownEmoji
literal={literal}
isMessageContainsOnlyEmoji={isMessageContainsOnlyEmoji.current}
getCustomEmoji={getCustomEmoji}
baseUrl={baseUrl || ''}
customEmojis={customEmojis}
style={style}
testID={markdownTestID}
/>
);
const renderImage = ({ src }: { src: string }) => {
if (!isValidURL(src)) {
return null;
}
return <Image style={styles.inlineImage} source={{ uri: encodeURI(src) }} testID={`${markdownTestID}-image`} />;
};
const renderHeading = ({ children, level }: { children: React.ReactElement; level: string }) => {
2022-03-21 20:44:06 +00:00
// @ts-ignore
const textStyle = styles[`heading${level}Text`];
return (
<Text testID={`${markdownTestID}-header`} numberOfLines={numberOfLines} style={[textStyle, { color: colors.bodyText }]}>
{children}
</Text>
);
};
const renderList = ({ children, start, tight, type }: any) => (
<MarkdownList ordered={type !== 'bullet'} start={start} tight={tight} numberOfLines={numberOfLines}>
{children}
</MarkdownList>
);
const renderListItem = ({ children, context, ...otherProps }: any) => {
const level = context.filter((type: string) => type === 'list').length;
return (
<MarkdownListItem level={level} {...otherProps}>
{children}
</MarkdownListItem>
);
};
const renderBlockQuote = ({ children }: { children: React.ReactElement }) => (
<MarkdownBlockQuote>{children}</MarkdownBlockQuote>
);
const renderTable = ({ children, numColumns }: { children: React.ReactElement; numColumns: number }) => (
<MarkdownTable numColumns={numColumns} testID={markdownTestID}>
{children}
</MarkdownTable>
);
const renderTableRow = ({ children, isLastRow }: ITableRow) => (
<MarkdownTableRow isLastRow={isLastRow}>{children}</MarkdownTableRow>
);
const renderTableCell = ({ align, children, isLastCell }: ITableCell) => (
<MarkdownTableCell align={align} isLastCell={isLastCell}>
{children}
</MarkdownTableCell>
);
[NEW] Support new message parser (#3313) * Add message parser to profile view and db * Add md to db * Remove changes to Xcode project * Remove message-parser lib and add enable message parser field to User model * Fix message parser * Remove admin enableMessageParserEarlyAdoption * Add NewMarkdown component * Remove NewMarkdown component and add specific components for new message parser * Add new parser components * Fix BigEmoji * Updated components and added more Code components * update components and add storybooks * Update Code component and add it to storybooks * Update Mention component * Minor tweaks * Add server message parser validation * Renamed folder, add @rocket.chat/message-parser, migrate some files to TypeScript * Migrate components to TypeScript and fix styling * Change interfaces and add TaskListComponent and styles * Fix new markdown and styles * Fix inlinecode * Stop using server setting * Use enableMessageParserEarlyAdoption on mapStateToProps * Remove React.FC * add link to bold, italic and strike * Update parser components * Fix missing components * Minor tweak * Fix lint and add getCustomEmojis * Fix customEmojis * Update emojis * Minor tweak * disconnect markdown from store * Use @rocket.chat/message-parser@0.30.0 * Fix link style * Unify lists and styles * Remove style prop * Use big emoji as a normal token * Remove unnecessary memo * Fix code styles * Update tests * Conditionally create renderer * Use Context instead of prop drill * Fix Link component * Fix plain text regression and update tests Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-10-20 16:32:58 +00:00
if (isNewMarkdown()) {
return (
<NewMarkdown
username={username || ''}
baseUrl={baseUrl || ''}
getCustomEmoji={getCustomEmoji}
useRealName={useRealName}
tokens={md}
mentions={mentions}
channels={channels}
navToRoomInfo={navToRoomInfo}
onLinkPress={onLinkPress}
/>
);
}
2019-12-04 16:39:53 +00:00
const formattedMessage = formatHyperlink(formatText(msg));
const ast = mergeTextNodes(parser.parse(formattedMessage));
isMessageContainsOnlyEmoji.current = isOnlyEmoji(formattedMessage) && emojiCount(formattedMessage) <= 3;
return renderer?.current?.render(ast) || null;
};
2019-12-04 16:39:53 +00:00
export default Markdown;