@mdgate/hwp 0.5.1 → 0.5.2

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.
Files changed (2) hide show
  1. package/dist/index.js +2 -2
  2. package/package.json +5 -5
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{CompoundFile as Ln,detectOleDoc as Fn,detectZipDoc as Rn,hasOleMagic as _,Package as In}from"@mdgate/containers";import{ConvertError as p}from"@mdgate/core";import{documentToMarkdown as kn}from"@mdgate/document";import{fileExtension as Xn}from"@mdgate/utils";import{hasOleMagic as Bn}from"@mdgate/containers";import{ConvertError as Nn}from"@mdgate/core";import{CompoundFile as Z,hasOleMagic as z}from"@mdgate/containers";import{ConvertError as m}from"@mdgate/core";import{emptyDocument as V,plain as J}from"@mdgate/document";import{cleanText as C,collapseWs as D,inflateZlib as Q,isAlphanumeric as Y}from"@mdgate/utils";var x="HWP Document File",S=2,j=16,q=j+51,K=new Set([1,2,3,4,5,6,7,8,11,12,13,14,15,16,17,21,22,23,26,27,28,29,30]);function d(n){if(n.length<x.length)return!1;for(let e=0;e<x.length;e+=1)if(n[e]!==x.charCodeAt(e))return!1;return!0}function B(n){if(z(n))return y(n);if(d(n))return N(fn(n));throw m.malformed("not an HWP document")}function y(n){let e;try{e=Z.open(n)}catch(i){if(i instanceof m)throw i;throw m.malformed("not an OLE2 compound file")}if(e.exists("EncryptionInfo")||e.exists("EncryptedPackage"))throw m.encrypted();let t=A(e,"FileHeader"),r=t!==void 0?b(t):void 0;if(r?.encrypted||r?.distribution)throw m.encrypted();let o=[],f=!1;for(let i=0;;i+=1){let c=`BodyText/Section${i}`;if(!e.exists(c)){if(f||i>0)break;continue}f=!0;let s=L(e,c);if(s===void 0)continue;let u=rn(s,r?.compressed);nn(u,o)}if(o.length===0){let i=A(e,"PrvText");if(i!==void 0)cn(i,o)}if(o.length===0)for(let i of e.readRootStorage()){if(i.type!==2)continue;let c=A(e,i.name);if(c!==void 0)g(c,o)}return N(o)}function b(n){if(n.length<40)return;if(!d(n))return;let e=new DataView(n.buffer,n.byteOffset,n.byteLength).getUint32(36,!0);return{compressed:(e&1)!==0,encrypted:(e&2)!==0,distribution:(e&4)!==0}}function nn(n,e){let t=en(n);if(t.length>0){for(let r of t)e.push(r);return}g(n,e)}function en(n){if(n.length<4)return[];let e=new DataView(n.buffer,n.byteOffset,n.byteLength),t=[],r=0,o=0,f=0;while(r+4<=n.length){let i=e.getUint32(r,!0),c=i&1023,s=i>>>20&4095,u=s,l=r+4;if(s===4095){if(l+4>n.length)break;u=e.getUint32(l,!0),l+=4}if(u<0||l+u>n.length)break;if(o+=1,f=l+u,c===q){let a=tn(n.subarray(l,l+u));if(a.length>0)t.push(a)}r=l+u}if(t.length===0&&(o===0||f<n.length/2))return[];return t}function tn(n){let e=n.length&-2,t="",r=0;while(r+1<e){let o=n[r]|n[r+1]<<8;if(r+=2,o<32){if(K.has(o))r+=14;else if(o===9)t+="\t";else if(o===10)t+=`
2
- `;else if(o===24||o===25)t+="-";else if(o===31)t+=" ";continue}if(o===65534||o===65535)continue;t+=String.fromCharCode(o)}return t}function rn(n,e){if(n.length>=2&&on(n)){let t=H(n);if(t!==void 0)return t}if(e===!1)return n;if(e===!0){let t=H(n);if(t!==void 0)return t}return n}function H(n){try{return Q(n,Number.MAX_SAFE_INTEGER)}catch{return}}function on(n){if(n.length<2)return!1;let e=n[0],t=n[1];if(((e<<8)+t)%31!==0)return!1;return(e&15)===8}function fn(n){let e=n.length>32?32:x.length,t=[];if(g(n.subarray(Math.min(e,n.length)),t),t.length===0)g(n,t);return t}function cn(n,e){let t=n.length&-2,r=new TextDecoder("utf-16le").decode(n.subarray(0,t));for(let o of r.split(/\0+/)){let f=D(C(o)).trim();if(W(f))e.push(f)}}function g(n,e){let t="",r=()=>{if(t.length>=S)e.push(t);t=""},o=n.length&-2;for(let f=0;f<o;f+=2){let i=n[f]|n[f+1]<<8;if(i!==0&&ln(i))t+=String.fromCharCode(i);else r()}r()}function N(n){let e=new Set,t=V();for(let r of n){let o=D(C(r)).trim();if(!W(o)||e.has(o))continue;e.add(o),t.blocks.push({type:"paragraph",inlines:[J(o)]})}if(t.blocks.length===0)throw m.malformed("no readable text in HWP document");return t}function ln(n){if(n===9||n===10||n===13||n===32)return!0;if(n<32||n>=127&&n<=159)return!1;return n!==65534&&n!==65535}function W(n){if(n.length<S)return!1;let e=0;for(let r of n)if(Y(r))e+=1;if(e<2)return!1;let t=n.toLowerCase();if(t.includes("xmlns")||t.includes("http://schemas.")||t.includes("hwp document file"))return!1;return!0}function A(n,e){if(!n.exists(e))return;return L(n,e)}function L(n,e){try{return n.readStream(e)}catch(t){if(t instanceof m&&t.isFatal())throw t;return}}import{Package as un}from"@mdgate/containers";import{ConvertError as h}from"@mdgate/core";import{cellSpanning as an,emptyDocument as dn,GridBuilder as sn,inlinesAreEmpty as mn,plain as w,resolveHeaderRows as hn}from"@mdgate/document";import{cleanText as U}from"@mdgate/utils";function I(n){let e;try{e=un.open(n)}catch(i){throw Sn(i)}let t=e.optionalXmlPart("Contents/header.xml")??e.optionalXmlPart("Contents/Header.xml"),r={styles:t!==void 0?xn(t):new Map},o=pn(e);if(o.length===0)throw h.missingPart("Contents/section0.xml");let f=dn();for(let i of o){let c=e.optionalXmlPart(i);if(c===void 0)continue;P(c,f.blocks,r)}if(f.blocks.length===0)vn(e,f);if(f.blocks.length===0)throw h.malformed("no readable text in HWPX document");return f}function pn(n){let e=[];for(let t of n.partNames())if(/^Contents\/section\d+\.xml$/i.test(t))e.push(t);return e.sort((t,r)=>F(t)-F(r)),e}function F(n){let e=n.match(/section(\d+)/i);return e!==null?Number(e[1]):0}function xn(n){let e=new Map;for(let t of n.descendantsAny("style")){let r=t.attrAny("id");if(r===void 0)continue;let o=gn(t);if(o!==void 0)e.set(r,o)}return e}function gn(n){let e=(n.attrAny("type")??"").toLowerCase(),r=`${n.attrAny("name")??""} ${n.attrAny("engName")??""}`.match(/(?:개요|제목|outline|heading)\s*(\d+)/i);if(r!==null)return wn(Number(r[1]));if(e==="outline"||e==="heading")return 1;return}function wn(n){if(!Number.isFinite(n)||n<1)return 1;return n>6?6:n|0}function P(n,e,t){if(n.local==="tbl"){let r=k(n,t);if(r!==void 0)e.push(r);return}if(n.local==="p"){Pn(n,e,t);return}for(let r of n.childElems())P(r,e,t)}function Pn(n,e,t){let r=An(n,t),o=[],f=!1,i=()=>{if(mn(o)){o=[];return}if(r!==void 0&&!f)e.push({type:"heading",level:r,anchor:void 0,content:o}),f=!0;else e.push({type:"paragraph",inlines:o});o=[]},c=(s)=>{for(let u of s.children){if(u.type==="text"){let a=U(u.text);if(a.length>0)o.push(w(a));continue}let l=u.elem;switch(l.local){case"tbl":{i();let a=k(l,t);if(a!==void 0)e.push(a);break}case"t":{let a=U(l.text());if(a.length>0)o.push(w(a));break}case"lineBreak":case"line-break":o.push({type:"lineBreak"});break;case"tab":o.push(w("\t"));break;default:c(l)}}};c(n),i()}function An(n,e){let t=n.attrAny("styleIDRef")??n.attrAny("styleId")??n.attrAny("styleID");if(t===void 0)return;return e.styles.get(t)}function k(n,e){let t=En(n);if(t.length===0)return;let r=new sn;for(let f of t){r.nextRow();for(let i of Un(f))r.place(an(Tn(i,e),R(i,"colSpan"),R(i,"rowSpan")))}let o=r.finish("data");if(o.grid.length===0)return;return o.headerRows=hn(o,0),{type:"table",table:o}}function En(n){let e=n.childElems().filter((t)=>t.local==="tr");if(e.length>0)return e;return[...n.descendantsAny("tr")]}function Un(n){let e=n.childElems().filter((t)=>t.local==="tc");if(e.length>0)return e;return[...n.descendantsAny("tc")]}function Tn(n,e){let t=[],r=n.childElems().find((o)=>o.local==="subList");if(r!==void 0)return P(r,t,e),t;for(let o of n.childElems())if(o.local==="p"||o.local==="tbl")P(o,t,e);return t}function R(n,e){let t=n.attrAny(e)??n.childElems().find((o)=>o.local==="cellSpan")?.attrAny(e);if(t===void 0||t.length===0)return 1;let r=Number(t);if(!Number.isFinite(r)||r<1)return 1;return r>1000?1000:r|0}function vn(n,e){let t=n.optionalPart("Preview/PrvText.txt")??n.optionalPart("Preview/PrvText.dat")??n.optionalPart("Preview/PrvText");if(t===void 0)return;for(let r of Dn(Hn(t)))e.blocks.push({type:"paragraph",inlines:[w(r)]})}function Hn(n){if(n.length>=2&&n[0]===255&&n[1]===254)return new TextDecoder("utf-16le").decode(n);if(n.length>=2&&n[0]===254&&n[1]===255)return new TextDecoder("utf-16be").decode(n);let e=n.length&-2;if(e>=4&&Cn(n.subarray(0,e)))return new TextDecoder("utf-16le").decode(n.subarray(0,e));return new TextDecoder("utf-8",{fatal:!1}).decode(n)}function Cn(n){let e=0;for(let t=1;t<n.length;t+=2)if(n[t]===0)e+=1;return e*2>=n.length/2}function Dn(n){let e=[];for(let t of n.split(/\0+/)){let r=U(t).trim();if(r.length>0)e.push(r)}return e}function Sn(n){if(n instanceof h){if(n.code==="encrypted"||E(n.message)||E(n.detail??""))return h.encrypted();return n}let e=n instanceof Error?n.message:String(n);if(E(e))return h.encrypted();return h.malformed(`not a readable zip archive: ${e}`)}function E(n){let e=n.toLowerCase();return e.includes("encrypted")||e.includes("password")}function X(n){if(Wn(n))return I(n);if(Bn(n)||d(n))return B(n);throw Nn.malformed("not an HWP document")}function Wn(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}var _n=new Set(["hwp","hwpx","hwt","hwtx"]);function v(){return{id:"hwp",sniff(n,e){if(O(n)==="hwpx"||M(n))return 2;if(G(n)==="hwp"||On(n))return 2;if(d(n))return 2;if(e?.path!==void 0&&_n.has(Xn(e.path)??""))return 1;return 0},convert(n){return Mn(n),{markdown:kn(X(n))}}}}function Mn(n){if($n(n))throw p.unsupported("pdf");if($(n)){let e=O(n);if(e==="hwpx")return;if(e!==void 0)throw p.unsupported(e);if(M(n))return;throw p.unsupported("zip")}if(_(n)){let e=G(n);if(e==="hwp")return;if(e!==void 0)throw p.unsupported(e);return}if(d(n))return;throw p.unsupported("hwp")}function M(n){if(!$(n))return!1;let e;try{e=In.open(n)}catch{return!1}for(let r of e.partNames()){let o=r.toLowerCase();if(o==="contents/content.hpf")return!0;if(/^contents\/section\d+\.xml$/.test(o))return!0}let t=e.optionalPart("mimetype");if(t!==void 0){let o=new TextDecoder("utf-8",{fatal:!1}).decode(t).trim().toLowerCase().replace(/-template$/,"");if(o==="application/hwp+zip"||o==="application/vnd.hancom.hwpx")return!0}return!1}function On(n){if(!_(n))return!1;try{let e=Ln.open(n);for(let t of e.readRootStorage())if(Gn(t.name))return!0}catch{return!1}return!1}function Gn(n){let e=n.charCodeAt(0)===5?n.slice(1):n;return T(n,"FileHeader")||T(e,"HwpSummaryInformation")||T(n,"HWPDocumentInfo")}function T(n,e){return n.length===e.length&&n.toLowerCase()===e.toLowerCase()}function O(n){return Rn(n)}function G(n){return Fn(n)}function $(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}function $n(n){let e=Zn(n);return zn(n,"%PDF-",e)}function Zn(n){let e=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)e=3;while(e<n.length){let t=n[e];if(t!==9&&t!==10&&t!==13&&t!==32)break;e+=1}return e}function zn(n,e,t){if(t+e.length>n.length)return!1;for(let r=0;r<e.length;r+=1)if(n[t+r]!==e.charCodeAt(r))return!1;return!0}import{create as Vn}from"@mdgate/core";var Jn=Vn([v()]);function Qn(n,e){return Jn(n,e)}export{Qn as toMarkdown,v as hwp};
1
+ import{CompoundFile as Kn,detectOleDoc as Qn,detectZipDoc as Yn,hasOleMagic as Z,Package as jn}from"@mdgate/containers";import{ConvertError as p}from"@mdgate/core";import{documentToMarkdown as qn}from"@mdgate/document";import{fileExtension as n0}from"@mdgate/utils";import{hasOleMagic as Zn}from"@mdgate/containers";import{ConvertError as zn}from"@mdgate/core";import{CompoundFile as tn,hasOleMagic as xn}from"@mdgate/containers";import{ConvertError as m}from"@mdgate/core";import{emptyDocument as rn,plain as on}from"@mdgate/document";import{cleanText as R,collapseWs as I,inflateRaw as fn,inflateZlib as cn,isAlphanumeric as an}from"@mdgate/utils";var g=Uint8Array.from([99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22]),Y=Uint8Array.from([82,9,106,213,48,54,165,56,191,64,163,158,129,243,215,251,124,227,57,130,155,47,255,135,52,142,67,68,196,222,233,203,84,123,148,50,166,194,35,61,238,76,149,11,66,250,195,78,8,46,161,102,40,217,36,178,118,91,162,73,109,139,209,37,114,248,246,100,134,104,152,22,212,164,92,204,93,101,182,146,108,112,72,80,253,237,185,218,94,21,70,87,167,141,157,132,144,216,171,0,140,188,211,10,247,228,88,5,184,179,69,6,208,44,30,143,202,63,15,2,193,175,189,3,1,19,138,107,58,145,17,65,79,103,220,234,151,242,207,206,240,180,230,115,150,172,116,34,231,173,53,133,226,249,55,232,28,117,223,110,71,241,26,113,29,41,197,137,111,183,98,14,170,24,190,27,252,86,62,75,198,210,121,32,154,219,192,254,120,205,90,244,31,221,168,51,136,7,199,49,177,18,16,89,39,128,236,95,96,81,127,169,25,181,74,13,45,229,122,159,147,201,156,239,160,224,59,77,174,42,245,176,200,235,187,60,131,83,153,97,23,43,4,126,186,119,214,38,225,105,20,99,85,33,12,125]),j=Uint8Array.from([0,1,2,4,8,16,32,64,128,27,54]);function H(n,e){let t=n.length-n.length%16,r=q(e),x=new Uint8Array(t),o=new Uint8Array(16);for(let i=0;i<t;i+=16)o.set(n.subarray(i,i+16)),nn(o,r),x.set(o,i);return x}function q(n){let e=new Uint8Array(176);e.set(n.subarray(0,16));for(let t=16,r=1;t<176;t+=4){let x=e[t-4],o=e[t-3],i=e[t-2],f=e[t-1];if(t%16===0){let c=x;x=(g[o]^j[r++])&255,o=g[i],i=g[f],f=g[c]}e[t]=e[t-16]^x,e[t+1]=e[t-15]^o,e[t+2]=e[t-14]^i,e[t+3]=e[t-13]^f}return e}function nn(n,e){P(n,e,10);for(let t=9;t>=1;t-=1)D(n),C(n),P(n,e,t),en(n);D(n),C(n),P(n,e,0)}function P(n,e,t){let r=t*16;for(let x=0;x<16;x+=1)n[x]^=e[r+x]}function C(n){for(let e=0;e<16;e+=1)n[e]=Y[n[e]]}function D(n){let e=n[13];n[13]=n[9],n[9]=n[5],n[5]=n[1],n[1]=e,e=n[2],n[2]=n[10],n[10]=e,e=n[6],n[6]=n[14],n[14]=e,e=n[3],n[3]=n[7],n[7]=n[11],n[11]=n[15],n[15]=e}function en(n){for(let e=0;e<4;e+=1){let t=e*4,r=n[t],x=n[t+1],o=n[t+2],i=n[t+3];n[t]=a(r,14)^a(x,11)^a(o,13)^a(i,9),n[t+1]=a(r,9)^a(x,14)^a(o,11)^a(i,13),n[t+2]=a(r,13)^a(x,9)^a(o,14)^a(i,11),n[t+3]=a(r,11)^a(x,13)^a(o,9)^a(i,14)}}function a(n,e){let t=0;for(let r=0;r<8;r+=1){if((e&1)!==0)t^=n;let x=(n&128)!==0;if(n=n<<1&255,x)n^=27;e>>=1}return t}var A="HWP Document File",F=2,un=16,ln=un+51,dn=new Set([1,2,3,4,5,6,7,8,11,12,13,14,15,16,17,21,22,23,26,27,28,29,30]);function s(n){if(n.length<A.length)return!1;for(let e=0;e<A.length;e+=1)if(n[e]!==A.charCodeAt(e))return!1;return!0}function W(n){if(xn(n))return sn(n);if(s(n))return L(En(n));throw m.malformed("not an HWP document")}function sn(n){let e;try{e=tn.open(n)}catch(o){if(o instanceof m)throw o;throw m.malformed("not an OLE2 compound file")}if(e.exists("EncryptionInfo")||e.exists("EncryptedPackage"))throw m.encrypted();let t=E(e,"FileHeader"),r=t!==void 0?An(t):void 0;if(r?.encrypted)throw m.encrypted();let x=[];if(r?.distribution)B(e,"ViewText",r.compressed,x,!0);else B(e,"BodyText",r?.compressed,x,!1);if(x.length===0){let o=E(e,"PrvText");if(o!==void 0)yn(o,x)}if(x.length===0)for(let o of e.readRootStorage()){if(o.type!==2)continue;let i=E(e,o.name);if(i!==void 0)w(i,x)}return L(x)}function B(n,e,t,r,x){let o=!1;for(let i=0;;i+=1){let f=`${e}/Section${i}`;if(!n.exists(f)){if(o||i>0)break;continue}o=!0;let c=X(n,f);if(c===void 0)continue;let l=x?mn(c):c;if(l===void 0)continue;let u=x?pn(l,t):k(l,t);wn(u,r)}}function mn(n){if(n.length<260)return;let e=hn(n.subarray(4,260));if(e===void 0)return;return H(n.subarray(260),e)}function hn(n){if(n.length<256)return;let e=n.subarray(0,256).slice(),t=new DataView(e.buffer,e.byteOffset,4).getInt32(0,!0),r=0,x=0,o=t|0,i=()=>{return o=Math.imul(o,214013)+2531011|0,o>>>16&32767};for(let c=0;c<256;c+=1,x-=1){if(x===0)r=i()&255,x=(i()&15)+1;if(c>=4)e[c]=e[c]^r}let f=4+(e[0]&15);if(f+16>e.length)return;return e.subarray(f,f+16)}function pn(n,e){if(e!==!0)return n;let t=gn(n);if(t!==void 0)return t;return k(n,!0)}function gn(n){if(n.length===0)return;try{return fn(n,Number.MAX_SAFE_INTEGER)}catch{return}}function An(n){if(n.length<40)return;if(!s(n))return;let e=new DataView(n.buffer,n.byteOffset,n.byteLength).getUint32(36,!0);return{compressed:(e&1)!==0,encrypted:(e&2)!==0,distribution:(e&4)!==0}}function wn(n,e){let t=Un(n);if(t.length>0){for(let r of t)e.push(r);return}w(n,e)}function Un(n){if(n.length<4)return[];let e=new DataView(n.buffer,n.byteOffset,n.byteLength),t=[],r=0,x=0,o=0;while(r+4<=n.length){let i=e.getUint32(r,!0),f=i&1023,c=i>>>20&4095,l=c,u=r+4;if(c===4095){if(u+4>n.length)break;l=e.getUint32(u,!0),u+=4}if(l<0||u+l>n.length)break;if(x+=1,o=u+l,f===ln){let d=bn(n.subarray(u,u+l));if(d.length>0)t.push(d)}r=u+l}if(t.length===0&&(x===0||o<n.length/2))return[];return t}function bn(n){let e=n.length&-2,t="",r=0;while(r+1<e){let x=n[r]|n[r+1]<<8;if(r+=2,x<32){if(dn.has(x))r+=14;else if(x===9)t+="\t";else if(x===10)t+=`
2
+ `;else if(x===24||x===25)t+="-";else if(x===31)t+=" ";continue}if(x===65534||x===65535)continue;t+=String.fromCharCode(x)}return t}function k(n,e){if(n.length>=2&&Pn(n)){let t=N(n);if(t!==void 0)return t}if(e===!1)return n;if(e===!0){let t=N(n);if(t!==void 0)return t}return n}function N(n){try{return cn(n,Number.MAX_SAFE_INTEGER)}catch{return}}function Pn(n){if(n.length<2)return!1;let e=n[0],t=n[1];if(((e<<8)+t)%31!==0)return!1;return(e&15)===8}function En(n){let e=n.length>32?32:A.length,t=[];if(w(n.subarray(Math.min(e,n.length)),t),t.length===0)w(n,t);return t}function yn(n,e){let t=n.length&-2,r=new TextDecoder("utf-16le").decode(n.subarray(0,t));for(let x of r.split(/\0+/)){let o=I(R(x)).trim();if(O(o))e.push(o)}}function w(n,e){let t="",r=()=>{if(t.length>=F)e.push(t);t=""},x=n.length&-2;for(let o=0;o<x;o+=2){let i=n[o]|n[o+1]<<8;if(i!==0&&vn(i))t+=String.fromCharCode(i);else r()}r()}function L(n){let e=new Set,t=rn();for(let r of n){let x=I(R(r)).trim();if(!O(x)||e.has(x))continue;e.add(x),t.blocks.push({type:"paragraph",inlines:[on(x)]})}if(t.blocks.length===0)throw m.malformed("no readable text in HWP document");return t}function vn(n){if(n===9||n===10||n===13||n===32)return!0;if(n<32||n>=127&&n<=159)return!1;return n!==65534&&n!==65535}function O(n){if(n.length<F)return!1;let e=0;for(let r of n)if(an(r))e+=1;if(e<2)return!1;let t=n.toLowerCase();if(t.includes("xmlns")||t.includes("http://schemas.")||t.includes("hwp document file"))return!1;return!0}function E(n,e){if(!n.exists(e))return;return X(n,e)}function X(n,e){try{return n.readStream(e)}catch(t){if(t instanceof m&&t.isFatal())throw t;return}}import{Package as Tn}from"@mdgate/containers";import{ConvertError as h}from"@mdgate/core";import{cellSpanning as Sn,emptyDocument as Cn,GridBuilder as Dn,inlinesAreEmpty as Hn,plain as U,resolveHeaderRows as Bn}from"@mdgate/document";import{cleanText as v}from"@mdgate/utils";function G(n){let e;try{e=Tn.open(n)}catch(i){throw $n(i)}let t=e.optionalXmlPart("Contents/header.xml")??e.optionalXmlPart("Contents/Header.xml"),r={styles:t!==void 0?Rn(t):new Map},x=Nn(e);if(x.length===0)throw h.missingPart("Contents/section0.xml");let o=Cn();for(let i of x){let f=e.optionalXmlPart(i);if(f===void 0)continue;b(f,o.blocks,r)}if(o.blocks.length===0)_n(e,o);if(o.blocks.length===0)throw h.malformed("no readable text in HWPX document");return o}function Nn(n){let e=[];for(let t of n.partNames())if(/^Contents\/section\d+\.xml$/i.test(t))e.push(t);return e.sort((t,r)=>_(t)-_(r)),e}function _(n){let e=n.match(/section(\d+)/i);return e!==null?Number(e[1]):0}function Rn(n){let e=new Map;for(let t of n.descendantsAny("style")){let r=t.attrAny("id");if(r===void 0)continue;let x=In(t);if(x!==void 0)e.set(r,x)}return e}function In(n){let e=(n.attrAny("type")??"").toLowerCase(),r=`${n.attrAny("name")??""} ${n.attrAny("engName")??""}`.match(/(?:개요|제목|outline|heading)\s*(\d+)/i);if(r!==null)return Fn(Number(r[1]));if(e==="outline"||e==="heading")return 1;return}function Fn(n){if(!Number.isFinite(n)||n<1)return 1;return n>6?6:n|0}function b(n,e,t){if(n.local==="tbl"){let r=V(n,t);if(r!==void 0)e.push(r);return}if(n.local==="p"){Wn(n,e,t);return}for(let r of n.childElems())b(r,e,t)}function Wn(n,e,t){let r=kn(n,t),x=[],o=!1,i=()=>{if(Hn(x)){x=[];return}if(r!==void 0&&!o)e.push({type:"heading",level:r,anchor:void 0,content:x}),o=!0;else e.push({type:"paragraph",inlines:x});x=[]},f=(c)=>{for(let l of c.children){if(l.type==="text"){let d=v(l.text);if(d.length>0)x.push(U(d));continue}let u=l.elem;switch(u.local){case"tbl":{i();let d=V(u,t);if(d!==void 0)e.push(d);break}case"t":{let d=v(u.text());if(d.length>0)x.push(U(d));break}case"lineBreak":case"line-break":x.push({type:"lineBreak"});break;case"tab":x.push(U("\t"));break;default:f(u)}}};f(n),i()}function kn(n,e){let t=n.attrAny("styleIDRef")??n.attrAny("styleId")??n.attrAny("styleID");if(t===void 0)return;return e.styles.get(t)}function V(n,e){let t=Ln(n);if(t.length===0)return;let r=new Dn;for(let o of t){r.nextRow();for(let i of On(o))r.place(Sn(Xn(i,e),M(i,"colSpan"),M(i,"rowSpan")))}let x=r.finish("data");if(x.grid.length===0)return;return x.headerRows=Bn(x,0),{type:"table",table:x}}function Ln(n){let e=n.childElems().filter((t)=>t.local==="tr");if(e.length>0)return e;return[...n.descendantsAny("tr")]}function On(n){let e=n.childElems().filter((t)=>t.local==="tc");if(e.length>0)return e;return[...n.descendantsAny("tc")]}function Xn(n,e){let t=[],r=n.childElems().find((x)=>x.local==="subList");if(r!==void 0)return b(r,t,e),t;for(let x of n.childElems())if(x.local==="p"||x.local==="tbl")b(x,t,e);return t}function M(n,e){let t=n.attrAny(e)??n.childElems().find((x)=>x.local==="cellSpan")?.attrAny(e);if(t===void 0||t.length===0)return 1;let r=Number(t);if(!Number.isFinite(r)||r<1)return 1;return r>1000?1000:r|0}function _n(n,e){let t=n.optionalPart("Preview/PrvText.txt")??n.optionalPart("Preview/PrvText.dat")??n.optionalPart("Preview/PrvText");if(t===void 0)return;for(let r of Vn(Mn(t)))e.blocks.push({type:"paragraph",inlines:[U(r)]})}function Mn(n){if(n.length>=2&&n[0]===255&&n[1]===254)return new TextDecoder("utf-16le").decode(n);if(n.length>=2&&n[0]===254&&n[1]===255)return new TextDecoder("utf-16be").decode(n);let e=n.length&-2;if(e>=4&&Gn(n.subarray(0,e)))return new TextDecoder("utf-16le").decode(n.subarray(0,e));return new TextDecoder("utf-8",{fatal:!1}).decode(n)}function Gn(n){let e=0;for(let t=1;t<n.length;t+=2)if(n[t]===0)e+=1;return e*2>=n.length/2}function Vn(n){let e=[];for(let t of n.split(/\0+/)){let r=v(t).trim();if(r.length>0)e.push(r)}return e}function $n(n){if(n instanceof h){if(n.code==="encrypted"||y(n.message)||y(n.detail??""))return h.encrypted();return n}let e=n instanceof Error?n.message:String(n);if(y(e))return h.encrypted();return h.malformed(`not a readable zip archive: ${e}`)}function y(n){let e=n.toLowerCase();return e.includes("encrypted")||e.includes("password")}function $(n){if(Jn(n))return G(n);if(Zn(n)||s(n))return W(n);throw zn.malformed("not an HWP document")}function Jn(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}var e0=new Set(["hwp","hwpx","hwt","hwtx"]);function S(){return{id:"hwp",sniff(n,e){if(J(n)==="hwpx"||z(n))return 2;if(K(n)==="hwp"||x0(n))return 2;if(s(n))return 2;if(e?.path!==void 0&&e0.has(n0(e.path)??""))return 1;return 0},convert(n){return t0(n),{markdown:qn($(n))}}}}function t0(n){if(o0(n))throw p.unsupported("pdf");if(Q(n)){let e=J(n);if(e==="hwpx")return;if(e!==void 0)throw p.unsupported(e);if(z(n))return;throw p.unsupported("zip")}if(Z(n)){let e=K(n);if(e==="hwp")return;if(e!==void 0)throw p.unsupported(e);return}if(s(n))return;throw p.unsupported("hwp")}function z(n){if(!Q(n))return!1;let e;try{e=jn.open(n)}catch{return!1}for(let r of e.partNames()){let x=r.toLowerCase();if(x==="contents/content.hpf")return!0;if(/^contents\/section\d+\.xml$/.test(x))return!0}let t=e.optionalPart("mimetype");if(t!==void 0){let x=new TextDecoder("utf-8",{fatal:!1}).decode(t).trim().toLowerCase().replace(/-template$/,"");if(x==="application/hwp+zip"||x==="application/vnd.hancom.hwpx")return!0}return!1}function x0(n){if(!Z(n))return!1;try{let e=Kn.open(n);for(let t of e.readRootStorage())if(r0(t.name))return!0}catch{return!1}return!1}function r0(n){let e=n.charCodeAt(0)===5?n.slice(1):n;return T(n,"FileHeader")||T(e,"HwpSummaryInformation")||T(n,"HWPDocumentInfo")}function T(n,e){return n.length===e.length&&n.toLowerCase()===e.toLowerCase()}function J(n){return Yn(n)}function K(n){return Qn(n)}function Q(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}function o0(n){let e=i0(n);return f0(n,"%PDF-",e)}function i0(n){let e=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)e=3;while(e<n.length){let t=n[e];if(t!==9&&t!==10&&t!==13&&t!==32)break;e+=1}return e}function f0(n,e,t){if(t+e.length>n.length)return!1;for(let r=0;r<e.length;r+=1)if(n[t+r]!==e.charCodeAt(r))return!1;return!0}import{create as c0}from"@mdgate/core";var a0=c0([S()]);function u0(n,e){return a0(n,e)}export{u0 as toMarkdown,S as hwp};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/hwp",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "mdgate Hangul HWP / HWPX converter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,10 +22,10 @@
22
22
  "prepublishOnly": "bun run build"
23
23
  },
24
24
  "dependencies": {
25
- "@mdgate/containers": "0.5.1",
26
- "@mdgate/core": "0.5.1",
27
- "@mdgate/document": "0.5.1",
28
- "@mdgate/utils": "0.5.1"
25
+ "@mdgate/containers": "0.5.2",
26
+ "@mdgate/core": "0.5.2",
27
+ "@mdgate/document": "0.5.2",
28
+ "@mdgate/utils": "0.5.2"
29
29
  },
30
30
  "publishConfig": {
31
31
  "access": "public"