Skip to content

Commit

Permalink
feat: support katex
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Jan 5, 2025
1 parent 05501c8 commit f14b4c3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<head>
<meta charset="UTF-8" />
<title>Hyper Chat</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
integrity="sha384-GvrOXuhMATgEsSwCs4smul74iXGOixntILdUW9XmUC6+HX0sLNAK3q71HotJqlAn"
crossorigin="anonymous"
/>
</head>
<body>
<div id="root"></div>
Expand Down
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"js-tiktoken": "^1.0.15",
"luxon": "^3.5.0",
"marked": "^15.0.3",
"marked-katex-extension": "^5.1.4",
"notistack": "^3.0.1",
"openai": "^4.76.0",
"react": "^18.3.1",
Expand Down
9 changes: 1 addition & 8 deletions src/components/ChatBox/InputBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import classNames from 'classnames'
import { useAtom, useAtomValue } from 'jotai'
import { FC, memo, useEffect, useRef, useState } from 'react'
import { useChatCompletion } from 'src/hooks'
import {
base64FilePromptAtom,
conversationAtom,
inputTextAtom
} from 'src/stores/conversation'
import { base64FilePromptAtom, inputTextAtom } from 'src/stores/conversation'
import { loadingAtom, settingsAtom } from 'src/stores/global'
import { ContentPartType, TextPrompt } from 'src/types/conversation'
import { LoadingIcon, SolidSendIcon } from '../Icons'
Expand All @@ -16,7 +12,6 @@ import AudioRecorder from './Recorder'
import TokenCount from './TokenCount'

const InputBox: FC = () => {
const conversation = useAtomValue(conversationAtom)
const settings = useAtomValue(settingsAtom)
const loading = useAtomValue(loadingAtom)
const [inputText, setInputText] = useAtom(inputTextAtom)
Expand Down Expand Up @@ -80,8 +75,6 @@ const InputBox: FC = () => {
}
}, [inputText])

if (!conversation) return null

return (
<section className="absolute bottom-6 left-6 w-[calc(100%_-_3rem)] rounded-md border border-black/10 bg-white dark:bg-gray-700">
<AttachmentUploader className="absolute bottom-3 left-4" />
Expand Down
9 changes: 6 additions & 3 deletions src/components/ChatBox/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import hljs from 'highlight.js'
import 'highlight.js/styles/atom-one-dark.css'
import { Marked, Renderer, Tokens } from 'marked'
import markedKatex from 'marked-katex-extension'
import { FC, memo, useCallback } from 'react'

interface Props {
src: string
}

const Markdown: FC<Props> = memo(({ src }) => {
const Markdown: FC<Props> = ({ src }) => {
const parseMarkdown = useCallback(() => {
const renderer = new Renderer()

Expand Down Expand Up @@ -42,6 +43,8 @@ const Markdown: FC<Props> = memo(({ src }) => {
}
})

marked.use(markedKatex())

return marked.parse(src)
}, [src])

Expand All @@ -51,6 +54,6 @@ const Markdown: FC<Props> = memo(({ src }) => {
dangerouslySetInnerHTML={{ __html: parseMarkdown() }}
/>
)
})
}

export default Markdown
export default memo(Markdown)
2 changes: 1 addition & 1 deletion src/components/ChatBox/TokenCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TokenCount: FC = () => {
const { maxInput } =
models.find((m) => m.modelName === configuration.model) ?? {}
const usedTokenCount =
conversation.messages.reduce((acc, val) => acc + val.tokenCount, 0) +
conversation?.messages.reduce((acc, val) => acc + val.tokenCount, 0) +
configuration.systemMessageTokensCount

return (
Expand Down
7 changes: 1 addition & 6 deletions src/components/Configuration/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import { useAtomValue } from 'jotai'
import { FC } from 'react'
import configurations from 'src/configurations'
import { useDB } from 'src/hooks'
import { configurationAtom, conversationAtom } from 'src/stores/conversation'
import { configurationAtom } from 'src/stores/conversation'
import { companyAtom } from 'src/stores/global'
import { Configuration as IConfiguration } from 'src/types/conversation'
import Divider from '../Divider'
import InputSlider from '../InputSlider'

const Configuration: FC = () => {
const conversation = useAtomValue(conversationAtom)
const company = useAtomValue(companyAtom)
const configuration = useAtomValue(configurationAtom)
const { updateOneById } = useDB('configurations')
Expand All @@ -29,10 +28,6 @@ const Configuration: FC = () => {
await updateOneById(configuration.company, configuration)
}

if (!conversation) {
return null
}

return (
<section className="w-87.75">
<section className="flex h-22 items-center justify-between pl-6">
Expand Down

0 comments on commit f14b4c3

Please sign in to comment.