From c7e4523d7c725c0a7fa0d364954241263eea73cb Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 18 Jun 2025 00:58:51 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=AF=20refactor:=20LaTeX=20and=20Math?= =?UTF-8?q?=20Rendering=20(#7952)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: Markdown LaTeX processing - Added micromark-extension-llm-math as a dependency in package.json and package-lock.json. - Updated Vite configuration to alias micromark-extension-math. - Modified Markdown components to use singleDollarTextMath: false for improved LaTeX rendering. - Refactored latex utility functions to enhance LaTeX processing and escaping mechanisms. * chore: linting of `EditTextPart` * fix: handle key up to initiate edit of latest user message by adding id prop to Edit Message HoverButton * chore: linting in Artifact component * refactor: enhance LaTeX preprocessing functionality - Updated `preprocessLaTeX` to improve handling of currency and LaTeX expressions. - Introduced optimized regex patterns for better performance. - Added support for escaping mhchem commands and handling code blocks. - Enhanced tests for various LaTeX scenarios, including currency and special characters. - Refactored existing tests to align with new preprocessing logic. * chore: filter out false positives in unused packages workflow - Added a grep command to exclude the micromark-extension-llm-math package from the list of unused dependencies in the GitHub Actions workflow. --- .github/workflows/unused-packages.yml | 2 + client/package.json | 1 + client/src/components/Artifacts/Artifact.tsx | 4 +- .../Chat/Messages/Content/Markdown.tsx | 2 +- .../Chat/Messages/Content/MarkdownLite.tsx | 2 +- .../Messages/Content/Parts/EditTextPart.tsx | 6 +- .../components/Chat/Messages/HoverButtons.tsx | 4 + .../Prompts/Groups/VariableForm.tsx | 2 +- .../src/components/Prompts/PromptDetails.tsx | 2 +- .../src/components/Prompts/PromptEditor.tsx | 2 +- client/src/utils/latex.spec.ts | 313 +++++++++--------- client/src/utils/latex.ts | 231 ++++++++----- client/vite.config.ts | 1 + package-lock.json | 20 ++ 14 files changed, 341 insertions(+), 251 deletions(-) diff --git a/.github/workflows/unused-packages.yml b/.github/workflows/unused-packages.yml index 442e70e52c..dc6ce3ba56 100644 --- a/.github/workflows/unused-packages.yml +++ b/.github/workflows/unused-packages.yml @@ -98,6 +98,8 @@ jobs: cd client UNUSED=$(depcheck --json | jq -r '.dependencies | join("\n")' || echo "") UNUSED=$(comm -23 <(echo "$UNUSED" | sort) <(cat ../client_used_deps.txt ../client_used_code.txt | sort) || echo "") + # Filter out false positives + UNUSED=$(echo "$UNUSED" | grep -v "^micromark-extension-llm-math$" || echo "") echo "CLIENT_UNUSED<> $GITHUB_ENV echo "$UNUSED" >> $GITHUB_ENV echo "EOF" >> $GITHUB_ENV diff --git a/client/package.json b/client/package.json index 5218ebdc6b..67cbec2820 100644 --- a/client/package.json +++ b/client/package.json @@ -75,6 +75,7 @@ "lodash": "^4.17.21", "lucide-react": "^0.394.0", "match-sorter": "^6.3.4", + "micromark-extension-llm-math": "^3.1.0", "qrcode.react": "^4.2.0", "rc-input-number": "^7.4.2", "react": "^18.2.0", diff --git a/client/src/components/Artifacts/Artifact.tsx b/client/src/components/Artifacts/Artifact.tsx index 2b06a2ccc0..902ac9191a 100644 --- a/client/src/components/Artifacts/Artifact.tsx +++ b/client/src/components/Artifacts/Artifact.tsx @@ -40,7 +40,7 @@ const defaultType = 'unknown'; const defaultIdentifier = 'lc-no-identifier'; export function Artifact({ - node, + node: _node, ...props }: Artifact & { children: React.ReactNode | { props: { children: React.ReactNode } }; @@ -95,7 +95,7 @@ export function Artifact({ setArtifacts((prevArtifacts) => { if ( prevArtifacts?.[artifactKey] != null && - prevArtifacts[artifactKey].content === content + prevArtifacts[artifactKey]?.content === content ) { return prevArtifacts; } diff --git a/client/src/components/Chat/Messages/Content/Markdown.tsx b/client/src/components/Chat/Messages/Content/Markdown.tsx index 740bf66670..7bd6511cfa 100644 --- a/client/src/components/Chat/Messages/Content/Markdown.tsx +++ b/client/src/components/Chat/Messages/Content/Markdown.tsx @@ -204,7 +204,7 @@ const Markdown = memo(({ content = '', isLatestMessage }: TContentProps) => { remarkGfm, remarkDirective, artifactPlugin, - [remarkMath, { singleDollarTextMath: true }], + [remarkMath, { singleDollarTextMath: false }], unicodeCitation, ]; diff --git a/client/src/components/Chat/Messages/Content/MarkdownLite.tsx b/client/src/components/Chat/Messages/Content/MarkdownLite.tsx index 972395c425..019783607c 100644 --- a/client/src/components/Chat/Messages/Content/MarkdownLite.tsx +++ b/client/src/components/Chat/Messages/Content/MarkdownLite.tsx @@ -32,7 +32,7 @@ const MarkdownLite = memo( /** @ts-ignore */ supersub, remarkGfm, - [remarkMath, { singleDollarTextMath: true }], + [remarkMath, { singleDollarTextMath: false }], ]} /** @ts-ignore */ rehypePlugins={rehypePlugins} diff --git a/client/src/components/Chat/Messages/Content/Parts/EditTextPart.tsx b/client/src/components/Chat/Messages/Content/Parts/EditTextPart.tsx index e6736b192e..1ce207fe1c 100644 --- a/client/src/components/Chat/Messages/Content/Parts/EditTextPart.tsx +++ b/client/src/components/Chat/Messages/Content/Parts/EditTextPart.tsx @@ -117,9 +117,9 @@ const EditTextPart = ({ messages.map((msg) => msg.messageId === messageId ? { - ...msg, - content: updatedContent, - } + ...msg, + content: updatedContent, + } : msg, ), ); diff --git a/client/src/components/Chat/Messages/HoverButtons.tsx b/client/src/components/Chat/Messages/HoverButtons.tsx index 644852c0b4..a13266f04c 100644 --- a/client/src/components/Chat/Messages/HoverButtons.tsx +++ b/client/src/components/Chat/Messages/HoverButtons.tsx @@ -25,6 +25,7 @@ type THoverButtons = { }; type HoverButtonProps = { + id?: string; onClick: (e?: React.MouseEvent) => void; title: string; icon: React.ReactNode; @@ -67,6 +68,7 @@ const extractMessageContent = (message: TMessage): string => { const HoverButton = memo( ({ + id, onClick, title, icon, @@ -89,6 +91,7 @@ const HoverButton = memo( return (