@docen/export-docx 0.0.9 → 0.0.10

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 CHANGED
@@ -1 +1 @@
1
- import{ExternalHyperlink as Mt,TextRun as v,ImageRun as q,Paragraph as d,HeadingLevel as b,TableCell as X,TableRow as St,Table as J,PageBreak as Ht,TableOfContents as Pt,Document as Nt,Packer as Rt,convertInchesToTwip as E,AlignmentType as K,LevelFormat as Q}from"docx";import{imageMeta as V}from"image-meta";import{ofetch as $t}from"ofetch";const Y=96;function T(t){return Math.round(t*96/1440)}function zt(t){return`${T(t)}px`}function f(t){return Math.round(t*(1440/96))}function Z(t){return Math.round(t/(914400/96))}function Wt(t){return Math.round(t*(914400/96))}function Ot(t){const e=parseInt(t,10);if(!isNaN(e))return Z(e)}const jt=/^([\d.]+)(px|pt|em|rem|%|)?$/,_t={px:1,pt:1.333,em:16,rem:16,"%":.16};function g(t){if(!t)return 0;t=t.trim();const e=t.match(jt);if(!e)return 0;const n=parseFloat(e[1]);if(isNaN(n))return 0;const r=e[2]||"px",o=_t[r]??1;return Math.round(n*o)}const Gt=/^([\d.]+)(in|mm|cm|pt|pc|pi)$/,Ut={in:1,mm:1/25.4,cm:1/2.54,pt:1/72,pc:1/6,pi:1/6};function I(t){if(typeof t=="number")return t;const e=t.match(Gt);if(e){const r=parseFloat(e[1]),o=e[2],a=Ut[o];return a!==void 0?r*a:r}const n=parseFloat(t);return isNaN(n)?6.5:n}function P(t){if(typeof t=="number")return t;const e=I(t);return Math.round(e*96)}const tt={black:"#000000",white:"#FFFFFF",red:"#FF0000",green:"#008000",blue:"#0000FF",yellow:"#FFFF00",orange:"#FFA500",purple:"#800080",pink:"#FFC0CB",brown:"#A52A2A",gray:"#808080",grey:"#808080",cyan:"#00FFFF",magenta:"#FF00FF",lime:"#00FF00",navy:"#000080",teal:"#008080",maroon:"#800000",olive:"#808000",silver:"#C0C0C0",gold:"#FFD700",indigo:"#4B0082",violet:"#EE82EE",aqua:"#00FFFF",fuchsia:"#FF00FF",darkblue:"#00008B",darkcyan:"#008B8B",darkgrey:"#A9A9A9",darkgreen:"#006400",darkkhaki:"#BDB76B",darkmagenta:"#8B008B",darkolivegreen:"#556B2F",darkorange:"#FF8C00",darkorchid:"#9932CC",darkred:"#8B0000",darksalmon:"#E9967A",darkviolet:"#9400D3",lightblue:"#ADD8E6",lightcyan:"#E0FFFF",lightgreen:"#90EE90",lightgrey:"#D3D3D3",lightpink:"#FFB6C1",lightyellow:"#FFFFE0"};function N(t){if(t)return t.startsWith("#")?t:tt[t.toLowerCase()]||t}function qt(t){return t.startsWith("#")?t:`#${t}`}function Xt(t,e){if(!t.children)return null;for(const n of t.children)if(n.type==="element"&&n.name===e)return n;return null}function et(t,e){if(!t.children)return null;for(const n of t.children)if(n.type==="element"){if(n.name===e)return n;const r=et(n,e);if(r)return r}return null}function nt(t,e){const n=[];if(!t.children)return n;for(const r of t.children)r.type==="element"&&(r.name===e&&n.push(r),n.push(...nt(r,e)));return n}function Jt(t,e){const n=t[e];if(!n)return;const r=parseInt(n,10);if(!isNaN(r))return n}const rt=(t,e)=>t?typeof t=="number"?t:Math.round(I(t)*1440):e;function ot(t){if(!t?.sections?.length)return T(11906-1440*2);const e=t.sections[0];if(!e.properties?.page)return T(11906-1440*2);const n=e.properties.page;let r=11906;if(n.size?.width){const p=n.size.width;r=typeof p=="number"?p:Math.round(I(p)*1440)}const o=n.margin,a=rt(o?.left,1440),i=rt(o?.right,1440),c=r-a-i;return Math.max(T(c),Y)}const at=6.5*96,it={jpg:"jpeg",jpeg:"jpeg",png:"png",gif:"gif",bmp:"bmp",tiff:"tiff"},Kt={jpeg:"jpg",jpg:"jpg",png:"png",gif:"gif",bmp:"bmp"};function st(t){if(!t)return"png";const e=t.toLowerCase();return Kt[e]??"png"}function R(t){if(t.startsWith("data:")){const e=t.match(/data:image\/(\w+);/);if(e){const n=e[1].toLowerCase();return it[n]||"png"}}else{const e=t.split(".").pop()?.toLowerCase();if(e)return it[e]||"png"}return"png"}const ct=(t,e=at)=>{if(!t.width||!t.height)return{width:e,height:Math.round(e*.75)};if(t.width<=e)return{width:t.width,height:t.height};const n=e/t.width;return{width:e,height:Math.round(t.height*n)}};function Qt(){return{horizontalPosition:{relative:"page",align:"center"},verticalPosition:{relative:"page",align:"top"},lockAnchor:!0,behindDocument:!1,inFrontOfText:!1}}function lt(t,e,n){if(t.attrs?.width!==void 0&&t.attrs?.width!==null)return t.attrs.width;const r=n!==void 0?P(n):void 0;return e?.width&&e?.height?ct(e,r).width:r||at}function dt(t,e,n,r){if(t.attrs?.height!==void 0&&t.attrs?.height!==null)return t.attrs.height;const o=r!==void 0?P(r):void 0;return n?.width&&n?.height?ct(n,o).height:Math.round(e*.75)}async function ut(t){try{const e=await(await $t(t,{responseType:"blob"})).bytes();let n;try{n=V(e)}catch(r){console.warn("Failed to extract image metadata:",r),n={width:void 0,height:void 0,type:R(t)||"png",orientation:void 0}}return{data:e,meta:n}}catch(e){throw console.warn(`Failed to fetch image from ${t}:`,e),e}}const $=(t,e)=>{if(!e)return t;let n={...t};return(e.indentLeft||e.indentRight||e.indentFirstLine)&&(n={...n,indent:{...e.indentLeft&&{left:f(g(e.indentLeft))},...e.indentRight&&{right:f(g(e.indentRight))},...e.indentFirstLine&&{firstLine:f(g(e.indentFirstLine))}}}),(e.spacingBefore||e.spacingAfter)&&(n={...n,spacing:{...e.spacingBefore&&{before:f(g(e.spacingBefore))},...e.spacingAfter&&{after:f(g(e.spacingAfter))}}}),e.textAlign&&(n={...n,alignment:{left:"left",right:"right",center:"center",justify:"both"}[e.textAlign]}),n};function x(t){const e=t.marks?.some(s=>s.type==="bold"),n=t.marks?.some(s=>s.type==="italic"),r=t.marks?.some(s=>s.type==="underline"),o=t.marks?.some(s=>s.type==="strike"),a=t.marks?.some(s=>s.type==="code"),i=t.marks?.some(s=>s.type==="subscript"),c=t.marks?.some(s=>s.type==="superscript"),p=t.marks?.find(s=>s.type==="link"),l=t.marks?.find(s=>s.type==="textStyle"),u=t.marks?.some(s=>s.type==="highlight"),m=N(l?.attrs?.color),h=N(l?.attrs?.backgroundColor);let F;if(l?.attrs?.fontSize){const s=l.attrs.fontSize;if(s.endsWith("px")){const A=parseFloat(s);isNaN(A)||(F=Math.round(A*1.5))}}let y;a?y="Consolas":l?.attrs?.fontFamily&&(y=l.attrs.fontFamily);const w={text:t.text||"",bold:e||void 0,italics:n||void 0,underline:r?{}:void 0,strike:o||void 0,font:y,size:F,subScript:i||void 0,superScript:c||void 0,color:m,shading:h?{fill:h}:void 0,highlight:u?"yellow":void 0};return p?.attrs?.href?new Mt({children:[new v({...w,style:"Hyperlink"})],link:p.attrs.href}):new v(w)}function k(t){const e={text:"",break:1};if(t)for(const n of t)switch(n.type){case"bold":e.bold=!0;break;case"italic":e.italics=!0;break;case"underline":e.underline={};break;case"strike":e.strike=!0;break;case"textStyle":n.attrs?.color&&(e.color=n.attrs.color);break}return new v(e)}function z(t=[]){return t.flatMap(e=>e.type==="text"?[x(e)]:e.type==="hardBreak"?[k(e.marks)]:[])}async function W(t,e){const n=l=>{const u=l||R(t.attrs?.src||"");return st(u)};let r,o;try{const l=t.attrs?.src||"";if(l.startsWith("http")){const u=await ut(l);r=u.data,o=u.meta}else if(l.startsWith("data:")){const u=l.split(",")[1];if(!u)throw new Error("Invalid data URL: missing base64 data");const m=atob(u);r=Uint8Array.from(m,h=>h.charCodeAt(0));try{o=V(r)}catch{o={type:"png",width:void 0,height:void 0,orientation:void 0}}}else throw new Error(`Unsupported image source format: ${l.substring(0,20)}...`)}catch(l){return console.warn("Failed to process image:",l),new q({type:"png",data:new Uint8Array(0),transformation:{width:100,height:100},altText:{name:t.attrs?.alt||"Failed to load image"}})}const a=lt(t,o,e?.maxWidth),i=dt(t,a,o,e?.maxWidth),c={width:a,height:i};t.attrs?.rotation!==void 0&&(c.rotation=t.attrs.rotation);const p={type:n(o.type),data:r,transformation:c,altText:{name:t.attrs?.alt||"",description:void 0,title:t.attrs?.title||void 0},...t.attrs?.floating&&{floating:t.attrs.floating},...t.attrs?.outline&&{outline:t.attrs.outline}};return new q(p)}async function C(t,e){const{options:n,image:r}=e||{},o=[];for(const i of t.content||[])if(i.type==="text")o.push(x(i));else if(i.type==="hardBreak")o.push(k(i.marks));else if(i.type==="image"){const c=await W(i,{maxWidth:r?.maxWidth});o.push(c)}let a={children:o};return n&&(a={...a,...n}),t.attrs&&(a=$(a,t.attrs)),new d(a)}function pt(t){const e=t?.attrs?.level,n=z(t.content).filter(a=>a!==void 0),r={1:b.HEADING_1,2:b.HEADING_2,3:b.HEADING_3,4:b.HEADING_4,5:b.HEADING_5,6:b.HEADING_6};let o={children:n,heading:r[e]};return t.attrs&&(o=$(o,t.attrs)),new d(o)}function ht(t){return t.content?t.content.map(e=>{if(e.type==="paragraph"){const n=e.content?.flatMap(r=>r.type==="text"?x(r):r.type==="hardBreak"?k(r.marks):[])||[];return new d({children:n,indent:{left:720},border:{left:{style:"single"}}})}return new d({})}):[]}function M(t){if(!t)return;const e={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},n=t.style&&e[t.style]||"single",r=t.color?.replace("#","")||"auto",o=t.width?t.width*6:4;return{color:r,size:o,style:n}}async function ft(t,e){const{options:n}=e;let r=n?.cell?.paragraph??n?.row?.paragraph??{};n?.style&&(r={...r,style:n.style.id});const o={children:await Promise.all((t.content||[]).map(i=>C(i,{options:r}))),...n?.cell?.run};if(t.attrs?.colspan&&t.attrs.colspan>1&&(o.columnSpan=t.attrs.colspan),t.attrs?.rowspan&&t.attrs.rowspan>1&&(o.rowSpan=t.attrs.rowspan),t.attrs?.colwidth!==null&&t.attrs?.colwidth!==void 0){const i=Array.isArray(t.attrs.colwidth)?t.attrs.colwidth[0]:t.attrs.colwidth;if(i&&i>0){const c=Math.round(i*15);o.width={size:c,type:"dxa"}}}if(t.attrs?.backgroundColor){const i=t.attrs.backgroundColor.replace("#","");o.shading={fill:i}}if(t.attrs?.verticalAlign){const i=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;o.verticalAlign=i}const a={top:M(t.attrs?.borderTop),bottom:M(t.attrs?.borderBottom),left:M(t.attrs?.borderLeft),right:M(t.attrs?.borderRight)};return(a.top||a.bottom||a.left||a.right)&&(o.borders=a),new X(o)}function S(t){if(!t)return;const e={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},n=t.style&&e[t.style]||"single",r=t.color?.replace("#","")||"auto",o=t.width?t.width*6:4;return{color:r,size:o,style:n}}async function gt(t,e){const{options:n}=e;let r=n?.header?.paragraph??n?.cell?.paragraph??n?.row?.paragraph??{};n?.style&&(r={...r,style:n.style.id});const o={children:await Promise.all((t.content||[]).map(i=>C(i,{options:r}))),...n?.header?.run};if(t.attrs?.colspan&&t.attrs.colspan>1&&(o.columnSpan=t.attrs.colspan),t.attrs?.rowspan&&t.attrs.rowspan>1&&(o.rowSpan=t.attrs.rowspan),t.attrs?.colwidth!==null&&t.attrs?.colwidth!==void 0){const i=Array.isArray(t.attrs.colwidth)?t.attrs.colwidth[0]:t.attrs.colwidth;if(i&&i>0){const c=Math.round(i*15);o.width={size:c,type:"dxa"}}}if(t.attrs?.backgroundColor){const i=t.attrs.backgroundColor.replace("#","");o.shading={fill:i}}if(t.attrs?.verticalAlign){const i=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;o.verticalAlign=i}const a={top:S(t.attrs?.borderTop),bottom:S(t.attrs?.borderBottom),left:S(t.attrs?.borderLeft),right:S(t.attrs?.borderRight)};return(a.top||a.bottom||a.left||a.right)&&(o.borders=a),new X(o)}async function mt(t,e){const{options:n}=e,r=n?.row,o={children:(await Promise.all((t.content||[]).map(async a=>a.type==="tableCell"?await ft(a,e):a.type==="tableHeader"?await gt(a,e):null))).filter(a=>a!==void 0),...r};if(t.attrs?.rowHeight){const a=g(t.attrs.rowHeight),i=f(a);i>0&&(o.height={rule:"atLeast",value:i})}return new St(o)}const Vt=(t,e)=>{const n={top:e.attrs?.marginTop??void 0,bottom:e.attrs?.marginBottom??void 0,left:e.attrs?.marginLeft??void 0,right:e.attrs?.marginRight??void 0};return n.top||n.bottom||n.left||n.right?{...t,margins:n}:t};async function yt(t,e){const{options:n}=e;let r={rows:await Promise.all((t.content||[]).map(o=>mt(o,e))),...n?.run};return r=Vt(r,t),new J(r)}function wt(t){const e=t.content?.map(n=>n.text||"").join("")||"";return new d({children:[new v({text:e,font:"Consolas"})]})}async function O(t,e){if(!t.content||t.content.length===0)return new d({});const n=t.content[0];return n.type==="paragraph"?await C(n,{options:e.options}):new d({})}function vt(){return{numbering:{reference:"bullet-list",level:0}}}function bt(t){const e=t.attrs?.start||1;return{numbering:{reference:"ordered-list",level:0},start:e}}async function j(t,e){const{listType:n}=e;if(!t.content)return[];const r=[],o=n==="bullet"?vt():bt(t);let a=o.numbering.reference;n==="ordered"&&o.start&&o.start!==1&&(a=`ordered-list-start-${o.start}`);for(const i of t.content)if(i.type==="listItem"){const c=await O(i,{options:{numbering:{reference:a,level:0}}});r.push(c)}return r}function _(t){if(!t.content||t.content.length===0)return new d({});const e=t.content[0];if(e.type==="paragraph"){const n=t.attrs?.checked?"\u2611 ":"\u2610 ",r=e.content?.flatMap(a=>a.type==="text"?x(a):a.type==="hardBreak"?k(a.marks):[])||[],o=new v({text:n});return new d({children:[o,...r]})}return new d({})}function kt(t){return!t.content||t.content.length===0?[]:t.content.filter(e=>e.type==="taskItem").map(e=>_(e))}function Ft(t,e){const n={children:[new Ht]};return new d({...n,...e.options?.paragraph})}function At(t,e){const n=z(t.content||[]).filter(r=>r!==void 0);return new d({children:n,...e.options?.summary?.paragraph})}async function Yt(t,e){const{title:n,subject:r,creator:o,keywords:a,description:i,lastModifiedBy:c,revision:p,styles:l,tableOfContents:u,sections:m,fonts:h,hyphenation:F,compatibility:y,customProperties:w,evenAndOddHeaderAndFooters:s,defaultTabStop:A,outputType:Ct}=e,H=await Tt(t,{options:e}),B=u?new Pt(u.title,{...u.run}):null,Bt=te(t),L=[];e.image?.style&&L.push(e.image.style),e.table?.style&&L.push(e.table.style);const Lt=l?{...l,...L.length>0&&{paragraphStyles:[...l.paragraphStyles||[],...L]}}:{},Dt={sections:m?m.map((It,U)=>{const D=[];return U===0&&B&&D.push(B),U===0&&D.push(...H),{...It,...D.length>0?{children:D}:{}}}):[{children:B?[B,...H]:H}],title:n||"Document",subject:r||"",creator:o||"",keywords:a||"",description:i||"",lastModifiedBy:c||"",revision:p||1,styles:Lt,numbering:Bt,...h&&h.length>0&&{fonts:h},...F&&{hyphenation:F},...y&&{compatibility:y},...w&&w.length>0&&{customProperties:w},...s!==void 0&&{evenAndOddHeaderAndFooters:s},...A!==void 0&&{defaultTabStop:A}},Et=new Nt(Dt);return Rt.pack(Et,Ct||"arraybuffer")}async function Tt(t,e){const n=[];if(!t||!Array.isArray(t.content))return n;const r=ot(e.options);for(const o of t.content){const a=await G(o,e.options,r);Array.isArray(a)?n.push(...a):a&&(n.push(a),o.type==="table"&&n.length>=2&&n[n.length-2]instanceof J&&n.push(new d({})))}return n}async function G(t,e,n){if(!t||!t.type)return null;switch(t.type){case"paragraph":return await C(t,{image:{maxWidth:n}});case"heading":return pt(t);case"blockquote":return ht(t);case"codeBlock":return wt(t);case"image":const r=await W(t,{maxWidth:n}),o=e.image?.style?{children:[r],style:e.image.style.id}:{children:[r]};return new d(o);case"table":return await yt(t,{options:e.table});case"bulletList":return await j(t,{listType:"bullet"});case"orderedList":return await j(t,{listType:"ordered"});case"taskList":return kt(t);case"listItem":return O(t,{options:void 0});case"taskItem":return _(t);case"hardBreak":return new d({children:[k()]});case"horizontalRule":return Ft(t,{options:e.horizontalRule});case"details":const a=[];if(t.content)for(const i of t.content){const c=await G(i,e,n);Array.isArray(c)?a.push(...c):c&&a.push(c)}return a;case"detailsSummary":return At(t,{options:e.details});default:return new d({children:[new v({text:`[Unsupported: ${t.type}]`})]})}}const Zt=t=>({level:0,format:Q.DECIMAL,text:"%1.",alignment:K.START,start:t??1,style:{paragraph:{indent:{left:E(.5),hanging:E(.25)}}}}),xt=t=>({reference:t&&t!==1?`ordered-list-start-${t}`:"ordered-list",levels:[Zt(t)]});function te(t){const e=new Set;function n(o){if(o.type==="orderedList"&&o.attrs?.start&&e.add(o.attrs.start),o.content)for(const a of o.content)n(a)}n(t);const r=[{reference:"bullet-list",levels:[{level:0,format:Q.BULLET,text:"\u2022",alignment:K.START,style:{paragraph:{indent:{left:E(.5),hanging:E(.25)}}}}]},xt(1)];return e.forEach(o=>{o!==1&&r.push(xt(o))}),{config:r}}export{tt as COLOR_NAME_TO_HEX,Y as DOCX_DPI,$ as applyParagraphStyleAttributes,ot as calculateEffectiveContentWidth,ht as convertBlockquote,vt as convertBulletList,wt as convertCodeBlock,N as convertColorToHex,g as convertCssLengthToPixels,At as convertDetailsSummary,Tt as convertDocument,Ot as convertEmuStringToPixels,Z as convertEmuToPixels,k as convertHardBreak,pt as convertHeading,Ft as convertHorizontalRule,W as convertImage,j as convertList,O as convertListItem,I as convertMeasureToInches,P as convertMeasureToPixels,G as convertNode,bt as convertOrderedList,C as convertParagraph,Wt as convertPixelsToEmu,f as convertPixelsToTwip,yt as convertTable,ft as convertTableCell,gt as convertTableHeader,mt as convertTableRow,_ as convertTaskItem,kt as convertTaskList,x as convertText,z as convertTextNodes,st as convertToDocxImageType,zt as convertTwipToCssString,T as convertTwipToPixels,Qt as createFloatingOptions,Xt as findChild,et as findDeepChild,nt as findDeepChildren,Yt as generateDOCX,ut as getImageDataAndMeta,dt as getImageHeight,R as getImageTypeFromSrc,lt as getImageWidth,qt as normalizeHexColor,Jt as parseTwipAttr};
1
+ import{TextRun as f,ExternalHyperlink as $t,ImageRun as Z,HeadingLevel as b,Paragraph as I,TableCell as tt,TableRow as zt,Table as B,PageBreak as jt,TableOfContents as Gt,Document as Ut,Packer as Xt,AlignmentType as et,LevelFormat as nt,convertInchesToTwip as _}from"docx";import{imageMeta as O}from"image-meta";import{ofetch as qt}from"ofetch";const W=96,$=1440,Kt=914400,rt=1.5,Yt=.6666666666666666,P="Consolas",z={checked:"\u2611",unchecked:"\u2610"},Vt={CODE_BLOCK:"CodeBlock",CODE_PREFIX:"Code"},ot={tiptapToDocx:{left:"left",right:"right",center:"center",justify:"both"},docxToTipTap:{left:"left",right:"right",center:"center",both:"justify"}},j={A4_WIDTH_TWIP:11906,DEFAULT_MARGIN_TWIP:1440},M=96;function E(t){return Math.round(t*M/1440)}function Jt(t){return`${E(t)}px`}function g(t){return Math.round(t*(1440/M))}function at(t){return Math.round(t/(914400/M))}function Qt(t){return Math.round(t*(914400/M))}function Zt(t){const e=parseInt(t,10);if(!isNaN(e))return at(e)}const te=/^(-?[\d.]+)(px|pt|em|rem|%|)?$/,ee={px:1,pt:1.333,em:16,rem:16,"%":.16};function m(t){if(!t)return 0;t=t.trim();const e=t.match(te);if(!e)return 0;const n=parseFloat(e[1]);if(isNaN(n))return 0;const o=e[2]||"px",r=ee[o]??1;return Math.round(n*r)}const ne=/^([\d.]+)(in|mm|cm|pt|pc|pi)$/,re={in:1,mm:1/25.4,cm:1/2.54,pt:1/72,pc:1/6,pi:1/6};function S(t){if(typeof t=="number")return t;const e=t.match(ne);if(e){const o=parseFloat(e[1]),r=e[2],a=re[r];return a!==void 0?o*a:o}const n=parseFloat(t);return isNaN(n)?6.5:n}function G(t){if(typeof t=="number")return t;const e=S(t);return Math.round(e*96)}const it={black:"#000000",white:"#FFFFFF",red:"#FF0000",green:"#008000",blue:"#0000FF",yellow:"#FFFF00",orange:"#FFA500",purple:"#800080",pink:"#FFC0CB",brown:"#A52A2A",gray:"#808080",grey:"#808080",cyan:"#00FFFF",magenta:"#FF00FF",lime:"#00FF00",navy:"#000080",teal:"#008080",maroon:"#800000",olive:"#808000",silver:"#C0C0C0",gold:"#FFD700",indigo:"#4B0082",violet:"#EE82EE",aqua:"#00FFFF",fuchsia:"#FF00FF",darkblue:"#00008B",darkcyan:"#008B8B",darkgrey:"#A9A9A9",darkgreen:"#006400",darkkhaki:"#BDB76B",darkmagenta:"#8B008B",darkolivegreen:"#556B2F",darkorange:"#FF8C00",darkorchid:"#9932CC",darkred:"#8B0000",darksalmon:"#E9967A",darkviolet:"#9400D3",lightblue:"#ADD8E6",lightcyan:"#E0FFFF",lightgreen:"#90EE90",lightgrey:"#D3D3D3",lightpink:"#FFB6C1",lightyellow:"#FFFFE0"};function U(t){if(t)return t.startsWith("#")?t:it[t.toLowerCase()]||t}function oe(t){return t.startsWith("#")?t:`#${t}`}function ae(t,e){if(!t.children)return null;for(const n of t.children)if(n.type==="element"&&n.name===e)return n;return null}function st(t,e){if(!t.children)return null;for(const n of t.children)if(n.type==="element"){if(n.name===e)return n;const o=st(n,e);if(o)return o}return null}function ct(t,e){const n=[];if(!t.children)return n;for(const o of t.children)o.type==="element"&&(o.name===e&&n.push(o),n.push(...ct(o,e)));return n}function ie(t,e){const n=t[e];if(!n)return;const o=parseInt(n,10);if(!isNaN(o))return n}function se(t){return e=>t.includes(e)}const lt=(t,e)=>t?typeof t=="number"?t:Math.round(S(t)*$):e;function dt(t){const e=j.A4_WIDTH_TWIP,n=j.DEFAULT_MARGIN_TWIP;if(!t?.sections?.length)return E(e-n*2);const o=t.sections[0];if(!o.properties?.page)return E(e-n*2);const r=o.properties.page;let a=e;if(r.size?.width){const d=r.size.width;a=typeof d=="number"?d:Math.round(S(d)*$)}const i=r.margin,l=lt(i?.left,n),u=lt(i?.right,n),c=a-l-u;return Math.max(E(c),W)}const ut=6.5*W,N={mimeToInternal:{jpg:"jpeg",jpeg:"jpeg",png:"png",gif:"gif",bmp:"bmp",tiff:"tiff"},internalToDocx:{jpeg:"jpg",png:"png",gif:"gif",bmp:"bmp",tiff:"bmp"}};function pt(t){if(!t)return"png";const e=t.toLowerCase(),n=N.mimeToInternal[e]||"png";return N.internalToDocx[n]||"png"}function H(t){if(t.startsWith("data:")){const e=t.match(/data:image\/(\w+);/);if(e){const n=e[1].toLowerCase();return N.mimeToInternal[n]||"png"}}else{const e=t.split(".").pop()?.toLowerCase();if(e)return N.mimeToInternal[e]||"png"}return"png"}const ht=(t,e=ut)=>{if(!t.width||!t.height)return{width:e,height:Math.round(e*.75)};if(t.width<=e)return{width:t.width,height:t.height};const n=e/t.width;return{width:e,height:Math.round(t.height*n)}};function ce(){return{horizontalPosition:{relative:"page",align:"center"},verticalPosition:{relative:"page",align:"top"},lockAnchor:!0,behindDocument:!1,inFrontOfText:!1}}function ft(t,e,n){if(t.attrs?.width!==void 0&&t.attrs?.width!==null)return t.attrs.width;const o=n!==void 0?G(n):void 0;return e?.width&&e?.height?ht(e,o).width:o||ut}function gt(t,e,n,o){if(t.attrs?.height!==void 0&&t.attrs?.height!==null)return t.attrs.height;const r=o!==void 0?G(o):void 0;return n?.width&&n?.height?ht(n,r).height:Math.round(e*.75)}async function mt(t){try{const e=await(await qt(t,{responseType:"blob"})).bytes();let n;try{n=O(e)}catch(o){console.warn("Failed to extract image metadata:",o),n={width:void 0,height:void 0,type:H(t)||"png",orientation:void 0}}return{data:e,meta:n}}catch(e){throw console.warn(`Failed to fetch image from ${t}:`,e),e}}const X=(t,e)=>{if(!e)return t;let n={...t};return(e.indentLeft||e.indentRight||e.indentFirstLine)&&(n={...n,indent:{...e.indentLeft&&{left:g(m(e.indentLeft))},...e.indentRight&&{right:g(m(e.indentRight))},...e.indentFirstLine&&{firstLine:g(m(e.indentFirstLine))}}}),(e.spacingBefore||e.spacingAfter)&&(n={...n,spacing:{...e.spacingBefore&&{before:g(m(e.spacingBefore))},...e.spacingAfter&&{after:g(m(e.spacingAfter))}}}),e.textAlign&&(n={...n,alignment:ot.tiptapToDocx[e.textAlign]}),n};function p(t){if(!t)return;const e={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},n=t.style&&e[t.style]||"single",o=t.color?.replace("#","")||"auto",r=t.width?t.width*6:4;return{color:o,size:r,style:n}}function T(t){const e=t.marks?.some(s=>s.type==="bold"),n=t.marks?.some(s=>s.type==="italic"),o=t.marks?.some(s=>s.type==="underline"),r=t.marks?.some(s=>s.type==="strike"),a=t.marks?.some(s=>s.type==="code"),i=t.marks?.some(s=>s.type==="subscript"),l=t.marks?.some(s=>s.type==="superscript"),u=t.marks?.find(s=>s.type==="link"),c=t.marks?.find(s=>s.type==="textStyle"),d=t.marks?.some(s=>s.type==="highlight"),y=U(c?.attrs?.color),h=U(c?.attrs?.backgroundColor);let F;if(c?.attrs?.fontSize){const s=c.attrs.fontSize;if(s.endsWith("px")){const A=parseFloat(s);isNaN(A)||(F=Math.round(A*rt))}}let w;a?w=P:c?.attrs?.fontFamily&&(w=c.attrs.fontFamily);const v={text:t.text||"",bold:e||void 0,italics:n||void 0,underline:o?{}:void 0,strike:r||void 0,font:w,size:F,subScript:i||void 0,superScript:l||void 0,color:y,shading:h?{fill:h}:void 0,highlight:d?"yellow":void 0};return u?.attrs?.href?new $t({children:[new f({...v,style:"Hyperlink"})],link:u.attrs.href}):new f(v)}function k(t){const e={text:"",break:1};if(t)for(const n of t)switch(n.type){case"bold":e.bold=!0;break;case"italic":e.italics=!0;break;case"underline":e.underline={};break;case"strike":e.strike=!0;break;case"textStyle":n.attrs?.color&&(e.color=n.attrs.color);break}return new f(e)}function q(t=[]){return t.flatMap(e=>e.type==="text"?[T(e)]:e.type==="hardBreak"?[k(e.marks)]:[])}async function K(t,e){const n=c=>{const d=c||H(t.attrs?.src||"");return pt(d)};let o,r;try{const c=t.attrs?.src||"";if(e?.handler){o=await e.handler(c);try{r=O(o)}catch{r={type:H(c),width:void 0,height:void 0,orientation:void 0}}}else if(c.startsWith("http")){const d=await mt(c);o=d.data,r=d.meta}else if(c.startsWith("data:")){const d=c.split(",")[1];if(!d)throw new Error("Invalid data URL: missing base64 data");const y=atob(d);o=Uint8Array.from(y,h=>h.charCodeAt(0));try{r=O(o)}catch{r={type:"png",width:void 0,height:void 0,orientation:void 0}}}else throw new Error(`Unsupported image source format: ${c.substring(0,20)}...`)}catch(c){return console.warn("Failed to process image:",c),new Z({type:"png",data:new Uint8Array(0),transformation:{width:100,height:100},altText:{name:t.attrs?.alt||"Failed to load image"}})}const a=ft(t,r,e?.maxWidth),i=gt(t,a,r,e?.maxWidth),l={width:a,height:i};t.attrs?.rotation!==void 0&&(l.rotation=t.attrs.rotation);const u={...e?.options,type:n(r.type),data:o,transformation:l,altText:{name:t.attrs?.alt||"",description:void 0,title:t.attrs?.title||void 0},...t.attrs?.floating&&{floating:t.attrs.floating},...t.attrs?.outline&&{outline:t.attrs.outline}};return new Z(u)}async function C(t,e){const{options:n,image:o}=e||{},r=[];for(const i of t.content||[])if(i.type==="text")r.push(T(i));else if(i.type==="hardBreak")r.push(k(i.marks));else if(i.type==="image"){const l=await K(i,{maxWidth:o?.maxWidth,options:o?.options,handler:o?.handler});r.push(l)}let a={children:r};return n&&(a={...a,...n}),t.attrs&&(a=X(a,t.attrs)),a}function yt(t){const e=t?.attrs?.level,n=q(t.content).filter(a=>a!==void 0),o={1:b.HEADING_1,2:b.HEADING_2,3:b.HEADING_3,4:b.HEADING_4,5:b.HEADING_5,6:b.HEADING_6};let r={children:n,heading:o[e]};return t.attrs&&(r=X(r,t.attrs)),r}function wt(t){return t.content?t.content.map(e=>e.type==="paragraph"?{children:e.content?.flatMap(n=>n.type==="text"?T(n):n.type==="hardBreak"?k(n.marks):[])||[],indent:{left:720},border:{left:{style:"single"}}}:{}):[]}async function vt(t,e){const{options:n}=e;let o=n?.cell?.paragraph??n?.row?.paragraph??{};n?.style&&(o={...o,style:n.style.id});const r={children:(await Promise.all((t.content||[]).map(i=>C(i,{options:o})))).map(i=>new I(i)),...n?.cell?.run};if(t.attrs?.colspan&&t.attrs.colspan>1&&(r.columnSpan=t.attrs.colspan),t.attrs?.rowspan&&t.attrs.rowspan>1&&(r.rowSpan=t.attrs.rowspan),t.attrs?.colwidth!==null&&t.attrs?.colwidth!==void 0){const i=Array.isArray(t.attrs.colwidth)?t.attrs.colwidth[0]:t.attrs.colwidth;if(i&&i>0){const l=Math.round(i*15);r.width={size:l,type:"dxa"}}}if(t.attrs?.backgroundColor){const i=t.attrs.backgroundColor.replace("#","");r.shading={fill:i}}if(t.attrs?.verticalAlign){const i=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;r.verticalAlign=i}const a={top:p(t.attrs?.borderTop),bottom:p(t.attrs?.borderBottom),left:p(t.attrs?.borderLeft),right:p(t.attrs?.borderRight)};return(a.top||a.bottom||a.left||a.right)&&(r.borders=a),new tt(r)}async function bt(t,e){const{options:n}=e;let o=n?.header?.paragraph??n?.cell?.paragraph??n?.row?.paragraph??{};n?.style&&(o={...o,style:n.style.id});const r={children:(await Promise.all((t.content||[]).map(i=>C(i,{options:o})))).map(i=>new I(i)),...n?.header?.run};if(t.attrs?.colspan&&t.attrs.colspan>1&&(r.columnSpan=t.attrs.colspan),t.attrs?.rowspan&&t.attrs.rowspan>1&&(r.rowSpan=t.attrs.rowspan),t.attrs?.colwidth!==null&&t.attrs?.colwidth!==void 0){const i=Array.isArray(t.attrs.colwidth)?t.attrs.colwidth[0]:t.attrs.colwidth;if(i&&i>0){const l=Math.round(i*15);r.width={size:l,type:"dxa"}}}if(t.attrs?.backgroundColor){const i=t.attrs.backgroundColor.replace("#","");r.shading={fill:i}}if(t.attrs?.verticalAlign){const i=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;r.verticalAlign=i}const a={top:p(t.attrs?.borderTop),bottom:p(t.attrs?.borderBottom),left:p(t.attrs?.borderLeft),right:p(t.attrs?.borderRight)};return(a.top||a.bottom||a.left||a.right)&&(r.borders=a),new tt(r)}async function Tt(t,e){const{options:n}=e,o=n?.row,r={children:(await Promise.all((t.content||[]).map(async a=>a.type==="tableCell"?await vt(a,e):a.type==="tableHeader"?await bt(a,e):null))).filter(a=>a!==void 0),...o};if(t.attrs?.rowHeight){const a=m(t.attrs.rowHeight),i=g(a);i>0&&(r.height={rule:"atLeast",value:i})}return new zt(r)}const kt=(t,e)=>{const n={top:e.attrs?.marginTop??void 0,bottom:e.attrs?.marginBottom??void 0,left:e.attrs?.marginLeft??void 0,right:e.attrs?.marginRight??void 0};return n.top||n.bottom||n.left||n.right?{...t,margins:n}:t};async function Ft(t,e){const{options:n}=e;let o={rows:await Promise.all((t.content||[]).map(r=>Tt(r,e))),...n?.run};return o=kt(o,t),new B(o)}function At(t){if(!t.content||t.content.length===0)return{children:[new f({text:"",font:P})]};const e=t.content.flatMap(n=>n.type==="text"?T(n):[]);return{children:e.length>0?e:[new f({text:"",font:P})]}}async function xt(t,e){if(!t.content||t.content.length===0)return{};const n=t.content[0];return n.type==="paragraph"?await C(n,{options:e.options}):{}}function It(){return{numbering:{reference:"bullet-list",level:0}}}function Et(t){const e=t.attrs?.start||1;return{numbering:{reference:"ordered-list",level:0},start:e}}async function Y(t,e){const{listType:n}=e;if(!t.content)return[];const o=[],r=n==="bullet"?It():Et(t);let a=r.numbering.reference;n==="ordered"&&r.start&&r.start!==1&&(a=`ordered-list-start-${r.start}`);for(const i of t.content)if(i.type==="listItem"){const l=await xt(i,{options:{numbering:{reference:a,level:0}}});o.push(l)}return o}function V(t){if(!t.content||t.content.length===0)return{};const e=t.content[0];if(e.type==="paragraph"){const n=t.attrs?.checked?z.checked+" ":z.unchecked+" ",o=e.content?.flatMap(r=>r.type==="text"?T(r):r.type==="hardBreak"?k(r.marks):[])||[];return{children:[new f({text:n}),...o]}}return{}}function Ct(t){return!t.content||t.content.length===0?[]:t.content.filter(e=>e.type==="taskItem").map(e=>V(e))}function Lt(t,e){return{children:[new jt],...e.options?.paragraph}}function Dt(t,e){return{children:q(t.content||[]).filter(n=>n!==void 0),...e.options?.summary?.paragraph}}async function le(t,e){const{title:n,subject:o,creator:r,keywords:a,description:i,lastModifiedBy:l,revision:u,styles:c,tableOfContents:d,sections:y,fonts:h,hyphenation:F,compatibility:w,customProperties:v,evenAndOddHeaderAndFooters:s,defaultTabStop:A,outputType:St}=e,R=await Bt(t,{options:e}),L=d?new Gt(d.title,{...d.run}):null,Nt=he(t),x=[];e.image?.style&&x.push(e.image.style),e.table?.style&&x.push(e.table.style),e.code?.style&&x.push(e.code.style);const Ht=c?{...c,...x.length>0&&{paragraphStyles:[...c.paragraphStyles||[],...x]}}:{},Rt={sections:y?y.map((Wt,Q)=>{const D=[];return Q===0&&L&&D.push(L),Q===0&&D.push(...R),{...Wt,...D.length>0?{children:D}:{}}}):[{children:L?[L,...R]:R}],title:n||"Document",subject:o||"",creator:r||"",keywords:a||"",description:i||"",lastModifiedBy:l||"",revision:u||1,styles:Ht,numbering:Nt,...h&&h.length>0&&{fonts:h},...F&&{hyphenation:F},...w&&{compatibility:w},...v&&v.length>0&&{customProperties:v},...s!==void 0&&{evenAndOddHeaderAndFooters:s},...A!==void 0&&{defaultTabStop:A}},Ot=new Ut(Rt);return Xt.pack(Ot,St||"arraybuffer")}async function Bt(t,e){const n=[];if(!t||!Array.isArray(t.content))return n;const o=dt(e.options);for(const r of t.content){const a=await J(r,e.options,o);Array.isArray(a)?n.push(...a):a&&(n.push(a),r.type==="table"&&n.length>=2&&n[n.length-2]instanceof B&&n.push(new I({})))}return n}async function J(t,e,n){if(!t||!t.type)return null;const o=await de(t,e,n);if(o instanceof B)return o;if(Array.isArray(o)){const i=Pt(t.type,e);return o.map(l=>{const u=Mt(l,i);return new I(u)})}const r=Pt(t.type,e),a=Mt(o,r);return fe(a)}async function de(t,e,n){switch(t.type){case"paragraph":return await C(t,{image:{maxWidth:n,options:e.image?.run,handler:e.image?.handler}});case"heading":return yt(t);case"blockquote":return wt(t);case"codeBlock":return At(t);case"image":return{children:[await K(t,{maxWidth:n,options:e.image?.run,handler:e.image?.handler})]};case"table":return await Ft(t,{options:e.table});case"bulletList":return await Y(t,{listType:"bullet"});case"orderedList":return await Y(t,{listType:"ordered"});case"taskList":return Ct(t);case"taskItem":return V(t);case"hardBreak":return{children:[k()]};case"horizontalRule":return Lt(t,{options:e.horizontalRule});case"details":return await ue(t,e,n);case"detailsSummary":return Dt(t,{options:e.details});default:return{children:[new f({text:`[Unsupported: ${t.type}]`})]}}}async function ue(t,e,n){const o=[];if(t.content)for(const r of t.content){const a=await J(r,e,n);Array.isArray(a)?o.push(...a):a&&o.push(a)}return o}const pe=t=>({level:0,format:nt.DECIMAL,text:"%1.",alignment:et.START,start:t??1,style:{paragraph:{indent:{left:_(.5),hanging:_(.25)}}}}),_t=t=>({reference:t&&t!==1?`ordered-list-start-${t}`:"ordered-list",levels:[pe(t)]});function he(t){const e=new Set;function n(r){if(r.type==="orderedList"&&r.attrs?.start&&e.add(r.attrs.start),r.content)for(const a of r.content)n(a)}n(t);const o=[{reference:"bullet-list",levels:[{level:0,format:nt.BULLET,text:"\u2022",alignment:et.START,style:{paragraph:{indent:{left:_(.5),hanging:_(.25)}}}}]},_t(1)];return e.forEach(r=>{r!==1&&o.push(_t(r))}),{config:o}}function Pt(t,e){return{codeBlock:e.code?.style?.id,image:e.image?.style?.id}[t]}function Mt(t,e){return e?{...t,style:e}:t}function fe(t){return t instanceof B?t:new I(t)}export{z as CHECKBOX_SYMBOLS,it as COLOR_NAME_TO_HEX,P as DEFAULT_CODE_FONT,W as DOCX_DPI,Vt as DOCX_STYLE_NAMES,Kt as EMUS_PER_INCH,rt as HALF_POINTS_PER_PIXEL,j as PAGE_DIMENSIONS,Yt as PIXELS_PER_HALF_POINT,ot as TEXT_ALIGN_MAP,$ as TWIPS_PER_INCH,X as applyParagraphStyleAttributes,kt as applyTableMargins,dt as calculateEffectiveContentWidth,wt as convertBlockquote,p as convertBorder,It as convertBulletList,At as convertCodeBlock,U as convertColorToHex,m as convertCssLengthToPixels,Dt as convertDetailsSummary,Bt as convertDocument,Zt as convertEmuStringToPixels,at as convertEmuToPixels,k as convertHardBreak,yt as convertHeading,Lt as convertHorizontalRule,K as convertImage,Y as convertList,xt as convertListItem,S as convertMeasureToInches,G as convertMeasureToPixels,J as convertNode,Et as convertOrderedList,C as convertParagraph,Qt as convertPixelsToEmu,g as convertPixelsToTwip,Ft as convertTable,vt as convertTableCell,bt as convertTableHeader,Tt as convertTableRow,V as convertTaskItem,Ct as convertTaskList,T as convertText,q as convertTextNodes,pt as convertToDocxImageType,Jt as convertTwipToCssString,E as convertTwipToPixels,ce as createFloatingOptions,se as createStringValidator,ae as findChild,st as findDeepChild,ct as findDeepChildren,le as generateDOCX,mt as getImageDataAndMeta,gt as getImageHeight,H as getImageTypeFromSrc,ft as getImageWidth,oe as normalizeHexColor,ie as parseTwipAttr};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docen/export-docx",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "A powerful TipTap/ProseMirror extension that converts editor content to Microsoft Word DOCX format",
5
5
  "keywords": [
6
6
  "converter",
@@ -54,9 +54,9 @@
54
54
  "ofetch": "1.5.1"
55
55
  },
56
56
  "devDependencies": {
57
- "@tiptap/core": "3.15.3",
58
- "@docen/extensions": "0.0.9",
59
- "@docen/utils": "0.0.9"
57
+ "@tiptap/core": "3.19.0",
58
+ "@docen/extensions": "0.0.10",
59
+ "@docen/utils": "0.0.10"
60
60
  },
61
61
  "scripts": {
62
62
  "dev": "unbuild --stub",