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: edge cases empty code block #4061

Merged
merged 1 commit into from
Nov 21, 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 @@ -48,20 +48,20 @@
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'

const SimpleTextMessage: React.FC<ThreadMessage> = (props) => {
let text = ''
const isUser = props.role === ChatCompletionRole.User
const isSystem = props.role === ChatCompletionRole.System
const editMessage = useAtomValue(editMessageAtom)
const activeThread = useAtomValue(activeThreadAtom)

Check warning on line 55 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

51-55 lines are not covered with tests

if (props.content && props.content.length > 0) {
text = props.content[0]?.text?.value ?? ''

Check warning on line 58 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

57-58 lines are not covered with tests
}

const clipboard = useClipboard({ timeout: 1000 })

Check warning on line 61 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

61 line is not covered with tests

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

Check warning on line 64 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

64 line is not covered with tests

// Helper function to extract text recursively from children
function getTextFromNode(node: {
Expand All @@ -69,42 +69,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 75 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

72-75 lines are not covered with tests
}
return ''

Check warning on line 77 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

77 line is not covered with tests
}

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

Check warning on line 81 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

81 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 90 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

88-90 lines are not covered with tests
}
}
)

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

Check warning on line 96 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

96 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') {
const language = node.children[0].properties.className?.[1]?.replace(

Check warning on line 102 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

99-102 lines are not covered with tests
'language-',
''
)

if (!language) return node
if (extractCodeLines(node) === '') {
return node
}

return {
type: 'element',
Expand Down Expand Up @@ -145,7 +147,7 @@
type: 'text',
value: language
? `${getLanguageFromExtension(language)}`
: 'No file name',
: '',
},
],
},
Expand Down
13 changes: 6 additions & 7 deletions web/styles/components/code-block.scss
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
.hljs {
overflow: auto;
display: block;
padding: 16px;
font-size: 14px;
border-bottom-left-radius: 0.4rem;
border-bottom-right-radius: 0.4rem;
Expand All @@ -65,15 +64,16 @@
pre {
background: hsla(var(--app-code-block));
overflow: auto;
padding: 8px 16px;

border-radius: 0.4rem;
}
pre > code {
text-indent: 0;
white-space: pre;
font-size: 14px;
overflow: auto;
color: #f8f8f2;
display: block;
padding: 16px;
}

.hljs-emphasis {
Expand Down Expand Up @@ -155,12 +155,11 @@ span.code-line {

.numbered-code-line::before {
content: attr(data-line-number);

margin-left: -8px;
margin-left: -4px;
margin-right: 16px;
width: 1rem;
width: 1.2rem;
font-size: 12px;
color: var(--color-text-weak);
color: hsla(var(--text-tertiary));
text-align: right;

display: inline-block;
Expand Down
Loading