Skip to content

Commit

Permalink
fix: escape heading on user message item (janhq#4301)
Browse files Browse the repository at this point in the history
  • Loading branch information
urmauur authored Dec 19, 2024
1 parent c7a5cb5 commit 7140978
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []

Expand Down Expand Up @@ -204,7 +210,7 @@ export const MarkdownTextMessage = memo(
wrapCodeBlocksWithoutVisit,
]}
>
{text}
{preprocessMarkdown(text)}
</Markdown>
</>
)
Expand Down
6 changes: 5 additions & 1 deletion web/screens/Thread/ThreadCenterPanel/TextMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
)}
</>
Expand Down

0 comments on commit 7140978

Please sign in to comment.