Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agent): render dollar-delimited math correctly (#5988)

Wolf Mermelstein6280d8c2789f
diff --git a/apps/web/src/features/block-agent/component/parts/TextPart.tsx b/apps/web/src/features/block-agent/component/parts/TextPart.tsx
index 219197f6ab..0a91350a03 100644
--- a/apps/web/src/features/block-agent/component/parts/TextPart.tsx
+++ b/apps/web/src/features/block-agent/component/parts/TextPart.tsx
@@ -9,7 +9,9 @@ export function TextPart(props: { text: string; inFlight?: boolean }) {
props.inFlight ? hideIncompleteMacroXml(props.text) : props.text;
return (
- <div class="whitespace-pre-wrap wrap-break-word max-w-full text-sm">
+ // `chat-markdown-container` is the overflow host for KaTeX display math
+ // (see index.css).
+ <div class="chat-markdown-container whitespace-pre-wrap wrap-break-word max-w-full text-sm">
<StaticMarkdown
markdown={markdown()}
theme={channelTheme}
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
index 17b450db70..84dbb6337e 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
@@ -776,7 +776,10 @@ const Equation: TypedRenderableEntity<EquationNode> = {
guard: (node: LexicalNode): node is EquationNode =>
node.__type === 'equation',
render: (props) => (
- <EquationDecorator equation={props.node.__equation} inline={true} />
+ <EquationDecorator
+ equation={props.node.__equation}
+ inline={props.node.__inline}
+ />
),
};
diff --git a/packages/lexical-core/tests/katex.test.ts b/packages/lexical-core/tests/katex.test.ts
new file mode 100644
index 0000000000..6246ebaabb
--- /dev/null
+++ b/packages/lexical-core/tests/katex.test.ts
@@ -0,0 +1,68 @@
+import { $convertFromMarkdownString } from '@lexical/markdown';
+import { $getRoot, $isParagraphNode, createEditor } from 'lexical';
+import { describe, expect, it } from 'vitest';
+import { SupportedNodeTypes } from '../node-list';
+import { $isEquationNode, type EquationNode } from '../nodes/EquationNode';
+import { ALL_TRANSFORMERS } from '../transformers';
+
+async function importMarkdown(markdown: string) {
+ const editor = createEditor({
+ nodes: SupportedNodeTypes,
+ onError: (error) => {
+ throw error;
+ },
+ });
+
+ await new Promise<void>((resolve) => {
+ editor.update(
+ () => {
+ $convertFromMarkdownString(markdown, ALL_TRANSFORMERS);
+ },
+ { onUpdate: () => resolve() }
+ );
+ });
+
+ return editor;
+}
+
+function firstEquation(): EquationNode {
+ const root = $getRoot();
+ for (const child of root.getChildren()) {
+ if ($isEquationNode(child)) return child;
+ if ($isParagraphNode(child)) {
+ for (const paragraphChild of child.getChildren()) {
+ if ($isEquationNode(paragraphChild)) return paragraphChild;
+ }
+ }
+ }
+ throw new Error('expected an equation node');
+}
+
+describe('equation markdown import', () => {
+ it('treats $$...$$ as display math', async () => {
+ const editor = await importMarkdown('$$ x = \\frac{1}{2} $$');
+ editor.getEditorState().read(() => {
+ const node = firstEquation();
+ expect(node.getEquation().trim()).toBe('x = \\frac{1}{2}');
+ expect(node.getInline()).toBe(false);
+ });
+ });
+
+ it('treats $...$ as inline math', async () => {
+ const editor = await importMarkdown('The result is $a + b$.');
+ editor.getEditorState().read(() => {
+ const node = firstEquation();
+ expect(node.getEquation()).toBe('a + b');
+ expect(node.getInline()).toBe(true);
+ });
+ });
+
+ it('parses multiline $$ blocks as display math', async () => {
+ const editor = await importMarkdown('$$\nE = mc^2\n$$');
+ editor.getEditorState().read(() => {
+ const node = firstEquation();
+ expect(node.getEquation()).toBe('E = mc^2');
+ expect(node.getInline()).toBe(false);
+ });
+ });
+});
diff --git a/packages/lexical-core/transformers/katex.ts b/packages/lexical-core/transformers/katex.ts
index 5ad2db79a8..7b3ad894b5 100644
--- a/packages/lexical-core/transformers/katex.ts
+++ b/packages/lexical-core/transformers/katex.ts
@@ -100,10 +100,7 @@ export const E_BLOCK_EQUATION_NODE: TextMatchTransformer = {
try {
const [equationMatch] = match;
const equation = equationMatch.replace(/^\$\$|\$\$$/g, '');
-
- // when AI Chat prompt gets updated to support inline vs block, update inline to false
- const isInline = true; // false
- const equationNode = $createEquationNode(equation, isInline);
+ const equationNode = $createEquationNode(equation, false);
node.replace(equationNode);
} catch (e) {
console.error('Error creating equation node:', e);
@@ -151,9 +148,7 @@ export const E_MULTILINE_BLOCK_EQUATION_NODE: MultilineElementTransformer = {
}
try {
- // when AI Chat prompt gets updated to support inline vs block, update inline to false
- const isInline = true; // false
- const equationNode = $createEquationNode(latexString, isInline);
+ const equationNode = $createEquationNode(latexString, false);
rootNode.append(equationNode);
} catch (e) {
console.error('Error creating multiline equation node:', e);