Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use Markdown when showing user-submitted messages #1084

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/app/components/Chat/ChatMessageCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ChatMessageModel } from '~/types'
import Markdown from '../Markdown'
import ErrorAction from './ErrorAction'
import MessageBubble from './MessageBubble'
import React from 'react';


const COPY_ICON_CLASS = 'self-top cursor-pointer invisible group-hover:visible mt-[12px] text-primary-text'

Expand Down Expand Up @@ -37,14 +39,34 @@ const ChatMessageCard: FC<Props> = ({ message, className }) => {
}
}, [copied])

/* For user-submitted messages do not use Markdown.
Split lines and replace leading spaces with nbsp*/
const preprocessMessage = (text: string) => {
return text.split('\n').map((line: string, index: number) => {
// Replace leading spaces with 'nbsp; entities
const processedLine = line.replace(/^[\s\t]+/g, (match: string) =>
match.replace(/ /g, '\u00a0').replace(/\t/g, '\u00a0\u00a0'));

return (
<React.Fragment key={index}>
<span style={{ whiteSpace: 'pre-wrap' }}>{processedLine}</span>
{index !== text.split('\n').length - 1 && <br />}
</React.Fragment>
);
});
};

return (
<div
className={cx('group flex gap-3 w-full', message.author === 'user' ? 'flex-row-reverse' : 'flex-row', className)}
>
<div className="flex flex-col w-11/12 max-w-fit items-start gap-2">
<MessageBubble color={message.author === 'user' ? 'primary' : 'flat'}>
{!!imageUrl && <img src={imageUrl} className="max-w-xs my-2" />}
{message.text ? (
{message.author === 'user' ? (
/*Do not use Markdown for user-submitted messages to avoid stripping leading spaces*/
preprocessMessage(message.text)
) : message.text ? (
<Markdown>{message.text}</Markdown>
) : (
!message.error && <BeatLoader size={10} className="leading-tight" color="rgb(var(--primary-text))" />
Expand Down