hwpx2f 0.0.0-stage → 0.1.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 +124 -2
- package/THIRD_PARTY_NOTICES.md +75 -0
- package/dist/cli.js +76 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +32 -0
- package/dist/lib/chunk-36PRCVPO.js +131 -0
- package/dist/lib/chunk-45BAITEX.js +9749 -0
- package/dist/lib/chunk-4NRZ6MWV.js +133 -0
- package/dist/lib/chunk-6CFXTS7A.js +11971 -0
- package/dist/lib/chunk-6CPCDYOD.js +104 -0
- package/dist/lib/chunk-B6MTS4XP.js +313 -0
- package/dist/lib/chunk-BX24E7EG.js +1427 -0
- package/dist/lib/chunk-C64I2SU4.js +82 -0
- package/dist/lib/chunk-CEKQASS3.js +258 -0
- package/dist/lib/chunk-CMWTKXM2.js +204 -0
- package/dist/lib/chunk-E6KSYRHG.js +2189 -0
- package/dist/lib/chunk-IZDWVMTV.js +28786 -0
- package/dist/lib/chunk-JK6YR5NT.js +4276 -0
- package/dist/lib/chunk-O6FSHPNE.js +111 -0
- package/dist/lib/chunk-OYXOOP2U.js +199 -0
- package/dist/lib/chunk-PM4KBWF4.js +49 -0
- package/dist/lib/chunk-R5F3GHLO.js +290 -0
- package/dist/lib/chunk-R76DWHJR.js +1596 -0
- package/dist/lib/chunk-RFS3RTSR.js +6876 -0
- package/dist/lib/chunk-RYJRNK5Z.js +47 -0
- package/dist/lib/chunk-UOXPGSFH.js +43 -0
- package/dist/lib/chunk-UZH4UBMX.js +44 -0
- package/dist/lib/chunk-VZTE4GKI.js +52 -0
- package/dist/lib/chunk-WEOKEO6L.js +35291 -0
- package/dist/lib/detect-4XZNNYZA-O7IPA4XQ.js +32 -0
- package/dist/lib/dist-HVSOXNCJ.js +263 -0
- package/dist/lib/formula-LC62GV4D-K3VFC5GV.js +1173 -0
- package/dist/lib/generator-KYTTL4OS-TRAQ66QE.js +14 -0
- package/dist/lib/hancom-EM62ZVDM-WZKVI7XB.js +91 -0
- package/dist/lib/image-ocr-ZUZQXLYS-U66LBMRA.js +293 -0
- package/dist/lib/page-images-VCIFZAXK-KJLWQDRH.js +95 -0
- package/dist/lib/page-range-7R3JU6SQ-MDDQRZVK.js +10 -0
- package/dist/lib/parser-JQNH5QMM-HFFI4HYK.js +4310 -0
- package/dist/lib/pdf-ocr-C3MBR6J2-J3UGJKWT.js +46 -0
- package/dist/lib/to-hwpx-JUT2SOTT-MOPPK6OW.js +17 -0
- package/dist/notices/core-util-is/LICENSE +19 -0
- package/dist/notices/entities/LICENSE +11 -0
- package/dist/notices/immediate/LICENSE.txt +20 -0
- package/dist/notices/inherits/LICENSE +16 -0
- package/dist/notices/jszip/LICENSE.markdown +651 -0
- package/dist/notices/kordoc/LICENSE +21 -0
- package/dist/notices/kordoc/NOTICE +376 -0
- package/dist/notices/kordoc/THIRD_PARTY/apache-2.0.LICENSE +201 -0
- package/dist/notices/kordoc/THIRD_PARTY/claw-hwp.LICENSE +21 -0
- package/dist/notices/kordoc/THIRD_PARTY/hayro-pdf.txt +361 -0
- package/dist/notices/kordoc/THIRD_PARTY/hml-equation-parser.LICENSE +201 -0
- package/dist/notices/kordoc/THIRD_PARTY/hml-equation-parser.txt +35 -0
- package/dist/notices/kordoc/THIRD_PARTY/korean-report-hwpx.LICENSE +21 -0
- package/dist/notices/kordoc/THIRD_PARTY/memchr.LICENSE +21 -0
- package/dist/notices/kordoc/THIRD_PARTY/opendataloader-pdf.txt +32 -0
- package/dist/notices/kordoc/THIRD_PARTY/pix2text.txt +17 -0
- package/dist/notices/kordoc/THIRD_PARTY/quick-xml.LICENSE +23 -0
- package/dist/notices/kordoc/THIRD_PARTY/rhwp-forms.txt +35 -0
- package/dist/notices/lie/license.md +7 -0
- package/dist/notices/linkify-it/LICENSE +22 -0
- package/dist/notices/markdown-it/LICENSE +22 -0
- package/dist/notices/mdurl/LICENSE +45 -0
- package/dist/notices/pako/LICENSE +21 -0
- package/dist/notices/process-nextick-args/license.md +19 -0
- package/dist/notices/punycode.js/LICENSE-MIT.txt +20 -0
- package/dist/notices/readable-stream/LICENSE +47 -0
- package/dist/notices/safe-buffer/LICENSE +21 -0
- package/dist/notices/setimmediate/LICENSE.txt +20 -0
- package/dist/notices/string_decoder/LICENSE +48 -0
- package/dist/notices/uc.micro/LICENSE.txt +20 -0
- package/dist/notices/util-deprecate/LICENSE +24 -0
- package/package.json +66 -4
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// node_modules/kordoc/dist/chunk-Y7JDDVCG.js
|
|
4
|
+
var OPS = {
|
|
5
|
+
dependency: 1,
|
|
6
|
+
setLineWidth: 2,
|
|
7
|
+
setLineCap: 3,
|
|
8
|
+
setLineJoin: 4,
|
|
9
|
+
setMiterLimit: 5,
|
|
10
|
+
setDash: 6,
|
|
11
|
+
setRenderingIntent: 7,
|
|
12
|
+
setFlatness: 8,
|
|
13
|
+
setGState: 9,
|
|
14
|
+
save: 10,
|
|
15
|
+
restore: 11,
|
|
16
|
+
transform: 12,
|
|
17
|
+
moveTo: 13,
|
|
18
|
+
lineTo: 14,
|
|
19
|
+
curveTo: 15,
|
|
20
|
+
curveTo2: 16,
|
|
21
|
+
curveTo3: 17,
|
|
22
|
+
closePath: 18,
|
|
23
|
+
rectangle: 19,
|
|
24
|
+
stroke: 20,
|
|
25
|
+
closeStroke: 21,
|
|
26
|
+
fill: 22,
|
|
27
|
+
eoFill: 23,
|
|
28
|
+
fillStroke: 24,
|
|
29
|
+
eoFillStroke: 25,
|
|
30
|
+
closeFillStroke: 26,
|
|
31
|
+
closeEOFillStroke: 27,
|
|
32
|
+
endPath: 28,
|
|
33
|
+
clip: 29,
|
|
34
|
+
eoClip: 30,
|
|
35
|
+
beginText: 31,
|
|
36
|
+
endText: 32,
|
|
37
|
+
setCharSpacing: 33,
|
|
38
|
+
setWordSpacing: 34,
|
|
39
|
+
setHScale: 35,
|
|
40
|
+
setLeading: 36,
|
|
41
|
+
setFont: 37,
|
|
42
|
+
setTextRenderingMode: 38,
|
|
43
|
+
setTextRise: 39,
|
|
44
|
+
moveText: 40,
|
|
45
|
+
setLeadingMoveText: 41,
|
|
46
|
+
setTextMatrix: 42,
|
|
47
|
+
nextLine: 43,
|
|
48
|
+
showText: 44,
|
|
49
|
+
showSpacedText: 45,
|
|
50
|
+
nextLineShowText: 46,
|
|
51
|
+
nextLineSetSpacingShowText: 47,
|
|
52
|
+
setCharWidth: 48,
|
|
53
|
+
setCharWidthAndBounds: 49,
|
|
54
|
+
setStrokeColorSpace: 50,
|
|
55
|
+
setFillColorSpace: 51,
|
|
56
|
+
setStrokeColor: 52,
|
|
57
|
+
setStrokeColorN: 53,
|
|
58
|
+
setFillColor: 54,
|
|
59
|
+
setFillColorN: 55,
|
|
60
|
+
setStrokeGray: 56,
|
|
61
|
+
setFillGray: 57,
|
|
62
|
+
setStrokeRGBColor: 58,
|
|
63
|
+
setFillRGBColor: 59,
|
|
64
|
+
setStrokeCMYKColor: 60,
|
|
65
|
+
setFillCMYKColor: 61,
|
|
66
|
+
shadingFill: 62,
|
|
67
|
+
beginInlineImage: 63,
|
|
68
|
+
beginImageData: 64,
|
|
69
|
+
endInlineImage: 65,
|
|
70
|
+
paintXObject: 66,
|
|
71
|
+
markPoint: 67,
|
|
72
|
+
markPointProps: 68,
|
|
73
|
+
beginMarkedContent: 69,
|
|
74
|
+
beginMarkedContentProps: 70,
|
|
75
|
+
endMarkedContent: 71,
|
|
76
|
+
beginCompat: 72,
|
|
77
|
+
endCompat: 73,
|
|
78
|
+
paintFormXObjectBegin: 74,
|
|
79
|
+
paintFormXObjectEnd: 75,
|
|
80
|
+
beginGroup: 76,
|
|
81
|
+
endGroup: 77,
|
|
82
|
+
beginAnnotation: 80,
|
|
83
|
+
endAnnotation: 81,
|
|
84
|
+
paintImageMaskXObject: 83,
|
|
85
|
+
paintImageMaskXObjectGroup: 84,
|
|
86
|
+
paintImageXObject: 85,
|
|
87
|
+
paintInlineImageXObject: 86,
|
|
88
|
+
paintInlineImageXObjectGroup: 87,
|
|
89
|
+
paintImageXObjectRepeat: 88,
|
|
90
|
+
paintImageMaskXObjectRepeat: 89,
|
|
91
|
+
paintSolidColorImageMask: 90,
|
|
92
|
+
constructPath: 91,
|
|
93
|
+
setStrokeTransparent: 92,
|
|
94
|
+
setFillTransparent: 93
|
|
95
|
+
};
|
|
96
|
+
var ImageKind = { GRAYSCALE_1BPP: 1, RGB_24BPP: 2, RGBA_32BPP: 3 };
|
|
97
|
+
var normalizeRegex;
|
|
98
|
+
function normalizeUnicode(str) {
|
|
99
|
+
if (str.length === 1) {
|
|
100
|
+
const codeUnit = str.charCodeAt(0);
|
|
101
|
+
if (codeUnit < 160 || codeUnit >= 12288 && codeUnit < 64256) return str;
|
|
102
|
+
}
|
|
103
|
+
normalizeRegex ??= /([ µ;ຳ - Ωff-fflstﬠ-זּטּ-לּמּנּ-סּףּ-פּצּ-ﮡﮤ-ﮩﮮ-ﮱﯓ-ﯜﯞ-ﯧﯪ-ﯸﯼ-ﯽﰀ-ﱝﱤ-ﳱﳵ-ﴽﶈﷴﷺ-ﷻﹱﹷﹹﹻﹽ]+)|(ſt+)/gu;
|
|
104
|
+
return str.replaceAll(normalizeRegex, (_, p1, p2) => p1 ? p1.normalize("NFKC") : String(p2 === "\uFB05" ? "\u017Ft" : void 0));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export {
|
|
108
|
+
OPS,
|
|
109
|
+
ImageKind,
|
|
110
|
+
normalizeUnicode
|
|
111
|
+
};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// node_modules/kordoc/dist/chunk-C5PNWPGL.js
|
|
4
|
+
var VERSION = true ? "4.25.40" : "0.0.0-dev";
|
|
5
|
+
function toArrayBuffer(buf) {
|
|
6
|
+
if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
|
|
7
|
+
return buf.buffer;
|
|
8
|
+
}
|
|
9
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
|
10
|
+
}
|
|
11
|
+
var KordocError = class extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "KordocError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
function isFileNotFound(err) {
|
|
18
|
+
if (typeof err !== "object" || err === null) return false;
|
|
19
|
+
if (err.code === "ENOENT") return true;
|
|
20
|
+
const msg = err.message;
|
|
21
|
+
return typeof msg === "string" && /ENOENT|no such file or directory/i.test(msg);
|
|
22
|
+
}
|
|
23
|
+
function sanitizeError(err) {
|
|
24
|
+
if (err instanceof KordocError) return err.message;
|
|
25
|
+
if (isFileNotFound(err)) return "\uD30C\uC77C \uB610\uB294 \uB514\uB809\uD1A0\uB9AC\uB97C \uCC3E\uC744 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4";
|
|
26
|
+
return "\uBB38\uC11C \uCC98\uB9AC \uC911 \uC624\uB958\uAC00 \uBC1C\uC0DD\uD588\uC2B5\uB2C8\uB2E4";
|
|
27
|
+
}
|
|
28
|
+
function isPathTraversal(name) {
|
|
29
|
+
if (name.includes("\0")) return true;
|
|
30
|
+
const normalized = name.replace(/\\/g, "/");
|
|
31
|
+
const segments = normalized.split("/");
|
|
32
|
+
return segments.some((s) => s === "..") || normalized.startsWith("/") || /^[A-Za-z]:/.test(normalized);
|
|
33
|
+
}
|
|
34
|
+
function normalizeSectionHref(href) {
|
|
35
|
+
if (!href) return null;
|
|
36
|
+
let normalized = href.replace(/\\/g, "/").replace(/^\/+/, "");
|
|
37
|
+
if (isPathTraversal(normalized)) return null;
|
|
38
|
+
if (/^[Ss]ection\d+\.xml$/.test(normalized)) normalized = "Contents/" + normalized;
|
|
39
|
+
return /(?:^|\/)[Ss]ection\d+\.xml$/.test(normalized) ? normalized : null;
|
|
40
|
+
}
|
|
41
|
+
function compareSectionPaths(a, b) {
|
|
42
|
+
const ai = Number(a.match(/[Ss]ection(\d+)\.xml$/)?.[1] ?? Number.MAX_SAFE_INTEGER);
|
|
43
|
+
const bi = Number(b.match(/[Ss]ection(\d+)\.xml$/)?.[1] ?? Number.MAX_SAFE_INTEGER);
|
|
44
|
+
return ai === bi ? a.localeCompare(b) : ai - bi;
|
|
45
|
+
}
|
|
46
|
+
var MAX_UNZIP_ENV_MB = 8192;
|
|
47
|
+
function unzipLimitBytes(defaultBytes) {
|
|
48
|
+
const mb = Number(process.env.KORDOC_MAX_UNZIP_MB);
|
|
49
|
+
return Number.isFinite(mb) && mb > 0 ? Math.min(mb, MAX_UNZIP_ENV_MB) * 1024 * 1024 : defaultBytes;
|
|
50
|
+
}
|
|
51
|
+
function precheckZipSize(buffer, maxUncompressedSize = 256 * 1024 * 1024, maxEntries = 500, media) {
|
|
52
|
+
try {
|
|
53
|
+
const data = new DataView(buffer);
|
|
54
|
+
const len = buffer.byteLength;
|
|
55
|
+
let eocdOffset = -1;
|
|
56
|
+
for (let i = len - 22; i >= Math.max(0, len - 65557); i--) {
|
|
57
|
+
if (data.getUint32(i, true) === 101010256) {
|
|
58
|
+
eocdOffset = i;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (eocdOffset < 0) return { totalUncompressed: 0, entryCount: 0 };
|
|
63
|
+
const entryCount = data.getUint16(eocdOffset + 10, true);
|
|
64
|
+
if (entryCount > maxEntries) {
|
|
65
|
+
throw new KordocError(`ZIP \uC5D4\uD2B8\uB9AC \uC218 \uCD08\uACFC: ${entryCount} (\uCD5C\uB300 ${maxEntries})`);
|
|
66
|
+
}
|
|
67
|
+
const cdSize = data.getUint32(eocdOffset + 12, true);
|
|
68
|
+
const cdOffset = data.getUint32(eocdOffset + 16, true);
|
|
69
|
+
if (cdOffset + cdSize > len) return { totalUncompressed: 0, entryCount };
|
|
70
|
+
let totalUncompressed = 0;
|
|
71
|
+
let mediaUncompressed = 0;
|
|
72
|
+
let pos = cdOffset;
|
|
73
|
+
for (let i = 0; i < entryCount && pos + 46 <= cdOffset + cdSize; i++) {
|
|
74
|
+
if (data.getUint32(pos, true) !== 33639248) break;
|
|
75
|
+
const localOff = data.getUint32(pos + 42, true);
|
|
76
|
+
const localSize = localOff + 30 <= len && data.getUint32(localOff, true) === 67324752 ? data.getUint32(localOff + 22, true) : 0;
|
|
77
|
+
const size = Math.max(data.getUint32(pos + 24, true), localSize);
|
|
78
|
+
const nameLen = data.getUint16(pos + 28, true);
|
|
79
|
+
const extraLen = data.getUint16(pos + 30, true);
|
|
80
|
+
const commentLen = data.getUint16(pos + 32, true);
|
|
81
|
+
const isMedia = !!media && pos + 46 + nameLen <= len && media.re.test(new TextDecoder().decode(new Uint8Array(buffer, pos + 46, nameLen)));
|
|
82
|
+
const never = isMedia && !!media?.never && media.never.test(new TextDecoder().decode(new Uint8Array(buffer, pos + 46, nameLen)));
|
|
83
|
+
if (isMedia && !never) mediaUncompressed += size;
|
|
84
|
+
if (!(isMedia && (media?.skip || never))) totalUncompressed += size;
|
|
85
|
+
pos += 46 + nameLen + extraLen + commentLen;
|
|
86
|
+
}
|
|
87
|
+
if (totalUncompressed > maxUncompressedSize) {
|
|
88
|
+
const mb = (n) => (n / 1024 / 1024).toFixed(1);
|
|
89
|
+
const mediaNote = media && !media.skip && mediaUncompressed > 0 ? ` \u2014 \uADF8\uB9BC\xB7\uAC1C\uCCB4 \uD30C\uD2B8\uAC00 ${mb(mediaUncompressed)}MB, \uC774\uBBF8\uC9C0 \uCD94\uCD9C\uC744 \uB044\uBA74(images: false\xB7--no-images) \uC138\uC9C0 \uC54A\uB294\uB2E4` : "";
|
|
90
|
+
throw new KordocError(`ZIP \uBE44\uC555\uCD95 \uD06C\uAE30 \uCD08\uACFC: ${mb(totalUncompressed)}MB (\uCD5C\uB300 ${maxUncompressedSize / 1024 / 1024}MB)${mediaNote}`);
|
|
91
|
+
}
|
|
92
|
+
return { totalUncompressed, entryCount };
|
|
93
|
+
} catch (err) {
|
|
94
|
+
if (err instanceof KordocError) throw err;
|
|
95
|
+
return { totalUncompressed: 0, entryCount: 0 };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function stripDtd(xml) {
|
|
99
|
+
return xml.replace(/^\uFEFF/, "").replace(/<!DOCTYPE\s[^[>]*(\[[\s\S]*?\])?\s*>/gi, "");
|
|
100
|
+
}
|
|
101
|
+
var SAFE_HREF_RE = /^(?:https?:|mailto:|tel:|#)/i;
|
|
102
|
+
var HREF_ESCAPE = { "(": "%28", ")": "%29", "<": "%3C", ">": "%3E", '"': "%22", "'": "%27", "`": "%60" };
|
|
103
|
+
function sanitizeHref(href) {
|
|
104
|
+
const trimmed = href.trim();
|
|
105
|
+
if (!trimmed || !SAFE_HREF_RE.test(trimmed)) return null;
|
|
106
|
+
return trimmed.replace(/[()<>"'`]/g, (c) => HREF_ESCAPE[c]).replace(/\s/g, (c) => encodeURIComponent(c));
|
|
107
|
+
}
|
|
108
|
+
var BARE_URL = String.raw`(?:(?<![A-Za-z])https?:\/\/|(?<![^\s(])www\.)[^\s<>\\[\]\x00]+`;
|
|
109
|
+
var LINK_LABEL_REST = /[^[\]\n]*\]\(/y;
|
|
110
|
+
function isGfmUrl(url, text, end) {
|
|
111
|
+
const start = end - url.length;
|
|
112
|
+
const line = text.slice(text.lastIndexOf("\n", start - 1) + 1, start);
|
|
113
|
+
if (/["']$/.test(line) || line.lastIndexOf("<") > line.lastIndexOf(">")) return false;
|
|
114
|
+
LINK_LABEL_REST.lastIndex = end;
|
|
115
|
+
return !LINK_LABEL_REST.test(text) && /^(?:[Hh][Tt][Tt][Pp][Ss]?:\/\/|www\.)[A-Za-z0-9.\-\u0080-\uffff]+(?:[/?#:]|$)/.test(url.slice(0, gfmUrlEnd(url)));
|
|
116
|
+
}
|
|
117
|
+
function gfmUrlHref(url) {
|
|
118
|
+
return /^www\./.test(url) ? "http://" + url : url;
|
|
119
|
+
}
|
|
120
|
+
function gfmUrlEnd(url) {
|
|
121
|
+
let n = url.length;
|
|
122
|
+
for (let c = url[n - 1]; `?!.,:*_~'")`.includes(c); c = url[n - 1]) {
|
|
123
|
+
const head = url.slice(0, n);
|
|
124
|
+
if (c === ")" && head.split("(").length >= head.split(")").length) break;
|
|
125
|
+
n--;
|
|
126
|
+
}
|
|
127
|
+
return n;
|
|
128
|
+
}
|
|
129
|
+
function escapeHtml(text, attr = false) {
|
|
130
|
+
const s = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
131
|
+
return attr ? s.replace(/"/g, """) : s;
|
|
132
|
+
}
|
|
133
|
+
function unescapeHtml(text) {
|
|
134
|
+
return text.replace(/&(lt|gt|quot|#39|amp);/g, (_, e) => HTML_ENTITY[e]);
|
|
135
|
+
}
|
|
136
|
+
var HTML_ENTITY = { lt: "<", gt: ">", quot: '"', "#39": "'", amp: "&" };
|
|
137
|
+
function safeMin(arr) {
|
|
138
|
+
let min = Infinity;
|
|
139
|
+
for (let i = 0; i < arr.length; i++) if (arr[i] < min) min = arr[i];
|
|
140
|
+
return min;
|
|
141
|
+
}
|
|
142
|
+
function safeMax(arr) {
|
|
143
|
+
let max = -Infinity;
|
|
144
|
+
for (let i = 0; i < arr.length; i++) if (arr[i] > max) max = arr[i];
|
|
145
|
+
return max;
|
|
146
|
+
}
|
|
147
|
+
var OPTIONAL_DEP_INSTALL_HINT = " \uB124\uD2B8\uC6CC\uD06C\uAC00 \uC81C\uD55C\uB41C linux/x64 \uC5D0\uC11C npx \uB85C \uC124\uCE58\uD588\uB2E4\uBA74 onnxruntime-node \uC758 CUDA \uB2E4\uC6B4\uB85C\uB4DC \uC2E4\uD328\uB85C \uD568\uAED8 \uBE60\uC84C\uC744 \uC218 \uC788\uC2B5\uB2C8\uB2E4 \u2014 `ONNXRUNTIME_NODE_INSTALL=skip npx -y kordoc@^4 \u2026` \uB85C \uB2E4\uC2DC \uC124\uCE58\uD558\uC138\uC694.";
|
|
148
|
+
function classifyError(err) {
|
|
149
|
+
if (!(err instanceof Error)) return "PARSE_ERROR";
|
|
150
|
+
const msg = err.message;
|
|
151
|
+
if (isFileNotFound(err)) return "FILE_NOT_FOUND";
|
|
152
|
+
if (msg.includes("DRM")) return "DRM_PROTECTED";
|
|
153
|
+
if (msg.includes("\uC554\uD638\uD654") || msg.includes("\uC554\uD638\uB85C \uBCF4\uD638")) return "ENCRYPTED";
|
|
154
|
+
if (msg.includes("optional dependency")) return "MISSING_DEPENDENCY";
|
|
155
|
+
if (msg.includes("Invalid string length") || msg.includes("Cannot create a string longer")) return "OUTPUT_TOO_LARGE";
|
|
156
|
+
if (msg.includes("ZIP bomb") || msg.includes("ZIP \uBE44\uC555\uCD95 \uD06C\uAE30 \uCD08\uACFC") || msg.includes("ZIP \uC5D4\uD2B8\uB9AC \uC218 \uCD08\uACFC")) return "ZIP_BOMB";
|
|
157
|
+
if (msg.includes("bomb") || msg.includes("\uD06C\uAE30 \uCD08\uACFC") || msg.includes("\uC555\uCD95 \uD574\uC81C")) return "DECOMPRESSION_BOMB";
|
|
158
|
+
if (msg.includes("\uC774\uBBF8\uC9C0 \uAE30\uBC18")) return "IMAGE_BASED_PDF";
|
|
159
|
+
if (msg.includes("\uC139\uC158") && (msg.includes("\uCC3E\uC744 \uC218 \uC5C6") || msg.includes("\uC5C6\uC74C"))) return "NO_SECTIONS";
|
|
160
|
+
if (msg.includes("\uC2DC\uADF8\uB2C8\uCC98") || msg.includes("\uBCF5\uAD6C\uD560 \uC218 \uC5C6")) return "CORRUPTED";
|
|
161
|
+
return "PARSE_ERROR";
|
|
162
|
+
}
|
|
163
|
+
function partExtension(path) {
|
|
164
|
+
return (/\.([A-Za-z0-9]{1,5})$/.exec(path.slice(path.lastIndexOf("/") + 1))?.[1] ?? "bin").toLowerCase();
|
|
165
|
+
}
|
|
166
|
+
function isFormLabelLine(text) {
|
|
167
|
+
const t = text.trim();
|
|
168
|
+
const count = (re) => t.match(re)?.length ?? 0;
|
|
169
|
+
if (/[가-힣A-Za-z)\]]\s*[::]$/.test(t) && count(/[가-힣A-Za-z)\]]\s*[::](?!\d|\/\/)/g) >= 2) return true;
|
|
170
|
+
if (t.length <= 60 && count(/[가-힣]\s?[::](?!\d|\/\/)/g) >= 2) return true;
|
|
171
|
+
const one = /^([가-힣][가-힣 ]{0,10}?)\s*[::](?!\d|\/\/)\s*(.*)$/.exec(t);
|
|
172
|
+
return !!one && one[1].replace(/ /g, "").length <= 8 && one[2].length <= 20 && !/[::]/.test(one[2]);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export {
|
|
176
|
+
VERSION,
|
|
177
|
+
toArrayBuffer,
|
|
178
|
+
KordocError,
|
|
179
|
+
sanitizeError,
|
|
180
|
+
isPathTraversal,
|
|
181
|
+
normalizeSectionHref,
|
|
182
|
+
compareSectionPaths,
|
|
183
|
+
unzipLimitBytes,
|
|
184
|
+
precheckZipSize,
|
|
185
|
+
stripDtd,
|
|
186
|
+
sanitizeHref,
|
|
187
|
+
BARE_URL,
|
|
188
|
+
isGfmUrl,
|
|
189
|
+
gfmUrlHref,
|
|
190
|
+
gfmUrlEnd,
|
|
191
|
+
escapeHtml,
|
|
192
|
+
unescapeHtml,
|
|
193
|
+
safeMin,
|
|
194
|
+
safeMax,
|
|
195
|
+
OPTIONAL_DEP_INSTALL_HINT,
|
|
196
|
+
classifyError,
|
|
197
|
+
partExtension,
|
|
198
|
+
isFormLabelLine
|
|
199
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// node_modules/kordoc/dist/chunk-ZDISS2SZ.js
|
|
4
|
+
import { isAbsolute } from "path";
|
|
5
|
+
function validateOcrOptions(o) {
|
|
6
|
+
if (!o) return null;
|
|
7
|
+
const n = o.ocrLineCandidates;
|
|
8
|
+
if (n !== void 0 && (typeof n !== "number" || !Number.isInteger(n) || n < 0 || n > 5)) return `ocrLineCandidates \uB294 0~5 \uC815\uC218\uC785\uB2C8\uB2E4 (\uBC1B\uC740 \uAC12: ${JSON.stringify(n)})`;
|
|
9
|
+
if (n && !o.ocrLines) return "ocrLineCandidates \uB294 ocrLines \uC640 \uD568\uAED8 \uC501\uB2C8\uB2E4 (\uD6C4\uBCF4\uB294 ocrLines \uC758 \uC904\uC5D0 \uC2E4\uB9BD\uB2C8\uB2E4)";
|
|
10
|
+
const ch = o.ocrChars;
|
|
11
|
+
if (ch !== void 0 && typeof ch !== "boolean" && (typeof ch !== "number" || !Number.isInteger(ch) || ch < 0 || ch > 5)) return `ocrChars \uB294 true\xB7false \uB610\uB294 0~5 \uC815\uC218\uC785\uB2C8\uB2E4 (\uBC1B\uC740 \uAC12: ${JSON.stringify(ch)})`;
|
|
12
|
+
if (ch && !o.ocrLines) return "ocrChars \uB294 ocrLines \uC640 \uD568\uAED8 \uC501\uB2C8\uB2E4 (\uAE00\uC790\uB294 ocrLines \uC758 \uC904\uC5D0 \uC2E4\uB9BD\uB2C8\uB2E4)";
|
|
13
|
+
if (o.pageImages !== void 0 && typeof o.pageImages !== "boolean" && o.pageImages !== "png" && o.pageImages !== "webp") return `pageImages \uB294 true\xB7false\xB7"png"\xB7"webp" \uC785\uB2C8\uB2E4 (\uBC1B\uC740 \uAC12: ${JSON.stringify(o.pageImages)})`;
|
|
14
|
+
if (o.ocrLines !== void 0 && typeof o.ocrLines !== "boolean" && o.ocrLines !== "all") return `ocrLines \uB294 true\xB7false\xB7"all" \uC785\uB2C8\uB2E4 (\uBC1B\uC740 \uAC12: ${JSON.stringify(o.ocrLines)})`;
|
|
15
|
+
const det = o.ocrDet;
|
|
16
|
+
if (det !== void 0 && det !== "mobile" && det !== "server" && det !== "medium") {
|
|
17
|
+
if (typeof det === "string" || !det || typeof det !== "object") return `ocrDet \uC740 "mobile"\xB7"server"\xB7"medium" \uB610\uB294 {path, sha256} \uC785\uB2C8\uB2E4 (\uBC1B\uC740 \uAC12: ${JSON.stringify(det)})`;
|
|
18
|
+
const bad = modelFileError("ocrDet", det);
|
|
19
|
+
if (bad) return bad;
|
|
20
|
+
}
|
|
21
|
+
const side = o.ocrDetLongSide;
|
|
22
|
+
if (side !== void 0 && (typeof side !== "number" || !Number.isInteger(side) || side < 32 || side > 4096)) {
|
|
23
|
+
return `ocrDetLongSide \uB294 32~4096 \uC815\uC218\uC785\uB2C8\uB2E4 (\uBC1B\uC740 \uAC12: ${JSON.stringify(side)})`;
|
|
24
|
+
}
|
|
25
|
+
const rec = o.ocrRec;
|
|
26
|
+
if (rec !== void 0 && rec !== "handwriting" && rec !== "auto" && rec !== "printed") {
|
|
27
|
+
if (typeof rec === "string") return `ocrRec \uC740 "handwriting"\xB7"auto"\xB7"printed" \uB610\uB294 {path, sha256} \uC785\uB2C8\uB2E4 (\uBC1B\uC740 \uAC12: ${JSON.stringify(rec)})`;
|
|
28
|
+
const bad = modelFileError("ocrRec", rec);
|
|
29
|
+
if (bad) return bad;
|
|
30
|
+
}
|
|
31
|
+
if (o.ocrRecDict !== void 0) {
|
|
32
|
+
const bad = modelFileError("ocrRecDict", o.ocrRecDict);
|
|
33
|
+
if (bad) return bad;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function modelFileError(key, f) {
|
|
38
|
+
if (!f || typeof f !== "object" || Array.isArray(f)) return `${key} \uB294 {path, sha256} \uAC1D\uCCB4\uC785\uB2C8\uB2E4`;
|
|
39
|
+
const { path, sha256, ...rest } = f;
|
|
40
|
+
const extra = Object.keys(rest);
|
|
41
|
+
if (extra.length) return `${key}: \uBAA8\uB974\uB294 \uD0A4 ${extra.join(", ")}`;
|
|
42
|
+
if (typeof path !== "string" || !isAbsolute(path)) return `${key}.path \uB294 \uC808\uB300 \uACBD\uB85C\uC785\uB2C8\uB2E4 (\uBC1B\uC740 \uAC12: ${JSON.stringify(path)})`;
|
|
43
|
+
if (typeof sha256 !== "string" || !/^[0-9a-fA-F]{64}$/.test(sha256)) return `${key}.sha256 \uC740 16\uC9C4 64\uC790\uC785\uB2C8\uB2E4`;
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
validateOcrOptions
|
|
49
|
+
};
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { createRequire as __hwpx2fRequire } from 'node:module'; const require = __hwpx2fRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
parseHwp,
|
|
4
|
+
parseHwpx
|
|
5
|
+
} from "./chunk-WEOKEO6L.js";
|
|
6
|
+
|
|
7
|
+
// src/index.ts
|
|
8
|
+
import { lstat, mkdir, readFile, realpath, stat, writeFile } from "node:fs/promises";
|
|
9
|
+
import { createRequire } from "node:module";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import path2 from "node:path";
|
|
12
|
+
import { HwpDocument, initSync } from "@rhwp/core";
|
|
13
|
+
|
|
14
|
+
// src/pdf.ts
|
|
15
|
+
import { existsSync, readdirSync } from "node:fs";
|
|
16
|
+
import os from "node:os";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import PDFDocument from "pdfkit";
|
|
19
|
+
import SVGtoPDF from "svg-to-pdfkit";
|
|
20
|
+
import * as fontkit from "fontkit";
|
|
21
|
+
var FONT_FILES = [
|
|
22
|
+
"NanumGothic.ttf",
|
|
23
|
+
"NanumBarunGothic.ttf",
|
|
24
|
+
"malgun.ttf",
|
|
25
|
+
"AppleSDGothicNeo.ttc",
|
|
26
|
+
"NotoSansKR-Regular.ttf",
|
|
27
|
+
"NotoSansKR-Regular.otf",
|
|
28
|
+
"NotoSansCJKkr-Regular.otf",
|
|
29
|
+
"NotoSansCJK-Regular.ttc",
|
|
30
|
+
"SourceHanSansKR-Regular.otf",
|
|
31
|
+
"UnDotum.ttf",
|
|
32
|
+
"AppleGothic.ttf",
|
|
33
|
+
"gulim.ttc"
|
|
34
|
+
];
|
|
35
|
+
function fontDirs() {
|
|
36
|
+
const home = os.homedir();
|
|
37
|
+
const win = process.env.WINDIR || process.env.SystemRoot || "C:\\Windows";
|
|
38
|
+
return [
|
|
39
|
+
"/usr/share/fonts",
|
|
40
|
+
"/usr/local/share/fonts",
|
|
41
|
+
path.join(home, ".local/share/fonts"),
|
|
42
|
+
path.join(home, ".fonts"),
|
|
43
|
+
"/System/Library/Fonts",
|
|
44
|
+
"/Library/Fonts",
|
|
45
|
+
path.join(home, "Library/Fonts"),
|
|
46
|
+
path.join(win, "Fonts"),
|
|
47
|
+
path.join(process.env.LOCALAPPDATA || home, "Microsoft/Windows/Fonts")
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
function findFiles(dir, depth, found) {
|
|
51
|
+
let entries;
|
|
52
|
+
try {
|
|
53
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
54
|
+
} catch {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
for (const e of entries) {
|
|
58
|
+
const p = path.join(dir, e.name);
|
|
59
|
+
if (e.isDirectory() && depth > 0) findFiles(p, depth - 1, found);
|
|
60
|
+
else if (FONT_FILES.includes(e.name) && !found.has(e.name)) found.set(e.name, p);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function pickFont() {
|
|
64
|
+
const candidates = [];
|
|
65
|
+
if (process.env.HWPX2F_FONT) candidates.push(process.env.HWPX2F_FONT);
|
|
66
|
+
else {
|
|
67
|
+
const found = /* @__PURE__ */ new Map();
|
|
68
|
+
for (const d of fontDirs()) if (existsSync(d)) findFiles(d, 4, found);
|
|
69
|
+
for (const name of FONT_FILES) if (found.has(name)) candidates.push(found.get(name));
|
|
70
|
+
}
|
|
71
|
+
for (const file of candidates) {
|
|
72
|
+
try {
|
|
73
|
+
const f = fontkit.openSync(file);
|
|
74
|
+
const faces = f.fonts ?? [f];
|
|
75
|
+
const face = faces.find((x) => /kr/i.test(x.postscriptName) && x.hasGlyphForCodePoint(44032)) ?? faces.find((x) => x.hasGlyphForCodePoint(44032));
|
|
76
|
+
if (face) return [file, f.fonts ? face.postscriptName : void 0];
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (process.env.HWPX2F_FONT) throw new Hwpx2fError(`HWPX2F_FONT is not a readable font with Korean (Hangul) glyphs: ${process.env.HWPX2F_FONT}`);
|
|
81
|
+
throw new Hwpx2fError("PDF output needs a font with Korean (Hangul) glyphs, but none was found. Install one (e.g. Nanum Gothic or Noto Sans CJK KR) or set HWPX2F_FONT=/path/to/font.ttf");
|
|
82
|
+
}
|
|
83
|
+
async function toPdf(doc) {
|
|
84
|
+
const [fontFile, psName] = pickFont();
|
|
85
|
+
const pdf = new PDFDocument({ autoFirstPage: false });
|
|
86
|
+
try {
|
|
87
|
+
pdf.registerFont("kr", fontFile, psName);
|
|
88
|
+
pdf.font("kr");
|
|
89
|
+
} catch {
|
|
90
|
+
throw new Hwpx2fError(`could not load the font ${fontFile}`);
|
|
91
|
+
}
|
|
92
|
+
const chunks = [];
|
|
93
|
+
pdf.on("data", (c) => chunks.push(c));
|
|
94
|
+
const done = new Promise((resolve, reject) => {
|
|
95
|
+
pdf.on("end", resolve);
|
|
96
|
+
pdf.on("error", reject);
|
|
97
|
+
});
|
|
98
|
+
const pages = doc.pageCount();
|
|
99
|
+
for (let i = 0; i < pages; i++) {
|
|
100
|
+
const svg = doc.renderPageSvg(i);
|
|
101
|
+
const m = /<svg[^>]*\swidth="([\d.]+)"[^>]*\sheight="([\d.]+)"/.exec(svg);
|
|
102
|
+
if (!m) throw new Error("unexpected SVG from renderer");
|
|
103
|
+
const [w, h] = [Number(m[1]) * 0.75, Number(m[2]) * 0.75];
|
|
104
|
+
pdf.addPage({ size: [w, h], margin: 0 });
|
|
105
|
+
SVGtoPDF(pdf, svg, 0, 0, {
|
|
106
|
+
width: w,
|
|
107
|
+
height: h,
|
|
108
|
+
// The renderer asks for Hancom/Windows fonts by name; draw all text with the one Korean font found.
|
|
109
|
+
fontCallback: (_family, bold, italic, o) => {
|
|
110
|
+
o.fauxBold = bold;
|
|
111
|
+
o.fauxItalic = italic;
|
|
112
|
+
return "kr";
|
|
113
|
+
},
|
|
114
|
+
// Only embedded data: images. Anything else (file paths, URLs) would make pdfkit read local files.
|
|
115
|
+
imageCallback: (href) => /^data:image\/(png|jpeg);base64,/i.test(href) ? href.replace(/\s+/g, "") : "",
|
|
116
|
+
warningCallback: (w2) => process.env.HWPX2F_DEBUG && console.error(w2)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
pdf.end();
|
|
120
|
+
await done;
|
|
121
|
+
return Buffer.concat(chunks);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// src/word.ts
|
|
125
|
+
import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, TextRun, WidthType } from "docx";
|
|
126
|
+
var HEADINGS = [
|
|
127
|
+
HeadingLevel.HEADING_1,
|
|
128
|
+
HeadingLevel.HEADING_2,
|
|
129
|
+
HeadingLevel.HEADING_3,
|
|
130
|
+
HeadingLevel.HEADING_4,
|
|
131
|
+
HeadingLevel.HEADING_5,
|
|
132
|
+
HeadingLevel.HEADING_6
|
|
133
|
+
];
|
|
134
|
+
function runs(b) {
|
|
135
|
+
const spans = b.spans?.length ? b.spans.filter((s) => !s.placeholder) : [{ text: b.text ?? "" }];
|
|
136
|
+
return spans.flatMap((s) => s.text.split("\n").map((t, i) => new TextRun({
|
|
137
|
+
text: t,
|
|
138
|
+
break: i > 0 ? 1 : void 0,
|
|
139
|
+
bold: "bold" in s ? s.bold : void 0,
|
|
140
|
+
italics: "italic" in s ? s.italic : void 0,
|
|
141
|
+
strike: "strike" in s ? s.strike : void 0,
|
|
142
|
+
underline: "underline" in s && s.underline ? {} : void 0
|
|
143
|
+
})));
|
|
144
|
+
}
|
|
145
|
+
function table(t) {
|
|
146
|
+
const covered = /* @__PURE__ */ new Set();
|
|
147
|
+
const rows = [];
|
|
148
|
+
for (let r = 0; r < t.rows; r++) {
|
|
149
|
+
const cells = [];
|
|
150
|
+
for (let c = 0; c < t.cols; c++) {
|
|
151
|
+
const cell = t.cells[r]?.[c];
|
|
152
|
+
if (!cell || covered.has(`${r},${c}`)) continue;
|
|
153
|
+
const [rowSpan, columnSpan] = [Math.max(1, cell.rowSpan), Math.max(1, cell.colSpan)];
|
|
154
|
+
for (let dr = 0; dr < rowSpan; dr++) for (let dc = 0; dc < columnSpan; dc++) covered.add(`${r + dr},${c + dc}`);
|
|
155
|
+
const content = cell.blocks?.length ? blocks(cell.blocks) : cell.text.split("\n").map((x) => new Paragraph(x));
|
|
156
|
+
if (!(content.at(-1) instanceof Paragraph)) content.push(new Paragraph(""));
|
|
157
|
+
cells.push(new TableCell({ children: content, columnSpan, rowSpan }));
|
|
158
|
+
}
|
|
159
|
+
rows.push(new TableRow({ children: cells, tableHeader: r === 0 && t.hasHeader }));
|
|
160
|
+
}
|
|
161
|
+
return new Table({ rows, width: { size: 100, type: WidthType.PERCENTAGE } });
|
|
162
|
+
}
|
|
163
|
+
function blocks(list) {
|
|
164
|
+
const out = [];
|
|
165
|
+
for (const b of list) {
|
|
166
|
+
if (b.type === "heading") out.push(new Paragraph({ heading: HEADINGS[Math.min(Math.max(b.level ?? 1, 1), 6) - 1], children: runs(b) }));
|
|
167
|
+
else if (b.type === "table" && b.table) {
|
|
168
|
+
if (b.table.caption) out.push(new Paragraph(b.table.caption));
|
|
169
|
+
out.push(table(b.table));
|
|
170
|
+
} else if (b.type === "list") {
|
|
171
|
+
for (const item of [b, ...b.children ?? []]) {
|
|
172
|
+
const level = item === b ? 0 : 1;
|
|
173
|
+
out.push(item.listType === "ordered" ? new Paragraph({ children: runs(item), indent: { left: 360 * (level + 1) } }) : new Paragraph({ children: runs(item), bullet: { level } }));
|
|
174
|
+
}
|
|
175
|
+
} else if (b.type === "separator") out.push(new Paragraph({ thematicBreak: true }));
|
|
176
|
+
else if (b.type === "paragraph") out.push(new Paragraph({ children: runs(b) }));
|
|
177
|
+
}
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
async function toDocx(list) {
|
|
181
|
+
const doc = new Document({ sections: [{ children: blocks(list) }] });
|
|
182
|
+
return Packer.toBuffer(doc);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// src/index.ts
|
|
186
|
+
var FORMATS = ["hwp", "hwpx", "md", "pdf", "word"];
|
|
187
|
+
var EXT = { hwp: "hwp", hwpx: "hwpx", md: "md", pdf: "pdf", word: "docx" };
|
|
188
|
+
var MAX_INPUT_BYTES = 200 * 1024 * 1024;
|
|
189
|
+
var MAX_XML_BYTES = 32 * 1024 * 1024;
|
|
190
|
+
function declaredXmlBytes(b) {
|
|
191
|
+
const v = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
192
|
+
let eocd = b.byteLength - 22;
|
|
193
|
+
while (eocd >= 0 && v.getUint32(eocd, true) !== 101010256) eocd--;
|
|
194
|
+
if (eocd < 0) return Infinity;
|
|
195
|
+
let p = v.getUint32(eocd + 16, true);
|
|
196
|
+
let total = 0;
|
|
197
|
+
for (let n = v.getUint16(eocd + 10, true); n > 0 && p + 46 <= b.byteLength && v.getUint32(p, true) === 33639248; n--) {
|
|
198
|
+
const nameLen = v.getUint16(p + 28, true);
|
|
199
|
+
if (/\.xml$/i.test(Buffer.from(b.subarray(p + 46, p + 46 + nameLen)).toString("latin1"))) total += v.getUint32(p + 24, true);
|
|
200
|
+
p += 46 + nameLen + v.getUint16(p + 30, true) + v.getUint16(p + 32, true);
|
|
201
|
+
}
|
|
202
|
+
return total;
|
|
203
|
+
}
|
|
204
|
+
var Hwpx2fError = class extends Error {
|
|
205
|
+
};
|
|
206
|
+
function detect(head) {
|
|
207
|
+
const cfb = [208, 207, 17, 224, 161, 177, 26, 225];
|
|
208
|
+
if (cfb.every((b, i) => head[i] === b)) return "hwp";
|
|
209
|
+
if (head[0] === 80 && head[1] === 75 && head[2] === 3 && head[3] === 4) {
|
|
210
|
+
return Buffer.from(head.subarray(0, 512)).toString("latin1").includes("application/hwp+zip") ? "hwpx" : null;
|
|
211
|
+
}
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
async function resolveOutput(inputPath, format, outPath) {
|
|
215
|
+
const name = path2.parse(inputPath).name + "." + EXT[format];
|
|
216
|
+
if (!outPath) return path2.join(path2.dirname(path2.resolve(inputPath)), name);
|
|
217
|
+
const isDir = /[\\/]$/.test(outPath) || await stat(outPath).then((s) => s.isDirectory(), () => false);
|
|
218
|
+
if (isDir || !path2.extname(outPath)) return path2.join(path2.resolve(outPath), name);
|
|
219
|
+
return path2.resolve(outPath);
|
|
220
|
+
}
|
|
221
|
+
var ready = false;
|
|
222
|
+
function initRhwp() {
|
|
223
|
+
if (ready) return;
|
|
224
|
+
const wasm = readFileSync(createRequire(import.meta.url).resolve("@rhwp/core/rhwp_bg.wasm"));
|
|
225
|
+
initSync({ module: wasm });
|
|
226
|
+
ready = true;
|
|
227
|
+
}
|
|
228
|
+
async function markdownAndBlocks(bytes, type) {
|
|
229
|
+
const buf = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
230
|
+
const opts = { images: false, ocr: false, hancomFallback: false };
|
|
231
|
+
const r = type === "hwp" ? await parseHwp(buf, opts) : await parseHwpx(buf, opts);
|
|
232
|
+
if (!r.success) throw new Error(`kordoc: ${r.code ?? ""} ${r.error ?? ""}`);
|
|
233
|
+
return r;
|
|
234
|
+
}
|
|
235
|
+
async function render(bytes, type, format) {
|
|
236
|
+
if (format === "md") {
|
|
237
|
+
const md = (await markdownAndBlocks(bytes, type)).markdown.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f\x9b]/g, "").replace(/!(?=\[[^\]]*\]\(\s*(?:https?:)?\/\/)/g, "\\!");
|
|
238
|
+
return Buffer.from(md + "\n", "utf8");
|
|
239
|
+
}
|
|
240
|
+
if (format === "word") return toDocx((await markdownAndBlocks(bytes, type)).blocks);
|
|
241
|
+
initRhwp();
|
|
242
|
+
const doc = new HwpDocument(bytes);
|
|
243
|
+
try {
|
|
244
|
+
if (format === "hwp") return doc.exportHwp();
|
|
245
|
+
if (format === "hwpx") return doc.exportHwpx();
|
|
246
|
+
return await toPdf(doc);
|
|
247
|
+
} finally {
|
|
248
|
+
doc.free();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
async function convert(inputPath, format, outPath, force = false) {
|
|
252
|
+
if (!FORMATS.includes(format)) throw new Hwpx2fError(`unknown format "${format}" (expected one of: ${FORMATS.join(", ")})`);
|
|
253
|
+
const info = await stat(inputPath).catch(() => null);
|
|
254
|
+
if (!info || !info.isFile()) throw new Hwpx2fError(`input file not found: ${inputPath}`);
|
|
255
|
+
if (info.size > MAX_INPUT_BYTES) throw new Hwpx2fError(`input file is too large (${info.size} bytes, limit ${MAX_INPUT_BYTES})`);
|
|
256
|
+
const bytes = new Uint8Array(await readFile(inputPath));
|
|
257
|
+
if (bytes.byteLength > MAX_INPUT_BYTES) throw new Hwpx2fError("input file is too large");
|
|
258
|
+
const type = detect(bytes);
|
|
259
|
+
if (!type) throw new Hwpx2fError(`not an HWP 5.x or HWPX document: ${inputPath}`);
|
|
260
|
+
if (type === format) throw new Hwpx2fError(`input is already ${format}; choose a different target format`);
|
|
261
|
+
if (type === "hwpx" && declaredXmlBytes(bytes) > MAX_XML_BYTES) throw new Hwpx2fError("document content is too large to convert safely");
|
|
262
|
+
const out = await resolveOutput(inputPath, format, outPath);
|
|
263
|
+
const inputReal = await realpath(inputPath);
|
|
264
|
+
const outReal = await realpath(out).catch(() => out);
|
|
265
|
+
if (outReal === inputReal) throw new Hwpx2fError("refusing to overwrite the input file");
|
|
266
|
+
const existing = await lstat(out).catch(() => null);
|
|
267
|
+
if (existing?.isSymbolicLink()) throw new Hwpx2fError(`refusing to write through a symbolic link: ${out}`);
|
|
268
|
+
if (existing && existing.ino === info.ino && existing.dev === info.dev) throw new Hwpx2fError("refusing to overwrite the input file");
|
|
269
|
+
if (existing && !force) throw new Hwpx2fError(`output already exists: ${out} (use --force to replace it)`);
|
|
270
|
+
let data;
|
|
271
|
+
try {
|
|
272
|
+
data = await render(bytes, type, format);
|
|
273
|
+
} catch (e) {
|
|
274
|
+
if (e instanceof Hwpx2fError) throw e;
|
|
275
|
+
throw new Hwpx2fError(`could not convert ${path2.basename(inputPath)} to ${format}: the document could not be read (damaged, encrypted, or not a supported HWP/HWPX file)`, { cause: e });
|
|
276
|
+
}
|
|
277
|
+
if (!data.byteLength) throw new Hwpx2fError(`conversion to ${format} produced no output`);
|
|
278
|
+
await mkdir(path2.dirname(out), { recursive: true });
|
|
279
|
+
await writeFile(out, data, { flag: force ? "w" : "wx" }).catch((e) => {
|
|
280
|
+
if (e?.code === "EEXIST") throw new Hwpx2fError(`output already exists: ${out} (use --force to replace it)`);
|
|
281
|
+
throw e;
|
|
282
|
+
});
|
|
283
|
+
return out;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export {
|
|
287
|
+
FORMATS,
|
|
288
|
+
Hwpx2fError,
|
|
289
|
+
convert
|
|
290
|
+
};
|