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

fix: escape heading on user message item #4301

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
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 })

Check warning on line 23 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

23 line is not covered with tests

// Escapes headings
function preprocessMarkdown(text: string): string {
if (!isUser) return text
return text.replace(/^#{1,6} /gm, (match) => `\\${match}`)

Check warning on line 28 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

27-28 lines are not covered with tests
}

function extractCodeLines(node: { children: { children: any[] }[] }) {
const codeLines: any[] = []

Check warning on line 32 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

32 line is not covered with tests

// Helper function to extract text recursively from children
function getTextFromNode(node: {
Expand All @@ -31,38 +37,38 @@
value: any
children: any[]
}): string {
if (node.type === 'text') {
return node.value
} else if (node.children) {
return node.children.map(getTextFromNode).join('')

Check warning on line 43 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

40-43 lines are not covered with tests
}
return ''

Check warning on line 45 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

45 line is not covered with tests
}

// Traverse each line in the <code> block
node.children[0].children.forEach(

Check warning on line 49 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

49 line is not covered with tests
(lineNode: {
type: string
tagName: string
value: any
children: any[]
}) => {
if (lineNode.type === 'element' && lineNode.tagName === 'span') {
const lineContent = getTextFromNode(lineNode)
codeLines.push(lineContent)

Check warning on line 58 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

56-58 lines are not covered with tests
}
}
)

// Join the lines with newline characters for proper formatting
return codeLines.join('\n')

Check warning on line 64 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

64 line is not covered with tests
}
function wrapCodeBlocksWithoutVisit() {
return (tree: { children: any[] }) => {
tree.children = tree.children.map((node) => {
if (node.tagName === 'pre' && node.children[0]?.tagName === 'code') {

Check warning on line 69 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

67-69 lines are not covered with tests
const language =
node.children[0].properties.className?.[1]?.replace(

Check warning on line 71 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

71 line is not covered with tests
'language-',
''
)
Expand Down Expand Up @@ -204,7 +210,7 @@
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 @@ -141,7 +141,11 @@ const MessageContainer: React.FC<
)}
dir="ltr"
>
<MarkdownTextMessage id={props.id} text={text} />
<MarkdownTextMessage
id={props.id}
text={text}
isUser={isUser}
/>
</div>
)}
</>
Expand Down
Loading