@mdgate/hwp 0.4.1 → 0.5.1
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.js +2 -2
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{CompoundFile as
|
|
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
|
|
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};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgate/hwp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
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.
|
|
26
|
-
"@mdgate/core": "0.
|
|
27
|
-
"@mdgate/document": "0.
|
|
28
|
-
"@mdgate/utils": "0.
|
|
25
|
+
"@mdgate/containers": "0.5.1",
|
|
26
|
+
"@mdgate/core": "0.5.1",
|
|
27
|
+
"@mdgate/document": "0.5.1",
|
|
28
|
+
"@mdgate/utils": "0.5.1"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|