okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3616148bb
main
1/** 2* Finding the math in an answer (#3268). Pure text work, no rendering: the pieces are drawn by components/MathView.tsx. 3* 4* Recognised, as models write them: 5* $$ ... $$ and \[ ... \] display math (may span lines) 6* $ ... $ and \( ... \) inline math (one line) 7* A dollar sign that is money stays text: "costs $5 and $10" has no math, because an opening $ must be followed by a non-space, 8* a closing $ must follow a non-space and must not be followed by a digit. `\$` is a literal dollar sign. 9* Code (backtick spans, fenced blocks) is never searched: the caller splits it off first. 10*/ 11export type MathPart = { kind :'text' ; text :string } | { kind :'math' ; tex :string ; display :boolean }; 12 13const isSpace = ( c :string | undefined ) => c === undefined || / \s / . test ( c ); 14const isDigit = ( c :string | undefined ) => c !== undefined && c >= '0' && c <= '9' ; 15 16/** Where the unescaped `close` starts at or after `from`, or -1. `\\` and `\$` style escapes are skipped. */ 17function findClose ( text :string , from :number , close :string , oneLine :boolean ) :number { 18for ( let i = from ; i < text . length ; i ++ ) { 19const c = text [ i ]; 20if ( oneLine && c === '\n' ) 21return - 1 ; 22if ( c === '\\' && close !== '\\)' && close !== '\\]' ) { 23i ++ ; 24continue ; 25} 26if ( text . startsWith ( close , i )) 27return i ; 28} 29return - 1 ; 30} 31 32export function splitMath ( text :string ) :MathPart [] { 33const parts :MathPart [] = []; 34let plain = '' ; 35const flush = () => { 36if ( plain ) { 37parts . push ({ kind :'text' , text :plain }); 38plain = '' ; 39} 40}; 41const push = ( tex :string , display :boolean ) => { 42if ( ! tex . trim ()) 43return false ; 44flush (); 45parts . push ({ kind :'math' , tex :tex . trim (), display}); 46return true ; 47}; 48for ( let i = 0 ; i < text . length ;) { 49const c = text [ i ]; 50if ( c === '\\' ) { 51const next = text [ i + 1 ]; 52if ( next === '$' ) { // \$ is a dollar sign 53plain += '$' ; 54i += 2 ; 55continue ; 56} 57if ( next === '(' || next === '[' ) { 58const close = next === '(' ?'\\)' :'\\]' ; 59const end = findClose ( text , i + 2 , close , next === '(' ); 60if ( end > 0 && push ( text . slice ( i + 2 , end ), next === '[' )) { 61i = end + 2 ; 62continue ; 63} 64} 65plain += c + ( next ?? '' ); 66i += 2 ; 67continue ; 68} 69if ( c === '$' ) { 70if ( text [ i + 1 ] === '$' ) { 71const end = findClose ( text , i + 2 , '$$' , false ); 72if ( end > 0 && push ( text . slice ( i + 2 , end ), true )) { 73i = end + 2 ; 74continue ; 75} 76plain += '$$' ; 77i += 2 ; 78continue ; 79} 80if ( ! isSpace ( text [ i + 1 ])) { 81const end = findClose ( text , i + 1 , '$' , true ); 82if ( end > i + 1 && ! isSpace ( text [ end - 1 ]) && ! isDigit ( text [ end + 1 ]) && push ( text . slice ( i + 1 , end ), false )) { 83i = end + 1 ; 84continue ; 85} 86} 87} 88plain += c ; 89i ++ ; 90} 91flush (); 92return parts ; 93} 94 95/** True when the text holds any math, so a plain answer costs nothing. */ 96export function hasMath ( text :string ) :boolean { 97return text . includes ( '$' ) || text . includes ( '\\(' ) || text . includes ( '\\[' ); 98} 99 100/** 101* A block of display math that starts a line: `$$ ... $$` on one line, or `$$` / `\[` on its own line up to the line with the closing 102* delimiter. Returns the formula and the number of lines used, or null (an unclosed block, e.g. while the answer is still streaming, 103* stays text). 104*/ 105export function displayBlock ( lines :string [], at :number ) :{ tex :string ; used :number } | null { 106const first = lines [ at ]. trim (); 107const opens = first . startsWith ( '$$' ) ?'$$' :first . startsWith ( '\\[' ) ?'\\[' :'' ; 108if ( ! opens ) 109return null ; 110const closes = opens === '$$' ?'$$' :'\\]' ; 111const rest = first . slice ( 2 ); 112const sameLine = rest . indexOf ( closes ); 113if ( sameLine >= 0 ) { 114// "$$ x $$" alone on the line; text after the closing delimiter makes it an inline display in a paragraph instead 115return rest . slice ( sameLine + closes . length ). trim () ?null :{ tex :rest . slice ( 0 , sameLine ), used :1 }; 116} 117const body = [ rest ]; 118for ( let j = at + 1 ; j < lines . length && j < at + 60 ; j ++ ) { 119const t = lines [ j ]. trim (); 120const end = t . indexOf ( closes ); 121if ( end >= 0 ) { 122if ( t . slice ( end + closes . length ). trim ()) 123return null ; 124body . push ( t . slice ( 0 , end )); 125return { tex :body . join ( '\n' ), used :j - at + 1 }; 126} 127body . push ( lines [ j ]); 128} 129return null ; 130}