fix: escape heading on user message item (#4301)

This commit is contained in:
Faisal Amir 2024-12-19 23:38:55 +08:00 committed by GitHub
parent c7a5cb52e3
commit 714097851a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -19,9 +19,15 @@ import { useClipboard } from '@/hooks/useClipboard'
import { getLanguageFromExtension } from '@/utils/codeLanguageExtension'
export const MarkdownTextMessage = memo(
({ text }: { id: string; text: string }) => {
({ text, isUser }: { id: string; text: string; isUser: boolean }) => {
const clipboard = useClipboard({ timeout: 1000 })
// Escapes headings
function preprocessMarkdown(text: string): string {
if (!isUser) return text
return text.replace(/^#{1,6} /gm, (match) => `\\${match}`)
}
function extractCodeLines(node: { children: { children: any[] }[] }) {
const codeLines: any[] = []
@ -204,7 +210,7 @@ export const MarkdownTextMessage = memo(
wrapCodeBlocksWithoutVisit,
]}
>
{text}
{preprocessMarkdown(text)}
</Markdown>
</>
)

View File

@ -144,7 +144,11 @@ const MessageContainer: React.FC<
)}
dir="ltr"
>
<MarkdownTextMessage id={props.id} text={text} />
<MarkdownTextMessage
id={props.id}
text={text}
isUser={isUser}
/>
</div>
)}
</>