@docen/export-docx 0.0.0
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/LICENSE +21 -0
- package/README.md +270 -0
- package/dist/docx.cjs +1 -0
- package/dist/docx.d.cts +1 -0
- package/dist/docx.d.mts +1 -0
- package/dist/docx.d.ts +1 -0
- package/dist/docx.mjs +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +6798 -0
- package/dist/index.d.mts +6798 -0
- package/dist/index.d.ts +6798 -0
- package/dist/index.mjs +1 -0
- package/package.json +64 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ExternalHyperlink as ct,TextRun as v,Paragraph as i,HeadingLevel as k,ImageRun as N,TableCell as z,TableRow as lt,Table as pt,BorderStyle as ut,TableOfContents as dt,Document as ht,Packer as ft,convertInchesToTwip as F,AlignmentType as j,LevelFormat as L}from"docx";import{imageMeta as R}from"image-meta";import{ofetch as gt}from"ofetch";function G(t){return t?t.startsWith("#")?t:{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"}[t.toLowerCase()]||t:void 0}function A(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"),a=t.marks?.some(s=>s.type==="strike"),o=t.marks?.some(s=>s.type==="code"),c=t.marks?.some(s=>s.type==="subscript"),u=t.marks?.some(s=>s.type==="superscript"),y=t.marks?.find(s=>s.type==="link"),d=t.marks?.find(s=>s.type==="textStyle"),l=t.marks?.some(s=>s.type==="highlight"),f=G(d?.attrs?.color),h=G(d?.attrs?.backgroundColor);let g;if(d?.attrs?.fontSize){const s=d.attrs.fontSize;if(s.endsWith("px")){const T=parseFloat(s);isNaN(T)||(g=Math.round(T*1.5))}}let p;o?p="Consolas":d?.attrs?.fontFamily&&(p=d.attrs.fontFamily);const b={text:t.text||"",bold:e||void 0,italics:n||void 0,underline:r?{}:void 0,strike:a||void 0,font:p,size:g,subScript:c||void 0,superScript:u||void 0,color:f,shading:h?{fill:h}:void 0,highlight:l?"yellow":void 0};return y?.attrs?.href?new ct({children:[new v({...b,style:"Hyperlink"})],link:y.attrs.href}):new v(b)}function w(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 x(t,e){const n={children:t.content?.flatMap(r=>r.type==="text"?A(r):r.type==="hardBreak"?w(r.marks):[])||[],...e};return new i(n)}function P(t){const e=t?.attrs?.level,n=t.content?.flatMap(a=>a.type==="text"?A(a):a.type==="hardBreak"?w(a.marks):[])||[],r={1:k.HEADING_1,2:k.HEADING_2,3:k.HEADING_3,4:k.HEADING_4,5:k.HEADING_5,6:k.HEADING_6};return new i({children:n,heading:r[e]})}function W(t){return t.content?t.content.map(e=>{if(e.type==="paragraph"){const n=e.content?.flatMap(r=>r.type==="text"?A(r):r.type==="hardBreak"?w(r.marks):[])||[];return new i({children:n,indent:{left:720},border:{left:{style:"single"}}})}return new i({})}):[]}function B(t){if(t.startsWith("data:")){const e=t.match(/data:image\/(\w+);/);if(e)switch(e[1].toLowerCase()){case"jpg":case"jpeg":return"jpeg";case"png":return"png";case"gif":return"gif";case"bmp":return"bmp";case"tiff":return"tiff";default:return"png"}}else switch(t.split(".").pop()?.toLowerCase()){case"jpg":case"jpeg":return"jpeg";case"png":return"png";case"gif":return"gif";case"bmp":return"bmp";case"tiff":return"tiff";default:return"png"}return"png"}function mt(){return{horizontalPosition:{relative:"page",align:"center"},verticalPosition:{relative:"page",align:"top"},lockAnchor:!0,behindDocument:!1,inFrontOfText:!1}}function _(t,e,n){return t.attrs?.width?t.attrs.width:e?.run?.transformation?.width?e.run.transformation.width:n?.width?Math.min(n.width,600):400}function U(t,e,n,r){return t.attrs?.height?t.attrs.height:n?.run?.transformation?.height?n.run.transformation.height:r?.width&&r?.height?Math.round(e*r.height/r.width):300}async function $(t){try{const e=await(await gt(t,{responseType:"blob"})).bytes();let n;try{n=R(e)}catch(r){console.warn("Failed to extract image metadata:",r),n={width:void 0,height:void 0,type:B(t)||"png",orientation:void 0}}return{data:e,meta:n}}catch(e){throw console.warn(`Failed to fetch image from ${t}:`,e),e}}async function q(t,e){const n=l=>{switch(l){case"jpeg":case"jpg":return"jpg";case"png":return"png";case"gif":return"gif";case"bmp":return"bmp"}switch(B(t.attrs?.src||"")){case"jpeg":return"jpg";case"png":return"png";case"gif":return"gif";case"bmp":return"bmp";default:return"png"}};let r,a;try{const l=t.attrs?.src||"";if(l.startsWith("http")){const f=await $(l);r=f.data,a=f.meta}else if(l.startsWith("data:")){const f=l.split(",")[1],h=atob(f),g=new Uint8Array(h.length);for(let p=0;p<h.length;p++)g[p]=h.charCodeAt(p);r=g;try{a=R(r)}catch{a={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 i({children:[new N({type:"png",data:new Uint8Array(0),transformation:{width:100,height:100},altText:{name:t.attrs?.alt||"Failed to load image"}})]})}const o=_(t,e,a),c=U(t,o,e,a),u={type:n(a.type),data:r,transformation:{width:o,height:c},altText:{name:t.attrs?.alt||"",description:void 0,title:t.attrs?.title||void 0},...e?.run&&e.run.floating&&{floating:e.run.floating},...e?.run&&e.run.outline&&{outline:e.run.outline}},y=new N(u),d=e?.paragraph||{};return new i({children:[y],...d})}function Q(t,e){const n={children:t.content?.map(r=>x(r,e?.cell?.paragraph??e?.row?.paragraph??e?.paragraph))||[],...e?.cell?.run};return 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&&t.attrs.colwidth.length>0&&(n.width={size:t.attrs.colwidth[0],type:"dxa"}),new z(n)}function X(t,e){const n={children:t.content?.map(r=>x(r,e?.header?.paragraph??e?.cell?.paragraph??e?.row?.paragraph??e?.paragraph))||[],...e?.header?.run};return 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&&t.attrs.colwidth.length>0&&(n.width={size:t.attrs.colwidth[0],type:"dxa"}),new z(n)}function J(t,e){const n=e?.row,r=t.content?.flatMap(a=>a.type==="tableCell"?Q(a,e):a.type==="tableHeader"?X(a,e):[])||[];return new lt({children:r,...n})}function K(t,e){const n={rows:t.content?.map(r=>J(r,e))||[],...e?.run};return[new pt(n),new i({})]}function V(t){const e=t.content?.map(n=>n.text||"").join("")||"";return new i({children:[new v({text:e,font:"Consolas"})]})}function H(t,e){if(!t.content||t.content.length===0)return new i({});const n=t.content[0];return n.type==="paragraph"?x(n,e):new i({})}function Y(){return{numbering:{reference:"bullet-list",level:0}}}function Z(t){const e=t.attrs?.start||1;return{numbering:{reference:"ordered-list",level:0},start:e}}function D(t,e){if(!t.content)return[];const n=[],r=e==="bullet"?Y():Z(t);let a=r.numbering.reference;e==="ordered"&&r.start&&r.start!==1&&(a=`ordered-list-start-${r.start}`);for(const o of t.content)if(o.type==="listItem"){const c=H(o,{numbering:{reference:a,level:0}});n.push(c)}return n}function E(t){if(!t.content||t.content.length===0)return new i({});const e=t.content[0];if(e.type==="paragraph"){const n=t.attrs?.checked?"\u2611 ":"\u2610 ",r=e.content?.flatMap(o=>o.type==="text"?A(o):o.type==="hardBreak"?w(o.marks):[])||[],a=new v({text:n});return new i({children:[a,...r]})}return new i({})}function tt(t){return!t.content||t.content.length===0?[]:t.content.filter(e=>e.type==="taskItem").map(e=>E(e))}function et(t,e){return new i({children:[],border:{bottom:{style:ut.SINGLE,size:1,color:"auto"}},...e?.paragraph})}async function nt(t,e){if(!t.content)return[];const n=[];let r,a;for(const o of t.content)o.type==="detailsSummary"?r=o:o.type==="detailsContent"&&(a=o);if(r?.content){const o=r.content.flatMap(u=>u.type==="text"?A(u):u.type==="hardBreak"?w(u.marks):[]),c=new i({children:o,...e.details?.summary?.paragraph});n.push(c)}if(a?.content)for(const o of a.content){const c=await M(o,e);Array.isArray(c)?n.push(...c):c&&n.push(c)}return n}async function wt(t,e){const{title:n,subject:r,creator:a,keywords:o,description:c,lastModifiedBy:u,revision:y,styles:d,tableOfContents:l,sections:f,fonts:h,hyphenation:g,compatibility:p,customProperties:b,evenAndOddHeaderAndFooters:s,defaultTabStop:T,outputType:at}=e,S=await rt(t,e),C=l?new dt(l.title,{...l.run}):null,ot=yt(t),m={sections:f?f.map((it,O)=>{const I=[];return O===0&&C&&I.push(C),O===0&&I.push(...S),{...it,...I.length>0?{children:I}:{}}}):[{children:C?[C,...S]:S}],title:n||"Document",subject:r||"",creator:a||"",keywords:o||"",description:c||"",lastModifiedBy:u||"",revision:y||1,styles:d,numbering:ot};h&&h.length>0&&Object.assign(m,{fonts:h}),g&&Object.assign(m,{hyphenation:g}),p&&Object.assign(m,{compatibility:p}),b&&b.length>0&&Object.assign(m,{customProperties:b}),s!==void 0&&Object.assign(m,{evenAndOddHeaderAndFooters:s}),T!==void 0&&Object.assign(m,{defaultTabStop:T});const st=new ht(m);return ft.pack(st,at||"arraybuffer")}async function rt(t,e){const n=[];if(!t||!Array.isArray(t.content))return n;for(const r of t.content){const a=await M(r,e);Array.isArray(a)?n.push(...a):a&&n.push(a)}return n}async function M(t,e){if(!t||!t.type)return null;switch(t.type){case"paragraph":return x(t);case"heading":return P(t);case"blockquote":return W(t);case"codeBlock":return V(t);case"image":return await q(t,e.image);case"table":return K(t,e.table);case"bulletList":return D(t,"bullet");case"orderedList":return D(t,"ordered");case"taskList":return tt(t);case"listItem":return H(t);case"taskItem":return E(t);case"hardBreak":return new i({children:[w()]});case"horizontalRule":return et(t,e.horizontalRule);case"details":return await nt(t,e);default:return new i({children:[new v({text:`[Unsupported: ${t.type}]`})]})}}function yt(t){const e=new Set;function n(o){if(o.type==="orderedList"&&o.attrs?.start&&e.add(o.attrs.start),o.content)for(const c of o.content)n(c)}n(t);const r=[{level:0,format:L.BULLET,text:"\u2022",alignment:j.START,style:{paragraph:{indent:{left:F(.5),hanging:F(.25)}}}},{level:0,format:L.DECIMAL,text:"%1.",alignment:j.START,style:{paragraph:{indent:{left:F(.5),hanging:F(.25)}}}}],a=[{reference:"bullet-list",levels:[r[0]]},{reference:"ordered-list",levels:[r[1]]}];for(const o of e)o!==1&&a.push({reference:`ordered-list-start-${o}`,levels:[{level:0,format:L.DECIMAL,text:"%1.",alignment:j.START,start:o,style:{paragraph:{indent:{left:F(.5),hanging:F(.25)}}}}]});return{config:a}}export{W as convertBlockquote,Y as convertBulletList,V as convertCodeBlock,nt as convertDetails,rt as convertDocumentContent,w as convertHardBreak,P as convertHeading,et as convertHorizontalRule,q as convertImage,D as convertList,H as convertListItem,M as convertNode,Z as convertOrderedList,x as convertParagraph,K as convertTable,Q as convertTableCell,X as convertTableHeader,J as convertTableRow,E as convertTaskItem,tt as convertTaskList,A as convertText,mt as createFloatingOptions,wt as generateDOCX,$ as getImageDataAndMeta,U as getImageHeight,B as getImageTypeFromSrc,_ as getImageWidth};
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@docen/export-docx",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "A powerful TipTap/ProseMirror extension that converts editor content to Microsoft Word DOCX format",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"converter",
|
|
7
|
+
"docen",
|
|
8
|
+
"document",
|
|
9
|
+
"docx",
|
|
10
|
+
"editor",
|
|
11
|
+
"export",
|
|
12
|
+
"microsoft-word",
|
|
13
|
+
"office",
|
|
14
|
+
"prosemirror",
|
|
15
|
+
"rich-text",
|
|
16
|
+
"tiptap",
|
|
17
|
+
"typescript",
|
|
18
|
+
"word",
|
|
19
|
+
"wysiwyg"
|
|
20
|
+
],
|
|
21
|
+
"homepage": "https://github.com/DemoMacro/docen#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/DemoMacro/docen/issues"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": {
|
|
27
|
+
"name": "Demo Macro",
|
|
28
|
+
"email": "abc@imst.xyz",
|
|
29
|
+
"url": "https://imst.xyz/"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/DemoMacro/docen.git"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"main": "dist/index.mjs",
|
|
39
|
+
"types": "dist/index.d.ts",
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"import": "./dist/index.mjs",
|
|
43
|
+
"require": "./dist/index.cjs"
|
|
44
|
+
},
|
|
45
|
+
"./docx": {
|
|
46
|
+
"types": "./dist/docx.d.ts",
|
|
47
|
+
"import": "./dist/docx.mjs",
|
|
48
|
+
"require": "./dist/docx.cjs"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"docx": "9.5.1",
|
|
53
|
+
"image-meta": "0.2.2",
|
|
54
|
+
"ofetch": "1.4.1"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@tiptap/core": "3.7.2",
|
|
58
|
+
"@docen/tiptap-extensions": "0.0.0"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"dev": "unbuild --stub",
|
|
62
|
+
"build": "unbuild"
|
|
63
|
+
}
|
|
64
|
+
}
|