@@ -411,7 +411,12 @@ function protectMathPipes(source: string) {
411411 if ( node . type === "math" || node . type === "inlineMath" ) {
412412 const start = node . position ?. start . offset ;
413413 const end = node . position ?. end . offset ;
414- if ( start !== undefined && end !== undefined && source . slice ( start , end ) . includes ( "|" ) ) {
414+ if ( start === undefined || end === undefined ) return ;
415+ const authored = source . slice ( start , end ) ;
416+ // A GFM cell cannot span rows. A prepass formula crossing a line ending
417+ // must not hide the table's separators, even if it closes in a later row.
418+ if ( node . type === "inlineMath" && / [ \r \n ] / . test ( authored ) ) return ;
419+ if ( authored . includes ( "|" ) ) {
415420 replacements . push ( { start, end } ) ;
416421 }
417422 } else if ( "children" in node ) node . children . forEach ( visit ) ;
@@ -431,8 +436,9 @@ function protectMathPipes(source: string) {
431436function restoreMathPipes ( tree : Root , pipe : string | null ) {
432437 const visit = ( node : Root | Root [ "children" ] [ number ] , inTable : boolean ) => {
433438 const table = inTable || node . type === "tableCell" ;
439+ // GFM may reinterpret a protected range. Never expose its placeholder as text.
440+ if ( pipe && "value" in node ) node . value = node . value . replaceAll ( pipe , "|" ) ;
434441 if ( node . type === "math" || node . type === "inlineMath" ) {
435- if ( pipe ) node . value = node . value . replaceAll ( pipe , "|" ) ;
436442 // GFM consumes one escaping backslash before a table pipe, including
437443 // inside code spans. TeX row separators before that escape stay intact.
438444 if ( table ) {
0 commit comments