@docen/export-docx 0.0.8 → 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.cjs +1 -1
- package/dist/index.d.cts +1084 -368
- package/dist/index.d.mts +1084 -368
- package/dist/index.d.ts +1084 -368
- package/dist/index.mjs +1 -1
- package/package.json +4 -3
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ExternalHyperlink as At,TextRun as b,ImageRun as J,Paragraph as p,HeadingLevel as v,TableCell as K,TableRow as Tt,Table as Q,PageBreak as Ft,TableOfContents as St,Document as Ct,Packer as Lt,convertInchesToTwip as k,AlignmentType as D,LevelFormat as j}from"docx";import{imageMeta as V}from"image-meta";import{ofetch as It}from"ofetch";const Mt=96,Bt=/^([\d.]+)(px|pt|em|rem|%|)?$/,Wt=/^([\d.]+)(in|mm|cm|pt|pc|pi)$/,Ht={px:1,pt:1.333,em:16,rem:16,"%":.16},Pt={in:1,mm:1/25.4,cm:1/2.54,pt:1/72,pc:1/6,pi:1/6},B=t=>Math.round(t*96/1440),g=t=>{if(!t)return 0;t=t.trim();const e=t.match(Bt);if(!e)return 0;const r=parseFloat(e[1]);if(isNaN(r))return 0;const a=e[2]||"px",n=Ht[a]??1;return Math.round(r*n)},f=t=>Math.round(t*15),F=t=>{if(typeof t=="number")return t;const e=t.match(Wt);if(e){const a=parseFloat(e[1]),n=e[2],o=Pt[n];return o!==void 0?a*o:a}const r=parseFloat(t);return isNaN(r)?6.5:r},E=t=>{if(typeof t=="number")return t;const e=F(t);return Math.round(e*96)},R=t=>{if(!t?.sections?.length)return B(11906-1440*2);const e=t.sections[0];if(!e.properties?.page)return B(11906-1440*2);const r=e.properties.page;let a=11906;if(r.size?.width){const u=r.size.width;a=typeof u=="number"?u:Math.round(F(u)*1440)}const n=r.margin,o=n?.left?typeof n.left=="number"?n.left:Math.round(F(n.left)*1440):1440,i=n?.right?typeof n.right=="number"?n.right:Math.round(F(n.right)*1440):1440,c=a-o-i;return Math.max(B(c),96)},Dt={red:"#FF0000",green:"#008000",blue:"#0000FF",yellow:"#FFFF00",orange:"#FFA500",purple:"#800080",pink:"#FFC0CB",brown:"#A52A2A",black:"#000000",white:"#FFFFFF",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"},N=t=>{if(t)return t.startsWith("#")?t:Dt[t.toLowerCase()]||t},Y=6.5*96,jt={jpg:"jpeg",jpeg:"jpeg",png:"png",gif:"gif",bmp:"bmp",tiff:"tiff"},Et={jpg:"jpeg",jpeg:"jpeg",png:"png",gif:"gif",bmp:"bmp",tiff:"tiff"},z=t=>{if(t.startsWith("data:")){const e=t.match(/data:image\/(\w+);/);if(e){const r=e[1].toLowerCase();return jt[r]||"png"}}else{const e=t.split(".").pop()?.toLowerCase();if(e)return Et[e]||"png"}return"png"},tt=(t,e=Y)=>{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 r=e/t.width;return{width:e,height:Math.round(t.height*r)}},Rt=()=>({horizontalPosition:{relative:"page",align:"center"},verticalPosition:{relative:"page",align:"top"},lockAnchor:!0,behindDocument:!1,inFrontOfText:!1}),et=(t,e,r)=>{if(t.attrs?.width!==void 0&&t.attrs?.width!==null)return t.attrs.width;const a=r!==void 0?E(r):void 0;return e?.width&&e?.height?tt(e,a).width:a||Y},rt=(t,e,r,a)=>{if(t.attrs?.height!==void 0&&t.attrs?.height!==null)return t.attrs.height;const n=a!==void 0?E(a):void 0;return r?.width&&r?.height?tt(r,n).height:Math.round(e*.75)},nt=async t=>{try{const e=await(await It(t,{responseType:"blob"})).bytes();let r;try{r=V(e)}catch(a){console.warn("Failed to extract image metadata:",a),r={width:void 0,height:void 0,type:z(t)||"png",orientation:void 0}}return{data:e,meta:r}}catch(e){throw console.warn(`Failed to fetch image from ${t}:`,e),e}},O=(t,e)=>{if(!e)return t;let r={...t};return(e.indentLeft||e.indentRight||e.indentFirstLine)&&(r={...r,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)&&(r={...r,spacing:{...e.spacingBefore&&{before:f(g(e.spacingBefore))},...e.spacingAfter&&{after:f(g(e.spacingAfter))}}}),e.textAlign&&(r={...r,alignment:{left:"left",right:"right",center:"center",justify:"both"}[e.textAlign]}),r};function S(t){const e=t.marks?.some(s=>s.type==="bold"),r=t.marks?.some(s=>s.type==="italic"),a=t.marks?.some(s=>s.type==="underline"),n=t.marks?.some(s=>s.type==="strike"),o=t.marks?.some(s=>s.type==="code"),i=t.marks?.some(s=>s.type==="subscript"),c=t.marks?.some(s=>s.type==="superscript"),u=t.marks?.find(s=>s.type==="link"),l=t.marks?.find(s=>s.type==="textStyle"),d=t.marks?.some(s=>s.type==="highlight"),m=N(l?.attrs?.color),h=N(l?.attrs?.backgroundColor);let A;if(l?.attrs?.fontSize){const s=l.attrs.fontSize;if(s.endsWith("px")){const T=parseFloat(s);isNaN(T)||(A=Math.round(T*1.5))}}let y;o?y="Consolas":l?.attrs?.fontFamily&&(y=l.attrs.fontFamily);const w={text:t.text||"",bold:e||void 0,italics:r||void 0,underline:a?{}:void 0,strike:n||void 0,font:y,size:A,subScript:i||void 0,superScript:c||void 0,color:m,shading:h?{fill:h}:void 0,highlight:d?"yellow":void 0};return u?.attrs?.href?new At({children:[new b({...w,style:"Hyperlink"})],link:u.attrs.href}):new b(w)}function x(t){const e={text:"",break:1};if(t)for(const r of t)switch(r.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":r.attrs?.color&&(e.color=r.attrs.color);break}return new b(e)}const $=(t=[])=>t.flatMap(e=>e.type==="text"?[S(e)]:e.type==="hardBreak"?[x(e.marks)]:[]);async function _(t,e){const r=l=>{switch(l){case"jpeg":case"jpg":return"jpg";case"png":return"png";case"gif":return"gif";case"bmp":return"bmp"}switch(z(t.attrs?.src||"")){case"jpeg":return"jpg";case"png":return"png";case"gif":return"gif";case"bmp":return"bmp";default:return"png"}};let a,n;try{const l=t.attrs?.src||"";if(l.startsWith("http")){const d=await nt(l);a=d.data,n=d.meta}else if(l.startsWith("data:")){const d=l.split(",")[1];if(!d)throw new Error("Invalid data URL: missing base64 data");const m=atob(d);a=Uint8Array.from(m,h=>h.charCodeAt(0));try{n=V(a)}catch{n={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 J({type:"png",data:new Uint8Array(0),transformation:{width:100,height:100},altText:{name:t.attrs?.alt||"Failed to load image"}})}const o=et(t,n,e?.maxWidth),i=rt(t,o,n,e?.maxWidth),c={width:o,height:i};t.attrs?.rotation!==void 0&&(c.rotation=t.attrs.rotation);const u={type:r(n.type),data:a,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 J(u)}async function C(t,e){const{options:r,image:a}=e||{},n=[];for(const i of t.content||[])if(i.type==="text")n.push(S(i));else if(i.type==="hardBreak")n.push(x(i.marks));else if(i.type==="image"){const c=await _(i,{maxWidth:a?.maxWidth});n.push(c)}let o={children:n};return r&&(o={...o,...r}),t.attrs&&(o=O(o,t.attrs)),new p(o)}function ot(t){const e=t?.attrs?.level,r=$(t.content).filter(o=>o!==void 0),a={1:v.HEADING_1,2:v.HEADING_2,3:v.HEADING_3,4:v.HEADING_4,5:v.HEADING_5,6:v.HEADING_6};let n={children:r,heading:a[e]};return t.attrs&&(n=O(n,t.attrs)),new p(n)}function at(t){return t.content?t.content.map(e=>{if(e.type==="paragraph"){const r=e.content?.flatMap(a=>a.type==="text"?S(a):a.type==="hardBreak"?x(a.marks):[])||[];return new p({children:r,indent:{left:720},border:{left:{style:"single"}}})}return new p({})}):[]}function W(t){if(!t)return;const e={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},r=t.style&&e[t.style]||"single",a=t.color?.replace("#","")||"auto",n=t.width?t.width*6:4;return{color:a,size:n,style:r}}async function it(t,e){const{options:r}=e;let a=r?.cell?.paragraph??r?.row?.paragraph??{};r?.style&&(a={...a,style:r.style.id});const n={children:await Promise.all((t.content||[]).map(i=>C(i,{options:a}))),...r?.cell?.run};if(t.attrs?.colSpan&&t.attrs.colSpan>1&&(n.columnSpan=t.attrs.colSpan),t.attrs?.rowSpan&&t.attrs.rowSpan>1&&(n.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);n.width={size:c,type:"dxa"}}}if(t.attrs?.backgroundColor){const i=t.attrs.backgroundColor.replace("#","");n.shading={fill:i}}if(t.attrs?.verticalAlign){const i=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;n.verticalAlign=i}const o={top:W(t.attrs?.borderTop),bottom:W(t.attrs?.borderBottom),left:W(t.attrs?.borderLeft),right:W(t.attrs?.borderRight)};return(o.top||o.bottom||o.left||o.right)&&(n.borders=o),new K(n)}function H(t){if(!t)return;const e={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},r=t.style&&e[t.style]||"single",a=t.color?.replace("#","")||"auto",n=t.width?t.width*6:4;return{color:a,size:n,style:r}}async function st(t,e){const{options:r}=e;let a=r?.header?.paragraph??r?.cell?.paragraph??r?.row?.paragraph??{};r?.style&&(a={...a,style:r.style.id});const n={children:await Promise.all((t.content||[]).map(i=>C(i,{options:a}))),...r?.header?.run};if(t.attrs?.colSpan&&t.attrs.colSpan>1&&(n.columnSpan=t.attrs.colSpan),t.attrs?.rowSpan&&t.attrs.rowSpan>1&&(n.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);n.width={size:c,type:"dxa"}}}if(t.attrs?.backgroundColor){const i=t.attrs.backgroundColor.replace("#","");n.shading={fill:i}}if(t.attrs?.verticalAlign){const i=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;n.verticalAlign=i}const o={top:H(t.attrs?.borderTop),bottom:H(t.attrs?.borderBottom),left:H(t.attrs?.borderLeft),right:H(t.attrs?.borderRight)};return(o.top||o.bottom||o.left||o.right)&&(n.borders=o),new K(n)}async function ct(t,e){const{options:r}=e,a=r?.row,n={children:(await Promise.all((t.content||[]).map(async o=>o.type==="tableCell"?await it(o,e):o.type==="tableHeader"?await st(o,e):null))).filter(o=>o!==void 0),...a};if(t.attrs?.rowHeight){const o=g(t.attrs.rowHeight),i=f(o);i>0&&(n.height={rule:"atLeast",value:i})}return new Tt(n)}const Nt=(t,e)=>{const r={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 r.top||r.bottom||r.left||r.right?{...t,margins:r}:t};async function lt(t,e){const{options:r}=e;let a={rows:await Promise.all((t.content||[]).map(n=>ct(n,e))),...r?.run};return a=Nt(a,t),new Q(a)}function pt(t){const e=t.content?.map(r=>r.text||"").join("")||"";return new p({children:[new b({text:e,font:"Consolas"})]})}async function G(t,e){if(!t.content||t.content.length===0)return new p({});const r=t.content[0];return r.type==="paragraph"?await C(r,{options:e.options}):new p({})}function dt(){return{numbering:{reference:"bullet-list",level:0}}}function ut(t){const e=t.attrs?.start||1;return{numbering:{reference:"ordered-list",level:0},start:e}}async function U(t,e){const{listType:r}=e;if(!t.content)return[];const a=[],n=r==="bullet"?dt():ut(t);let o=n.numbering.reference;r==="ordered"&&n.start&&n.start!==1&&(o=`ordered-list-start-${n.start}`);for(const i of t.content)if(i.type==="listItem"){const c=await G(i,{options:{numbering:{reference:o,level:0}}});a.push(c)}return a}function q(t){if(!t.content||t.content.length===0)return new p({});const e=t.content[0];if(e.type==="paragraph"){const r=t.attrs?.checked?"\u2611 ":"\u2610 ",a=e.content?.flatMap(o=>o.type==="text"?S(o):o.type==="hardBreak"?x(o.marks):[])||[],n=new b({text:r});return new p({children:[n,...a]})}return new p({})}function ht(t){return!t.content||t.content.length===0?[]:t.content.filter(e=>e.type==="taskItem").map(e=>q(e))}function gt(t,e){const r={children:[new Ft]};return new p({...r,...e.options?.paragraph})}async function ft(t,e){if(!t.content)return[];const r=[];let a,n;for(const o of t.content)o.type==="detailsSummary"?a=o:o.type==="detailsContent"&&(n=o);if(a?.content){const o=$(a.content).filter(c=>c!==void 0),i=new p({children:o,...e.options?.summary?.paragraph});r.push(i)}if(n?.content){const o=R(e.exportOptions);for(const i of n.content){const c=await X(i,e.exportOptions,o);Array.isArray(c)?r.push(...c):c&&r.push(c)}}return r}async function zt(t,e){const{title:r,subject:a,creator:n,keywords:o,description:i,lastModifiedBy:c,revision:u,styles:l,tableOfContents:d,sections:m,fonts:h,hyphenation:A,compatibility:y,customProperties:w,evenAndOddHeaderAndFooters:s,defaultTabStop:T,outputType:yt}=e,P=await mt(t,e),L=d?new St(d.title,{...d.run}):null,wt=Ot(t),I=[];e.image?.style&&I.push(e.image.style),e.table?.style&&I.push(e.table.style);const bt=l?{...l,...I.length>0&&{paragraphStyles:[...l.paragraphStyles||[],...I]}}:{},vt={sections:m?m.map((xt,Z)=>{const M=[];return Z===0&&L&&M.push(L),Z===0&&M.push(...P),{...xt,...M.length>0?{children:M}:{}}}):[{children:L?[L,...P]:P}],title:r||"Document",subject:a||"",creator:n||"",keywords:o||"",description:i||"",lastModifiedBy:c||"",revision:u||1,styles:bt,numbering:wt,...h&&h.length>0&&{fonts:h},...A&&{hyphenation:A},...y&&{compatibility:y},...w&&w.length>0&&{customProperties:w},...s!==void 0&&{evenAndOddHeaderAndFooters:s},...T!==void 0&&{defaultTabStop:T}},kt=new Ct(vt);return Lt.pack(kt,yt||"arraybuffer")}async function mt(t,e){const r=[];if(!t||!Array.isArray(t.content))return r;const a=R(e);for(const n of t.content){const o=await X(n,e,a);Array.isArray(o)?r.push(...o):o&&(r.push(o),n.type==="table"&&r.length>=2&&r[r.length-2]instanceof Q&&r.push(new p({})))}return r}async function X(t,e,r){if(!t||!t.type)return null;switch(t.type){case"paragraph":return await C(t,{image:{maxWidth:r}});case"heading":return ot(t);case"blockquote":return at(t);case"codeBlock":return pt(t);case"image":const a=await _(t,{maxWidth:r}),n=e.image?.style?{children:[a],style:e.image.style.id}:{children:[a]};return new p(n);case"table":return await lt(t,{options:e.table});case"bulletList":return await U(t,{listType:"bullet"});case"orderedList":return await U(t,{listType:"ordered"});case"taskList":return ht(t);case"listItem":return G(t,{options:void 0});case"taskItem":return q(t);case"hardBreak":return new p({children:[x()]});case"horizontalRule":return gt(t,{options:e.horizontalRule});case"details":return await ft(t,{options:e.details,exportOptions:e});default:return new p({children:[new b({text:`[Unsupported: ${t.type}]`})]})}}function Ot(t){const e=new Set;function r(o){if(o.type==="orderedList"&&o.attrs?.start&&e.add(o.attrs.start),o.content)for(const i of o.content)r(i)}r(t);const a=[{level:0,format:j.BULLET,text:"\u2022",alignment:D.START,style:{paragraph:{indent:{left:k(.5),hanging:k(.25)}}}},{level:0,format:j.DECIMAL,text:"%1.",alignment:D.START,style:{paragraph:{indent:{left:k(.5),hanging:k(.25)}}}}],n=[{reference:"bullet-list",levels:[a[0]]},{reference:"ordered-list",levels:[a[1]]}];for(const o of e)o!==1&&n.push({reference:`ordered-list-start-${o}`,levels:[{level:0,format:j.DECIMAL,text:"%1.",alignment:D.START,start:o,style:{paragraph:{indent:{left:k(.5),hanging:k(.25)}}}}]});return{config:n}}export{Mt as DOCX_DPI,O as applyParagraphStyleAttributes,R as calculateEffectiveContentWidth,at as convertBlockquote,dt as convertBulletList,pt as convertCodeBlock,N as convertColorToHex,g as convertCssLengthToPixels,ft as convertDetails,mt as convertDocumentContent,x as convertHardBreak,ot as convertHeading,gt as convertHorizontalRule,_ as convertImage,U as convertList,G as convertListItem,F as convertMeasureToInches,E as convertMeasureToPixels,X as convertNode,ut as convertOrderedList,C as convertParagraph,f as convertPixelsToTwip,lt as convertTable,it as convertTableCell,st as convertTableHeader,ct as convertTableRow,q as convertTaskItem,ht as convertTaskList,S as convertText,$ as convertTextNodes,B as convertTwipToPixels,Rt as createFloatingOptions,zt as generateDOCX,nt as getImageDataAndMeta,rt as getImageHeight,z as getImageTypeFromSrc,et as getImageWidth};
|
|
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.
|
|
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,8 +54,9 @@
|
|
|
54
54
|
"ofetch": "1.5.1"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@tiptap/core": "3.
|
|
58
|
-
"@docen/extensions": "0.0.
|
|
57
|
+
"@tiptap/core": "3.19.0",
|
|
58
|
+
"@docen/extensions": "0.0.10",
|
|
59
|
+
"@docen/utils": "0.0.10"
|
|
59
60
|
},
|
|
60
61
|
"scripts": {
|
|
61
62
|
"dev": "unbuild --stub",
|