@docen/import-docx 0.0.10 → 0.0.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docen/import-docx",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "A powerful TipTap/ProseMirror extension that imports Microsoft Word DOCX files to editor content",
5
5
  "keywords": [
6
6
  "converter",
@@ -35,12 +35,13 @@
35
35
  "files": [
36
36
  "dist"
37
37
  ],
38
+ "type": "module",
38
39
  "main": "dist/index.mjs",
39
- "types": "dist/index.d.ts",
40
+ "types": "dist/index.d.mts",
40
41
  "exports": {
41
42
  ".": {
42
- "import": "./dist/index.mjs",
43
- "require": "./dist/index.cjs"
43
+ "types": "./dist/index.d.mts",
44
+ "import": "./dist/index.mjs"
44
45
  }
45
46
  },
46
47
  "dependencies": {
@@ -50,10 +51,10 @@
50
51
  "xast-util-from-xml": "4.0.0"
51
52
  },
52
53
  "devDependencies": {
53
- "@tiptap/core": "3.19.0",
54
+ "@tiptap/core": "3.20.1",
54
55
  "@types/xast": "2.0.4",
55
- "@docen/extensions": "0.0.10",
56
- "@docen/utils": "0.0.10"
56
+ "@docen/utils": "0.0.11",
57
+ "@docen/extensions": "0.0.11"
57
58
  },
58
59
  "peerDependencies": {
59
60
  "@napi-rs/canvas": "^0.1.88"
@@ -64,10 +65,10 @@
64
65
  }
65
66
  },
66
67
  "optionalDependencies": {
67
- "@napi-rs/canvas": "^0.1.91"
68
+ "@napi-rs/canvas": "^0.1.96"
68
69
  },
69
70
  "scripts": {
70
- "dev": "unbuild --stub",
71
- "build": "unbuild"
71
+ "dev": "basis build --stub",
72
+ "build": "basis build"
72
73
  }
73
74
  }
