@docen/import-docx 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{fromXml as P}from"xast-util-from-xml";import{unzipSync as lt}from"fflate";import{toUint8Array as ut}from"undio";import{imageMeta as ft}from"image-meta";function a(n,t){for(const e of n.children)if(e.type==="element"&&e.name===t)return e}function y(n,t){for(const e of n.children){if(e.type==="element"&&e.name===t)return e;if(e.type==="element"){const r=y(e,t);if(r)return r}}}function x(n,t){const e=[];for(const r of n.children)r.type==="element"&&r.name===t&&e.push(r),r.type==="element"&&e.push(...x(r,t));return e}const N="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function U(n){const t=n.length,e=Math.ceil(t/3)*4,r=Array.from({length:e});let o=0;for(let i=0;i<t;i+=3){const s=n[i],c=i+1<t?n[i+1]:0,l=i+2<t?n[i+2]:0,u=s>>2,f=(s&3)<<4|c>>4,w=(c&15)<<2|l>>6,p=l&63;r[o++]=N[u],r[o++]=N[f],r[o++]=i+1<t?N[w]:"=",r[o++]=i+2<t?N[p]:"="}return r.join("")}function pt(n){const t=atob(n),e=new Uint8Array(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e}const wt=globalThis.process?.release?.name==="node",H=typeof window<"u";async function dt(n){const t=await n;return t.default||t}let F,_=class{#t=!1;constructor({enableHWA:t=!1}={}){this.#t=t}create(t,e){const r=this._createCanvas(t,e);return{canvas:r,context:r.getContext("2d",{willReadFrequently:!this.#t})}}reset({canvas:t},e,r){if(!t)throw new Error("Canvas is not specified");t.width=e,t.height=r}destroy(t){if(!t.canvas)throw new Error("Canvas is not specified");t.canvas.width=0,t.canvas.height=0,t.canvas=void 0,t.context=void 0}_createCanvas(t,e){throw new Error("Not implemented")}};class ht extends _{_document;constructor({ownerDocument:t=globalThis.document,enableHWA:e=!1}={}){super({enableHWA:e}),this._document=t}_createCanvas(t,e){const r=this._document.createElement("canvas");return r.width=t,r.height=e,r}}class gt extends _{constructor({enableHWA:t=!1}={}){super({enableHWA:t})}_createCanvas(t,e){if(!F)throw new Error("@napi-rs/canvas module is not resolved");return F.createCanvas(t,e)}}async function mt(n){F??=await dt(n())}async function bt(n){if(H)return ht;if(wt){if(!n)throw new Error("In Node.js environment, @napi-rs/canvas is required for image cropping. Please provide canvasImport parameter or install it: pnpm add @napi-rs/canvas");return await mt(n),gt}throw new Error("Unsupported environment for canvas operations")}async function yt(n,t,e={}){if(!t||!t.left&&!t.top&&!t.right&&!t.bottom||e.enabled===!1)return n;try{const r=await bt(e.canvasImport),o=await vt(n,r),i=(t.left||0)/1e5*o.width,s=(t.top||0)/1e5*o.height,c=(t.right||0)/1e5*o.width,l=(t.bottom||0)/1e5*o.height,u=Math.round(o.width-i-c),f=Math.round(o.height-s-l);if(u<=0||f<=0)return console.warn("Invalid crop dimensions, returning original image"),n;const w=new r().create(u,f);if(!w.context)throw new Error("Failed to get 2D context from canvas");w.context.drawImage(o,i,s,u,f,0,0,u,f);const p=w.canvas.toDataURL(),d=await(await fetch(p)).arrayBuffer();return new Uint8Array(d)}catch(r){return console.warn("Image cropping failed, returning original image:",r),n}}async function vt(n,t){if(H){const e=new Blob([n.buffer]),r=URL.createObjectURL(e);try{const o=new Image;return new Promise((i,s)=>{o.onload=()=>{URL.revokeObjectURL(r),i(o)},o.onerror=()=>{URL.revokeObjectURL(r),s(new Error("Failed to load image"))},o.src=r})}catch(o){throw URL.revokeObjectURL(r),o}}else{if(!F)throw new Error("@napi-rs/canvas module is not resolved");return await F.loadImage(Buffer.from(n))}}const It="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";function R(n){const t=parseInt(n,10);if(!isNaN(t))return Math.round(t/9525)}function xt(n){const t=n.attributes.l,e=n.attributes.t,r=n.attributes.r,o=n.attributes.b;if(!(!t&&!e&&!r&&!o))return{left:t?parseInt(t,10):void 0,top:e?parseInt(e,10):void 0,right:r?parseInt(r,10):void 0,bottom:o?parseInt(o,10):void 0}}function X(n){const t=a(n,"wp:align"),e=a(n,"wp:posOffset"),r=t?.children[0]?.type==="text"?t.children[0].value:void 0,o=e?.children[0]?.type==="text"?parseInt(e.children[0].value,10):void 0;if(!(!r&&o===void 0))return{...r&&{align:r},...o!==void 0&&{offset:o}}}function q(n){let t=a(n,"w:drawing");if(t)return t;const e=a(n,"mc:AlternateContent"),r=e&&a(e,"mc:Choice");return r&&a(r,"w:drawing")}function J(n,t,e,r){const o=e/r,i=n/t;return Math.abs(o-i)>.1?o>i?{width:n,height:Math.round(n/o)}:{width:Math.round(t*o),height:t}:{width:n,height:t}}function kt(n){const t=new Map,e=n["word/_rels/document.xml.rels"];if(!e)return t;const r=P(new TextDecoder().decode(e)),o=a(r,"Relationships");if(!o)return t;const i=x(o,"Relationship");for(const s of i)if(s.attributes.Type===It&&s.attributes.Id&&s.attributes.Target){const c="word/"+s.attributes.Target,l=n[c];if(!l)continue;let u,f,w="png";try{const h=ft(l);u=h.width,f=h.height,h.type&&(w=h.type)}catch{}const p=U(l),d=`data:image/${w};base64,${p}`;t.set(s.attributes.Id,{src:d,width:u,height:f})}return t}async function $(n,t){const{images:e,options:r}=t,o=y(n,"a:blip");if(!o?.attributes["r:embed"])return null;const i=o.attributes["r:embed"],s=e.get(i);if(!s)return null;let c=s.src;const l=y(n,"a:srcRect");if(l){const g=xt(l);if(g&&c.startsWith("data:")){const[I,M]=c.split(",");if(M){const at=pt(M);try{const A=await yt(at,g,{canvasImport:r?.canvasImport,enabled:r?.enableImageCrop!==!1}),ct=U(A);c=`${I},${ct}`}catch(A){console.warn("Image cropping failed, using original image:",A)}}}}const u=y(n,"wp:extent");let f,w;if(u){const g=u.attributes.cx,I=u.attributes.cy;typeof g=="string"&&(f=R(g)),typeof I=="string"&&(w=R(I))}const p=y(n,"a:xfrm");let d;if(p?.attributes.rot){const g=parseInt(p.attributes.rot,10);isNaN(g)||(d=g/6e4)}const h=y(n,"wp:docPr")?.attributes.title,m=y(n,"wp:positionH"),v=y(n,"wp:positionV");let b;if(m||v){const g=m?X(m):void 0,I=v?X(v):void 0;b={horizontalPosition:{relative:m?.attributes.relativeFrom||"page",...g?.align&&{align:g.align},...g?.offset!==void 0&&{offset:g.offset}},verticalPosition:{relative:v?.attributes.relativeFrom||"page",...I?.align&&{align:I.align},...I?.offset!==void 0&&{offset:I.offset}}}}const k=y(n,"pic:spPr");let C;if(k){const g=y(k,"a:ln"),I=g&&y(g,"a:solidFill"),M=I&&y(I,"a:srgbClr");M?.attributes.val&&(C={type:"solidFill",solidFillType:"rgb",value:M.attributes.val})}return{type:"image",attrs:{src:c,alt:"",...f!==void 0&&{width:f},...w!==void 0&&{height:w},...d!==void 0&&{rotation:d},...h&&{title:h},...b&&{floating:b},...C&&{outline:C}}}}function Ct(n,t,e){if(t&&e&&n.width&&n.height){const r=J(t,e,n.width,n.height);return{type:"image",attrs:{src:n.src,alt:"",width:r.width,height:r.height}}}return{type:"image",attrs:{src:n.src,alt:"",...t!==void 0&&{width:t},...e!==void 0&&{height:e}}}}async function V(n,t){const e=[],r=a(n,"wp:inline")||a(n,"wp:anchor");if(!r)return e;const o=a(r,"wp:extent");let i,s;if(o){const f=o.attributes.cx,w=o.attributes.cy;typeof f=="string"&&(i=R(f)),typeof w=="string"&&(s=R(w))}const c=a(r,"a:graphic");if(!c)return e;const l=a(c,"a:graphicData");if(!l)return e;const u=a(l,"wpg:wgp");if(u){const f=a(u,"wpg:grpSp"),w=f?[...x(f,"pic:pic"),...x(f,"pic")]:[...x(u,"pic:pic"),...x(u,"pic")];for(const p of w){const d=a(p,"a:graphic");if(!d){const b=a(p,"pic:blipFill")||y(p,"a:blipFill");if(!b)continue;const k=a(b,"a:blip")||y(b,"a:blip");if(!k?.attributes["r:embed"])continue;const C=k.attributes["r:embed"],g=t.images.get(C);if(!g)continue;e.push(Ct(g,i,s));continue}const h={children:[d]},m=await $(h,t);if(!m)continue;const v=h.children[0]?.type==="element"?y(h.children[0],"a:blip")?.attributes["r:embed"]:void 0;if(i&&s&&v){const b=t.images.get(v);if(b?.width&&b?.height){const k=J(i,s,b.width,b.height);m.attrs.width=k.width,m.attrs.height=k.height}else m.attrs.width=i,m.attrs.height=s}e.push(m)}}else{const f=await $(n,t);f&&e.push(f)}return e}const Mt="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";function Pt(n){const t=new Map,e=n["word/_rels/document.xml.rels"];if(!e)return t;const r=P(new TextDecoder().decode(e)),o=a(r,"Relationships");if(!o)return t;const i=x(o,"Relationship");for(const s of i)s.attributes.Type===Mt&&s.attributes.Id&&s.attributes.Target&&t.set(s.attributes.Id,s.attributes.Target);return t}function Ft(n){const t=new Map,e=new Map,r=n["word/numbering.xml"];if(!r)return t;const o=P(new TextDecoder().decode(r)),i=new Map,s=a(o,"w:numbering");if(!s)return t;const c=x(s,"w:abstractNum");for(const u of c){const f=u.attributes["w:abstractNumId"],w=a(u,"w:lvl");if(!w)continue;const p=a(w,"w:numFmt");p?.attributes["w:val"]&&i.set(f,p.attributes["w:val"]);const d=a(w,"w:start");d?.attributes["w:val"]&&e.set(f,parseInt(d.attributes["w:val"],10))}const l=x(s,"w:num");for(const u of l){const f=u.attributes["w:numId"],w=a(u,"w:abstractNumId");if(!w?.attributes["w:val"])continue;const p=w.attributes["w:val"],d=i.get(p);if(!d)continue;const h=e.get(p);d==="bullet"?t.set(f,{type:"bullet"}):t.set(f,{type:"ordered",...h!==void 0&&{start:h}})}return t}function Nt(n){const t=new Map,e=n["word/styles.xml"];if(!e)return t;const r=P(new TextDecoder().decode(e)),o=a(r,"w:styles");if(!o)return t;const i=x(o,"w:style").filter(s=>s.attributes["w:type"]==="paragraph");for(const s of i){const c=s.attributes["w:styleId"];if(!c)continue;const l={styleId:c},u=a(s,"w:name");u?.attributes["w:val"]&&(l.name=u.attributes["w:val"]);const f=a(s,"w:pPr");if(f){const p=a(f,"w:outlineLvl");p?.attributes["w:val"]!==void 0&&(l.outlineLvl=parseInt(p.attributes["w:val"],10))}const w=a(s,"w:rPr");if(w){const p={},d=a(w,"w:color");if(d?.attributes["w:val"]&&d.attributes["w:val"]!=="auto"){const v=d.attributes["w:val"];p.color=v.startsWith("#")?v:`#${v}`}a(w,"w:b")&&(p.bold=!0),a(w,"w:i")&&(p.italic=!0),a(w,"w:u")&&(p.underline=!0),a(w,"w:strike")&&(p.strike=!0);const h=a(w,"w:sz");if(h?.attributes["w:val"]){const v=h.attributes["w:val"],b=parseInt(v,10);isNaN(b)||(p.fontSize=b)}const m=a(w,"w:rFonts");m?.attributes["w:ascii"]&&(p.fontFamily=m.attributes["w:ascii"]),Object.keys(p).length>0&&(l.charFormat=p)}t.set(c,l)}return t}function G(n,t){const e=a(n,"w:t");if(!e)return null;const r=e.children.find(i=>i.type==="text");if(!r?.value)return null;const o=D(n,t);return{type:"text",text:r.value,...o.length&&{marks:o}}}async function B(n,t){const e=[];for(const r of n.children)if(r.type==="element"){if(r.name==="w:hyperlink"){const o=r,i=o.attributes["r:id"],s=t.hyperlinks.get(i);if(!s)continue;for(const c of o.children){if(c.type!=="element"||c.name!=="w:r")continue;const l=c,u=q(l);if(u){const w=await $(u,t);if(w){e.push(w);continue}const p=await V(u,t);if(p.length){e.push(...p);continue}}const f=G(l,t.styleInfo);f&&(f.marks=f.marks||[],f.marks.push({type:"link",attrs:{href:s}}),e.push(f))}}else if(r.name==="w:r"){const o=r,i=q(o);if(i){const c=await V(i,t);if(c.length){e.push(...c);continue}}if(a(o,"w:br")){const c=D(o,t.styleInfo);e.push({type:"hardBreak",...c.length&&{marks:c}})}const s=G(o,t.styleInfo);s&&e.push(s)}}return e}function D(n,t){const e=[],r=a(n,"w:rPr");let o={};if(t?.charFormat&&(o={...t.charFormat}),r){const i=a(r,"w:b");i&&(i.attributes["w:val"]==="false"?o.bold=!1:o.bold=!0);const s=a(r,"w:i");s&&(s.attributes["w:val"]==="false"?o.italic=!1:o.italic=!0),a(r,"w:u")&&(o.underline=!0),a(r,"w:strike")&&(o.strike=!0);const c=a(r,"w:color");if(c?.attributes["w:val"]&&c.attributes["w:val"]!=="auto"){const p=c.attributes["w:val"];o.color=p.startsWith("#")?p:`#${p}`}const l=a(r,"w:sz");if(l?.attributes["w:val"]){const p=l.attributes["w:val"],d=parseInt(p,10);isNaN(d)||(o.fontSize=d)}const u=a(r,"w:rFonts");u?.attributes["w:ascii"]&&(o.fontFamily=u.attributes["w:ascii"]);const f=a(r,"w:shd");if(f?.attributes["w:fill"]&&f.attributes["w:fill"]!=="auto"){const p=f.attributes["w:fill"];o.backgroundColor=p.startsWith("#")?p:`#${p}`}a(r,"w:highlight")&&e.push({type:"highlight"});const w=a(r,"w:vertAlign");if(w){const p=w.attributes["w:val"];p==="subscript"?e.push({type:"subscript"}):p==="superscript"&&e.push({type:"superscript"})}}if(o.bold&&e.push({type:"bold"}),o.italic&&e.push({type:"italic"}),o.underline&&e.push({type:"underline"}),o.strike&&e.push({type:"strike"}),o.color||o.backgroundColor||o.fontSize||o.fontFamily){const i={color:o.color||"",backgroundColor:o.backgroundColor||"",fontSize:"",fontFamily:"",lineHeight:""};if(o.fontSize){const s=Math.round(o.fontSize/1.5*10)/10;i.fontSize=`${s}px`}o.fontFamily&&(i.fontFamily=o.fontFamily),e.push({type:"textStyle",attrs:i})}return e}function W(n){const t=a(n,"w:pPr");if(!t)return;const e=a(t,"w:jc");if(!e?.attributes["w:val"])return;const r=e.attributes["w:val"],o={left:"left",right:"right",center:"center",both:"justify"}[r];return o?{textAlign:o}:void 0}function K(n){return`${Math.round(n/15)}px`}function Q(n){const t=a(n,"w:pPr");if(!t)return null;const e={},r=a(t,"w:ind");if(r){const i=u=>{const f=r.attributes[u];if(typeof f!="string")return null;const w=parseInt(f,10);return isNaN(w)?null:K(w)},s=i("w:left");s&&(e.indentLeft=s);const c=i("w:right");c&&(e.indentRight=c);const l=i("w:firstLine");if(l)e.indentFirstLine=l;else{const u=i("w:hanging");u&&(e.indentFirstLine=`-${u}`)}}const o=a(t,"w:spacing");if(o){const i=l=>{const u=o.attributes[l];if(typeof u!="string")return null;const f=parseInt(u,10);return isNaN(f)?null:K(f)},s=i("w:before");s&&(e.spacingBefore=s);const c=i("w:after");c&&(e.spacingAfter=c)}return Object.keys(e).length?e:null}async function S(n,t){const e=a(n,"w:pPr"),r=(e&&a(e,"w:pStyle"))?.attributes["w:val"];if(r&&t.styleMap){const c=t.styleMap.get(r);if(c?.outlineLvl!==void 0&&c.outlineLvl>=0&&c.outlineLvl<=5){const u=c.outlineLvl+1;return Y(n,t,c,u)}const l=r.match(/^Heading(\d+)$/);if(l){const u=parseInt(l[1],10);return Y(n,t,c,u)}}const o=r&&t.styleMap?t.styleMap.get(r):void 0,i=await B(n,{...t,styleInfo:o}),s={...W(n),...Q(n)};if(Rt(n)){const c=i.filter(l=>l.type!=="hardBreak");return[{type:"paragraph",...Object.keys(s).length&&{attrs:s},content:c.length?c:void 0},{type:"horizontalRule"}]}if(i.length===1&&i[0].type==="hardBreak"){const c=a(n,"w:r");if((c&&a(c,"w:br"))?.attributes["w:type"]==="page")return{type:"horizontalRule"}}return i.length===1&&i[0].type==="image"?i[0]:{type:"paragraph",...Object.keys(s).length&&{attrs:s},content:i}}function Rt(n){const t=[],e=r=>{if(r.name==="w:r")t.push(r);else for(const o of r.children)o.type==="element"&&e(o)};return e(n),t.some(r=>a(r,"w:br")?.attributes["w:type"]==="page")}async function Y(n,t,e,r){return{type:"heading",attrs:{level:r,...Q(n)},content:await B(n,{...t,styleInfo:e})}}function T(n){if(!n)return null;const t=n.attributes["w:val"],e=n.attributes["w:sz"],r=n.attributes["w:color"],o={single:"solid",dashed:"dashed",dotted:"dotted",double:"double",none:"none",nil:"none"},i={};if(r&&r!=="auto"&&(i.color=`#${r}`),e){const s=parseInt(e);isNaN(s)||(i.width=Math.round(s/6))}return t&&o[t]&&(i.style=o[t]),Object.keys(i).length>0?i:null}function St(n){const t={marginTop:void 0,marginBottom:void 0,marginLeft:void 0,marginRight:void 0},e=a(n,"w:tblPr");if(!e)return null;const r=a(e,"w:tblCellMar");if(!r)return null;const o=a(r,"w:top");if(o?.attributes["w:w"]){const l=parseInt(o.attributes["w:w"]);isNaN(l)||(t.marginTop=l)}const i=a(r,"w:bottom");if(i?.attributes["w:w"]){const l=parseInt(i.attributes["w:w"]);isNaN(l)||(t.marginBottom=l)}const s=a(r,"w:left");if(s?.attributes["w:w"]){const l=parseInt(s.attributes["w:w"]);isNaN(l)||(t.marginLeft=l)}const c=a(r,"w:right");if(c?.attributes["w:w"]){const l=parseInt(c.attributes["w:w"]);isNaN(l)||(t.marginRight=l)}return t.marginTop===void 0&&t.marginBottom===void 0&&t.marginLeft===void 0&&t.marginRight===void 0?null:t}function Tt(n){const t={rowHeight:null},e=a(n,"w:trPr");if(!e)return t;const r=a(e,"w:trHeight");if(r?.attributes["w:val"]){const o=parseInt(r.attributes["w:val"]),i=Math.round(o/15);t.rowHeight=`${i}px`}return t}function Z(n){const t={colSpan:1,rowSpan:1,colWidth:null},e=a(n,"w:tcPr");if(!e)return t;const r=a(e,"w:gridSpan");r?.attributes["w:val"]&&(t.colSpan=parseInt(r.attributes["w:val"])),a(e,"w:vMerge")?.attributes["w:val"]==="continue"&&(t.rowSpan=0);const o=a(e,"w:tcW");if(o?.attributes["w:w"]){const l=parseInt(o.attributes["w:w"]);t.colWidth=Math.round(l/15)}const i=a(e,"w:shd");i?.attributes["w:fill"]&&(t.backgroundColor=`#${i.attributes["w:fill"]}`);const s=a(e,"w:vAlign");s?.attributes["w:val"]&&(t.verticalAlign=s.attributes["w:val"]);const c=a(e,"w:tcBorders");if(c){const l=T(a(c,"w:top"));l&&(t.borderTop=l);const u=T(a(c,"w:bottom"));u&&(t.borderBottom=u);const f=T(a(c,"w:left"));f&&(t.borderLeft=f);const w=T(a(c,"w:right"));w&&(t.borderRight=w)}return t}function Lt(n){return n.name==="w:tbl"}async function tt(n,t){const e=[];for(const s of n.children)s.type==="element"&&s.name==="w:tr"&&e.push(s);const r=new Map,o=await Promise.all(e.map((s,c)=>At(s,{...t,activeRowspans:r,rows:e,rowIndex:c}))),i=St(n);return{type:"table",...i&&{attrs:i},content:o}}async function At(n,t){const e=[];let r=0;const o=Tt(n);for(const i of n.children){if(i.type!=="element"||i.name!=="w:tc")continue;const s=t.activeRowspans.get(r);if(s&&s>0){t.activeRowspans.set(r,s-1),r++;continue}let c=Z(i);if(c?.rowSpan===1){const u=$t({...t,colIndex:r});u>1&&(c={...c,rowSpan:u})}if(c?.rowSpan&&c.rowSpan>1&&t.activeRowspans.set(r,c.rowSpan-1),c?.rowSpan===0){r++;continue}const l=await Bt(i,t);e.push({type:"tableCell",...c&&{attrs:c},content:l}),r+=c?.colSpan||1}return{type:"tableRow",...o&&{attrs:o},content:e}}function $t(n){let t=1,e=n.colIndex;for(let r=n.rowIndex+1;r<n.rows.length;r++){const o=n.rows[r];let i=!1;for(const s of o.children){if(s.type!=="element"||s.name!=="w:tc")continue;const c=Z(s),l=c?.colSpan||1;if(e>=0&&e<l){if(c?.rowSpan===0)t++,i=!0;else return t;break}e-=l}if(!i)break}return t}async function Bt(n,t){const e=[];for(const r of n.children)if(r.type==="element"&&r.name==="w:p"){const o=await S(r,t);Array.isArray(o)?e.push(...o):e.push(o)}return e.length?e:[{type:"paragraph",content:[]}]}function L(n){const t=a(n,"w:pPr");return!!t&&a(t,"w:numPr")!==void 0}function E(n){const t=a(n,"w:pPr"),e=t&&a(t,"w:numPr");if(!e)return null;const r=a(e,"w:ilvl"),o=a(e,"w:numId");return!r||!o?null:{numId:o.attributes["w:val"],level:parseInt(r.attributes["w:val"]||"0",10)}}function z(n){const t=a(n,"w:pPr"),e=(t&&a(t,"w:pStyle"))?.attributes["w:val"];return e==="CodeBlock"||e?.startsWith("Code")||!1}function et(n){const t=a(n,"w:pPr"),e=(t&&a(t,"w:pStyle"))?.attributes["w:val"];return e?.startsWith("CodeBlock")&&e.replace("CodeBlock","").toLowerCase()||void 0}const nt="\u2610",O="\u2611";function rt(n){const t=a(n,"w:r");if(!t)return null;const e=a(t,"w:t");if(!e)return null;const r=e.children.find(o=>o.type==="text");return r?.value&&r||null}function j(n){const t=rt(n);if(!t)return!1;const e=t.value;return e.startsWith(nt)||e.startsWith(O)}function ot(n){return rt(n)?.value.startsWith(O)||!1}function Dt(n){return{type:"taskItem",attrs:{checked:ot(n)},content:[Wt(n)]}}function Wt(n){const t=[];let e=!1;for(const o of n.children){if(o.type!=="element"||o.name!=="w:r")continue;if(!e){const c=a(o,"w:t")?.children.find(l=>l.type==="text");if(c?.value){const l=c.value;if(l.startsWith(nt)||l.startsWith(O)){e=!0;const u=l.substring(2).trimStart();u&&t.push({type:"text",text:u});continue}}}const i=Et(o),s=a(o,"w:t")?.children.find(c=>c.type==="text");if(s?.value){const c={type:"text",text:s.value};i.length&&(c.marks=i),t.push(c)}}const r=W(n);return{type:"paragraph",...r&&{attrs:r},content:t.length?t:void 0}}function Et(n){const t=[],e=a(n,"w:rPr");return e&&(a(e,"w:b")&&t.push({type:"bold"}),a(e,"w:i")&&t.push({type:"italic"}),a(e,"w:u")&&t.push({type:"underline"}),a(e,"w:strike")&&t.push({type:"strike"})),t}function it(n){const t=a(n,"w:r");if(!t)return!1;let e=!1,r=!1;for(const o of t.children)o.type==="element"&&(o.name==="w:br"&&o.attributes["w:type"]==="page"?e=!0:o.name==="w:t"?o.children.find(i=>i.type==="text")?.value?.trim().length&&(r=!0):o.name!=="w:rPr"&&(r=!0));return e&&!r}const zt=n=>{const t=[],e=x(n,"w:r");for(const r of e){const o=a(r,"w:t");if(!o)continue;const i=o.children.find(s=>s.type==="text");i&&"value"in i&&i.value&&t.push({type:"text",text:i.value})}return t},Ot=async(n,t,e)=>{const r=await tt(n[t],{hyperlinks:e.hyperlinks,images:e.images,options:e.options,styleMap:e.styleMap});let o=1;return t+1<n.length&&n[t+1].name==="w:p"&&st(n[t+1])&&o++,{nodes:[r],consumed:o}},jt=async(n,t)=>{const e=[];let r=t;for(;r<n.length;){const o=n[r];if(o.name!=="w:p"||!z(o))break;const i=et(o),s={type:"codeBlock",...i&&{attrs:{language:i}},content:zt(o)};e.push(s),r++}return{nodes:e,consumed:r-t}},Ut=async(n,t,e)=>{const{listTypeMap:r}=e,o=[];let i=t;for(;i<n.length;){const s=n[i];if(s.name!=="w:p"||!L(s))break;const c=E(s);if(!c)break;const l=r.get(c.numId),u=l?.type||"bullet",f=[];for(;i<n.length;){const p=n[i];if(p.name!=="w:p"||!L(p))break;const d=E(p);if(!d||d.numId!==c.numId)break;const h=await S(p,e),m=Array.isArray(h)?h[0]:h;f.push({type:"listItem",content:[m]}),i++}const w={type:u==="bullet"?"bulletList":"orderedList",content:f};u==="ordered"&&(w.attrs={type:null,...l?.start!==void 0&&{start:l.start}}),o.push(w)}return{nodes:o,consumed:i-t}},Ht=async(n,t)=>{const e=[];let r=t;for(;r<n.length;){const o=n[r];if(o.name!=="w:p"||!j(o))break;const{convertTaskItem:i}=await import("./chunks/index.mjs"),s=i(o);e.push(s),r++}return{nodes:[{type:"taskList",content:e}],consumed:r-t}},_t=async()=>({nodes:[{type:"horizontalRule"}],consumed:1}),Xt=async(n,t,e)=>{const r=await S(n[t],e);return Array.isArray(r)?{nodes:r,consumed:1}:{nodes:[r],consumed:1}},st=n=>{const t=x(n,"w:r");for(const e of t){const r=a(e,"w:t");if(r){const i=r.children.find(s=>s.type==="text");if(i&&"value"in i&&i.value&&i.value.trim().length>0)return!1}if(a(e,"w:drawing")||a(e,"mc:AlternateContent")||a(e,"w:pict"))return!1;const o=a(e,"w:br");if(o&&o.attributes["w:type"]==="page")return!1}return!0},qt=n=>n.name==="w:tbl"?Ot:n.name==="w:p"?z(n)?jt:j(n)?Ht:L(n)?Ut:it(n)?_t:Xt:null,Jt=async(n,t)=>{const e=[];let r=0;for(;r<n.length;){const o=n[r],i=qt(o);if(!i){r++;continue}if(o.name==="w:p"&&t.ignoreEmptyParagraphs&&st(o)){r++;continue}const{nodes:s,consumed:c}=await i(n,r,t);e.push(...s),r+=c}return e};async function Vt(n,t={}){const{ignoreEmptyParagraphs:e=!1}=t,r=await ut(n),o=lt(r),i=Pt(o),s=kt(o),c=o["word/document.xml"];if(!c)throw new Error("Invalid DOCX file: missing word/document.xml");const l=P(new TextDecoder().decode(c)),u=Ft(o),f=Nt(o);return await Gt(l,s,i,u,f,e,t)}async function Gt(n,t,e,r,o,i,s){if(n.type!=="root")return{type:"doc",content:[]};const c=a(n,"w:document");if(!c)return{type:"doc",content:[]};const l=a(c,"w:body");if(!l)return{type:"doc",content:[]};const u={hyperlinks:e,images:t,listTypeMap:r,styleMap:o,ignoreEmptyParagraphs:i,options:s};return{type:"doc",content:await Jt(l.children.filter(f=>f.type==="element"),u)}}export{S as convertParagraph,tt as convertTable,Dt as convertTaskItem,W as extractAlignment,D as extractMarks,B as extractRuns,et as getCodeBlockLanguage,E as getListInfo,ot as getTaskItemChecked,z as isCodeBlock,it as isHorizontalRule,L as isListItem,Lt as isTable,j as isTaskItem,Vt as parseDOCX};
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@docen/import-docx",
3
+ "version": "0.0.8",
4
+ "description": "A powerful TipTap/ProseMirror extension that imports Microsoft Word DOCX files to editor content",
5
+ "keywords": [
6
+ "converter",
7
+ "docen",
8
+ "document",
9
+ "docx",
10
+ "editor",
11
+ "import",
12
+ "microsoft-word",
13
+ "office",
14
+ "prosemirror",
15
+ "rich-text",
16
+ "tiptap",
17
+ "typescript",
18
+ "word",
19
+ "wysiwyg"
20
+ ],
21
+ "homepage": "https://github.com/DemoMacro/docen#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/DemoMacro/docen/issues"
24
+ },
25
+ "license": "MIT",
26
+ "author": {
27
+ "name": "Demo Macro",
28
+ "email": "abc@imst.xyz",
29
+ "url": "https://imst.xyz/"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/DemoMacro/docen.git"
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "main": "dist/index.mjs",
39
+ "types": "dist/index.d.ts",
40
+ "exports": {
41
+ ".": {
42
+ "import": "./dist/index.mjs",
43
+ "require": "./dist/index.cjs"
44
+ }
45
+ },
46
+ "dependencies": {
47
+ "fflate": "0.8.2",
48
+ "image-meta": "0.2.2",
49
+ "undio": "0.2.0",
50
+ "xast-util-from-xml": "4.0.0"
51
+ },
52
+ "devDependencies": {
53
+ "@tiptap/core": "3.15.3",
54
+ "@types/xast": "2.0.4",
55
+ "@docen/extensions": "0.0.8"
56
+ },
57
+ "peerDependencies": {
58
+ "@napi-rs/canvas": "^0.1.88"
59
+ },
60
+ "peerDependenciesMeta": {
61
+ "@napi-rs/canvas": {
62
+ "optional": true
63
+ }
64
+ },
65
+ "optionalDependencies": {
66
+ "@napi-rs/canvas": "^0.1.88"
67
+ },
68
+ "scripts": {
69
+ "dev": "unbuild --stub",
70
+ "build": "unbuild"
71
+ }
72
+ }