okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3616148bb
main
1// #3268: LaTeX in answers is found (and drawn by KaTeX), money and code are not mistaken for it. 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { readFile } from 'node:fs/promises' ; 5import { transform } from 'esbuild' ; 6// KaTeX is a dependency of the app (npm install / npm ci); a checkout that shares another folder's node_modules may not have it yet 7const katex = await import ( 'katex' ). then ( m => m . default , () => null ); 8 9async function loadTs ( path ) { 10const { code} = await transform ( await readFile ( new URL ( path , import . meta. url ), 'utf8' ), { loader :'ts' , format :'esm' }); 11return import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 12} 13const { splitMath, displayBlock, hasMath} = await loadTs ( '../src/lib/math.ts' ); 14const maths = text => splitMath ( text ). filter ( p => p . kind === 'math' ). map ( p => [ p . tex , p . display ]); 15 16test ( 'inline and display forms' , () => { 17assert . deepEqual ( maths ( 'a sphere of radius $r \\le R$ here' ), [[ 'r \\le R' , false ]]); 18assert . deepEqual ( maths ( 'so \\(x^2\\) and \\[y^2\\]' ), [[ 'x^2' , false ], [ 'y^2' , true ]]); 19assert . deepEqual ( maths ( 'then $$g = -k r$$ done' ), [[ 'g = -k r' , true ]]); 20assert . deepEqual ( splitMath ( 'plain words' ). map ( p => p . kind ), [ 'text' ]); 21}); 22 23test ( 'money, escaped dollars and unclosed math stay text' , () => { 24assert . deepEqual ( maths ( 'it costs $5 and $10 in total' ), []); 25assert . deepEqual ( maths ( 'only $5 here' ), []); 26assert . deepEqual ( maths ( 'a \\$5 and a \\$7 fee' ), []); 27assert . deepEqual ( maths ( 'an open $$ x + y' ), []); 28assert . equal ( splitMath ( 'a \\$5 fee' ). map ( p => p . text ). join ( '' ), 'a $5 fee' ); 29assert . equal ( hasMath ( 'no math here' ), false ); 30}); 31 32test ( 'inline math does not cross a line, display math does' , () => { 33assert . deepEqual ( maths ( 'start $a\nb$ end' ), []); 34assert . deepEqual ( maths ( 'x $$a\nb$$ y' ), [[ 'a\nb' , true ]]); 35}); 36 37test ( 'display blocks: one line, several lines, bracket form, and unclosed ones (still streaming) stay text' , () => { 38assert . deepEqual ( displayBlock ([ '$$ a = b $$' ], 0 ), { tex :' a = b ' , used :1 }); 39assert . deepEqual ( displayBlock ([ '$$' , '\\oint g = 1' , '$$' , 'after' ], 0 ), { tex :'\n\\oint g = 1\n' , used :3 }); 40assert . deepEqual ( displayBlock ([ '\\[' , 'x' , '\\]' ], 0 ), { tex :'\nx\n' , used :3 }); 41assert . equal ( displayBlock ([ '$$' , 'x' ], 0 ), null ); 42assert . equal ( displayBlock ([ '$$a$$ and more' ], 0 ), null ); 43assert . equal ( displayBlock ([ 'plain' ], 0 ), null ); 44}); 45 46test ( 'the formulas of the report are found in their paragraph and draw without an error' , { skip :katex ?false :'katex is not installed in node_modules' }, () => { 47const answer = [ 48'Apply Gauss\'s Law for gravity to a sphere of radius $r \\le R$ concentric with the planet:' , 49'' , 50'$$\\oint \\vec{g}\\cdot d\\vec{A} = -4\\pi G\\, M_{\\text{enc}}(r)$$' , 51'' , 52'By spherical symmetry, $\\vec{g}$ is radial and constant on the Gaussian sphere, so:' , 53'' , 54'$$g\\,(4\\pi r^2) = -4\\pi G\\left(\\tfrac{4}{3}\\pi r^3\\rho\\right)$$' , 55'' , 56'$$\\boxed{g(r) = -\\frac{4\\pi G\\rho}{3}\\,r}$$' , 57]. join ( '\n' ). split ( '\n' ); 58const found = []; 59for ( let i = 0 ; i < answer . length ;) { 60const block = displayBlock ( answer , i ); 61if ( block ) { found . push ([ block . tex , true ]); i += block . used ; continue ; } 62for ( const [ tex , display ] of maths ( answer [ i ])) found . push ([ tex , display ]); 63i ++ ; 64} 65assert . equal ( found . length , 5 ); 66for ( const [ tex , display ] of found ) { 67const html = katex . renderToString ( tex , { displayMode :display , throwOnError :false , strict :'ignore' , trust :false }); 68assert . ok ( html . includes ( 'class="katex' ), tex ); 69assert . ok ( ! html . includes ( 'katex-error' ), tex ); 70} 71// trust is off: no link can come out of a model's formula 72const bad = katex . renderToString ( '\\href{https://example.com}{x}' , { throwOnError :false , strict :'ignore' , trust :false }); 73assert . ok ( ! bad . includes ( '<a ' )); 74});