From 2cbb4c883108dccc5f301515a222ae8d0db7538e Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Thu, 24 Oct 2024 15:30:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=AC=E5=BC=8F=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20#239?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/renderer/src/i18n/en-us.json | 1 + src/renderer/src/i18n/zh-cn.json | 1 + src/renderer/src/i18n/zh-tw.json | 3 +- .../src/pages/home/Markdown/Markdown.tsx | 9 ++-- src/renderer/src/pages/home/Tabs/Settings.tsx | 38 ++++++++++++----- src/renderer/src/store/index.ts | 2 +- src/renderer/src/store/migrate.ts | 4 ++ src/renderer/src/store/settings.ts | 8 +++- yarn.lock | 42 +++++++++++++++++++ 10 files changed, 92 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 1618f5b0..6cedf118 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,7 @@ "react-syntax-highlighter": "^15.5.0", "redux": "^5.0.1", "redux-persist": "^6.0.0", + "rehype-katex": "^7.0.1", "rehype-mathjax": "^6.0.0", "rehype-raw": "^7.0.0", "remark-gfm": "^4.0.0", diff --git a/src/renderer/src/i18n/en-us.json b/src/renderer/src/i18n/en-us.json index 967d446f..8304bbde 100644 --- a/src/renderer/src/i18n/en-us.json +++ b/src/renderer/src/i18n/en-us.json @@ -226,6 +226,7 @@ "messages.input.send_shortcuts": "Send shortcuts", "messages.input.paste_long_text_as_file": "Paste long text as file", "messages.markdown_rendering_input_message": "Markdown render input msg", + "messages.math_engine": "Math render engine", "general.title": "General Settings", "general.user_name": "User Name", "general.user_name.placeholder": "Enter your name", diff --git a/src/renderer/src/i18n/zh-cn.json b/src/renderer/src/i18n/zh-cn.json index d0eeaf6a..d1b2c9fe 100644 --- a/src/renderer/src/i18n/zh-cn.json +++ b/src/renderer/src/i18n/zh-cn.json @@ -226,6 +226,7 @@ "messages.input.send_shortcuts": "发送快捷键", "messages.input.paste_long_text_as_file": "长文本粘贴为文件", "messages.markdown_rendering_input_message": "Markdown 渲染输入消息", + "messages.math_engine": "数学公式引擎", "general.title": "常规设置", "general.user_name": "用户名", "general.user_name.placeholder": "请输入用户名", diff --git a/src/renderer/src/i18n/zh-tw.json b/src/renderer/src/i18n/zh-tw.json index 3f15a7a9..23ae2b0d 100644 --- a/src/renderer/src/i18n/zh-tw.json +++ b/src/renderer/src/i18n/zh-tw.json @@ -225,7 +225,8 @@ "messages.input.show_estimated_tokens": "顯示預估輸入 Token 數", "messages.input.send_shortcuts": "發送快捷鍵", "messages.input.paste_long_text_as_file": "將長文本貼上為檔案", - "messages.markdown_rendering_input_message": "Markdown 渲染輸入訊息", + "messages.math_engine": "Markdown 渲染輸入訊息", + "messages.math_render_engine": "數學公式引擎", "general.title": "一般設定", "general.user_name": "使用者名稱", "general.user_name.placeholder": "輸入您的名稱", diff --git a/src/renderer/src/pages/home/Markdown/Markdown.tsx b/src/renderer/src/pages/home/Markdown/Markdown.tsx index 0b810c49..db6a0471 100644 --- a/src/renderer/src/pages/home/Markdown/Markdown.tsx +++ b/src/renderer/src/pages/home/Markdown/Markdown.tsx @@ -7,6 +7,7 @@ import { isEmpty } from 'lodash' import { FC, useMemo } from 'react' import { useTranslation } from 'react-i18next' import ReactMarkdown, { Components } from 'react-markdown' +import rehypeKatex from 'rehype-katex' // @ts-ignore next-line import rehypeMathjax from 'rehype-mathjax' import rehypeRaw from 'rehype-raw' @@ -23,7 +24,9 @@ interface Props { const Markdown: FC = ({ message }) => { const { t } = useTranslation() - const { renderInputMessageAsMarkdown } = useSettings() + const { renderInputMessageAsMarkdown, mathEngine } = useSettings() + + const rehypeMath = mathEngine === 'KaTeX' ? rehypeKatex : rehypeMathjax const messageContent = useMemo(() => { const empty = isEmpty(message.content) @@ -34,8 +37,8 @@ const Markdown: FC = ({ message }) => { const rehypePlugins = useMemo(() => { const hasUnsafeElements = /<(input|textarea|select)/i.test(messageContent) - return hasUnsafeElements ? [rehypeMathjax] : [rehypeRaw, rehypeMathjax] - }, [messageContent]) + return hasUnsafeElements ? [rehypeMath] : [rehypeRaw, rehypeMath] + }, [messageContent, rehypeMath]) if (message.role === 'user' && !renderInputMessageAsMarkdown) { return

