Skip to content

Commit

Permalink
Extracts markdown rendering out of ChatMessageBox.tsx (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhonsby authored Apr 20, 2021
1 parent 0821ce0 commit a89ff64
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
41 changes: 41 additions & 0 deletions assets/src/components/MarkdownRenderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import ReactMarkdown from 'react-markdown';
import breaks from 'remark-breaks';
import {Twemoji} from 'react-emoji-render';
import {allowedNodeTypes} from './common';

const renderers = {
text: (props: any) => {
return <Twemoji text={props.children} />;
},
image: (props: any) => {
// TODO: fix scroll behavior after image loads
return (
<img
alt={props.alt || ''}
src={props.src}
{...props}
style={{maxWidth: '100%', maxHeight: 400}}
/>
);
},
};

type Props = {
className?: string;
source: string;
};

const MarkdownRenderer = ({className, source}: Props) => {
return (
<ReactMarkdown
className={`Text--markdown ${className}`}
source={source}
allowedTypes={allowedNodeTypes}
renderers={renderers}
plugins={[breaks]}
/>
);
};

export default MarkdownRenderer;
2 changes: 2 additions & 0 deletions assets/src/components/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
} from '@ant-design/colors';

import DatePicker from './DatePicker';
import MarkdownRenderer from './MarkdownRenderer';

export type {UploadChangeParam} from 'antd/lib/upload';
export type {UploadFile} from 'antd/lib/upload/interface';
Expand Down Expand Up @@ -122,6 +123,7 @@ export {
Dropdown,
Empty,
Input,
MarkdownRenderer,
Menu,
Modal,
notification,
Expand Down
30 changes: 2 additions & 28 deletions assets/src/components/conversations/ChatMessageBox.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import React from 'react';
import ReactMarkdown from 'react-markdown';
import breaks from 'remark-breaks';
import {Twemoji} from 'react-emoji-render';
import {Box} from 'theme-ui';
import {allowedNodeTypes} from '../common';
import {MarkdownRenderer} from '../common';
import {Attachment} from '../../types';
import {PaperClipOutlined} from '../icons';

const renderers = {
text: (props: any) => {
return <Twemoji text={props.children} />;
},
image: (props: any) => {
// TODO: fix scroll behavior after image loads
return (
<img
alt={props.alt || ''}
src={props.src}
{...props}
style={{maxWidth: '100%', maxHeight: 400}}
/>
);
},
};

const ChatMessageAttachment = ({
attachment,
color,
Expand Down Expand Up @@ -78,13 +58,7 @@ const ChatMessageBox = ({

return (
<Box sx={parsedSx}>
<ReactMarkdown
className={`Text--markdown ${className}`}
source={content}
allowedTypes={allowedNodeTypes}
renderers={renderers}
plugins={[breaks]}
/>
<MarkdownRenderer className={className} source={content} />

{attachments && attachments.length > 0 && (
<Box mt={2} className={className}>
Expand Down

0 comments on commit a89ff64

Please sign in to comment.