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: crash markdown render code block without triple backtick #4248

Merged
merged 1 commit into from
Dec 9, 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
1 change: 0 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"rehype-highlight": "^7.0.1",
"rehype-highlight-code-lines": "^1.0.4",
"rehype-katex": "^7.0.1",
"rehype-raw": "^7.0.0",
"remark-math": "^6.0.0",
"sass": "^1.69.4",
"slate": "latest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import rehypeHighlight from 'rehype-highlight'
import rehypeHighlightCodeLines from 'rehype-highlight-code-lines'

import rehypeKatex from 'rehype-katex'
import rehypeRaw from 'rehype-raw'

import remarkMath from 'remark-math'

import 'katex/dist/katex.min.css'
Expand All @@ -19,10 +20,10 @@

export const MarkdownTextMessage = memo(
({ text }: { id: string; text: string }) => {
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

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

26 line is not covered with tests

// Helper function to extract text recursively from children
function getTextFromNode(node: {
Expand All @@ -30,44 +31,44 @@
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 37 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

34-37 lines are not covered with tests
}
return ''

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

View workflow job for this annotation

GitHub Actions / coverage-check

39 line is not covered with tests
}

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

43 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 52 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

50-52 lines are not covered with tests
}
}
)

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

58 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 63 in web/screens/Thread/ThreadCenterPanel/TextMessage/MarkdownTextMessage.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

65 line is not covered with tests
'language-',
''
)

if (extractCodeLines(node) === '') {
return node

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

View workflow job for this annotation

GitHub Actions / coverage-check

70-71 lines are not covered with tests
}

return {
Expand Down Expand Up @@ -198,12 +199,10 @@
remarkPlugins={[remarkMath]}
rehypePlugins={[
[rehypeKatex, { throwOnError: false }],
rehypeRaw,
rehypeHighlight,
[rehypeHighlightCodeLines, { showLineNumbers: true }],
wrapCodeBlocksWithoutVisit,
]}
skipHtml={true}
>
{text}
</Markdown>
Expand Down
Loading