@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.71 kB
JavaScript
var m=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var L=(t,a)=>{for(var s in a)m(t,s,{get:a[s],enumerable:!0})},B=(t,a,s,r)=>{if(a&&typeof a=="object"||typeof a=="function")for(let n of A(a))!P.call(t,n)&&n!==s&&m(t,n,{get:()=>a[n],enumerable:!(r=N(a,n))||r.enumerable});return t};var E=t=>B(m({},"__esModule",{value:!0}),t);var T={};L(T,{mathPlugin:()=>G,parseLatex:()=>b});module.exports=E(T);var R=require("latex-math"),h=t=>!!(t&&t.openMark==="{"&&t.closeMark==="}"),g=(t,a)=>new t.MathRun(a),I={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"},u=(t,a)=>{let s=[];for(let r of a)s.push(...d(t,r,s)||[]);return s},_=(t,a,s)=>{var n,i,o,f,M,S,y,w,v,C,O,X,D,k,q;let r=null;switch(a.content){case"newline":case"\\":return null;case"textcolor":{let e=(n=a.args)!=null?n:[];h(e[2])&&(r=u(t,e[2].content));break}case"text":{let e=(i=a.args)!=null?i:[];h(e[0])&&(r=u(t,e[0].content));break}case"^":{let e=s.pop();if(!e)break;let p=u(t,(M=(f=(o=a.args)==null?void 0:o[0])==null?void 0:f.content)!=null?M:[]);if(e.isSum){let c=new t.MathSum({children:[],superScript:p,subScript:e.sub});return c.sub=e.sub,c.sup=p,c.isSum=1,c}else if(e.sub)return new t.MathSubSuperScript({subScript:e.sub,superScript:p,children:[e.prev]});let l=new t.MathSuperScript({children:[e],superScript:p});return l.sup=p,l.prev=e,l}case"_":{let e=s.pop();if(!e)break;let p=u(t,(w=(y=(S=a.args)==null?void 0:S[0])==null?void 0:y.content)!=null?w:[]);if(e.isSum){let c=new t.MathSum({children:[],subScript:p,superScript:e.sup});return c.sup=e.sup,c.sub=p,c.isSum=1,c}else if(e.sup)return new t.MathSubSuperScript({subScript:p,superScript:e.sup,children:[e.prev]});let l=new t.MathSubScript({children:[e],subScript:p});return l.sub=p,l.prev=e,l}case"hat":case"widehat":r=t.createMathAccentCharacter({accent:"^"});break;case"sum":{let e=new t.MathSum({children:[]});return e.isSum=1,e}case"frac":case"tfrac":case"dfrac":{let e=(v=a.args)!=null?v:[];e.length===2&&h(e[0])&&h(e[1])&&(r=new t.MathFraction({numerator:u(t,e[0].content),denominator:u(t,e[1].content)}));break}case"sqrt":{let e=(C=a.args)!=null?C:[];e.length===1?r=new t.MathRadical({children:u(t,e[0].content)}):e.length===2&&(r=new t.MathRadical(e[0].content?{children:u(t,e[1].content),degree:u(t,e[0].content)}:{children:u(t,e[1].content)}));break}case"left":case"right":case"vec":return[];case"mathbf":return u(t,(D=(X=(O=a.args)==null?void 0:O[0])==null?void 0:X.content)!=null?D:[]);default:r=g(t,(k=I[a.content])!=null?k:a.content)}if((q=s[s.length-1])!=null&&q.isSum&&r){let e=s.pop();return[new t.MathSum({children:Array.isArray(r)?r:[r],superScript:e.sup,subScript:e.sub})]}return r},d=(t,a,s)=>{var n;let r=[];switch(a.type){case"string":r=[g(t,a.content)];break;case"whitespace":r=[g(t," ")];break;case"macro":{let i=_(t,a,s);if(i)r=Array.isArray(i)?i:[i];else return!1;break}case"group":r=u(t,a.content);break;case"environment":break;default:}if(a.type!=="macro"&&((n=s[s.length-1])!=null&&n.isSum)){let i=s.pop();return[new t.MathSum({children:r,superScript:i.sup,subScript:i.sub})]}return r},b=(t,a)=>{let s=(0,R.parseMath)(a),r=[[]],n=r[0];for(let i of s){let o=d(t,i,n);o?n.push(...o):r.push(n=[])}return r},G=()=>({inline:(t,a)=>{var s;return a.type!=="inlineMath"&&a.type!=="math"?[]:(a._type=a.type,a.type="",[new t.Math({children:b(t,(s=a.value)!=null?s:"").flat()})])},block:(t,a)=>{var s;return a.type!=="math"&&a.type!=="inlineMath"?[]:(a.type="",b(t,(s=a.value)!=null?s:"").map(r=>new t.Paragraph({children:[new t.Math({children:r})]})))}});0&&(module.exports={mathPlugin,parseLatex});
;