package/dist/index.cjs DELETED
@@ -1 +0,0 @@
1
- "use strict";const xastUtilFromXml=require("xast-util-from-xml"),fflate=require("fflate"),undio=require("undio"),imageMeta=require("image-meta"),C=.6666666666666666,g={CODE_BLOCK:"CodeBlock",CODE_PREFIX:"Code"},_$1={docxToTipTap:{left:"left",right:"right",center:"center",both:"justify"}},o$1=96;function s$1(n){return Math.round(n*o$1/1440)}function A$1(n){return`${s$1(n)}px`}function c(n){return Math.round(n/(914400/o$1))}function I(n){const t=parseInt(n,10);if(!isNaN(t))return c(t)}function S$1(n,t){if(!n.children)return null;for(const e of n.children)if(e.type==="element"&&e.name===t)return e;return null}function F(n,t){if(!n.children)return null;for(const e of n.children)if(e.type==="element"){if(e.name===t)return e;const r=F(e,t);if(r)return r}return null}function f(n,t){const e=[];if(!n.children)return e;for(const r of n.children)r.type==="element"&&(r.name===t&&e.push(r),e.push(...f(r,t)));return e}function H$1(n,t){const e=n[t];if(!e)return;const r=parseInt(e,10);if(!isNaN(r))return e}function b$2(n){return t=>n.includes(t)}const s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function uint8ArrayToBase64(n){const t=n.length,e=Math.ceil(t/3)*4,r=Array.from({length:e});let i=0;for(let a=0;a<t;a+=3){const v=n[a],k=a+1<t?n[a+1]:0,T=a+2<t?n[a+2]:0,M=v>>2,O=(v&3)<<4|k>>4,E=(k&15)<<2|T>>6,$=T&63;r[i++]=s[M],r[i++]=s[O],r[i++]=a+1<t?s[E]:"=",r[i++]=a+2<t?s[$]:"="}return r.join("")}function base64ToUint8Array(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 isNode=globalThis.process?.release?.name==="node",isBrowser=typeof window<"u";async function h(n){const t=await n;return t.default||t}let o,u$1=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 DOMCanvasFactory extends u$1{_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 NodeCanvasFactory extends u$1{constructor({enableHWA:t=!1}={}){super({enableHWA:t})}_createCanvas(t,e){if(!o)throw new Error("@napi-rs/canvas module is not resolved");return o.createCanvas(t,e)}}async function resolveCanvasModule(n){o??=await h(n())}async function createCanvasFactory(n){if(isBrowser)return DOMCanvasFactory;if(isNode){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 resolveCanvasModule(n),NodeCanvasFactory}throw new Error("Unsupported environment for canvas operations")}async function cropImageIfNeeded(n,t,e={}){if(!t||!t.left&&!t.top&&!t.right&&!t.bottom||e.enabled===!1)return n;try{const r=await createCanvasFactory(e.canvasImport),i=await w$1(n,r),a=(t.left||0)/1e5*i.width,v=(t.top||0)/1e5*i.height,k=(t.right||0)/1e5*i.width,T=(t.bottom||0)/1e5*i.height,M=Math.round(i.width-a-k),O=Math.round(i.height-v-T);if(M<=0||O<=0)return console.warn("Invalid crop dimensions, returning original image"),n;const E=new r().create(M,O);if(!E.context)throw new Error("Failed to get 2D context from canvas");E.context.drawImage(i,a,v,M,O,0,0,M,O);const $=E.canvas.toDataURL(),W=await(await fetch($)).arrayBuffer();return new Uint8Array(W)}catch(r){return console.warn("Image cropping failed, returning original image:",r),n}}async function w$1(n,t){if(isBrowser){const e=new Blob([n.buffer]),r=URL.createObjectURL(e);try{const i=new Image;return new Promise((a,v)=>{i.onload=()=>{URL.revokeObjectURL(r),a(i)},i.onerror=()=>{URL.revokeObjectURL(r),v(new Error("Failed to load image"))},i.src=r})}catch(i){throw URL.revokeObjectURL(r),i}}else{if(!o)throw new Error("@napi-rs/canvas module is not resolved");return await o.loadImage(Buffer.from(n))}}const G="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",S=b$2(["left","right","center","inside","outside"]),X$1=b$2(["top","bottom","center","inside","outside"]),_=b$2(["page","character","column","margin","leftMargin","rightMargin","insideMargin","outsideMargin"]),j=b$2(["page","paragraph","margin","topMargin","bottomMargin","insideMargin","outsideMargin","line"]);function B$1(n){const t=n.attributes.l,e=n.attributes.t,r=n.attributes.r,i=n.attributes.b;if(!(!t&&!e&&!r&&!i))return{left:t?parseInt(t,10):void 0,top:e?parseInt(e,10):void 0,right:r?parseInt(r,10):void 0,bottom:i?parseInt(i,10):void 0}}function U(n){const t=S$1(n,"wp:align"),e=S$1(n,"wp:posOffset");let r;if(t?.children[0]?.type==="text"){const a=t.children[0].value;S(a)&&(r=a)}const i=e?.children[0]?.type==="text"?parseInt(e.children[0].value,10):void 0;if(!(!r&&i===void 0))return{...r&&{align:r},...i!==void 0&&{offset:i}}}function H(n){const t=S$1(n,"wp:align"),e=S$1(n,"wp:posOffset");let r;if(t?.children[0]?.type==="text"){const a=t.children[0].value;X$1(a)&&(r=a)}const i=e?.children[0]?.type==="text"?parseInt(e.children[0].value,10):void 0;if(!(!r&&i===void 0))return{...r&&{align:r},...i!==void 0&&{offset:i}}}function findDrawingElement(n){let t=S$1(n,"w:drawing");if(t)return t;const e=S$1(n,"mc:AlternateContent"),r=e&&S$1(e,"mc:Choice");return r?S$1(r,"w:drawing"):null}function N(n,t,e,r){const i=e/r,a=n/t;return Math.abs(i-a)>.1?i>a?{width:n,height:Math.round(n/i)}:{width:Math.round(t*i),height:t}:{width:n,height:t}}async function extractImages(n,t){const e=new Map,r=n["word/_rels/document.xml.rels"];if(!r)return e;const i=xastUtilFromXml.fromXml(new TextDecoder().decode(r)),a=S$1(i,"Relationships");if(!a)return e;const v=f(a,"Relationship");for(const k of v)if(k.attributes.Type===G&&k.attributes.Id&&k.attributes.Target){const T="word/"+k.attributes.Target,M=n[T];if(!M)continue;let O,E,$="png";try{const q=imageMeta.imageMeta(M);O=q.width,E=q.height,q.type&&($=q.type)}catch{}let W;if(t)W=(await t({id:k.attributes.Id,contentType:`image/${$}`,data:M})).src;else{const q=uint8ArrayToBase64(M);W=`data:image/${$};base64,${q}`}e.set(k.attributes.Id,{src:W,width:O,height:E})}return e}async function extractImageFromDrawing(n,t){const{context:e}=t,r=F(n,"a:blip");if(!r?.attributes["r:embed"])return null;const i=r.attributes["r:embed"],a=e.images.get(i);if(!a)return null;let v=a.src;const k=F(n,"a:srcRect");if(k){const K=B$1(k);if(K&&v.startsWith("data:")){const[V,tt]=v.split(",");if(tt){const et=base64ToUint8Array(tt);try{const nt=await cropImageIfNeeded(et,K,{canvasImport:e.image?.canvasImport,enabled:e.image?.enableImageCrop??!1}),rt=uint8ArrayToBase64(nt);v=`${V},${rt}`}catch(nt){console.warn("Image cropping failed, using original image:",nt)}}}}const T=F(n,"wp:extent");let M,O;if(T){const K=T.attributes.cx,V=T.attributes.cy;typeof K=="string"&&(M=I(K)),typeof V=="string"&&(O=I(V))}const E=F(n,"a:xfrm");let $;if(E?.attributes.rot){const K=parseInt(E.attributes.rot,10);isNaN(K)||($=K/6e4)}const W=F(n,"wp:docPr")?.attributes.title,q=F(n,"wp:positionH"),J=F(n,"wp:positionV");let Y;if(q||J){const K=q?U(q):void 0,V=J?H(J):void 0,tt=q?.attributes.relativeFrom,et=J?.attributes.relativeFrom,nt=typeof tt=="string"&&_(tt)?tt:"page",rt=typeof et=="string"&&j(et)?et:"page";Y={horizontalPosition:{relative:nt,...K?.align&&{align:K.align},...K?.offset!==void 0&&{offset:K.offset}},verticalPosition:{relative:rt,...V?.align&&{align:V.align},...V?.offset!==void 0&&{offset:V.offset}}}}const Q=F(n,"pic:spPr");let Z;if(Q){const K=F(Q,"a:ln"),V=K&&F(K,"a:solidFill"),tt=V&&F(V,"a:srgbClr");tt?.attributes.val&&(Z={type:"solidFill",solidFillType:"rgb",value:tt.attributes.val})}return{type:"image",attrs:{src:v,alt:"",...M!==void 0&&{width:M},...O!==void 0&&{height:O},...$!==void 0&&{rotation:$},...W&&{title:W},...Y&&{floating:Y},...Z&&{outline:Z}}}}function L$1(n,t,e){if(t&&e&&n.width&&n.height){const r=N(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 extractImagesFromDrawing(n,t){const e=[],r=S$1(n,"wp:inline")||S$1(n,"wp:anchor");if(!r)return e;const i=S$1(r,"wp:extent");let a,v;if(i){const O=i.attributes.cx,E=i.attributes.cy;typeof O=="string"&&(a=I(O)),typeof E=="string"&&(v=I(E))}const k=S$1(r,"a:graphic");if(!k)return e;const T=S$1(k,"a:graphicData");if(!T)return e;const M=S$1(T,"wpg:wgp");if(M){const O=S$1(M,"wpg:grpSp"),E=O?[...f(O,"pic:pic"),...f(O,"pic")]:[...f(M,"pic:pic"),...f(M,"pic")];for(const $ of E){const W=S$1($,"a:graphic");if(!W){const Q=S$1($,"pic:blipFill")||F($,"a:blipFill");if(!Q)continue;const Z=S$1(Q,"a:blip")||F(Q,"a:blip");if(!Z?.attributes["r:embed"])continue;const K=Z.attributes["r:embed"],V=t.context.images.get(K);if(!V)continue;e.push(L$1(V,a,v));continue}const q={children:[W]},J=await extractImageFromDrawing(q,t);if(!J)continue;const Y=q.children[0]?.type==="element"?F(q.children[0],"a:blip")?.attributes["r:embed"]:void 0;if(a&&v&&Y){const Q=t.context.images.get(Y);if(Q?.width&&Q?.height){const Z=N(a,v,Q.width,Q.height);J.attrs.width=Z.width,J.attrs.height=Z.height}else J.attrs.width=a,J.attrs.height=v}e.push(J)}}else{const O=await extractImageFromDrawing(n,t);O&&e.push(O)}return e}const p="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";function extractHyperlinks(n){const t=new Map,e=n["word/_rels/document.xml.rels"];if(!e)return t;const r=xastUtilFromXml.fromXml(new TextDecoder().decode(e)),i=S$1(r,"Relationships");if(!i)return t;const a=f(i,"Relationship");for(const v of a)v.attributes.Type===p&&v.attributes.Id&&v.attributes.Target&&t.set(v.attributes.Id,v.attributes.Target);return t}function parseNumberingXml(n){const t=new Map,e=new Map,r=n["word/numbering.xml"];if(!r)return t;const i=xastUtilFromXml.fromXml(new TextDecoder().decode(r)),a=new Map,v=S$1(i,"w:numbering");if(!v)return t;const k=f(v,"w:abstractNum");for(const M of k){const O=M.attributes["w:abstractNumId"],E=S$1(M,"w:lvl");if(!E)continue;const $=S$1(E,"w:numFmt");$?.attributes["w:val"]&&a.set(O,$.attributes["w:val"]);const W=S$1(E,"w:start");W?.attributes["w:val"]&&e.set(O,parseInt(W.attributes["w:val"],10))}const T=f(v,"w:num");for(const M of T){const O=M.attributes["w:numId"],E=S$1(M,"w:abstractNumId");if(!E?.attributes["w:val"])continue;const $=E.attributes["w:val"],W=a.get($);if(!W)continue;const q=e.get($);W==="bullet"?t.set(O,{type:"bullet"}):t.set(O,{type:"ordered",...q!==void 0&&{start:q}})}return t}function parseStylesXml(n){const t=new Map,e=n["word/styles.xml"];if(!e)return t;const r=xastUtilFromXml.fromXml(new TextDecoder().decode(e)),i=S$1(r,"w:styles");if(!i)return t;const a=f(i,"w:style").filter(v=>v.attributes["w:type"]==="paragraph");for(const v of a){const k=v.attributes["w:styleId"];if(!k)continue;const T={styleId:k},M=S$1(v,"w:name");M?.attributes["w:val"]&&(T.name=M.attributes["w:val"]);const O=S$1(v,"w:pPr");if(O){const $=S$1(O,"w:outlineLvl");$?.attributes["w:val"]!==void 0&&(T.outlineLvl=parseInt($.attributes["w:val"],10))}const E=S$1(v,"w:rPr");if(E){const $={},W=S$1(E,"w:color");if(W?.attributes["w:val"]&&W.attributes["w:val"]!=="auto"){const Y=W.attributes["w:val"];$.color=Y.startsWith("#")?Y:`#${Y}`}S$1(E,"w:b")&&($.bold=!0),S$1(E,"w:i")&&($.italic=!0),S$1(E,"w:u")&&($.underline=!0),S$1(E,"w:strike")&&($.strike=!0);const q=S$1(E,"w:sz");if(q?.attributes["w:val"]){const Y=q.attributes["w:val"],Q=parseInt(Y,10);isNaN(Q)||($.fontSize=Q)}const J=S$1(E,"w:rFonts");J?.attributes["w:ascii"]&&($.fontFamily=J.attributes["w:ascii"]),Object.keys($).length>0&&(T.charFormat=$)}t.set(k,T)}return t}function w(n,t){const e=S$1(n,"w:t");if(!e)return null;const r=e.children.find(a=>a.type==="text");if(!r?.value)return null;const i=extractMarks(n,t);return{type:"text",text:r.value,...i.length&&{marks:i}}}async function extractRuns(n,t){const{context:e,styleInfo:r}=t,i=[];for(const a of n.children)if(a.type==="element"){if(a.name==="w:hyperlink"){const v=a,k=v.attributes["r:id"],T=e.hyperlinks.get(k);if(!T)continue;for(const M of v.children){if(M.type!=="element"||M.name!=="w:r")continue;const O=M,E=findDrawingElement(O);if(E){const W=await extractImageFromDrawing(E,{context:e});if(W){i.push(W);continue}const q=await extractImagesFromDrawing(E,{context:e});if(q.length){i.push(...q);continue}}const $=w(O,r);$&&($.marks=$.marks||[],$.marks.push({type:"link",attrs:{href:T}}),i.push($))}}else if(a.name==="w:r"){const v=a,k=findDrawingElement(v);if(k){const M=await extractImagesFromDrawing(k,{context:e});if(M.length){i.push(...M);continue}}if(S$1(v,"w:br")){const M=extractMarks(v,r);i.push({type:"hardBreak",...M.length&&{marks:M}})}const T=w(v,r);T&&i.push(T)}}return i}function extractMarks(n,t){const e=[],r=S$1(n,"w:rPr");let i={};if(t?.charFormat&&(i={...t.charFormat}),r){const a=S$1(r,"w:b");a&&(a.attributes["w:val"]==="false"?i.bold=!1:i.bold=!0);const v=S$1(r,"w:i");v&&(v.attributes["w:val"]==="false"?i.italic=!1:i.italic=!0),S$1(r,"w:u")&&(i.underline=!0),S$1(r,"w:strike")&&(i.strike=!0);const k=S$1(r,"w:color");if(k?.attributes["w:val"]&&k.attributes["w:val"]!=="auto"){const $=k.attributes["w:val"];i.color=$.startsWith("#")?$:`#${$}`}const T=S$1(r,"w:sz");if(T?.attributes["w:val"]){const $=T.attributes["w:val"],W=parseInt($,10);isNaN(W)||(i.fontSize=W)}const M=S$1(r,"w:rFonts");M?.attributes["w:ascii"]&&(i.fontFamily=M.attributes["w:ascii"]);const O=S$1(r,"w:shd");if(O?.attributes["w:fill"]&&O.attributes["w:fill"]!=="auto"){const $=O.attributes["w:fill"];i.backgroundColor=$.startsWith("#")?$:`#${$}`}S$1(r,"w:highlight")&&e.push({type:"highlight"});const E=S$1(r,"w:vertAlign");if(E){const $=E.attributes["w:val"];$==="subscript"?e.push({type:"subscript"}):$==="superscript"&&e.push({type:"superscript"})}}if(i.bold&&e.push({type:"bold"}),i.italic&&e.push({type:"italic"}),i.underline&&e.push({type:"underline"}),i.strike&&e.push({type:"strike"}),i.color||i.backgroundColor||i.fontSize||i.fontFamily){const a={color:i.color||"",backgroundColor:i.backgroundColor||"",fontSize:"",fontFamily:"",lineHeight:""};if(i.fontSize){const v=Math.round(i.fontSize*C*10)/10;a.fontSize=`${v}px`}i.fontFamily&&(a.fontFamily=i.fontFamily),e.push({type:"textStyle",attrs:a})}return e}function extractAlignment(n){const t=S$1(n,"w:pPr");if(!t)return;const e=S$1(t,"w:jc");if(!e?.attributes["w:val"])return;const r=e.attributes["w:val"],i=_$1.docxToTipTap[r];return i?{textAlign:i}:void 0}function d(n){const t=S$1(n,"w:pPr");if(!t)return null;const e={},r=S$1(t,"w:ind");if(r){const a=H$1(r.attributes,"w:left");if(a){const T=parseInt(a,10);e.indentLeft=A$1(T)}const v=H$1(r.attributes,"w:right");if(v){const T=parseInt(v,10);e.indentRight=A$1(T)}const k=H$1(r.attributes,"w:firstLine");if(k){const T=parseInt(k,10);e.indentFirstLine=A$1(T)}else{const T=H$1(r.attributes,"w:hanging");if(T){const M=a?parseInt(a,10):0,O=parseInt(T,10),E=M-O;e.indentFirstLine=A$1(E)}}}const i=S$1(t,"w:spacing");if(i){const a=H$1(i.attributes,"w:before");if(a){const k=parseInt(a,10);e.spacingBefore=A$1(k)}const v=H$1(i.attributes,"w:after");if(v){const k=parseInt(v,10);e.spacingAfter=A$1(k)}}return Object.keys(e).length?e:null}async function convertParagraph(n,t){const{context:e,styleInfo:r}=t,i=S$1(n,"w:pPr"),a=(i&&S$1(i,"w:pStyle"))?.attributes["w:val"];if(a&&e.styleMap){const M=e.styleMap.get(a);if(M?.outlineLvl!==void 0&&M.outlineLvl>=0&&M.outlineLvl<=5){const E=M.outlineLvl+1;return b$1(n,t,M,E)}const O=a.match(/^Heading(\d+)$/);if(O){const E=parseInt(O[1],10);return b$1(n,t,M,E)}}const v=a&&e.styleMap?e.styleMap.get(a):void 0,k=await extractRuns(n,{context:e,styleInfo:r||v}),T={...extractAlignment(n),...d(n)};if(P(n)){const M=k.filter(O=>O.type!=="hardBreak");return[{type:"paragraph",...Object.keys(T).length&&{attrs:T},content:M.length?M:void 0},{type:"horizontalRule"}]}if(k.length===1&&k[0].type==="hardBreak"){const M=S$1(n,"w:r");if((M&&S$1(M,"w:br"))?.attributes["w:type"]==="page")return{type:"horizontalRule"}}if(k.length===1&&k[0].type==="image"){const M=k[0];return{type:"paragraph",...Object.keys(T).length&&{attrs:T},content:[M]}}return{type:"paragraph",...Object.keys(T).length&&{attrs:T},content:k}}function P(n){const t=[],e=r=>{if(r.name==="w:r")t.push(r);else for(const i of r.children)i.type==="element"&&e(i)};return e(n),t.some(r=>S$1(r,"w:br")?.attributes["w:type"]==="page")}async function b$1(n,t,e,r){return{type:"heading",attrs:{level:r,...d(n)},content:await extractRuns(n,{context:t.context,styleInfo:e})}}function parseBorder(n){if(!n)return null;const t=n.attributes["w:val"],e=n.attributes["w:sz"],r=n.attributes["w:color"],i={single:"solid",dashed:"dashed",dotted:"dotted",double:"double",none:"none",nil:"none"},a={};if(r&&r!=="auto"&&(a.color=`#${r}`),e){const v=parseInt(e);isNaN(v)||(a.width=Math.round(v/6))}return t&&i[t]&&(a.style=i[t]),Object.keys(a).length>0?a:null}function parseTableProperties(n){const t={marginTop:void 0,marginBottom:void 0,marginLeft:void 0,marginRight:void 0},e=S$1(n,"w:tblPr");if(!e)return null;const r=S$1(e,"w:tblCellMar");if(!r)return null;const i=S$1(r,"w:top");if(i?.attributes["w:w"]){const T=parseInt(i.attributes["w:w"]);isNaN(T)||(t.marginTop=T)}const a=S$1(r,"w:bottom");if(a?.attributes["w:w"]){const T=parseInt(a.attributes["w:w"]);isNaN(T)||(t.marginBottom=T)}const v=S$1(r,"w:left");if(v?.attributes["w:w"]){const T=parseInt(v.attributes["w:w"]);isNaN(T)||(t.marginLeft=T)}const k=S$1(r,"w:right");if(k?.attributes["w:w"]){const T=parseInt(k.attributes["w:w"]);isNaN(T)||(t.marginRight=T)}return t.marginTop===void 0&&t.marginBottom===void 0&&t.marginLeft===void 0&&t.marginRight===void 0?null:t}function parseRowProperties(n){const t={rowHeight:null},e=S$1(n,"w:trPr");if(!e)return t;const r=S$1(e,"w:trHeight");if(r?.attributes["w:val"]){const i=parseInt(r.attributes["w:val"]),a=s$1(i);t.rowHeight=`${a}px`}return t}function parseCellProperties(n){const t={colspan:1,rowspan:1,colwidth:null},e=S$1(n,"w:tcPr");if(!e)return t;const r=S$1(e,"w:gridSpan");r?.attributes["w:val"]&&(t.colspan=parseInt(r.attributes["w:val"])),S$1(e,"w:vMerge")?.attributes["w:val"]==="continue"&&(t.rowspan=0);const i=S$1(e,"w:tcW");if(i?.attributes["w:w"]){const T=parseInt(i.attributes["w:w"]),M=s$1(T);t.colwidth=[M]}const a=S$1(e,"w:shd");a?.attributes["w:fill"]&&(t.backgroundColor=`#${a.attributes["w:fill"]}`);const v=S$1(e,"w:vAlign");v?.attributes["w:val"]&&(t.verticalAlign=v.attributes["w:val"]);const k=S$1(e,"w:tcBorders");if(k){const T=parseBorder(S$1(k,"w:top"));T&&(t.borderTop=T);const M=parseBorder(S$1(k,"w:bottom"));M&&(t.borderBottom=M);const O=parseBorder(S$1(k,"w:left"));O&&(t.borderLeft=O);const E=parseBorder(S$1(k,"w:right"));E&&(t.borderRight=E)}return t}function isTable(n){return n.name==="w:tbl"}async function convertTable(n,t){const e=[];for(const v of n.children)v.type==="element"&&v.name==="w:tr"&&e.push(v);const r=new Map,i=await Promise.all(e.map((v,k)=>u(v,{context:t.context,activeRowspans:r,rows:e,rowIndex:k}))),a=parseTableProperties(n);return{type:"table",...a&&{attrs:a},content:i}}async function u(n,t){const e=[];let r=0;const i=parseRowProperties(n);for(const a of n.children){if(a.type!=="element"||a.name!=="w:tc")continue;const v=t.activeRowspans.get(r);if(v&&v>0){t.activeRowspans.set(r,v-1),r++;continue}let k=parseCellProperties(a);if(k?.rowspan===1){const M=x$1({rows:t.rows,rowIndex:t.rowIndex,colIndex:r});M>1&&(k={...k,rowspan:M})}if(k?.rowspan&&k.rowspan>1&&t.activeRowspans.set(r,k.rowspan-1),k?.rowspan===0){r++;continue}const T=await b(a,t);e.push({type:"tableCell",...k&&{attrs:k},content:T}),r+=k?.colspan||1}return{type:"tableRow",...i&&{attrs:i},content:e}}function x$1(n){let t=1,e=n.colIndex;for(let r=n.rowIndex+1;r<n.rows.length;r++){const i=n.rows[r];let a=!1;for(const v of i.children){if(v.type!=="element"||v.name!=="w:tc")continue;const k=parseCellProperties(v),T=k?.colspan||1;if(e>=0&&e<T){if(k?.rowspan===0)t++,a=!0;else return t;break}e-=T}if(!a)break}return t}async function b(n,t){const e=[];for(const r of n.children)if(r.type==="element"&&r.name==="w:p"){const i=await convertParagraph(r,t);Array.isArray(i)?e.push(...i):e.push(i)}return e.length?e:[{type:"paragraph",content:[]}]}const m="\u2610",l="\u2611";function x(n){const t=S$1(n,"w:r");if(!t)return null;const e=S$1(t,"w:t");if(!e)return null;const r=e.children.find(i=>i.type==="text");return r?.value&&r||null}function isTaskItem(n){const t=x(n);if(!t)return!1;const e=t.value;return e.startsWith(m)||e.startsWith(l)}function getTaskItemChecked(n){return x(n)?.value.startsWith(l)||!1}async function convertTaskItem(n,t){return{type:"taskItem",attrs:{checked:getTaskItemChecked(n)},content:[await y(n,t)]}}async function convertTaskList(n,t){const{siblings:e,index:r,processedIndices:i}=t,a=[];let v=r;for(;v<e.length;){const k=e[v];if(k.name!=="w:p"||!isTaskItem(k))break;i.add(v);const T=await convertTaskItem(k,{context:t.context,styleInfo:t.styleInfo});a.push(T),v++}return{type:"taskList",content:a}}async function y(n,t){const{context:e,styleInfo:r}=t,i=await extractRuns(n,{context:e,styleInfo:r});if(i.length>0&&i[0].type==="text"){const v=i[0],k=v.text;if(k.startsWith(m)||k.startsWith(l)){const T=k.substring(2).trimStart();T?v.text=T:i.shift()}}const a=extractAlignment(n);return{type:"paragraph",...a&&{attrs:a},content:i.length?i:void 0}}function isCodeBlock(n){const t=S$1(n,"w:pPr"),e=(t&&S$1(t,"w:pStyle"))?.attributes["w:val"];return e===g.CODE_BLOCK||e?.startsWith(g.CODE_PREFIX)||!1}function getCodeBlockLanguage(n){const t=S$1(n,"w:pPr"),e=(t&&S$1(t,"w:pStyle"))?.attributes["w:val"];return e?.startsWith(g.CODE_BLOCK)&&e.replace(g.CODE_BLOCK,"").toLowerCase()||void 0}function isListItem(n){const t=S$1(n,"w:pPr");return!!t&&S$1(t,"w:numPr")!==void 0}function getListInfo(n){const t=S$1(n,"w:pPr"),e=t&&S$1(t,"w:numPr");if(!e)return null;const r=S$1(e,"w:ilvl"),i=S$1(e,"w:numId");return!r||!i?null:{numId:i.attributes["w:val"],level:parseInt(r.attributes["w:val"]||"0",10)}}function isHorizontalRule(n){const t=S$1(n,"w:r");if(!t)return!1;let e=!1,r=!1;for(const i of t.children)i.type==="element"&&(i.name==="w:br"&&i.attributes["w:type"]==="page"?e=!0:i.name==="w:t"?i.children.find(a=>a.type==="text")?.value?.trim().length&&(r=!0):i.name!=="w:rPr"&&(r=!0));return e&&!r}async function parseDOCX(n,t={}){const e=await undio.toUint8Array(n),r=fflate.unzipSync(e),i=extractHyperlinks(r),a=await extractImages(r,t.image?.handler),v=r["word/document.xml"];if(!v)throw new Error("Invalid DOCX file: missing word/document.xml");const k=xastUtilFromXml.fromXml(new TextDecoder().decode(v)),T=parseNumberingXml(r),M=parseStylesXml(r),O={...t,hyperlinks:i,images:a,listTypeMap:T,styleMap:M};return await D(k,{context:O})}async function D(n,t){if(n.type!=="root")return{type:"doc",content:[]};const e=S$1(n,"w:document");if(!e)return{type:"doc",content:[]};const r=S$1(e,"w:body");return r?{type:"doc",content:await A(r.children.filter(i=>i.type==="element"),t)}:{type:"doc",content:[]}}async function A(n,t){const e=[],r=new Set;for(let i=0;i<n.length;i++){if(r.has(i))continue;const a=n[i];if(t.context.ignoreEmptyParagraphs&&a.name==="w:p"&&z(a))continue;const v=await L(a,n,i,t,r);Array.isArray(v)?e.push(...v):v&&e.push(v)}return e}async function L(n,t,e,r,i){switch(n.name){case"w:tbl":return await convertTable(n,r);case"w:p":return isCodeBlock(n)?await X(n):isTaskItem(n)?await convertTaskList(n,{...r,siblings:t,index:e,processedIndices:i}):isListItem(n)?await B(n,t,e,r,i):isHorizontalRule(n)?{type:"horizontalRule"}:await convertParagraph(n,r);default:return null}}async function X(n){const t=getCodeBlockLanguage(n),e=R(n);return{type:"codeBlock",...t&&{attrs:{language:t}},content:e}}async function B(n,t,e,r,i){const a=getListInfo(n);if(!a)return await convertParagraph(n,r);const v=r.context.listTypeMap.get(a.numId),k=v?.type||"bullet",T=[];let M=e;for(;M<t.length;){const E=t[M];if(E.name!=="w:p"||!isListItem(E))break;const $=getListInfo(E);if(!$||$.numId!==a.numId)break;i.add(M);const W=await convertParagraph(E,r),q=Array.isArray(W)?W[0]:W;T.push({type:"listItem",content:[q]}),M++}const O={type:k==="bullet"?"bulletList":"orderedList",content:T};return k==="ordered"&&(O.attrs={type:null,...v?.start!==void 0&&{start:v.start}}),O}function R(n){const t=[],e=f(n,"w:r");for(const r of e){const i=S$1(r,"w:t");if(!i)continue;const a=i.children.find(v=>v.type==="text");a&&"value"in a&&a.value&&t.push({type:"text",text:a.value})}return t}function z(n){const t=f(n,"w:r");for(const e of t){const r=S$1(e,"w:t");if(r){const a=r.children.find(v=>v.type==="text");if(a&&"value"in a&&a.value&&a.value.trim().length>0)return!1}if(S$1(e,"w:drawing")||S$1(e,"mc:AlternateContent")||S$1(e,"w:pict"))return!1;const i=S$1(e,"w:br");if(i&&i.attributes["w:type"]==="page")return!1}return!0}exports.convertParagraph=convertParagraph,exports.convertTable=convertTable,exports.convertTaskItem=convertTaskItem,exports.convertTaskList=convertTaskList,exports.extractAlignment=extractAlignment,exports.extractMarks=extractMarks,exports.extractRuns=extractRuns,exports.getCodeBlockLanguage=getCodeBlockLanguage,exports.getListInfo=getListInfo,exports.getTaskItemChecked=getTaskItemChecked,exports.isCodeBlock=isCodeBlock,exports.isHorizontalRule=isHorizontalRule,exports.isListItem=isListItem,exports.isTable=isTable,exports.isTaskItem=isTaskItem,exports.parseDOCX=parseDOCX;