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

chore: use rehype-highlight v6 update latex language #4144

Merged
merged 1 commit into from
Nov 27, 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
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"react-icons": "^4.12.0",
"react-markdown": "^9.0.1",
"react-toastify": "^9.1.3",
"rehype-highlight": "^7.0.1",
"rehype-highlight": "^6.0.0",
"rehype-highlight-code-lines": "^1.0.4",
"rehype-katex": "^7.0.1",
"rehype-raw": "^7.0.0",
Expand Down
12 changes: 11 additions & 1 deletion web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import { Tooltip } from '@janhq/joi'

import latex from 'highlight.js/lib/languages/latex'
import { useAtomValue } from 'jotai'
import { FolderOpenIcon } from 'lucide-react'
import rehypeHighlight from 'rehype-highlight'
Expand Down Expand Up @@ -48,20 +49,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 56 in web/screens/Thread/ThreadCenterPanel/SimpleTextMessage/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

52-56 lines are not covered with tests

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

58-59 lines are not covered with tests
}

const clipboard = useClipboard({ timeout: 1000 })

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

View workflow job for this annotation

GitHub Actions / coverage-check

62 line is not covered with tests

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

65 line is not covered with tests

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

View workflow job for this annotation

GitHub Actions / coverage-check

73-76 lines are not covered with tests
}
return ''

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

View workflow job for this annotation

GitHub Actions / coverage-check

78 line is not covered with tests
}

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

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

View workflow job for this annotation

GitHub Actions / coverage-check

89-91 lines are not covered with tests
}
}
)

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

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

View workflow job for this annotation

GitHub Actions / coverage-check

100-103 lines are not covered with tests
'language-',
''
)
Expand Down Expand Up @@ -394,7 +395,16 @@
rehypePlugins={[
[rehypeKatex, { throwOnError: false }],
rehypeRaw,
rehypeHighlight,
[
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
rehypeHighlight,
{
languages: { latex },
subset: false,
plainText: ['txt', 'text'],
},
],
[rehypeHighlightCodeLines, { showLineNumbers: true }],
wrapCodeBlocksWithoutVisit,
]}
Expand Down
Loading