fix: Editor math block parsing and NaN media dimensions (#12668)

* fix: Block math not closed by trailing $$ on a content line

The closing delimiter check compared a 3-character slice against the
2-character "$$" delimiter, so block math closed on the same line as
content (e.g. "c = d$$") was never detected and the block swallowed the
rest of the document. Use the delimiter length rather than a hardcoded
slice. Also fix the indexOf sentinel comparison (!== 1 instead of
!== -1) in inline math parsing, which terminated correctly only by
coincidence.

Adds tests for the math markdown rules and moves the findNodes test
helper into shared/test/editor for reuse.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: NaN width and height parsed for video and image nodes

Video parseDOM and parseMarkdown used parseInt on a missing attribute,
storing NaN instead of null and persisting it to markdown as NaNxNaN.
Image size syntax with a missing dimension (e.g. "=x100") hit the same
issue through optional regex groups. Parse dimensions only when
present, matching the existing guard in Image parseDOM, and correct the
video getAttrs element type.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: Normalize non-numeric video dimensions, avoid serializing nullxnull

Review feedback: parseInt could still produce NaN when the attribute
exists but is not numeric (e.g. width="auto"), and toMarkdown wrote
null dimensions as "nullxnull". Parse dimensions through a helper that
normalizes non-finite values to null, and serialize nullish dimensions
as empty strings, which still round-trips as a video node.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Tom Moor
2026-06-11 22:29:29 -04:00
committed by GitHub
parent b1bf7c488b
commit 5ea63aa1a2
6 changed files with 111 additions and 36 deletions
+32
View File
@@ -238,3 +238,35 @@ export function doc(
) {
return schema.nodes.doc.create(null, content);
}
/**
* A plain-object representation of a ProseMirror node, as returned by
* `Node.toJSON()`.
*/
export interface JSONNode {
type: string;
content?: JSONNode[];
attrs?: Record<string, unknown>;
text?: string;
}
/**
* Recursively collects all nodes of the given type from a `Node.toJSON()`
* tree, including the root node itself.
*
* @param node - the JSON node to search, may be undefined for convenience.
* @param type - the node type name to match.
* @returns array of matching nodes in document order.
*/
export function findNodes(
node: JSONNode | undefined,
type: string
): JSONNode[] {
if (!node) {
return [];
}
return [
...(node.type === type ? [node] : []),
...(node.content ?? []).flatMap((child) => findNodes(child, type)),
];
}