@m2d/html
Version:
Extend MDAST by parsing embedded HTML in Markdown. Converts HTML into structured MDAST nodes compatible with @m2d/core for DOCX generation.
4 lines (2 loc) • 6.33 kB
JavaScript
var b=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var R=(e,r)=>{for(var t in r)b(e,t,{get:r[t],enumerable:!0})},k=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of A(r))!C.call(e,s)&&s!==t&&b(e,s,{get:()=>r[s],enumerable:!(n=B(r,s))||n.enumerable});return e};var H=e=>k(b({},"__esModule",{value:!0}),e);var G={};R(G,{htmlPlugin:()=>w});module.exports=H(G);var E=e=>{let r=document.createElement("canvas").getContext("2d");return r?(r.fillStyle=e,r.fillStyle.slice(1)):e.startsWith("#")?e.slice(1):"auto"};var a=require("docx"),D=["BR","IMG","EM","I","STRONG","B","DEL","S","A","SUP","SUB","svg"],O={BR:"break",IMG:"image",EM:"emphasis",I:"emphasis",STRONG:"strong",B:"strong",DEL:"delete",S:"delete",A:"link"},N=["solid","dashed","dotted","double","none","ridge","groove","inset","outset"],P=e=>{var n;if(!e)return{};let r={},t=e.match(/border(-\w+)?:\s*[^;]+/gi);if(!t)return{};for(let s of t){let[o,c]=s.split(":").map(l=>l.trim()),d=c.split(/\s+/),p=(n=d.find(l=>l.endsWith("px")))==null?void 0:n.replace("px",""),i=d.find(l=>N.includes(l.toLowerCase())),m=d.find(l=>!l.endsWith("px")&&!N.includes(l.toLowerCase())),y=o==="border"?"border":o.replace("border-","");r[y]={...p?{width:parseInt(p,1)}:{},...i?{style:i}:{},...m?{color:m}:{}}}return r},x={solid:a.BorderStyle.SINGLE,dashed:a.BorderStyle.DASHED,dotted:a.BorderStyle.DOTTED,double:a.BorderStyle.DOUBLE,none:a.BorderStyle.NONE,ridge:a.BorderStyle.THREE_D_EMBOSS,groove:a.BorderStyle.THREE_D_ENGRAVE,inset:a.BorderStyle.INSET,outset:a.BorderStyle.OUTSET},h=e=>{if(!e||!Object.keys(e).length)return;let{width:r,color:t,style:n}=e,s={style:n?x[n]:a.BorderStyle.SINGLE};return r&&(s.size=r),t&&(s.color=E(t)),s},T=(e,r=!0)=>{let t={};if(!(e instanceof HTMLElement||e instanceof SVGElement))return t;let{textAlign:n,fontWeight:s,fontStyle:o,textDecoration:c,textTransform:d,color:p}=e.style,i=P(e.getAttribute("style"));if(r&&i.border)t.border=h(i.border);else if(Object.keys(i).length){let m={...i.border,...i.left},y={...i.border,...i.right},l={...i.border,...i.top},L={...i.border,...i.bottom};t.border={left:h(m),right:h(y),top:h(l),bottom:h(L)}}switch(n&&(Object.keys(a.AlignmentType).includes(n)?t.alignment=n:n==="justify"&&(t.alignment=a.AlignmentType.JUSTIFIED)),(/bold/.test(s)||parseInt(s)>=500)&&(t.bold=!0),/(italic|oblique)/.test(o)&&(t.italics=!0),c){case"underline":t.underline={};break;case"overline":t.emphasisMark={};break;case"line-through":t.strike=!0;break}return d==="uppercase"&&(t.allCaps=!0),d==="lowercase"&&(t.smallCaps=!0),p&&(t.color=E(p)),e.tagName==="SUP"?t.superScript=!0:e.tagName==="SUB"?t.subScript=!0:["STRONG","B"].includes(e.tagName)?t.bold=!0:["EM","I"].includes(e.tagName)?t.italics=!0:["DEL","S"].includes(e.tagName)?t.strike=!0:["U","INS"].includes(e.tagName)?t.underline={}:e.tagName==="MARK"?(t.highlight="yellow",t.emphasisMark={}):e.tagName==="PRE"&&(t.pre=!0),t},u=e=>{var s,o,c,d;if(!(e instanceof HTMLElement||e instanceof SVGElement))return{type:"text",value:(s=e.textContent)!=null?s:""};let r=Array.from(e.childNodes).map(u),t=T(e),n=e.getAttributeNames().reduce((p,i)=>({...p,[i]:e.getAttribute(i)}),{});switch(e.tagName){case"BR":return{type:"break"};case"IMG":return{type:"image",url:(o=n.src)!=null?o:"",alt:(c=n.alt)!=null?c:"",data:{...t,...n}};case"svg":return{type:"svg",id:`s${crypto.randomUUID()}`,value:e.outerHTML,data:t};case"EM":case"I":case"STRONG":case"B":case"DEL":case"S":return{type:O[e.tagName],children:r,data:t};case"A":return{type:"link",url:(d=n.href)!=null?d:"",children:r,data:t};case"INPUT":if(/(radio|checkbox)/.test(e.type))return{type:"checkbox"}}return{type:"fragment",children:r,data:t}},g=(e,r)=>{let t=Array.from(e.childNodes),n=[],s=[];for(let o of t)(o instanceof HTMLElement||o instanceof SVGElement)&&!D.includes(o.tagName)?(s.length&&(n.push({type:"paragraph",children:s.map(u)}),s.length=0),n.push(v(o))):s.push(o);return s.length&&n.push({type:"paragraph",children:s.map(u)}),n.length===1?{...n[0],data:{...r,...n[0].data}}:{type:"fragment",children:n,data:r}},M=(e,r)=>Array.from(e.children).map(t=>{let n={...r,...T(t)};return t instanceof HTMLTableRowElement?{type:"tableRow",children:Array.from(t.children).map(s=>({type:"tableCell",children:[u(s)]})),data:n}:M(t,n)}).flat(),f={style:"single"},S={left:f,right:f,top:f,bottom:f},v=e=>{let r=T(e);switch(e.tagName){case"H1":case"H2":case"H3":case"H4":case"H5":case"H6":return{type:"heading",depth:parseInt(e.tagName[1]),children:Array.from(e.childNodes).map(u),data:r};case"PRE":case"P":case"DIV":case"DETAILS":case"SUMMARY":case"FORM":case"LI":return g(e,r);case"UL":case"OL":return{type:"list",ordered:e.tagName==="OL",children:Array.from(e.childNodes).map(t=>({type:"listItem",children:[g(t)],data:r}))};case"HR":return{type:"thematicBreak",data:r};case"BLOCKQUOTE":return{type:"blockquote",children:Array.from(e.childNodes).map(t=>g(t)),data:r};case"TABLE":return{type:"table",children:M(e),data:r};case"STYLE":return{type:"paragraph",children:[{type:"text",value:`Not supported yet!
${e.textContent}`}],data:{...r,pre:!0,border:S}};case"INPUT":if(!/(radio|checkbox)/.test(e.type))return{type:"paragraph",children:[],data:{...r,frame:{width:5e3,height:90,alignment:{x:a.HorizontalPositionAlign.LEFT,y:a.VerticalPositionAlign.CENTER},anchor:{horizontal:a.FrameAnchorType.TEXT,vertical:a.FrameAnchorType.TEXT},type:"alignment"},border:S}}}return{type:"paragraph",children:[u(e)],data:r}},I=e=>{var n,s;let r=[],t=[];for(let o of e.children)if((n=o.children)!=null&&n.length&&I(o),o.type==="html"&&/^<[^>]*[^/]>$/.test(o.value)){let c=o.value.split(" ")[0].slice(1);c[0]==="/"?((s=t[0])==null?void 0:s.tag)===c.slice(1,-1)&&r.push(t.shift()):t.unshift({...o,children:[],tag:c})}else t.length?t[0].children.push(o):r.push(o);e.children=r},w=()=>({block:async(e,r)=>{if(r.type==="html"){let t=document.createElement("div");t.innerHTML=r.value,Object.assign(r,g(t))}return[]},inline:async(e,r)=>{var t,n;if(r.type==="html"){let s=(n=(t=r.value)==null?void 0:t.trim())!=null?n:"",o=s.split(" ")[0].slice(1),c=document.createElement("div");c.innerHTML=s.endsWith("/>")?s:`${s}</${o}>`,Object.assign(r,{...u(c.children[0]),children:r.children})}return[]},preprocess:I});0&&(module.exports={htmlPlugin});
;