@docen/export-docx 0.0.8 → 0.0.9

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 CHANGED
@@ -1 +1 @@
1
- "use strict";const docx=require("docx"),imageMeta=require("image-meta"),ofetch=require("ofetch"),DOCX_DPI=96,a$1=/^([\d.]+)(px|pt|em|rem|%|)?$/,T=/^([\d.]+)(in|mm|cm|pt|pc|pi)$/,_={px:1,pt:1.333,em:16,rem:16,"%":.16},f={in:1,mm:1/25.4,cm:1/2.54,pt:1/72,pc:1/6,pi:1/6},convertTwipToPixels=t=>Math.round(t*96/1440),convertCssLengthToPixels=t=>{if(!t)return 0;t=t.trim();const r=t.match(a$1);if(!r)return 0;const n=parseFloat(r[1]);if(isNaN(n))return 0;const c=r[2]||"px",o=_[c]??1;return Math.round(n*o)},convertPixelsToTwip=t=>Math.round(t*15),convertMeasureToInches=t=>{if(typeof t=="number")return t;const r=t.match(T);if(r){const c=parseFloat(r[1]),o=r[2],i=f[o];return i!==void 0?c*i:c}const n=parseFloat(t);return isNaN(n)?6.5:n},convertMeasureToPixels=t=>{if(typeof t=="number")return t;const r=convertMeasureToInches(t);return Math.round(r*96)},calculateEffectiveContentWidth=t=>{if(!t?.sections?.length)return convertTwipToPixels(11906-1440*2);const r=t.sections[0];if(!r.properties?.page)return convertTwipToPixels(11906-1440*2);const n=r.properties.page;let c=11906;if(n.size?.width){const w=n.size.width;c=typeof w=="number"?w:Math.round(convertMeasureToInches(w)*1440)}const o=n.margin,i=o?.left?typeof o.left=="number"?o.left:Math.round(convertMeasureToInches(o.left)*1440):1440,d=o?.right?typeof o.right=="number"?o.right:Math.round(convertMeasureToInches(o.right)*1440):1440,m=c-i-d;return Math.max(convertTwipToPixels(m),96)},e={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"},convertColorToHex=t=>{if(t)return t.startsWith("#")?t:e[t.toLowerCase()]||t},a=6.5*96,p={jpg:"jpeg",jpeg:"jpeg",png:"png",gif:"gif",bmp:"bmp",tiff:"tiff"},u={jpg:"jpeg",jpeg:"jpeg",png:"png",gif:"gif",bmp:"bmp",tiff:"tiff"},getImageTypeFromSrc=t=>{if(t.startsWith("data:")){const r=t.match(/data:image\/(\w+);/);if(r){const n=r[1].toLowerCase();return p[n]||"png"}}else{const r=t.split(".").pop()?.toLowerCase();if(r)return u[r]||"png"}return"png"},h=(t,r=a)=>{if(!t.width||!t.height)return{width:r,height:Math.round(r*.75)};if(t.width<=r)return{width:t.width,height:t.height};const n=r/t.width;return{width:r,height:Math.round(t.height*n)}},createFloatingOptions=()=>({horizontalPosition:{relative:"page",align:"center"},verticalPosition:{relative:"page",align:"top"},lockAnchor:!0,behindDocument:!1,inFrontOfText:!1}),getImageWidth=(t,r,n)=>{if(t.attrs?.width!==void 0&&t.attrs?.width!==null)return t.attrs.width;const c=n!==void 0?convertMeasureToPixels(n):void 0;return r?.width&&r?.height?h(r,c).width:c||a},getImageHeight=(t,r,n,c)=>{if(t.attrs?.height!==void 0&&t.attrs?.height!==null)return t.attrs.height;const o=c!==void 0?convertMeasureToPixels(c):void 0;return n?.width&&n?.height?h(n,o).height:Math.round(r*.75)},getImageDataAndMeta=async t=>{try{const r=await(await ofetch.ofetch(t,{responseType:"blob"})).bytes();let n;try{n=imageMeta.imageMeta(r)}catch(c){console.warn("Failed to extract image metadata:",c),n={width:void 0,height:void 0,type:getImageTypeFromSrc(t)||"png",orientation:void 0}}return{data:r,meta:n}}catch(r){throw console.warn(`Failed to fetch image from ${t}:`,r),r}},applyParagraphStyleAttributes=(t,r)=>{if(!r)return t;let n={...t};return(r.indentLeft||r.indentRight||r.indentFirstLine)&&(n={...n,indent:{...r.indentLeft&&{left:convertPixelsToTwip(convertCssLengthToPixels(r.indentLeft))},...r.indentRight&&{right:convertPixelsToTwip(convertCssLengthToPixels(r.indentRight))},...r.indentFirstLine&&{firstLine:convertPixelsToTwip(convertCssLengthToPixels(r.indentFirstLine))}}}),(r.spacingBefore||r.spacingAfter)&&(n={...n,spacing:{...r.spacingBefore&&{before:convertPixelsToTwip(convertCssLengthToPixels(r.spacingBefore))},...r.spacingAfter&&{after:convertPixelsToTwip(convertCssLengthToPixels(r.spacingAfter))}}}),r.textAlign&&(n={...n,alignment:{left:"left",right:"right",center:"center",justify:"both"}[r.textAlign]}),n};function convertText(t){const r=t.marks?.some(g=>g.type==="bold"),n=t.marks?.some(g=>g.type==="italic"),c=t.marks?.some(g=>g.type==="underline"),o=t.marks?.some(g=>g.type==="strike"),i=t.marks?.some(g=>g.type==="code"),d=t.marks?.some(g=>g.type==="subscript"),m=t.marks?.some(g=>g.type==="superscript"),w=t.marks?.find(g=>g.type==="link"),y=t.marks?.find(g=>g.type==="textStyle"),v=t.marks?.some(g=>g.type==="highlight"),x=convertColorToHex(y?.attrs?.color),b=convertColorToHex(y?.attrs?.backgroundColor);let F;if(y?.attrs?.fontSize){const g=y.attrs.fontSize;if(g.endsWith("px")){const L=parseFloat(g);isNaN(L)||(F=Math.round(L*1.5))}}let k;i?k="Consolas":y?.attrs?.fontFamily&&(k=y.attrs.fontFamily);const A={text:t.text||"",bold:r||void 0,italics:n||void 0,underline:c?{}:void 0,strike:o||void 0,font:k,size:F,subScript:d||void 0,superScript:m||void 0,color:x,shading:b?{fill:b}:void 0,highlight:v?"yellow":void 0};return w?.attrs?.href?new docx.ExternalHyperlink({children:[new docx.TextRun({...A,style:"Hyperlink"})],link:w.attrs.href}):new docx.TextRun(A)}function convertHardBreak(t){const r={text:"",break:1};if(t)for(const n of t)switch(n.type){case"bold":r.bold=!0;break;case"italic":r.italics=!0;break;case"underline":r.underline={};break;case"strike":r.strike=!0;break;case"textStyle":n.attrs?.color&&(r.color=n.attrs.color);break}return new docx.TextRun(r)}const convertTextNodes=(t=[])=>t.flatMap(r=>r.type==="text"?[convertText(r)]:r.type==="hardBreak"?[convertHardBreak(r.marks)]:[]);async function convertImage(t,r){const n=y=>{switch(y){case"jpeg":case"jpg":return"jpg";case"png":return"png";case"gif":return"gif";case"bmp":return"bmp"}switch(getImageTypeFromSrc(t.attrs?.src||"")){case"jpeg":return"jpg";case"png":return"png";case"gif":return"gif";case"bmp":return"bmp";default:return"png"}};let c,o;try{const y=t.attrs?.src||"";if(y.startsWith("http")){const v=await getImageDataAndMeta(y);c=v.data,o=v.meta}else if(y.startsWith("data:")){const v=y.split(",")[1];if(!v)throw new Error("Invalid data URL: missing base64 data");const x=atob(v);c=Uint8Array.from(x,b=>b.charCodeAt(0));try{o=imageMeta.imageMeta(c)}catch{o={type:"png",width:void 0,height:void 0,orientation:void 0}}}else throw new Error(`Unsupported image source format: ${y.substring(0,20)}...`)}catch(y){return console.warn("Failed to process image:",y),new docx.ImageRun({type:"png",data:new Uint8Array(0),transformation:{width:100,height:100},altText:{name:t.attrs?.alt||"Failed to load image"}})}const i=getImageWidth(t,o,r?.maxWidth),d=getImageHeight(t,i,o,r?.maxWidth),m={width:i,height:d};t.attrs?.rotation!==void 0&&(m.rotation=t.attrs.rotation);const w={type:n(o.type),data:c,transformation:m,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 docx.ImageRun(w)}async function convertParagraph(t,r){const{options:n,image:c}=r||{},o=[];for(const d of t.content||[])if(d.type==="text")o.push(convertText(d));else if(d.type==="hardBreak")o.push(convertHardBreak(d.marks));else if(d.type==="image"){const m=await convertImage(d,{maxWidth:c?.maxWidth});o.push(m)}let i={children:o};return n&&(i={...i,...n}),t.attrs&&(i=applyParagraphStyleAttributes(i,t.attrs)),new docx.Paragraph(i)}function convertHeading(t){const r=t?.attrs?.level,n=convertTextNodes(t.content).filter(i=>i!==void 0),c={1:docx.HeadingLevel.HEADING_1,2:docx.HeadingLevel.HEADING_2,3:docx.HeadingLevel.HEADING_3,4:docx.HeadingLevel.HEADING_4,5:docx.HeadingLevel.HEADING_5,6:docx.HeadingLevel.HEADING_6};let o={children:n,heading:c[r]};return t.attrs&&(o=applyParagraphStyleAttributes(o,t.attrs)),new docx.Paragraph(o)}function convertBlockquote(t){return t.content?t.content.map(r=>{if(r.type==="paragraph"){const n=r.content?.flatMap(c=>c.type==="text"?convertText(c):c.type==="hardBreak"?convertHardBreak(c.marks):[])||[];return new docx.Paragraph({children:n,indent:{left:720},border:{left:{style:"single"}}})}return new docx.Paragraph({})}):[]}function s$1(t){if(!t)return;const r={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},n=t.style&&r[t.style]||"single",c=t.color?.replace("#","")||"auto",o=t.width?t.width*6:4;return{color:c,size:o,style:n}}async function convertTableCell(t,r){const{options:n}=r;let c=n?.cell?.paragraph??n?.row?.paragraph??{};n?.style&&(c={...c,style:n.style.id});const o={children:await Promise.all((t.content||[]).map(d=>convertParagraph(d,{options:c}))),...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 d=Array.isArray(t.attrs.colWidth)?t.attrs.colWidth[0]:t.attrs.colWidth;if(d&&d>0){const m=Math.round(d*15);o.width={size:m,type:"dxa"}}}if(t.attrs?.backgroundColor){const d=t.attrs.backgroundColor.replace("#","");o.shading={fill:d}}if(t.attrs?.verticalAlign){const d=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;o.verticalAlign=d}const i={top:s$1(t.attrs?.borderTop),bottom:s$1(t.attrs?.borderBottom),left:s$1(t.attrs?.borderLeft),right:s$1(t.attrs?.borderRight)};return(i.top||i.bottom||i.left||i.right)&&(o.borders=i),new docx.TableCell(o)}function l(t){if(!t)return;const r={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},n=t.style&&r[t.style]||"single",c=t.color?.replace("#","")||"auto",o=t.width?t.width*6:4;return{color:c,size:o,style:n}}async function convertTableHeader(t,r){const{options:n}=r;let c=n?.header?.paragraph??n?.cell?.paragraph??n?.row?.paragraph??{};n?.style&&(c={...c,style:n.style.id});const o={children:await Promise.all((t.content||[]).map(d=>convertParagraph(d,{options:c}))),...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 d=Array.isArray(t.attrs.colWidth)?t.attrs.colWidth[0]:t.attrs.colWidth;if(d&&d>0){const m=Math.round(d*15);o.width={size:m,type:"dxa"}}}if(t.attrs?.backgroundColor){const d=t.attrs.backgroundColor.replace("#","");o.shading={fill:d}}if(t.attrs?.verticalAlign){const d=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;o.verticalAlign=d}const i={top:l(t.attrs?.borderTop),bottom:l(t.attrs?.borderBottom),left:l(t.attrs?.borderLeft),right:l(t.attrs?.borderRight)};return(i.top||i.bottom||i.left||i.right)&&(o.borders=i),new docx.TableCell(o)}async function convertTableRow(t,r){const{options:n}=r,c=n?.row,o={children:(await Promise.all((t.content||[]).map(async i=>i.type==="tableCell"?await convertTableCell(i,r):i.type==="tableHeader"?await convertTableHeader(i,r):null))).filter(i=>i!==void 0),...c};if(t.attrs?.rowHeight){const i=convertCssLengthToPixels(t.attrs.rowHeight),d=convertPixelsToTwip(i);d>0&&(o.height={rule:"atLeast",value:d})}return new docx.TableRow(o)}const s=(t,r)=>{const n={top:r.attrs?.marginTop??void 0,bottom:r.attrs?.marginBottom??void 0,left:r.attrs?.marginLeft??void 0,right:r.attrs?.marginRight??void 0};return n.top||n.bottom||n.left||n.right?{...t,margins:n}:t};async function convertTable(t,r){const{options:n}=r;let c={rows:await Promise.all((t.content||[]).map(o=>convertTableRow(o,r))),...n?.run};return c=s(c,t),new docx.Table(c)}function convertCodeBlock(t){const r=t.content?.map(n=>n.text||"").join("")||"";return new docx.Paragraph({children:[new docx.TextRun({text:r,font:"Consolas"})]})}async function convertListItem(t,r){if(!t.content||t.content.length===0)return new docx.Paragraph({});const n=t.content[0];return n.type==="paragraph"?await convertParagraph(n,{options:r.options}):new docx.Paragraph({})}function convertBulletList(){return{numbering:{reference:"bullet-list",level:0}}}function convertOrderedList(t){const r=t.attrs?.start||1;return{numbering:{reference:"ordered-list",level:0},start:r}}async function convertList(t,r){const{listType:n}=r;if(!t.content)return[];const c=[],o=n==="bullet"?convertBulletList():convertOrderedList(t);let i=o.numbering.reference;n==="ordered"&&o.start&&o.start!==1&&(i=`ordered-list-start-${o.start}`);for(const d of t.content)if(d.type==="listItem"){const m=await convertListItem(d,{options:{numbering:{reference:i,level:0}}});c.push(m)}return c}function convertTaskItem(t){if(!t.content||t.content.length===0)return new docx.Paragraph({});const r=t.content[0];if(r.type==="paragraph"){const n=t.attrs?.checked?"\u2611 ":"\u2610 ",c=r.content?.flatMap(i=>i.type==="text"?convertText(i):i.type==="hardBreak"?convertHardBreak(i.marks):[])||[],o=new docx.TextRun({text:n});return new docx.Paragraph({children:[o,...c]})}return new docx.Paragraph({})}function convertTaskList(t){return!t.content||t.content.length===0?[]:t.content.filter(r=>r.type==="taskItem").map(r=>convertTaskItem(r))}function convertHorizontalRule(t,r){const n={children:[new docx.PageBreak]};return new docx.Paragraph({...n,...r.options?.paragraph})}async function convertDetails(t,r){if(!t.content)return[];const n=[];let c,o;for(const i of t.content)i.type==="detailsSummary"?c=i:i.type==="detailsContent"&&(o=i);if(c?.content){const i=convertTextNodes(c.content).filter(m=>m!==void 0),d=new docx.Paragraph({children:i,...r.options?.summary?.paragraph});n.push(d)}if(o?.content){const i=calculateEffectiveContentWidth(r.exportOptions);for(const d of o.content){const m=await convertNode(d,r.exportOptions,i);Array.isArray(m)?n.push(...m):m&&n.push(m)}}return n}async function generateDOCX(t,r){const{title:n,subject:c,creator:o,keywords:i,description:d,lastModifiedBy:m,revision:w,styles:y,tableOfContents:v,sections:x,fonts:b,hyphenation:F,compatibility:k,customProperties:A,evenAndOddHeaderAndFooters:g,defaultTabStop:L,outputType:M}=r,S=await convertDocumentContent(t,r),P=v?new docx.TableOfContents(v.title,{...v.run}):null,B=Z(t),C=[];r.image?.style&&C.push(r.image.style),r.table?.style&&C.push(r.table.style);const R=y?{...y,...C.length>0&&{paragraphStyles:[...y.paragraphStyles||[],...C]}}:{},W={sections:x?x.map((E,H)=>{const I=[];return H===0&&P&&I.push(P),H===0&&I.push(...S),{...E,...I.length>0?{children:I}:{}}}):[{children:P?[P,...S]:S}],title:n||"Document",subject:c||"",creator:o||"",keywords:i||"",description:d||"",lastModifiedBy:m||"",revision:w||1,styles:R,numbering:B,...b&&b.length>0&&{fonts:b},...F&&{hyphenation:F},...k&&{compatibility:k},...A&&A.length>0&&{customProperties:A},...g!==void 0&&{evenAndOddHeaderAndFooters:g},...L!==void 0&&{defaultTabStop:L}},D=new docx.Document(W);return docx.Packer.pack(D,M||"arraybuffer")}async function convertDocumentContent(t,r){const n=[];if(!t||!Array.isArray(t.content))return n;const c=calculateEffectiveContentWidth(r);for(const o of t.content){const i=await convertNode(o,r,c);Array.isArray(i)?n.push(...i):i&&(n.push(i),o.type==="table"&&n.length>=2&&n[n.length-2]instanceof docx.Table&&n.push(new docx.Paragraph({})))}return n}async function convertNode(t,r,n){if(!t||!t.type)return null;switch(t.type){case"paragraph":return await convertParagraph(t,{image:{maxWidth:n}});case"heading":return convertHeading(t);case"blockquote":return convertBlockquote(t);case"codeBlock":return convertCodeBlock(t);case"image":const c=await convertImage(t,{maxWidth:n}),o=r.image?.style?{children:[c],style:r.image.style.id}:{children:[c]};return new docx.Paragraph(o);case"table":return await convertTable(t,{options:r.table});case"bulletList":return await convertList(t,{listType:"bullet"});case"orderedList":return await convertList(t,{listType:"ordered"});case"taskList":return convertTaskList(t);case"listItem":return convertListItem(t,{options:void 0});case"taskItem":return convertTaskItem(t);case"hardBreak":return new docx.Paragraph({children:[convertHardBreak()]});case"horizontalRule":return convertHorizontalRule(t,{options:r.horizontalRule});case"details":return await convertDetails(t,{options:r.details,exportOptions:r});default:return new docx.Paragraph({children:[new docx.TextRun({text:`[Unsupported: ${t.type}]`})]})}}function Z(t){const r=new Set;function n(i){if(i.type==="orderedList"&&i.attrs?.start&&r.add(i.attrs.start),i.content)for(const d of i.content)n(d)}n(t);const c=[{level:0,format:docx.LevelFormat.BULLET,text:"\u2022",alignment:docx.AlignmentType.START,style:{paragraph:{indent:{left:docx.convertInchesToTwip(.5),hanging:docx.convertInchesToTwip(.25)}}}},{level:0,format:docx.LevelFormat.DECIMAL,text:"%1.",alignment:docx.AlignmentType.START,style:{paragraph:{indent:{left:docx.convertInchesToTwip(.5),hanging:docx.convertInchesToTwip(.25)}}}}],o=[{reference:"bullet-list",levels:[c[0]]},{reference:"ordered-list",levels:[c[1]]}];for(const i of r)i!==1&&o.push({reference:`ordered-list-start-${i}`,levels:[{level:0,format:docx.LevelFormat.DECIMAL,text:"%1.",alignment:docx.AlignmentType.START,start:i,style:{paragraph:{indent:{left:docx.convertInchesToTwip(.5),hanging:docx.convertInchesToTwip(.25)}}}}]});return{config:o}}exports.DOCX_DPI=DOCX_DPI,exports.applyParagraphStyleAttributes=applyParagraphStyleAttributes,exports.calculateEffectiveContentWidth=calculateEffectiveContentWidth,exports.convertBlockquote=convertBlockquote,exports.convertBulletList=convertBulletList,exports.convertCodeBlock=convertCodeBlock,exports.convertColorToHex=convertColorToHex,exports.convertCssLengthToPixels=convertCssLengthToPixels,exports.convertDetails=convertDetails,exports.convertDocumentContent=convertDocumentContent,exports.convertHardBreak=convertHardBreak,exports.convertHeading=convertHeading,exports.convertHorizontalRule=convertHorizontalRule,exports.convertImage=convertImage,exports.convertList=convertList,exports.convertListItem=convertListItem,exports.convertMeasureToInches=convertMeasureToInches,exports.convertMeasureToPixels=convertMeasureToPixels,exports.convertNode=convertNode,exports.convertOrderedList=convertOrderedList,exports.convertParagraph=convertParagraph,exports.convertPixelsToTwip=convertPixelsToTwip,exports.convertTable=convertTable,exports.convertTableCell=convertTableCell,exports.convertTableHeader=convertTableHeader,exports.convertTableRow=convertTableRow,exports.convertTaskItem=convertTaskItem,exports.convertTaskList=convertTaskList,exports.convertText=convertText,exports.convertTextNodes=convertTextNodes,exports.convertTwipToPixels=convertTwipToPixels,exports.createFloatingOptions=createFloatingOptions,exports.generateDOCX=generateDOCX,exports.getImageDataAndMeta=getImageDataAndMeta,exports.getImageHeight=getImageHeight,exports.getImageTypeFromSrc=getImageTypeFromSrc,exports.getImageWidth=getImageWidth;
1
+ "use strict";const docx=require("docx"),imageMeta=require("image-meta"),ofetch=require("ofetch"),f=96;function a$1(t){return Math.round(t*96/1440)}function d(t){return`${a$1(t)}px`}function p(t){return Math.round(t*(1440/96))}function s$1(t){return Math.round(t/(914400/96))}function h$1(t){return Math.round(t*(914400/96))}function m(t){const r=parseInt(t,10);if(!isNaN(r))return s$1(r)}const g$1=/^([\d.]+)(px|pt|em|rem|%|)?$/,C$1={px:1,pt:1.333,em:16,rem:16,"%":.16};function k(t){if(!t)return 0;t=t.trim();const r=t.match(g$1);if(!r)return 0;const n=parseFloat(r[1]);if(isNaN(n))return 0;const o=r[2]||"px",i=C$1[o]??1;return Math.round(n*i)}const v=/^([\d.]+)(in|mm|cm|pt|pc|pi)$/,y={in:1,mm:1/25.4,cm:1/2.54,pt:1/72,pc:1/6,pi:1/6};function u$1(t){if(typeof t=="number")return t;const r=t.match(v);if(r){const o=parseFloat(r[1]),i=r[2],w=y[i];return w!==void 0?o*w:o}const n=parseFloat(t);return isNaN(n)?6.5:n}function E(t){if(typeof t=="number")return t;const r=u$1(t);return Math.round(r*96)}const c={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 T(t){if(t)return t.startsWith("#")?t:c[t.toLowerCase()]||t}function x(t){return t.startsWith("#")?t:`#${t}`}function B(t,r){if(!t.children)return null;for(const n of t.children)if(n.type==="element"&&n.name===r)return n;return null}function l$1(t,r){if(!t.children)return null;for(const n of t.children)if(n.type==="element"){if(n.name===r)return n;const o=l$1(n,r);if(o)return o}return null}function F(t,r){const n=[];if(!t.children)return n;for(const o of t.children)o.type==="element"&&(o.name===r&&n.push(o),n.push(...F(o,r)));return n}function D(t,r){const n=t[r];if(!n)return;const o=parseInt(n,10);if(!isNaN(o))return n}const _$1=(t,r)=>t?typeof t=="number"?t:Math.round(u$1(t)*1440):r;function calculateEffectiveContentWidth(t){if(!t?.sections?.length)return a$1(11906-1440*2);const r=t.sections[0];if(!r.properties?.page)return a$1(11906-1440*2);const n=r.properties.page;let o=11906;if(n.size?.width){const H=n.size.width;o=typeof H=="number"?H:Math.round(u$1(H)*1440)}const i=n.margin,w=_$1(i?.left,1440),b=_$1(i?.right,1440),L=o-w-b;return Math.max(a$1(L),f)}const a=6.5*96,g={jpg:"jpeg",jpeg:"jpeg",png:"png",gif:"gif",bmp:"bmp",tiff:"tiff"},u={jpeg:"jpg",jpg:"jpg",png:"png",gif:"gif",bmp:"bmp"};function convertToDocxImageType(t){if(!t)return"png";const r=t.toLowerCase();return u[r]??"png"}function getImageTypeFromSrc(t){if(t.startsWith("data:")){const r=t.match(/data:image\/(\w+);/);if(r){const n=r[1].toLowerCase();return g[n]||"png"}}else{const r=t.split(".").pop()?.toLowerCase();if(r)return g[r]||"png"}return"png"}const h=(t,r=a)=>{if(!t.width||!t.height)return{width:r,height:Math.round(r*.75)};if(t.width<=r)return{width:t.width,height:t.height};const n=r/t.width;return{width:r,height:Math.round(t.height*n)}};function createFloatingOptions(){return{horizontalPosition:{relative:"page",align:"center"},verticalPosition:{relative:"page",align:"top"},lockAnchor:!0,behindDocument:!1,inFrontOfText:!1}}function getImageWidth(t,r,n){if(t.attrs?.width!==void 0&&t.attrs?.width!==null)return t.attrs.width;const o=n!==void 0?E(n):void 0;return r?.width&&r?.height?h(r,o).width:o||a}function getImageHeight(t,r,n,o){if(t.attrs?.height!==void 0&&t.attrs?.height!==null)return t.attrs.height;const i=o!==void 0?E(o):void 0;return n?.width&&n?.height?h(n,i).height:Math.round(r*.75)}async function getImageDataAndMeta(t){try{const r=await(await ofetch.ofetch(t,{responseType:"blob"})).bytes();let n;try{n=imageMeta.imageMeta(r)}catch(o){console.warn("Failed to extract image metadata:",o),n={width:void 0,height:void 0,type:getImageTypeFromSrc(t)||"png",orientation:void 0}}return{data:r,meta:n}}catch(r){throw console.warn(`Failed to fetch image from ${t}:`,r),r}}const applyParagraphStyleAttributes=(t,r)=>{if(!r)return t;let n={...t};return(r.indentLeft||r.indentRight||r.indentFirstLine)&&(n={...n,indent:{...r.indentLeft&&{left:p(k(r.indentLeft))},...r.indentRight&&{right:p(k(r.indentRight))},...r.indentFirstLine&&{firstLine:p(k(r.indentFirstLine))}}}),(r.spacingBefore||r.spacingAfter)&&(n={...n,spacing:{...r.spacingBefore&&{before:p(k(r.spacingBefore))},...r.spacingAfter&&{after:p(k(r.spacingAfter))}}}),r.textAlign&&(n={...n,alignment:{left:"left",right:"right",center:"center",justify:"both"}[r.textAlign]}),n};function convertText(t){const r=t.marks?.some(A=>A.type==="bold"),n=t.marks?.some(A=>A.type==="italic"),o=t.marks?.some(A=>A.type==="underline"),i=t.marks?.some(A=>A.type==="strike"),w=t.marks?.some(A=>A.type==="code"),b=t.marks?.some(A=>A.type==="subscript"),L=t.marks?.some(A=>A.type==="superscript"),H=t.marks?.find(A=>A.type==="link"),I=t.marks?.find(A=>A.type==="textStyle"),P=t.marks?.some(A=>A.type==="highlight"),S=T(I?.attrs?.color),M=T(I?.attrs?.backgroundColor);let $;if(I?.attrs?.fontSize){const A=I.attrs.fontSize;if(A.endsWith("px")){const O=parseFloat(A);isNaN(O)||($=Math.round(O*1.5))}}let R;w?R="Consolas":I?.attrs?.fontFamily&&(R=I.attrs.fontFamily);const N={text:t.text||"",bold:r||void 0,italics:n||void 0,underline:o?{}:void 0,strike:i||void 0,font:R,size:$,subScript:b||void 0,superScript:L||void 0,color:S,shading:M?{fill:M}:void 0,highlight:P?"yellow":void 0};return H?.attrs?.href?new docx.ExternalHyperlink({children:[new docx.TextRun({...N,style:"Hyperlink"})],link:H.attrs.href}):new docx.TextRun(N)}function convertHardBreak(t){const r={text:"",break:1};if(t)for(const n of t)switch(n.type){case"bold":r.bold=!0;break;case"italic":r.italics=!0;break;case"underline":r.underline={};break;case"strike":r.strike=!0;break;case"textStyle":n.attrs?.color&&(r.color=n.attrs.color);break}return new docx.TextRun(r)}function convertTextNodes(t=[]){return t.flatMap(r=>r.type==="text"?[convertText(r)]:r.type==="hardBreak"?[convertHardBreak(r.marks)]:[])}async function convertImage(t,r){const n=I=>{const P=I||getImageTypeFromSrc(t.attrs?.src||"");return convertToDocxImageType(P)};let o,i;try{const I=t.attrs?.src||"";if(I.startsWith("http")){const P=await getImageDataAndMeta(I);o=P.data,i=P.meta}else if(I.startsWith("data:")){const P=I.split(",")[1];if(!P)throw new Error("Invalid data URL: missing base64 data");const S=atob(P);o=Uint8Array.from(S,M=>M.charCodeAt(0));try{i=imageMeta.imageMeta(o)}catch{i={type:"png",width:void 0,height:void 0,orientation:void 0}}}else throw new Error(`Unsupported image source format: ${I.substring(0,20)}...`)}catch(I){return console.warn("Failed to process image:",I),new docx.ImageRun({type:"png",data:new Uint8Array(0),transformation:{width:100,height:100},altText:{name:t.attrs?.alt||"Failed to load image"}})}const w=getImageWidth(t,i,r?.maxWidth),b=getImageHeight(t,w,i,r?.maxWidth),L={width:w,height:b};t.attrs?.rotation!==void 0&&(L.rotation=t.attrs.rotation);const H={type:n(i.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 docx.ImageRun(H)}async function convertParagraph(t,r){const{options:n,image:o}=r||{},i=[];for(const b of t.content||[])if(b.type==="text")i.push(convertText(b));else if(b.type==="hardBreak")i.push(convertHardBreak(b.marks));else if(b.type==="image"){const L=await convertImage(b,{maxWidth:o?.maxWidth});i.push(L)}let w={children:i};return n&&(w={...w,...n}),t.attrs&&(w=applyParagraphStyleAttributes(w,t.attrs)),new docx.Paragraph(w)}function convertHeading(t){const r=t?.attrs?.level,n=convertTextNodes(t.content).filter(w=>w!==void 0),o={1:docx.HeadingLevel.HEADING_1,2:docx.HeadingLevel.HEADING_2,3:docx.HeadingLevel.HEADING_3,4:docx.HeadingLevel.HEADING_4,5:docx.HeadingLevel.HEADING_5,6:docx.HeadingLevel.HEADING_6};let i={children:n,heading:o[r]};return t.attrs&&(i=applyParagraphStyleAttributes(i,t.attrs)),new docx.Paragraph(i)}function convertBlockquote(t){return t.content?t.content.map(r=>{if(r.type==="paragraph"){const n=r.content?.flatMap(o=>o.type==="text"?convertText(o):o.type==="hardBreak"?convertHardBreak(o.marks):[])||[];return new docx.Paragraph({children:n,indent:{left:720},border:{left:{style:"single"}}})}return new docx.Paragraph({})}):[]}function e(t){if(!t)return;const r={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},n=t.style&&r[t.style]||"single",o=t.color?.replace("#","")||"auto",i=t.width?t.width*6:4;return{color:o,size:i,style:n}}async function convertTableCell(t,r){const{options:n}=r;let o=n?.cell?.paragraph??n?.row?.paragraph??{};n?.style&&(o={...o,style:n.style.id});const i={children:await Promise.all((t.content||[]).map(b=>convertParagraph(b,{options:o}))),...n?.cell?.run};if(t.attrs?.colspan&&t.attrs.colspan>1&&(i.columnSpan=t.attrs.colspan),t.attrs?.rowspan&&t.attrs.rowspan>1&&(i.rowSpan=t.attrs.rowspan),t.attrs?.colwidth!==null&&t.attrs?.colwidth!==void 0){const b=Array.isArray(t.attrs.colwidth)?t.attrs.colwidth[0]:t.attrs.colwidth;if(b&&b>0){const L=Math.round(b*15);i.width={size:L,type:"dxa"}}}if(t.attrs?.backgroundColor){const b=t.attrs.backgroundColor.replace("#","");i.shading={fill:b}}if(t.attrs?.verticalAlign){const b=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;i.verticalAlign=b}const w={top:e(t.attrs?.borderTop),bottom:e(t.attrs?.borderBottom),left:e(t.attrs?.borderLeft),right:e(t.attrs?.borderRight)};return(w.top||w.bottom||w.left||w.right)&&(i.borders=w),new docx.TableCell(i)}function l(t){if(!t)return;const r={solid:"single",dashed:"dashed",dotted:"dotted",double:"double",none:"none"},n=t.style&&r[t.style]||"single",o=t.color?.replace("#","")||"auto",i=t.width?t.width*6:4;return{color:o,size:i,style:n}}async function convertTableHeader(t,r){const{options:n}=r;let o=n?.header?.paragraph??n?.cell?.paragraph??n?.row?.paragraph??{};n?.style&&(o={...o,style:n.style.id});const i={children:await Promise.all((t.content||[]).map(b=>convertParagraph(b,{options:o}))),...n?.header?.run};if(t.attrs?.colspan&&t.attrs.colspan>1&&(i.columnSpan=t.attrs.colspan),t.attrs?.rowspan&&t.attrs.rowspan>1&&(i.rowSpan=t.attrs.rowspan),t.attrs?.colwidth!==null&&t.attrs?.colwidth!==void 0){const b=Array.isArray(t.attrs.colwidth)?t.attrs.colwidth[0]:t.attrs.colwidth;if(b&&b>0){const L=Math.round(b*15);i.width={size:L,type:"dxa"}}}if(t.attrs?.backgroundColor){const b=t.attrs.backgroundColor.replace("#","");i.shading={fill:b}}if(t.attrs?.verticalAlign){const b=t.attrs.verticalAlign==="middle"?"center":t.attrs.verticalAlign;i.verticalAlign=b}const w={top:l(t.attrs?.borderTop),bottom:l(t.attrs?.borderBottom),left:l(t.attrs?.borderLeft),right:l(t.attrs?.borderRight)};return(w.top||w.bottom||w.left||w.right)&&(i.borders=w),new docx.TableCell(i)}async function convertTableRow(t,r){const{options:n}=r,o=n?.row,i={children:(await Promise.all((t.content||[]).map(async w=>w.type==="tableCell"?await convertTableCell(w,r):w.type==="tableHeader"?await convertTableHeader(w,r):null))).filter(w=>w!==void 0),...o};if(t.attrs?.rowHeight){const w=k(t.attrs.rowHeight),b=p(w);b>0&&(i.height={rule:"atLeast",value:b})}return new docx.TableRow(i)}const s=(t,r)=>{const n={top:r.attrs?.marginTop??void 0,bottom:r.attrs?.marginBottom??void 0,left:r.attrs?.marginLeft??void 0,right:r.attrs?.marginRight??void 0};return n.top||n.bottom||n.left||n.right?{...t,margins:n}:t};async function convertTable(t,r){const{options:n}=r;let o={rows:await Promise.all((t.content||[]).map(i=>convertTableRow(i,r))),...n?.run};return o=s(o,t),new docx.Table(o)}function convertCodeBlock(t){const r=t.content?.map(n=>n.text||"").join("")||"";return new docx.Paragraph({children:[new docx.TextRun({text:r,font:"Consolas"})]})}async function convertListItem(t,r){if(!t.content||t.content.length===0)return new docx.Paragraph({});const n=t.content[0];return n.type==="paragraph"?await convertParagraph(n,{options:r.options}):new docx.Paragraph({})}function convertBulletList(){return{numbering:{reference:"bullet-list",level:0}}}function convertOrderedList(t){const r=t.attrs?.start||1;return{numbering:{reference:"ordered-list",level:0},start:r}}async function convertList(t,r){const{listType:n}=r;if(!t.content)return[];const o=[],i=n==="bullet"?convertBulletList():convertOrderedList(t);let w=i.numbering.reference;n==="ordered"&&i.start&&i.start!==1&&(w=`ordered-list-start-${i.start}`);for(const b of t.content)if(b.type==="listItem"){const L=await convertListItem(b,{options:{numbering:{reference:w,level:0}}});o.push(L)}return o}function convertTaskItem(t){if(!t.content||t.content.length===0)return new docx.Paragraph({});const r=t.content[0];if(r.type==="paragraph"){const n=t.attrs?.checked?"\u2611 ":"\u2610 ",o=r.content?.flatMap(w=>w.type==="text"?convertText(w):w.type==="hardBreak"?convertHardBreak(w.marks):[])||[],i=new docx.TextRun({text:n});return new docx.Paragraph({children:[i,...o]})}return new docx.Paragraph({})}function convertTaskList(t){return!t.content||t.content.length===0?[]:t.content.filter(r=>r.type==="taskItem").map(r=>convertTaskItem(r))}function convertHorizontalRule(t,r){const n={children:[new docx.PageBreak]};return new docx.Paragraph({...n,...r.options?.paragraph})}function convertDetailsSummary(t,r){const n=convertTextNodes(t.content||[]).filter(o=>o!==void 0);return new docx.Paragraph({children:n,...r.options?.summary?.paragraph})}async function generateDOCX(t,r){const{title:n,subject:o,creator:i,keywords:w,description:b,lastModifiedBy:L,revision:H,styles:I,tableOfContents:P,sections:S,fonts:M,hyphenation:$,compatibility:R,customProperties:N,evenAndOddHeaderAndFooters:A,defaultTabStop:O,outputType:U}=r,q=await convertDocument(t,{options:r}),W=P?new docx.TableOfContents(P.title,{...P.run}):null,X=ee(t),z=[];r.image?.style&&z.push(r.image.style),r.table?.style&&z.push(r.table.style);const J=I?{...I,...z.length>0&&{paragraphStyles:[...I.paragraphStyles||[],...z]}}:{},K={sections:S?S.map((V,G)=>{const j=[];return G===0&&W&&j.push(W),G===0&&j.push(...q),{...V,...j.length>0?{children:j}:{}}}):[{children:W?[W,...q]:q}],title:n||"Document",subject:o||"",creator:i||"",keywords:w||"",description:b||"",lastModifiedBy:L||"",revision:H||1,styles:J,numbering:X,...M&&M.length>0&&{fonts:M},...$&&{hyphenation:$},...R&&{compatibility:R},...N&&N.length>0&&{customProperties:N},...A!==void 0&&{evenAndOddHeaderAndFooters:A},...O!==void 0&&{defaultTabStop:O}},Q=new docx.Document(K);return docx.Packer.pack(Q,U||"arraybuffer")}async function convertDocument(t,r){const n=[];if(!t||!Array.isArray(t.content))return n;const o=calculateEffectiveContentWidth(r.options);for(const i of t.content){const w=await convertNode(i,r.options,o);Array.isArray(w)?n.push(...w):w&&(n.push(w),i.type==="table"&&n.length>=2&&n[n.length-2]instanceof docx.Table&&n.push(new docx.Paragraph({})))}return n}async function convertNode(t,r,n){if(!t||!t.type)return null;switch(t.type){case"paragraph":return await convertParagraph(t,{image:{maxWidth:n}});case"heading":return convertHeading(t);case"blockquote":return convertBlockquote(t);case"codeBlock":return convertCodeBlock(t);case"image":const o=await convertImage(t,{maxWidth:n}),i=r.image?.style?{children:[o],style:r.image.style.id}:{children:[o]};return new docx.Paragraph(i);case"table":return await convertTable(t,{options:r.table});case"bulletList":return await convertList(t,{listType:"bullet"});case"orderedList":return await convertList(t,{listType:"ordered"});case"taskList":return convertTaskList(t);case"listItem":return convertListItem(t,{options:void 0});case"taskItem":return convertTaskItem(t);case"hardBreak":return new docx.Paragraph({children:[convertHardBreak()]});case"horizontalRule":return convertHorizontalRule(t,{options:r.horizontalRule});case"details":const w=[];if(t.content)for(const b of t.content){const L=await convertNode(b,r,n);Array.isArray(L)?w.push(...L):L&&w.push(L)}return w;case"detailsSummary":return convertDetailsSummary(t,{options:r.details});default:return new docx.Paragraph({children:[new docx.TextRun({text:`[Unsupported: ${t.type}]`})]})}}const _=t=>({level:0,format:docx.LevelFormat.DECIMAL,text:"%1.",alignment:docx.AlignmentType.START,start:t??1,style:{paragraph:{indent:{left:docx.convertInchesToTwip(.5),hanging:docx.convertInchesToTwip(.25)}}}}),C=t=>({reference:t&&t!==1?`ordered-list-start-${t}`:"ordered-list",levels:[_(t)]});function ee(t){const r=new Set;function n(i){if(i.type==="orderedList"&&i.attrs?.start&&r.add(i.attrs.start),i.content)for(const w of i.content)n(w)}n(t);const o=[{reference:"bullet-list",levels:[{level:0,format:docx.LevelFormat.BULLET,text:"\u2022",alignment:docx.AlignmentType.START,style:{paragraph:{indent:{left:docx.convertInchesToTwip(.5),hanging:docx.convertInchesToTwip(.25)}}}}]},C(1)];return r.forEach(i=>{i!==1&&o.push(C(i))}),{config:o}}exports.COLOR_NAME_TO_HEX=c,exports.DOCX_DPI=f,exports.applyParagraphStyleAttributes=applyParagraphStyleAttributes,exports.calculateEffectiveContentWidth=calculateEffectiveContentWidth,exports.convertBlockquote=convertBlockquote,exports.convertBulletList=convertBulletList,exports.convertCodeBlock=convertCodeBlock,exports.convertColorToHex=T,exports.convertCssLengthToPixels=k,exports.convertDetailsSummary=convertDetailsSummary,exports.convertDocument=convertDocument,exports.convertEmuStringToPixels=m,exports.convertEmuToPixels=s$1,exports.convertHardBreak=convertHardBreak,exports.convertHeading=convertHeading,exports.convertHorizontalRule=convertHorizontalRule,exports.convertImage=convertImage,exports.convertList=convertList,exports.convertListItem=convertListItem,exports.convertMeasureToInches=u$1,exports.convertMeasureToPixels=E,exports.convertNode=convertNode,exports.convertOrderedList=convertOrderedList,exports.convertParagraph=convertParagraph,exports.convertPixelsToEmu=h$1,exports.convertPixelsToTwip=p,exports.convertTable=convertTable,exports.convertTableCell=convertTableCell,exports.convertTableHeader=convertTableHeader,exports.convertTableRow=convertTableRow,exports.convertTaskItem=convertTaskItem,exports.convertTaskList=convertTaskList,exports.convertText=convertText,exports.convertTextNodes=convertTextNodes,exports.convertToDocxImageType=convertToDocxImageType,exports.convertTwipToCssString=d,exports.convertTwipToPixels=a$1,exports.createFloatingOptions=createFloatingOptions,exports.findChild=B,exports.findDeepChild=l$1,exports.findDeepChildren=F,exports.generateDOCX=generateDOCX,exports.getImageDataAndMeta=getImageDataAndMeta,exports.getImageHeight=getImageHeight,exports.getImageTypeFromSrc=getImageTypeFromSrc,exports.getImageWidth=getImageWidth,exports.normalizeHexColor=x,exports.parseTwipAttr=D;