From aeaa0b32f9e51613633dafb3c3be16d4fc679e82 Mon Sep 17 00:00:00 2001 From: yangdx Date: Mon, 1 Dec 2025 12:16:43 +0800 Subject: [PATCH 1/4] Add mhchem extension support for chemistry formulas in ChatMessage --- lightrag_webui/src/components/retrieval/ChatMessage.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lightrag_webui/src/components/retrieval/ChatMessage.tsx b/lightrag_webui/src/components/retrieval/ChatMessage.tsx index 7570f503..c9b67a87 100644 --- a/lightrag_webui/src/components/retrieval/ChatMessage.tsx +++ b/lightrag_webui/src/components/retrieval/ChatMessage.tsx @@ -76,10 +76,14 @@ export const ChatMessage = ({ ? message.content : (displayContent !== undefined ? displayContent : (message.content || '')) - // Load KaTeX dynamically + // Load KaTeX dynamically with mhchem extension for chemistry formulas useEffect(() => { const loadKaTeX = async () => { try { + // First load mhchem extension (must be loaded before rehype-katex) + // This enables \ce and \pu commands for chemistry formulas + await import('katex/contrib/mhchem'); + // Then load rehype-katex const { default: rehypeKatex } = await import('rehype-katex'); setKatexPlugin(() => rehypeKatex); } catch (error) { From 8f4bfbf1a3419dcce63efaca8948c29f1cd3785c Mon Sep 17 00:00:00 2001 From: yangdx Date: Mon, 1 Dec 2025 12:20:17 +0800 Subject: [PATCH 2/4] Add KaTeX copy-tex extension support for formula copying --- lightrag_webui/src/components/retrieval/ChatMessage.tsx | 8 +++++--- lightrag_webui/src/types/katex.d.ts | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lightrag_webui/src/components/retrieval/ChatMessage.tsx b/lightrag_webui/src/components/retrieval/ChatMessage.tsx index c9b67a87..a4703e46 100644 --- a/lightrag_webui/src/components/retrieval/ChatMessage.tsx +++ b/lightrag_webui/src/components/retrieval/ChatMessage.tsx @@ -76,13 +76,15 @@ export const ChatMessage = ({ ? message.content : (displayContent !== undefined ? displayContent : (message.content || '')) - // Load KaTeX dynamically with mhchem extension for chemistry formulas + // Load KaTeX dynamically with extensions useEffect(() => { const loadKaTeX = async () => { try { - // First load mhchem extension (must be loaded before rehype-katex) - // This enables \ce and \pu commands for chemistry formulas + // Load KaTeX extensions (must be loaded before rehype-katex) + // 1. mhchem: enables \ce and \pu commands for chemistry formulas await import('katex/contrib/mhchem'); + // 2. copy-tex: allows users to copy rendered formulas as LaTeX source code + await import('katex/contrib/copy-tex'); // Then load rehype-katex const { default: rehypeKatex } = await import('rehype-katex'); setKatexPlugin(() => rehypeKatex); diff --git a/lightrag_webui/src/types/katex.d.ts b/lightrag_webui/src/types/katex.d.ts index de362150..499dbf30 100644 --- a/lightrag_webui/src/types/katex.d.ts +++ b/lightrag_webui/src/types/katex.d.ts @@ -1 +1,2 @@ declare module 'katex/contrib/mhchem'; +declare module 'katex/contrib/copy-tex'; From 112ed234c4413965c58a993675ae93abb9851134 Mon Sep 17 00:00:00 2001 From: yangdx Date: Mon, 1 Dec 2025 12:20:27 +0800 Subject: [PATCH 3/4] Bump API version to 0258 --- lightrag/api/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightrag/api/__init__.py b/lightrag/api/__init__.py index aae48894..417d3445 100644 --- a/lightrag/api/__init__.py +++ b/lightrag/api/__init__.py @@ -1 +1 @@ -__api_version__ = "0257" +__api_version__ = "0258" From 3f6423df33815f3efc5285cfbba869bdc989efb4 Mon Sep 17 00:00:00 2001 From: yangdx Date: Mon, 1 Dec 2025 12:44:04 +0800 Subject: [PATCH 4/4] Fix KaTeX extension loading by moving imports to app startup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Move mhchem/copy-tex to main.tsx • Add katex to Vite dedupe config • Remove dynamic extension loading • Ensure extensions available globally • Fix chemistry formula rendering --- .../src/components/retrieval/ChatMessage.tsx | 10 ++-------- lightrag_webui/src/main.tsx | 3 +++ lightrag_webui/vite.config.ts | 5 ++++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lightrag_webui/src/components/retrieval/ChatMessage.tsx b/lightrag_webui/src/components/retrieval/ChatMessage.tsx index a4703e46..e490941c 100644 --- a/lightrag_webui/src/components/retrieval/ChatMessage.tsx +++ b/lightrag_webui/src/components/retrieval/ChatMessage.tsx @@ -76,21 +76,15 @@ export const ChatMessage = ({ ? message.content : (displayContent !== undefined ? displayContent : (message.content || '')) - // Load KaTeX dynamically with extensions + // Load KaTeX rehype plugin dynamically + // Note: KaTeX extensions (mhchem, copy-tex) are imported statically in main.tsx useEffect(() => { const loadKaTeX = async () => { try { - // Load KaTeX extensions (must be loaded before rehype-katex) - // 1. mhchem: enables \ce and \pu commands for chemistry formulas - await import('katex/contrib/mhchem'); - // 2. copy-tex: allows users to copy rendered formulas as LaTeX source code - await import('katex/contrib/copy-tex'); - // Then load rehype-katex const { default: rehypeKatex } = await import('rehype-katex'); setKatexPlugin(() => rehypeKatex); } catch (error) { console.error('Failed to load KaTeX plugin:', error); - // Set to null to ensure we don't try to use a failed plugin setKatexPlugin(null); } }; diff --git a/lightrag_webui/src/main.tsx b/lightrag_webui/src/main.tsx index fb234a60..95a3e587 100644 --- a/lightrag_webui/src/main.tsx +++ b/lightrag_webui/src/main.tsx @@ -4,6 +4,9 @@ import './index.css' import AppRouter from './AppRouter' import './i18n.ts'; import 'katex/dist/katex.min.css'; +// Import KaTeX extensions at app startup to ensure they are registered before any rendering +import 'katex/contrib/mhchem'; // Chemistry formulas: \ce{} and \pu{} +import 'katex/contrib/copy-tex'; // Allow copying rendered formulas as LaTeX source diff --git a/lightrag_webui/vite.config.ts b/lightrag_webui/vite.config.ts index f39969c6..c89c34b4 100644 --- a/lightrag_webui/vite.config.ts +++ b/lightrag_webui/vite.config.ts @@ -10,7 +10,10 @@ export default defineConfig({ resolve: { alias: { '@': path.resolve(__dirname, './src') - } + }, + // Force all modules to use the same katex instance + // This ensures mhchem extension registered in main.tsx is available to rehype-katex + dedupe: ['katex'] }, // base: import.meta.env.VITE_BASE_URL || '/webui/', base: webuiPrefix,