@m2d/math
Version:
Plugin to convert mathematical expressions in Markdown (MDAST) to DOCX using LaTeX-style syntax. Integrates seamlessly with mdast2docx.
2 lines (1 loc) • 5.21 kB
JavaScript
import{parseMath as R}from"latex-math";var h=t=>!!(t&&t.openMark==="{"&&t.closeMark==="}"),m=(t,a)=>new t.MathRun(a),d={textasciitilde:"~",textasciicircum:"^",textbackslash:"\u2216",textbar:"|",textless:"<",textgreater:">",neq:"\u2260",sim:"\u223C",simeq:"\u2243",approx:"\u2248",fallingdotseq:"\u2252",risingdotseq:"\u2253",equiv:"\u2261",geq:"\u2265",geqq:"\u2267",leq:"\u2264",leqq:"\u2266",gg:"\u226B",ll:"\u226A",times:"\xD7",div:"\xF7",pm:"\xB1",mp:"\u2213",oplus:"\u2295",ominus:"\u2296",otimes:"\u2297",oslash:"\u2298",circ:"\u2218",cdot:"\u22C5",bullet:"\u2219",ltimes:"\u22C9",rtimes:"\u22CA",in:"\u2208",ni:"\u220B",notin:"\u2209",subset:"\u2282",supset:"\u2283",subseteq:"\u2286",supseteq:"\u2287",nsubseteq:"\u2288",nsupseteq:"\u2289",subsetneq:"\u228A",supsetneq:"\u228B",cap:"\u2229",cup:"\u222A",emptyset:"\u2205",infty:"\u221E",partial:"\u2202",aleph:"\u2135",hbar:"\u210F",wp:"\u2118",Re:"\u211C",Im:"\u2111",alpha:"\u03B1",beta:"\u03B2",gamma:"\u03B3",delta:"\u03B4",epsilon:"\u03F5",zeta:"\u03B6",eta:"\u03B7",theta:"\u03B8",iota:"\u03B9",kappa:"\u03BA",lambda:"\u03BB",mu:"\u03BC",nu:"\u03BD",xi:"\u03BE",pi:"\u03C0",rho:"\u03C1",sigma:"\u03C3",tau:"\u03C4",upsilon:"\u03C5",phi:"\u03D5",chi:"\u03C7",psi:"\u03C8",omega:"\u03C9",varepsilon:"\u03B5",vartheta:"\u03D1",varrho:"\u03F1",varsigma:"\u03C2",varphi:"\u03C6",Gamma:"\u0393",Delta:"\u0394",Theta:"\u0398",Lambda:"\u039B",Xi:"\u039E",Pi:"\u03A0",Sigma:"\u03A3",Upsilon:"\u03A5",Phi:"\u03A6",Psi:"\u03A8",Omega:"\u03A9",int:"\u222B",oint:"\u222E",prod:"\u220F",coprod:"\u2210",sum:"\u2211",log:"log",exp:"exp",lim:"lim",inf:"\u221E",perp:"\u22A5",and:"\u2227",or:"\u2228",not:"\xAC",to:"\u2192",gets:"\u27F9",implies:"\u27F9",impliedby:"\u27F8",forall:"\u2200",exists:"\u2203",empty:"\u2205",nabla:"\u2207",top:"\u22A4",bot:"\u22A5",angle:"\u2220",backslash:"\u2216",neg:"\xAC",lnot:"\xAC",flat:"\u266D",natural:"\u266E",sharp:"\u266F",clubsuit:"\u2663",diamondsuit:"\u2666",heartsuit:"\u2665",spadesuit:"\u2660",varnothing:"\u2205",S:"\u2216",P:"\u220F",bigcap:"\u22C0",bigcup:"\u22C1",bigwedge:"\u2293",bigvee:"\u2294",bigsqcap:"\u2293",bigsqcup:"\u2294",biguplus:"\u2295",bigoplus:"\u2295",bigotimes:"\u2297",bigodot:"\u2299",biginterleave:"\u22BA",bigtimes:"\u2A2F"},i=(t,a)=>{let s=[];for(let r of a)s.push(...k(t,r,s)||[]);return s},N=(t,a,s)=>{var p,n,o,g,b,f,M,S,y,w,v,C,O,X,D;let r=null;switch(a.content){case"newline":case"\\":return null;case"textcolor":{let e=(p=a.args)!=null?p:[];h(e[2])&&(r=i(t,e[2].content));break}case"text":{let e=(n=a.args)!=null?n:[];h(e[0])&&(r=i(t,e[0].content));break}case"^":{let e=s.pop();if(!e)break;let u=i(t,(b=(g=(o=a.args)==null?void 0:o[0])==null?void 0:g.content)!=null?b:[]);if(e.isSum){let c=new t.MathSum({children:[],superScript:u,subScript:e.sub});return c.sub=e.sub,c.sup=u,c.isSum=1,c}else if(e.sub)return new t.MathSubSuperScript({subScript:e.sub,superScript:u,children:[e.prev]});let l=new t.MathSuperScript({children:[e],superScript:u});return l.sup=u,l.prev=e,l}case"_":{let e=s.pop();if(!e)break;let u=i(t,(S=(M=(f=a.args)==null?void 0:f[0])==null?void 0:M.content)!=null?S:[]);if(e.isSum){let c=new t.MathSum({children:[],subScript:u,superScript:e.sup});return c.sup=e.sup,c.sub=u,c.isSum=1,c}else if(e.sup)return new t.MathSubSuperScript({subScript:u,superScript:e.sup,children:[e.prev]});let l=new t.MathSubScript({children:[e],subScript:u});return l.sub=u,l.prev=e,l}case"hat":case"widehat":r=new t.MathAccentCharacter("^");break;case"sum":{let e=new t.MathSum({children:[]});return e.isSum=1,e}case"frac":case"tfrac":case"dfrac":{let e=(y=a.args)!=null?y:[];e.length===2&&h(e[0])&&h(e[1])&&(r=new t.MathFraction({numerator:i(t,e[0].content),denominator:i(t,e[1].content)}));break}case"sqrt":{let e=(w=a.args)!=null?w:[];e.length===1?r=new t.MathRadical({children:i(t,e[0].content)}):e.length===2&&(r=new t.MathRadical(e[0].content?{children:i(t,e[1].content),degree:i(t,e[0].content)}:{children:i(t,e[1].content)}));break}case"left":case"right":case"vec":return[];case"mathbf":return i(t,(O=(C=(v=a.args)==null?void 0:v[0])==null?void 0:C.content)!=null?O:[]);default:r=m(t,(X=d[a.content])!=null?X:a.content)}if((D=s[s.length-1])!=null&&D.isSum&&r){let e=s.pop();return[new t.MathSum({children:Array.isArray(r)?r:[r],superScript:e.sup,subScript:e.sub})]}return r},k=(t,a,s)=>{var p;let r=[];switch(a.type){case"string":r=[m(t,a.content)];break;case"whitespace":r=[m(t," ")];break;case"macro":{let n=N(t,a,s);if(n)r=Array.isArray(n)?n:[n];else return!1;break}case"group":r=i(t,a.content);break;case"environment":break;default:}if(a.type!=="macro"&&((p=s[s.length-1])!=null&&p.isSum)){let n=s.pop();return[new t.MathSum({children:r,superScript:n.sup,subScript:n.sub})]}return r},q=(t,a)=>{let s=R(a),r=[[]],p=r[0];for(let n of s){let o=k(t,n,p);o?p.push(...o):r.push(p=[])}return r},P=()=>({inline:async(t,a)=>{var s;return a.type!=="inlineMath"&&a.type!=="math"?[]:(a.type="",[new t.Math({children:await q(t,(s=a.value)!=null?s:"").flat()})])},block:async(t,a)=>{var s;return a.type!=="math"&&a.type!=="inlineMath"?[]:(a.type="",await q(t,(s=a.value)!=null?s:"").map(r=>new t.Paragraph({children:[new t.Math({children:r})]})))}});export{P as mathPlugin,q as parseLatex};