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(md): render mid-sentence $$…$$ as inline math (#7180)

teo44a2e9efa62b
diff --git a/packages/lexical-core/tests/katex.test.ts b/packages/lexical-core/tests/katex.test.ts
index 6246ebaabb..ccacd13179 100644
--- a/packages/lexical-core/tests/katex.test.ts
+++ b/packages/lexical-core/tests/katex.test.ts
@@ -25,17 +25,23 @@ async function importMarkdown(markdown: string) {
return editor;
}
-function firstEquation(): EquationNode {
- const root = $getRoot();
- for (const child of root.getChildren()) {
- if ($isEquationNode(child)) return child;
+function allEquations(): EquationNode[] {
+ const equations: EquationNode[] = [];
+ for (const child of $getRoot().getChildren()) {
+ if ($isEquationNode(child)) equations.push(child);
if ($isParagraphNode(child)) {
for (const paragraphChild of child.getChildren()) {
- if ($isEquationNode(paragraphChild)) return paragraphChild;
+ if ($isEquationNode(paragraphChild)) equations.push(paragraphChild);
}
}
}
- throw new Error('expected an equation node');
+ return equations;
+}
+
+function firstEquation(): EquationNode {
+ const [first] = allEquations();
+ if (!first) throw new Error('expected an equation node');
+ return first;
}
describe('equation markdown import', () => {
@@ -57,6 +63,48 @@ describe('equation markdown import', () => {
});
});
+ it('keeps $$...$$ display math when only whitespace surrounds it', async () => {
+ const editor = await importMarkdown(' $$ E = mc^2 $$ ');
+ editor.getEditorState().read(() => {
+ const node = firstEquation();
+ expect(node.getEquation().trim()).toBe('E = mc^2');
+ expect(node.getInline()).toBe(false);
+ });
+ });
+
+ it('treats $$...$$ embedded in a sentence as inline math', async () => {
+ const editor = await importMarkdown(
+ 'The order form includes $$750{,}000$$ unit-minutes monthly.'
+ );
+ editor.getEditorState().read(() => {
+ const node = firstEquation();
+ expect(node.getEquation()).toBe('750{,}000');
+ expect(node.getInline()).toBe(true);
+ });
+ });
+
+ it('treats every $$...$$ on a shared line as inline math', async () => {
+ const editor = await importMarkdown(
+ 'They charged $$\\$1{,}137.30$$, making this $$\\$1{,}125$$ too high.'
+ );
+ editor.getEditorState().read(() => {
+ const equations = allEquations();
+ expect(equations.map((node) => node.getEquation())).toEqual([
+ '\\$1{,}137.30',
+ '\\$1{,}125',
+ ]);
+ expect(equations.every((node) => node.getInline())).toBe(true);
+ });
+ });
+
+ it('treats $$...$$ followed by punctuation as inline math', async () => {
+ const editor = await importMarkdown('$$x = 1$$.');
+ editor.getEditorState().read(() => {
+ const node = firstEquation();
+ expect(node.getInline()).toBe(true);
+ });
+ });
+
it('parses multiline $$ blocks as display math', async () => {
const editor = await importMarkdown('$$\nE = mc^2\n$$');
editor.getEditorState().read(() => {
diff --git a/packages/lexical-core/transformers/katex.ts b/packages/lexical-core/transformers/katex.ts
index 7b3ad894b5..6acd397654 100644
--- a/packages/lexical-core/transformers/katex.ts
+++ b/packages/lexical-core/transformers/katex.ts
@@ -2,7 +2,12 @@ import type {
MultilineElementTransformer,
TextMatchTransformer,
} from '@lexical/markdown';
-import type { TextNode } from 'lexical';
+import {
+ $isLineBreakNode,
+ $isTextNode,
+ type LexicalNode,
+ type TextNode,
+} from 'lexical';
import {
$createEquationNode,
$isEquationNode,
@@ -82,6 +87,35 @@ export const E_INLINE_EQUATION_NODE: TextMatchTransformer = {
// External Block Equation Node
+function isBlankText(node: LexicalNode): boolean {
+ return $isTextNode(node) && node.getTextContent().trim() === '';
+}
+
+/**
+ * Whether other content sits on the same line as `node` inside its parent.
+ * The match has already been split into its own text node, so anything
+ * between the nearest line breaks other than blank text is neighbouring
+ * prose.
+ */
+function $sharesLineWithContent(node: TextNode): boolean {
+ const scan = (
+ step: (current: LexicalNode) => LexicalNode | null
+ ): boolean => {
+ for (
+ let sibling = step(node);
+ sibling !== null && !$isLineBreakNode(sibling);
+ sibling = step(sibling)
+ ) {
+ if (!isBlankText(sibling)) return true;
+ }
+ return false;
+ };
+ return (
+ scan((current) => current.getPreviousSibling()) ||
+ scan((current) => current.getNextSibling())
+ );
+}
+
export const E_BLOCK_EQUATION_NODE: TextMatchTransformer = {
dependencies: [EquationNode],
type: 'text-match',
@@ -100,7 +134,12 @@ export const E_BLOCK_EQUATION_NODE: TextMatchTransformer = {
try {
const [equationMatch] = match;
const equation = equationMatch.replace(/^\$\$|\$\$$/g, '');
- const equationNode = $createEquationNode(equation, false);
+ // `$$…$$` is display math only when it has the line to itself. Models
+ // routinely wrap amounts in `$$…$$` mid-sentence, and a centred block
+ // with vertical margins inside a line of prose renders far above the
+ // text around it.
+ const inline = $sharesLineWithContent(node);
+ const equationNode = $createEquationNode(equation, inline);
node.replace(equationNode);
} catch (e) {
console.error('Error creating equation node:', e);