@docen/import-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 +230 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +6492 -0
- package/dist/index.d.mts +6492 -0
- package/dist/index.d.ts +6492 -0
- package/dist/index.mjs +1 -0
- package/package.json +60 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{fromXml as g}from"xast-util-from-xml";import{unzipSync as H}from"fflate";import{toBase64 as W,toUint8Array as X}from"undio";function P(o,n,t){const r=[];for(const e of n.children)if(e.type==="element"){if(e.name==="w:hyperlink"){const s=e,i=s.attributes["r:id"],a=o.get(i);if(a){for(const c of s.children)if(c.type==="element"&&c.name==="w:r"){const f=c,u=h(f,"w:drawing");if(u){const w=$(u,t);w&&r.push(w);continue}const l=h(f,"w:t");if(!l)continue;const p=l.children.find(w=>w.type==="text");if(!p||!p.value)continue;const m=k(f);m.push({type:"link",attrs:{href:a}});const d={type:"text",text:p.value};m.length>0&&(d.marks=m),r.push(d)}}continue}if(e.name==="w:r"){const s=e,i=h(s,"w:drawing");if(i){const l=$(i,t);l&&r.push(l);continue}if(h(s,"w:br")){const l=k(s),p={type:"hardBreak"};l.length>0&&(p.marks=l),r.push(p)}const a=h(s,"w:t");if(!a)continue;const c=a.children.find(l=>l.type==="text");if(!c||!c.value)continue;const f=k(s),u={type:"text",text:c.value};f.length>0&&(u.marks=f),r.push(u)}}return r}function k(o){const n=[],t=h(o,"w:rPr");if(!t)return n;h(t,"w:b")&&n.push({type:"bold"}),h(t,"w:i")&&n.push({type:"italic"}),h(t,"w:u")&&n.push({type:"underline"}),h(t,"w:strike")&&n.push({type:"strike"}),h(t,"w:highlight")&&n.push({type:"highlight"});const r=h(t,"w:vertAlign");if(r){const c=r.attributes["w:val"];c==="subscript"?n.push({type:"subscript"}):c==="superscript"&&n.push({type:"superscript"})}const e=h(t,"w:color"),s=h(t,"w:shd"),i=h(t,"w:sz"),a=h(t,"w:rFonts");if(e||s||i||a){const c={color:"",backgroundColor:"",fontSize:"",fontFamily:"",lineHeight:""};if(e&&e.attributes["w:val"]){const f=e.attributes["w:val"];if(f!=="auto"){const u=f.startsWith("#")?f:`#${f}`;c.color=u}}if(s&&s.attributes["w:fill"]){const f=s.attributes["w:fill"];if(f!=="auto"){const u=f.startsWith("#")?f:`#${f}`;c.backgroundColor=u}}if(i&&i.attributes["w:val"]){const f=i.attributes["w:val"],u=parseFloat(f);if(!isNaN(u)){const l=Math.round(u/1.5*10)/10;c.fontSize=`${l}px`}}a&&a.attributes["w:ascii"]&&(c.fontFamily=a.attributes["w:ascii"]),n.push({type:"textStyle",attrs:c})}return n}function B(o){const n=h(o,"w:pPr");if(!n)return;const t=h(n,"w:jc");if(!t?.attributes["w:val"])return;const r=t.attributes["w:val"],e={left:"left",right:"right",center:"center",both:"justify"}[r];return e?{textAlign:e}:void 0}function $(o,n){const t=A(o,"a:blip");if(!t?.attributes["r:embed"])return null;const r=t.attributes["r:embed"],e=n.get(r);return e?{type:"image",attrs:{src:e,alt:""}}:null}function h(o,n){for(const t of o.children)if(t.type==="element"&&t.name===n)return t}function A(o,n){for(const t of o.children)if(t.type==="element"&&t.name===n)return t;for(const t of o.children)if(t.type==="element"){const r=A(t,n);if(r)return r}}function v(o,n,t){let r;for(const i of o.children)if(i.type==="element"&&i.name==="w:pPr"){const a=i;for(const c of a.children)if(c.type==="element"&&c.name==="w:pStyle"){r=c.attributes["w:val"];break}break}if(r){const i=r.match(/^Heading(\d)$/);if(i){const a=parseInt(i[1]);return E(o,n,a,t)}}const e=P(n,o,t);if(e.length===1&&e[0].type==="hardBreak"){for(const i of o.children)if(i.type==="element"&&i.name==="w:r"){for(const a of i.children)if(a.type==="element"&&a.name==="w:br"&&a.attributes["w:type"]==="page")return{type:"horizontalRule"}}}if(e.length===1&&e[0].type==="image")return e[0];const s=B(o);return{type:"paragraph",...s&&{attrs:s},content:e}}function E(o,n,t,r){return{type:"heading",attrs:{level:t},content:P(n,o,r)}}function b(o){const n=y(o,"w:pPr");return n?!!y(n,"w:numPr"):!1}function C(o){const n=y(o,"w:pPr");if(!n)return null;const t=y(n,"w:numPr");if(!t)return null;const r=y(t,"w:ilvl"),e=y(t,"w:numId");return!r||!e?null:{numId:e.attributes["w:val"],level:parseInt(r.attributes["w:val"]||"0")}}function y(o,n){for(const t of o.children)if(t.type==="element"&&t.name===n)return t}function T(o){const n=x(o,"w:pPr");if(!n)return!1;const t=x(n,"w:pStyle");if(!t)return!1;const r=t.attributes["w:val"];return r==="CodeBlock"||r?.startsWith("Code")}function R(o){const n=x(o,"w:pPr");if(!n)return;const t=x(n,"w:pStyle");if(!t)return;const r=t.attributes["w:val"];if(r?.startsWith("CodeBlock"))return r.replace("CodeBlock","").toLowerCase()||void 0}function x(o,n){for(const t of o.children)if(t.type==="element"&&t.name===n)return t}function O(o){return o.name==="w:tbl"}function S(o,n,t){const r=[],e=[];for(const i of o.children)i.type==="element"&&i.name==="w:tr"&&e.push(i);const s=new Map;return e.forEach((i,a)=>{r.push(_(i,a===0,n,t,s,e,a))}),{type:"table",content:r}}function _(o,n,t,r,e,s,i){const a=[];let c=0;for(const f of o.children)if(f.type==="element"&&f.name==="w:tc"){const u=e.get(c);if(u&&u>0){e.set(c,u-1),c++;continue}let l=z(f);if(l&&l.rowspan===1){const d=U(s,i,c);d>1&&(l={...l,rowspan:d})}if(l&&l.rowspan>1&&e.set(c,l.rowspan-1),l&&l.rowspan===0){c++;continue}const p="tableCell",m=J(f,t,r);a.push({type:p,...l&&{attrs:l},content:[m]}),c+=l?.colspan||1}return{type:"tableRow",content:a}}function z(o){const n={colspan:1,rowspan:1,colwidth:null};let t;for(const r of o.children)if(r.type==="element"&&r.name==="w:tcPr"){t=r;break}if(!t)return n;for(const r of t.children)if(r.type==="element"&&r.name==="w:gridSpan"){const e=r.attributes["w:val"];e&&(n.colspan=parseInt(e));break}for(const r of t.children)if(r.type==="element"&&r.name==="w:vMerge"){r.attributes["w:val"]==="continue"&&(n.rowspan=0);break}for(const r of t.children)if(r.type==="element"&&r.name==="w:tcW"){const e=r.attributes["w:w"];e&&(n.colwidth=parseInt(e));break}return n}function U(o,n,t){let r=1,e=t;for(let s=n+1;s<o.length;s++){const i=o[s];let a=!1;for(const c of i.children)if(c.type==="element"&&c.name==="w:tc"){const f=z(c),u=f?.colspan||1;if(e>=0&&e<u){if(f?.rowspan===0)r++,a=!0;else return r;break}e-=u}if(!a)break}return r}function J(o,n,t){const r=[];for(const e of o.children)if(e.type==="element"&&e.name==="w:p"){const s=v(e,n,t);r.push(s)}return r[0]||{type:"paragraph",content:[]}}function I(o){for(const n of o.children)if(n.type==="element"&&n.name==="w:r"){for(const t of n.children)if(t.type==="element"&&t.name==="w:t"){const r=t.children.find(e=>e.type==="text");if(r&&"value"in r){const e=r.value;return e.startsWith("\u2610")||e.startsWith("\u2611")}}break}return!1}function D(o){for(const n of o.children)if(n.type==="element"&&n.name==="w:r"){for(const t of n.children)if(t.type==="element"&&t.name==="w:t"){const r=t.children.find(e=>e.type==="text");if(r&&"value"in r)return r.value.startsWith("\u2611")}break}return!1}function L(o){const n=D(o),t=q(o);return{type:"taskItem",attrs:{checked:n},content:[t]}}function q(o){const n=[];let t=!1;for(const e of o.children)if(e.type==="element"&&e.name==="w:r"){let s=!1;if(!t){for(const i of e.children)if(i.type==="element"&&i.name==="w:t"){const a=i.children.find(c=>c.type==="text");if(a&&"value"in a){const c=a.value;if(c.startsWith("\u2610")||c.startsWith("\u2611")){s=!0,t=!0;const f=c.substring(2).trimStart();f.length>0&&n.push({type:"text",text:f})}}}}if(!s){const i=G(e);for(const a of e.children)if(a.type==="element"&&a.name==="w:t"){const c=a.children.find(f=>f.type==="text");if(c&&"value"in c){const f={type:"text",text:c.value};i.length>0&&(f.marks=i),n.push(f)}}}}const r=K(o);return{type:"paragraph",...r&&{attrs:r},content:n.length>0?n:void 0}}function G(o){const n=[];for(const t of o.children)if(t.type==="element"&&t.name==="w:rPr"){const r=t;for(const e of r.children)if(e.type==="element"&&e.name==="w:b"){n.push({type:"bold"});break}for(const e of r.children)if(e.type==="element"&&e.name==="w:i"){n.push({type:"italic"});break}for(const e of r.children)if(e.type==="element"&&e.name==="w:u"){n.push({type:"underline"});break}for(const e of r.children)if(e.type==="element"&&e.name==="w:strike"){n.push({type:"strike"});break}break}return n}function K(o){for(const n of o.children)if(n.type==="element"&&n.name==="w:pPr"){const t=n;for(const r of t.children)if(r.type==="element"&&r.name==="w:jc"){const e=r.attributes["w:val"];if(e==="both")return{textAlign:"justify"};if(e==="center")return{textAlign:"center"};if(e==="right")return{textAlign:"right"};if(e==="left")return{textAlign:"left"}}}}function F(o){for(const n of o.children)if(n.type==="element"&&n.name==="w:r"){const t=n;let r=!1,e=!1;for(const s of t.children)if(s.type==="element")if(s.name==="w:br")s.attributes["w:type"]==="page"&&(r=!0);else if(s.name==="w:t"){const i=s.children.find(a=>a.type==="text");i&&"value"in i&&i.value&&i.value.trim().length>0&&(e=!0)}else s.name!=="w:rPr"&&(e=!0);if(r&&!e)return!0}return!1}const j=async o=>({src:W(o.data)});async function Q(o,n={}){const{convertImage:t=j,ignoreEmptyParagraphs:r=!1}=n,e=await X(o),s=H(e),i=Z(s),a=Y(s),c=new Map;for(const[p,m]of a.entries())try{const d=`image/${Object.keys(s).find(M=>M.endsWith(p)||M.includes(`media/${p}`))?.split(".").pop()?.toLowerCase()||"png"}`,w=await t({id:p,contentType:d,data:m});c.set(p,w.src)}catch(d){console.warn(`Failed to convert image ${p}:`,d);const w=W(m);c.set(p,w)}const f=s["word/document.xml"];if(!f)throw new Error("Invalid DOCX file: missing word/document.xml");const u=g(new TextDecoder().decode(f)),l=V(s);return tt(u,c,i,l,r)}function V(o){const n=new Map,t=new Map,r=o["word/numbering.xml"];if(!r)return n;const e=g(new TextDecoder().decode(r)),s=new Map;if(e.type==="root"){for(const i of e.children)if(i.type==="element"&&i.name==="w:numbering"){const a=i;for(const c of a.children)if(c.type==="element"&&c.name==="w:abstractNum"){const f=c,u=f.attributes["w:abstractNumId"];for(const l of f.children)if(l.type==="element"&&l.name==="w:lvl"){for(const p of l.children)if(p.type==="element"&&p.name==="w:numFmt"){const m=p.attributes["w:val"];if(m){s.set(u,m);break}}for(const p of l.children)if(p.type==="element"&&p.name==="w:start"){const m=p.attributes["w:val"];m&&t.set(u,parseInt(m,10));break}break}}for(const c of a.children)if(c.type==="element"&&c.name==="w:num"){const f=c,u=f.attributes["w:numId"];for(const l of f.children)if(l.type==="element"&&l.name==="w:abstractNumId"){const p=l.attributes["w:val"],m=s.get(p);if(m){const d=t.get(p);m==="bullet"?n.set(u,{type:"bullet"}):n.set(u,{type:"ordered",...d!==void 0&&{start:d}})}break}}break}}return n}function Y(o){const n=new Map,t=o["word/_rels/document.xml.rels"];if(!t)return n;const r=g(new TextDecoder().decode(t));if(r.type==="root"){for(const e of r.children)if(e.type==="element"&&e.name==="Relationships"){const s=e;for(const i of s.children)if(i.type==="element"&&i.name==="Relationship"){const a=i,c=a.attributes.Type;if(c&&c==="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"){const f=a.attributes.Id,u=a.attributes.Target;if(f&&u){const l="word/"+u,p=o[l];p&&n.set(f,p)}}}break}}return n}function Z(o){const n=new Map,t=o["word/_rels/document.xml.rels"];if(!t)return n;const r=g(new TextDecoder().decode(t));if(r.type==="root"){for(const e of r.children)if(e.type==="element"&&e.name==="Relationships"){const s=e;for(const i of s.children)if(i.type==="element"&&i.name==="Relationship"){const a=i,c=a.attributes.Type;if(c&&c==="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"){const f=a.attributes.Id,u=a.attributes.Target;f&&u&&n.set(f,u)}}break}}return n}function tt(o,n,t,r,e){if(o.type!=="root")return{type:"doc",content:[]};for(const s of o.children)if(s.type==="element"&&s.name==="w:document"){const i=s;for(const a of i.children)if(a.type==="element"&&a.name==="w:body")return{type:"doc",content:et(a.children.filter(c=>c.type==="element"),n,t,r,e)};break}return{type:"doc",content:[]}}function et(o,n,t,r,e){const s=[];let i=0;for(;i<o.length;){const a=o[i];if(a.name==="w:tbl"){s.push(S(a,t,n)),i++,i<o.length&&o[i].name==="w:p"&&N(o[i])&&i++;continue}if(a.name==="w:p"){if(e&&N(a)){i++;continue}if(T(a)){const c=nt(o,i);s.push(...c),i+=c.length;continue}if(I(a)){const c=it(o,i);s.push(...c),i+=st(o,i);continue}if(b(a)){const c=rt(o,i,n,t,r);s.push(...c),i+=ot(o,i);continue}if(F(a)){s.push({type:"horizontalRule"}),i++;continue}s.push(v(a,t,n)),i++;continue}i++}return s}function nt(o,n){const t=[];let r=n;for(;r<o.length;){const e=o[r];if(e.name!=="w:p"||!T(e))break;const s=R(e),i={type:"codeBlock",...s&&{attrs:{language:s}},content:ct(e)};t.push(i),r++}return t}function rt(o,n,t,r,e){const s=[];let i=n;for(;i<o.length;){const a=o[i];if(a.name!=="w:p"||!b(a))break;const c=C(a);if(!c)break;const f=e.get(c.numId),u=f?.type||"bullet",l=[];for(;i<o.length;){const m=o[i];if(m.name!=="w:p"||!b(m))break;const d=C(m);if(!d||d.numId!==c.numId)break;const w={type:"listItem",content:[v(m,r,t)]};l.push(w),i++}const p={type:u==="bullet"?"bulletList":"orderedList",content:l};u==="ordered"&&(p.attrs={type:null,...f?.start!==void 0&&{start:f.start}}),s.push(p)}return s}function ot(o,n){let t=0,r=n;for(;r<o.length;){const e=o[r];if(e.name!=="w:p"||!b(e))break;t++,r++}return t}function it(o,n){const t=[];let r=n;for(;r<o.length;){const e=o[r];if(e.name!=="w:p"||!I(e))break;const s=L(e);t.push(s),r++}return[{type:"taskList",content:t}]}function st(o,n){let t=0,r=n;for(;r<o.length;){const e=o[r];if(e.name!=="w:p"||!I(e))break;t++,r++}return t}function ct(o){const n=[];for(const t of o.children){if(t.type!=="element"||t.name!=="w:r")continue;const r=t;for(const e of r.children)if(e.type==="element"&&e.name==="w:t"){const s=e.children.find(i=>i.type==="text");s&&"value"in s&&n.push({type:"text",text:s.value})}}return n}function N(o){for(const n of o.children){if(n.type!=="element"||n.name!=="w:r")continue;const t=n;for(const r of t.children)if(r.type==="element"&&r.name==="w:t"){const e=r.children.find(s=>s.type==="text");if(e&&"value"in e&&e.value.trim().length>0)return!1}}return!0}export{v as convertParagraph,S as convertTable,L as convertTaskItem,j as defaultImageConverter,B as extractAlignment,k as extractMarks,P as extractRuns,R as getCodeBlockLanguage,C as getListInfo,D as getTaskItemChecked,T as isCodeBlock,F as isHorizontalRule,b as isListItem,O as isTable,I as isTaskItem,Q as parseDOCX};
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@docen/import-docx",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "A powerful TipTap/ProseMirror extension that imports Microsoft Word DOCX files to editor content",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"converter",
|
|
7
|
+
"docen",
|
|
8
|
+
"document",
|
|
9
|
+
"docx",
|
|
10
|
+
"editor",
|
|
11
|
+
"import",
|
|
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
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"fflate": "0.8.2",
|
|
48
|
+
"undio": "0.2.0",
|
|
49
|
+
"xast-util-from-xml": "4.0.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@tiptap/core": "3.7.2",
|
|
53
|
+
"@types/xast": "2.0.4",
|
|
54
|
+
"@docen/tiptap-extensions": "0.0.0"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"dev": "unbuild --stub",
|
|
58
|
+
"build": "unbuild"
|
|
59
|
+
}
|
|
60
|
+
}
|