{messageContent}

diff --git a/src/renderer/src/pages/home/Tabs/Settings.tsx b/src/renderer/src/pages/home/Tabs/Settings.tsx index 909a500c..648c2417 100644 --- a/src/renderer/src/pages/home/Tabs/Settings.tsx +++ b/src/renderer/src/pages/home/Tabs/Settings.tsx @@ -9,6 +9,7 @@ import { useAppDispatch } from '@renderer/store' import { setCodeShowLineNumbers, setFontSize, + setMathEngine, setMessageFont, setPasteLongTextAsFile, setRenderInputMessageAsMarkdown, @@ -46,7 +47,8 @@ const SettingsTab: FC = (props) => { setSendMessageShortcut, pasteLongTextAsFile, renderInputMessageAsMarkdown, - codeShowLineNumbers + codeShowLineNumbers, + mathEngine } = useSettings() const onUpdateAssistantSettings = (settings: Partial) => { @@ -213,6 +215,18 @@ const SettingsTab: FC = (props) => { /> + + {t('settings.messages.math_engine')} + + + {t('settings.font_size.title')} @@ -264,17 +278,18 @@ const SettingsTab: FC = (props) => { {t('settings.messages.input.send_shortcuts')} + } - options={[ - { value: 'Enter', label: `Enter ${t('chat.input.send')}` }, - { value: 'Shift+Enter', label: `Shift + Enter ${t('chat.input.send')}` } - ]} - onChange={(value) => setSendMessageShortcut(value)} - style={{ width: '100%', marginTop: 10 }} - /> ) @@ -286,6 +301,7 @@ const Container = styled.div` flex-direction: column; padding-bottom: 10px; padding: 10px 15px; + margin-bottom: 10px; ` const Label = styled.p` diff --git a/src/renderer/src/store/index.ts b/src/renderer/src/store/index.ts index 7b8f039e..16b34ba9 100644 --- a/src/renderer/src/store/index.ts +++ b/src/renderer/src/store/index.ts @@ -22,7 +22,7 @@ const persistedReducer = persistReducer( { key: 'cherry-studio', storage, - version: 34, + version: 35, blacklist: ['runtime'], migrate }, diff --git a/src/renderer/src/store/migrate.ts b/src/renderer/src/store/migrate.ts index 39a83752..b99fdb5a 100644 --- a/src/renderer/src/store/migrate.ts +++ b/src/renderer/src/store/migrate.ts @@ -610,6 +610,10 @@ const migrateConfig = { }) }) return state + }, + '35': (state: RootState) => { + state.settings.mathEngine = 'KaTeX' + return state } } diff --git a/src/renderer/src/store/settings.ts b/src/renderer/src/store/settings.ts index 5964591e..74f4c99c 100644 --- a/src/renderer/src/store/settings.ts +++ b/src/renderer/src/store/settings.ts @@ -23,6 +23,7 @@ export interface SettingsState { manualUpdateCheck: boolean renderInputMessageAsMarkdown: boolean codeShowLineNumbers: boolean + mathEngine: 'MathJax' | 'KaTeX' // webdav 配置 host, user, pass, path webdavHost: string webdavUser: string @@ -50,6 +51,7 @@ const initialState: SettingsState = { manualUpdateCheck: false, renderInputMessageAsMarkdown: true, codeShowLineNumbers: false, + mathEngine: 'MathJax', webdavHost: '', webdavUser: '', webdavPass: '', @@ -135,6 +137,9 @@ const settingsSlice = createSlice({ }, setCodeShowLineNumbers: (state, action: PayloadAction) => { state.codeShowLineNumbers = action.payload + }, + setMathEngine: (state, action: PayloadAction<'MathJax' | 'KaTeX'>) => { + state.mathEngine = action.payload } } }) @@ -164,7 +169,8 @@ export const { setWebdavUser, setWebdavPass, setWebdavPath, - setCodeShowLineNumbers + setCodeShowLineNumbers, + setMathEngine } = settingsSlice.actions export default settingsSlice.reducer diff --git a/yarn.lock b/yarn.lock index a36c73d7..ca061000 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2355,6 +2355,7 @@ __metadata: react-syntax-highlighter: "npm:^15.5.0" redux: "npm:^5.0.1" redux-persist: "npm:^6.0.0" + rehype-katex: "npm:^7.0.1" rehype-mathjax: "npm:^6.0.0" rehype-raw: "npm:^7.0.0" remark-gfm: "npm:^4.0.0" @@ -5988,6 +5989,32 @@ __metadata: languageName: node linkType: hard +"hast-util-from-html-isomorphic@npm:^2.0.0": + version: 2.0.0 + resolution: "hast-util-from-html-isomorphic@npm:2.0.0" + dependencies: + "@types/hast": "npm:^3.0.0" + hast-util-from-dom: "npm:^5.0.0" + hast-util-from-html: "npm:^2.0.0" + unist-util-remove-position: "npm:^5.0.0" + checksum: 10c0/fc68d9245e794483a802d5c85a9f6c25959e00db78cc796411efc965134f3206f9cc9fa38134572ea781ad74663e801f1f83202007b208e27a770855566a62b6 + languageName: node + linkType: hard + +"hast-util-from-html@npm:^2.0.0": + version: 2.0.3 + resolution: "hast-util-from-html@npm:2.0.3" + dependencies: + "@types/hast": "npm:^3.0.0" + devlop: "npm:^1.1.0" + hast-util-from-parse5: "npm:^8.0.0" + parse5: "npm:^7.0.0" + vfile: "npm:^6.0.0" + vfile-message: "npm:^4.0.0" + checksum: 10c0/993ef707c1a12474c8d4094fc9706a72826c660a7e308ea54c50ad893353d32e139b7cbc67510c2e82feac572b320e3b05aeb13d0f9c6302d61261f337b46764 + languageName: node + linkType: hard + "hast-util-from-parse5@npm:^8.0.0": version: 8.0.1 resolution: "hast-util-from-parse5@npm:8.0.1" @@ -10466,6 +10493,21 @@ __metadata: languageName: node linkType: hard +"rehype-katex@npm:^7.0.1": + version: 7.0.1 + resolution: "rehype-katex@npm:7.0.1" + dependencies: + "@types/hast": "npm:^3.0.0" + "@types/katex": "npm:^0.16.0" + hast-util-from-html-isomorphic: "npm:^2.0.0" + hast-util-to-text: "npm:^4.0.0" + katex: "npm:^0.16.0" + unist-util-visit-parents: "npm:^6.0.0" + vfile: "npm:^6.0.0" + checksum: 10c0/73c770319536128b75055d904d06951789d00a0552c11724c0dac2e244dcb21041630552d118a11cc42233fdcd1bfee525e78a0020fde635bd916cceb281dfb1 + languageName: node + linkType: hard + "rehype-mathjax@npm:^6.0.0": version: 6.0.0 resolution: "rehype-mathjax@npm:6.0.0"