kordoc 2.2.4 → 2.2.6

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 (41) hide show
  1. package/README.md +43 -4
  2. package/dist/chunk-FCQEF2ZM.js +457 -0
  3. package/dist/chunk-FCQEF2ZM.js.map +1 -0
  4. package/dist/chunk-HXUCZ2IL.cjs +450 -0
  5. package/dist/chunk-HXUCZ2IL.cjs.map +1 -0
  6. package/dist/chunk-MUOQXDZ4.cjs +33 -0
  7. package/dist/chunk-MUOQXDZ4.cjs.map +1 -0
  8. package/dist/chunk-NL5XLN5R.js +450 -0
  9. package/dist/chunk-NL5XLN5R.js.map +1 -0
  10. package/dist/{chunk-SY2RFVLW.js → chunk-RF6UJXR3.js} +135 -2805
  11. package/dist/chunk-RF6UJXR3.js.map +1 -0
  12. package/dist/chunk-SBVRCJFH.js +33 -0
  13. package/dist/chunk-SBVRCJFH.js.map +1 -0
  14. package/dist/cli.js +12 -7
  15. package/dist/cli.js.map +1 -1
  16. package/dist/index.cjs +294 -3084
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.js +77 -2817
  21. package/dist/index.js.map +1 -1
  22. package/dist/mcp.js +15 -9
  23. package/dist/mcp.js.map +1 -1
  24. package/dist/page-range-3C7UGGEK.cjs +7 -0
  25. package/dist/page-range-3C7UGGEK.cjs.map +1 -0
  26. package/dist/page-range-H35FN3OQ.js +7 -0
  27. package/dist/page-range-H35FN3OQ.js.map +1 -0
  28. package/dist/parser-43IAQ5KE.js +2278 -0
  29. package/dist/parser-43IAQ5KE.js.map +1 -0
  30. package/dist/parser-AMP7MAOH.js +2279 -0
  31. package/dist/parser-AMP7MAOH.js.map +1 -0
  32. package/dist/parser-KOWPTDJU.cjs +2278 -0
  33. package/dist/parser-KOWPTDJU.cjs.map +1 -0
  34. package/dist/provider-WPIYEALY.js +37 -0
  35. package/dist/provider-WPIYEALY.js.map +1 -0
  36. package/dist/provider-YN2SSK4X.cjs +37 -0
  37. package/dist/provider-YN2SSK4X.cjs.map +1 -0
  38. package/dist/{watch-5P7DJ3HG.js → watch-IUQXOXW3.js} +6 -4
  39. package/dist/{watch-5P7DJ3HG.js.map → watch-IUQXOXW3.js.map} +1 -1
  40. package/package.json +1 -1
  41. package/dist/chunk-SY2RFVLW.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,93 +1,25 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropNames = Object.getOwnPropertyNames;
3
- var __esm = (fn, res) => function __init() {
4
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
5
- };
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
-
11
- // src/page-range.ts
12
- var page_range_exports = {};
13
- __export(page_range_exports, {
14
- parsePageRange: () => parsePageRange
15
- });
16
- function parsePageRange(spec, maxPages) {
17
- const result = /* @__PURE__ */ new Set();
18
- if (maxPages <= 0) return result;
19
- if (Array.isArray(spec)) {
20
- for (const n of spec) {
21
- const page = Math.round(n);
22
- if (page >= 1 && page <= maxPages) result.add(page);
23
- }
24
- return result;
25
- }
26
- if (typeof spec !== "string" || spec.trim() === "") return result;
27
- const parts = spec.split(",");
28
- for (const part of parts) {
29
- const trimmed = part.trim();
30
- if (!trimmed) continue;
31
- const rangeMatch = trimmed.match(/^(\d+)\s*-\s*(\d+)$/);
32
- if (rangeMatch) {
33
- const start = Math.max(1, parseInt(rangeMatch[1], 10));
34
- const end = Math.min(maxPages, parseInt(rangeMatch[2], 10));
35
- for (let i = start; i <= end; i++) result.add(i);
36
- } else {
37
- const page = parseInt(trimmed, 10);
38
- if (!isNaN(page) && page >= 1 && page <= maxPages) result.add(page);
39
- }
40
- }
41
- return result;
42
- }
43
- var init_page_range = __esm({
44
- "src/page-range.ts"() {
45
- "use strict";
46
- }
47
- });
48
-
49
- // src/ocr/provider.ts
50
- var provider_exports = {};
51
- __export(provider_exports, {
52
- ocrPages: () => ocrPages
53
- });
54
- async function ocrPages(doc, provider, pageFilter, effectivePageCount) {
55
- const blocks = [];
56
- for (let i = 1; i <= effectivePageCount; i++) {
57
- if (pageFilter && !pageFilter.has(i)) continue;
58
- const page = await doc.getPage(i);
59
- try {
60
- const imageData = await renderPageToPng(page);
61
- const text = await provider(imageData, i, "image/png");
62
- if (text.trim()) {
63
- blocks.push({ type: "paragraph", text: text.trim(), pageNumber: i });
64
- }
65
- } catch {
66
- blocks.push({ type: "paragraph", text: `[OCR \uC2E4\uD328: \uD398\uC774\uC9C0 ${i}]` });
67
- }
68
- }
69
- return blocks;
70
- }
71
- async function renderPageToPng(page) {
72
- let createCanvas;
73
- try {
74
- const canvasModule = await import("canvas");
75
- createCanvas = canvasModule.createCanvas;
76
- } catch {
77
- throw new Error("OCR\uC744 \uC0AC\uC6A9\uD558\uB824\uBA74 'canvas' \uD328\uD0A4\uC9C0\uB97C \uC124\uCE58\uD558\uC138\uC694: npm install canvas");
78
- }
79
- const scale = 2;
80
- const viewport = page.getViewport({ scale });
81
- const canvas = createCanvas(Math.floor(viewport.width), Math.floor(viewport.height));
82
- const ctx = canvas.getContext("2d");
83
- await page.render({ canvasContext: ctx, viewport }).promise;
84
- return new Uint8Array(canvas.toBuffer("image/png"));
85
- }
86
- var init_provider = __esm({
87
- "src/ocr/provider.ts"() {
88
- "use strict";
89
- }
90
- });
1
+ import {
2
+ HEADING_RATIO_H1,
3
+ HEADING_RATIO_H2,
4
+ HEADING_RATIO_H3,
5
+ KordocError,
6
+ MAX_COLS,
7
+ MAX_ROWS,
8
+ VERSION,
9
+ blocksToMarkdown,
10
+ buildTable,
11
+ classifyError,
12
+ convertTableToText,
13
+ flattenLayoutTables,
14
+ isPathTraversal,
15
+ precheckZipSize,
16
+ sanitizeHref,
17
+ stripDtd,
18
+ toArrayBuffer
19
+ } from "./chunk-NL5XLN5R.js";
20
+ import {
21
+ parsePageRange
22
+ } from "./chunk-SBVRCJFH.js";
91
23
 
92
24
  // src/index.ts
93
25
  import { readFile } from "fs/promises";
@@ -137,437 +69,6 @@ async function detectZipFormat(buffer) {
137
69
  import JSZip2 from "jszip";
138
70
  import { inflateRawSync } from "zlib";
139
71
  import { DOMParser } from "@xmldom/xmldom";
140
-
141
- // src/utils.ts
142
- var VERSION = true ? "2.2.4" : "0.0.0-dev";
143
- function toArrayBuffer(buf) {
144
- if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
145
- return buf.buffer;
146
- }
147
- return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
148
- }
149
- var KordocError = class extends Error {
150
- constructor(message) {
151
- super(message);
152
- this.name = "KordocError";
153
- }
154
- };
155
- function isPathTraversal(name) {
156
- if (name.includes("\0")) return true;
157
- const normalized = name.replace(/\\/g, "/");
158
- const segments = normalized.split("/");
159
- return segments.some((s) => s === "..") || normalized.startsWith("/") || /^[A-Za-z]:/.test(normalized);
160
- }
161
- function precheckZipSize(buffer, maxUncompressedSize = 100 * 1024 * 1024, maxEntries = 500) {
162
- try {
163
- const data = new DataView(buffer);
164
- const len = buffer.byteLength;
165
- let eocdOffset = -1;
166
- for (let i = len - 22; i >= Math.max(0, len - 65557); i--) {
167
- if (data.getUint32(i, true) === 101010256) {
168
- eocdOffset = i;
169
- break;
170
- }
171
- }
172
- if (eocdOffset < 0) return { totalUncompressed: 0, entryCount: 0 };
173
- const entryCount = data.getUint16(eocdOffset + 10, true);
174
- if (entryCount > maxEntries) {
175
- throw new KordocError(`ZIP \uC5D4\uD2B8\uB9AC \uC218 \uCD08\uACFC: ${entryCount} (\uCD5C\uB300 ${maxEntries})`);
176
- }
177
- const cdSize = data.getUint32(eocdOffset + 12, true);
178
- const cdOffset = data.getUint32(eocdOffset + 16, true);
179
- if (cdOffset + cdSize > len) return { totalUncompressed: 0, entryCount };
180
- let totalUncompressed = 0;
181
- let pos = cdOffset;
182
- for (let i = 0; i < entryCount && pos + 46 <= cdOffset + cdSize; i++) {
183
- if (data.getUint32(pos, true) !== 33639248) break;
184
- totalUncompressed += data.getUint32(pos + 24, true);
185
- const nameLen = data.getUint16(pos + 28, true);
186
- const extraLen = data.getUint16(pos + 30, true);
187
- const commentLen = data.getUint16(pos + 32, true);
188
- pos += 46 + nameLen + extraLen + commentLen;
189
- }
190
- if (totalUncompressed > maxUncompressedSize) {
191
- throw new KordocError(`ZIP \uBE44\uC555\uCD95 \uD06C\uAE30 \uCD08\uACFC: ${(totalUncompressed / 1024 / 1024).toFixed(1)}MB (\uCD5C\uB300 ${maxUncompressedSize / 1024 / 1024}MB)`);
192
- }
193
- return { totalUncompressed, entryCount };
194
- } catch (err) {
195
- if (err instanceof KordocError) throw err;
196
- return { totalUncompressed: 0, entryCount: 0 };
197
- }
198
- }
199
- function stripDtd(xml) {
200
- return xml.replace(/<!DOCTYPE\s[^[>]*(\[[\s\S]*?\])?\s*>/gi, "");
201
- }
202
- var SAFE_HREF_RE = /^(?:https?:|mailto:|tel:|#)/i;
203
- function sanitizeHref(href) {
204
- const trimmed = href.trim();
205
- if (!trimmed || !SAFE_HREF_RE.test(trimmed)) return null;
206
- return trimmed;
207
- }
208
- function safeMin(arr) {
209
- let min = Infinity;
210
- for (let i = 0; i < arr.length; i++) if (arr[i] < min) min = arr[i];
211
- return min;
212
- }
213
- function safeMax(arr) {
214
- let max = -Infinity;
215
- for (let i = 0; i < arr.length; i++) if (arr[i] > max) max = arr[i];
216
- return max;
217
- }
218
- function classifyError(err) {
219
- if (!(err instanceof Error)) return "PARSE_ERROR";
220
- const msg = err.message;
221
- if (msg.includes("\uC554\uD638\uD654")) return "ENCRYPTED";
222
- if (msg.includes("DRM")) return "DRM_PROTECTED";
223
- 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";
224
- if (msg.includes("bomb") || msg.includes("\uD06C\uAE30 \uCD08\uACFC") || msg.includes("\uC555\uCD95 \uD574\uC81C")) return "DECOMPRESSION_BOMB";
225
- if (msg.includes("\uC774\uBBF8\uC9C0 \uAE30\uBC18")) return "IMAGE_BASED_PDF";
226
- if (msg.includes("\uC139\uC158") && (msg.includes("\uCC3E\uC744 \uC218 \uC5C6") || msg.includes("\uC5C6\uC74C"))) return "NO_SECTIONS";
227
- if (msg.includes("\uC2DC\uADF8\uB2C8\uCC98") || msg.includes("\uBCF5\uAD6C\uD560 \uC218 \uC5C6")) return "CORRUPTED";
228
- return "PARSE_ERROR";
229
- }
230
-
231
- // src/table/builder.ts
232
- var MAX_COLS = 200;
233
- var MAX_ROWS = 1e4;
234
- function buildTable(rows) {
235
- if (rows.length > MAX_ROWS) rows = rows.slice(0, MAX_ROWS);
236
- const numRows = rows.length;
237
- const hasAddr = rows.some((row) => row.some((c) => c.colAddr !== void 0 && c.rowAddr !== void 0));
238
- if (hasAddr) return buildTableDirect(rows, numRows);
239
- let maxCols = 0;
240
- const tempOccupied = Array.from({ length: numRows }, () => []);
241
- for (let rowIdx = 0; rowIdx < numRows; rowIdx++) {
242
- let colIdx = 0;
243
- for (const cell of rows[rowIdx]) {
244
- while (colIdx < MAX_COLS && tempOccupied[rowIdx][colIdx]) colIdx++;
245
- if (colIdx >= MAX_COLS) break;
246
- for (let r = rowIdx; r < Math.min(rowIdx + cell.rowSpan, numRows); r++) {
247
- for (let c = colIdx; c < Math.min(colIdx + cell.colSpan, MAX_COLS); c++) {
248
- tempOccupied[r][c] = true;
249
- }
250
- }
251
- colIdx += cell.colSpan;
252
- if (colIdx > maxCols) maxCols = colIdx;
253
- }
254
- }
255
- if (maxCols === 0) return { rows: 0, cols: 0, cells: [], hasHeader: false };
256
- const grid = Array.from(
257
- { length: numRows },
258
- () => Array.from({ length: maxCols }, () => ({ text: "", colSpan: 1, rowSpan: 1 }))
259
- );
260
- const occupied = Array.from({ length: numRows }, () => Array(maxCols).fill(false));
261
- for (let rowIdx = 0; rowIdx < numRows; rowIdx++) {
262
- let colIdx = 0;
263
- let cellIdx = 0;
264
- while (colIdx < maxCols && cellIdx < rows[rowIdx].length) {
265
- while (colIdx < maxCols && occupied[rowIdx][colIdx]) colIdx++;
266
- if (colIdx >= maxCols) break;
267
- const cell = rows[rowIdx][cellIdx];
268
- grid[rowIdx][colIdx] = {
269
- text: cell.text.trim(),
270
- colSpan: cell.colSpan,
271
- rowSpan: cell.rowSpan
272
- };
273
- for (let r = rowIdx; r < Math.min(rowIdx + cell.rowSpan, numRows); r++) {
274
- for (let c = colIdx; c < Math.min(colIdx + cell.colSpan, maxCols); c++) {
275
- occupied[r][c] = true;
276
- }
277
- }
278
- colIdx += cell.colSpan;
279
- cellIdx++;
280
- }
281
- }
282
- return trimAndReturn(grid, numRows, maxCols);
283
- }
284
- function buildTableDirect(rows, numRows) {
285
- let maxCols = 0;
286
- for (const row of rows) {
287
- for (const cell of row) {
288
- const end = (cell.colAddr ?? 0) + cell.colSpan;
289
- if (end > maxCols) maxCols = end;
290
- }
291
- }
292
- if (maxCols > MAX_COLS) maxCols = MAX_COLS;
293
- if (maxCols === 0) return { rows: 0, cols: 0, cells: [], hasHeader: false };
294
- const grid = Array.from(
295
- { length: numRows },
296
- () => Array.from({ length: maxCols }, () => ({ text: "", colSpan: 1, rowSpan: 1 }))
297
- );
298
- for (const row of rows) {
299
- for (const cell of row) {
300
- const r = cell.rowAddr ?? 0;
301
- const c = cell.colAddr ?? 0;
302
- if (r >= numRows || c >= maxCols || r < 0 || c < 0) continue;
303
- grid[r][c] = { text: cell.text.trim(), colSpan: cell.colSpan, rowSpan: cell.rowSpan };
304
- for (let dr = 0; dr < cell.rowSpan; dr++) {
305
- for (let dc = 0; dc < cell.colSpan; dc++) {
306
- if (dr === 0 && dc === 0) continue;
307
- if (r + dr < numRows && c + dc < maxCols) {
308
- grid[r + dr][c + dc] = { text: "", colSpan: 1, rowSpan: 1 };
309
- }
310
- }
311
- }
312
- }
313
- }
314
- return trimAndReturn(grid, numRows, maxCols);
315
- }
316
- function trimAndReturn(grid, numRows, maxCols) {
317
- let effectiveCols = maxCols;
318
- while (effectiveCols > 0) {
319
- const colEmpty = grid.every((row) => !row[effectiveCols - 1]?.text?.trim());
320
- if (!colEmpty) break;
321
- effectiveCols--;
322
- }
323
- if (effectiveCols < maxCols && effectiveCols > 0) {
324
- const trimmed = grid.map((row) => row.slice(0, effectiveCols));
325
- return { rows: numRows, cols: effectiveCols, cells: trimmed, hasHeader: numRows > 1 };
326
- }
327
- return { rows: numRows, cols: maxCols, cells: grid, hasHeader: numRows > 1 };
328
- }
329
- function convertTableToText(rows) {
330
- return rows.map(
331
- (row) => row.map((c) => c.text.trim().replace(/\n/g, " ").replace(/\|/g, "\\|")).filter(Boolean).join(" / ")
332
- ).filter(Boolean).join("\n");
333
- }
334
- function escapeGfm(text) {
335
- return text.replace(/~/g, "\\~");
336
- }
337
- var HWP_SHAPE_ALT_TEXT_RE = /(?:모서리가 둥근 |둥근 )?(?:사각형|직사각형|정사각형|원|타원|삼각형|이등변 삼각형|직각 삼각형|선|직선|곡선|화살표|굵은 화살표|이중 화살표|오각형|육각형|팔각형|별|[4-8]점별|십자|십자형|구름|구름형|마름모|도넛|평행사변형|사다리꼴|부채꼴|호|반원|물결|번개|하트|빗금|블록 화살표|수식|표|그림|개체|그리기\s?개체|묶음\s?개체|글상자|수식\s?개체|OLE\s?개체)\s?입니다\.?/g;
338
- function sanitizeText(text) {
339
- let result = text.replace(/[\u{F0000}-\u{FFFFD}]/gu, "").replace(HWP_SHAPE_ALT_TEXT_RE, "").replace(/ +/g, " ").trim();
340
- if (result.length <= 30 && result.includes(" ")) {
341
- const tokens = result.split(" ");
342
- const koreanSingleCharCount = tokens.filter((t) => t.length === 1 && /[\uAC00-\uD7AF\u3131-\u318E]/.test(t)).length;
343
- if (tokens.length >= 3 && koreanSingleCharCount / tokens.length >= 0.7) {
344
- result = tokens.join("");
345
- }
346
- }
347
- return result;
348
- }
349
- function flattenLayoutTables(blocks) {
350
- const result = [];
351
- for (const block of blocks) {
352
- if (block.type !== "table" || !block.table) {
353
- result.push(block);
354
- continue;
355
- }
356
- const { rows: numRows, cols: numCols, cells } = block.table;
357
- if (numRows === 1 && numCols === 1) {
358
- result.push(block);
359
- continue;
360
- }
361
- if (numRows <= 3) {
362
- let totalNewlines = 0;
363
- let totalTextLen = 0;
364
- for (let r = 0; r < numRows; r++) {
365
- for (let c = 0; c < numCols; c++) {
366
- const t = cells[r]?.[c]?.text || "";
367
- totalNewlines += (t.match(/\n/g) || []).length;
368
- totalTextLen += t.length;
369
- }
370
- }
371
- if (totalNewlines > 5 || numRows <= 2 && totalTextLen > 300) {
372
- for (let r = 0; r < numRows; r++) {
373
- for (let c = 0; c < numCols; c++) {
374
- const cellText = cells[r]?.[c]?.text?.trim();
375
- if (!cellText) continue;
376
- for (const line of cellText.split("\n")) {
377
- const trimmed = line.trim();
378
- if (!trimmed) continue;
379
- result.push({ type: "paragraph", text: trimmed, pageNumber: block.pageNumber });
380
- }
381
- }
382
- }
383
- continue;
384
- }
385
- }
386
- result.push(block);
387
- }
388
- return result;
389
- }
390
- function blocksToMarkdown(blocks) {
391
- const lines = [];
392
- for (let i = 0; i < blocks.length; i++) {
393
- const block = blocks[i];
394
- if (block.type === "heading" && block.text) {
395
- const prefix = "#".repeat(Math.min(block.level || 2, 6));
396
- const headingText = sanitizeText(block.text);
397
- if (headingText) lines.push("", `${prefix} ${headingText}`, "");
398
- continue;
399
- }
400
- if (block.type === "image" && block.text) {
401
- lines.push("", `![image](${block.text})`, "");
402
- continue;
403
- }
404
- if (block.type === "separator") {
405
- lines.push("", "---", "");
406
- continue;
407
- }
408
- if (block.type === "list" && block.text) {
409
- const listText = sanitizeText(block.text);
410
- if (!listText) continue;
411
- const alreadyNumbered = block.listType === "ordered" && /^\d+\.\s/.test(listText);
412
- const prefix = alreadyNumbered ? "" : block.listType === "ordered" ? "1. " : "- ";
413
- lines.push(`${prefix}${listText}`);
414
- if (block.children) {
415
- for (const child of block.children) {
416
- const childPrefix = child.listType === "ordered" ? "1." : "-";
417
- lines.push(` ${childPrefix} ${child.text || ""}`);
418
- }
419
- }
420
- continue;
421
- }
422
- if (block.type === "paragraph" && block.text) {
423
- let text = sanitizeText(block.text);
424
- if (!text) continue;
425
- if (/^\[별표\s*\d+/.test(text)) {
426
- const nextBlock = blocks[i + 1];
427
- if (nextBlock?.type === "paragraph" && nextBlock.text && /관련\)?$/.test(nextBlock.text)) {
428
- lines.push("", `## ${text} ${nextBlock.text}`, "");
429
- i++;
430
- } else {
431
- lines.push("", `## ${text}`, "");
432
- }
433
- continue;
434
- }
435
- if (/^\([^)]*조[^)]*관련\)$/.test(text)) {
436
- lines.push(`*${text}*`, "");
437
- continue;
438
- }
439
- if (block.href) {
440
- const href = sanitizeHref(block.href);
441
- if (href) text = `[${text}](${href})`;
442
- }
443
- if (block.footnoteText) {
444
- text += ` (\uC8FC: ${block.footnoteText})`;
445
- }
446
- lines.push(escapeGfm(text), "");
447
- } else if (block.type === "table" && block.table) {
448
- if (lines.length > 0 && lines[lines.length - 1] !== "") {
449
- lines.push("");
450
- }
451
- const tableMd = tableToMarkdown(block.table);
452
- if (tableMd) {
453
- lines.push(tableMd);
454
- lines.push("");
455
- }
456
- }
457
- }
458
- return lines.join("\n").trim();
459
- }
460
- function hasMergedCells(table) {
461
- for (const row of table.cells) {
462
- for (const cell of row) {
463
- if (cell.colSpan > 1 || cell.rowSpan > 1) return true;
464
- }
465
- }
466
- return false;
467
- }
468
- function tableToHtml(table) {
469
- const { cells, rows: numRows, cols: numCols } = table;
470
- const skip = /* @__PURE__ */ new Set();
471
- const lines = ["<table>"];
472
- for (let r = 0; r < numRows; r++) {
473
- const tag = r === 0 ? "th" : "td";
474
- const rowHtml = [];
475
- for (let c = 0; c < numCols; c++) {
476
- if (skip.has(`${r},${c}`)) continue;
477
- const cell = cells[r]?.[c];
478
- if (!cell) continue;
479
- for (let dr = 0; dr < cell.rowSpan; dr++) {
480
- for (let dc = 0; dc < cell.colSpan; dc++) {
481
- if (dr === 0 && dc === 0) continue;
482
- if (r + dr < numRows && c + dc < numCols) skip.add(`${r + dr},${c + dc}`);
483
- }
484
- }
485
- const text = sanitizeText(cell.text).replace(/\n/g, "<br>");
486
- const attrs = [];
487
- if (cell.colSpan > 1) attrs.push(`colspan="${cell.colSpan}"`);
488
- if (cell.rowSpan > 1) attrs.push(`rowspan="${cell.rowSpan}"`);
489
- const attrStr = attrs.length ? " " + attrs.join(" ") : "";
490
- rowHtml.push(`<${tag}${attrStr}>${text}</${tag}>`);
491
- }
492
- if (rowHtml.length) lines.push(`<tr>${rowHtml.join("")}</tr>`);
493
- }
494
- lines.push("</table>");
495
- return lines.join("\n");
496
- }
497
- function tableToMarkdown(table) {
498
- if (table.rows === 0 || table.cols === 0) return "";
499
- const { cells, rows: numRows, cols: numCols } = table;
500
- if (hasMergedCells(table)) return tableToHtml(table);
501
- if (numRows === 1 && numCols === 1) {
502
- const content = sanitizeText(cells[0][0].text);
503
- if (!content) return "";
504
- return content.split(/\n/).map((line) => {
505
- const trimmed = line.trim();
506
- if (!trimmed) return "";
507
- if (/^\d+\.\s/.test(trimmed)) return `**${escapeGfm(trimmed)}**`;
508
- if (/^[가-힣]\.\s/.test(trimmed)) return ` ${escapeGfm(trimmed)}`;
509
- return escapeGfm(trimmed);
510
- }).filter(Boolean).join("\n");
511
- }
512
- if (numCols === 1 && numRows >= 2) {
513
- return cells.map((row) => escapeGfm(sanitizeText(row[0].text)).replace(/\n/g, " ")).filter(Boolean).join("\n");
514
- }
515
- const display = Array.from({ length: numRows }, () => Array(numCols).fill(""));
516
- const skip = /* @__PURE__ */ new Set();
517
- for (let r = 0; r < numRows; r++) {
518
- for (let c = 0; c < numCols; c++) {
519
- if (skip.has(`${r},${c}`)) continue;
520
- const cell = cells[r]?.[c];
521
- if (!cell) continue;
522
- display[r][c] = escapeGfm(sanitizeText(cell.text)).replace(/\|/g, "\\|").replace(/\n/g, "<br>");
523
- for (let dr = 0; dr < cell.rowSpan; dr++) {
524
- for (let dc = 0; dc < cell.colSpan; dc++) {
525
- if (dr === 0 && dc === 0) continue;
526
- if (r + dr < numRows && c + dc < numCols) {
527
- skip.add(`${r + dr},${c + dc}`);
528
- }
529
- }
530
- }
531
- c += cell.colSpan - 1;
532
- }
533
- }
534
- const uniqueRows = [];
535
- let pendingFirstCol = "";
536
- for (let r = 0; r < display.length; r++) {
537
- const row = display[r];
538
- const isEmptyPlaceholder = row.every((cell) => cell === "");
539
- if (isEmptyPlaceholder) continue;
540
- const nonEmptyCols = row.filter((cell) => cell !== "");
541
- const hasSkipInRow = row.some((_, c) => skip.has(`${r},${c}`));
542
- if (!hasSkipInRow && nonEmptyCols.length === 1 && row[0] !== "" && row.slice(1).every((c) => c === "")) {
543
- pendingFirstCol = row[0];
544
- continue;
545
- }
546
- if (pendingFirstCol && row[0] === "") {
547
- row[0] = pendingFirstCol;
548
- pendingFirstCol = "";
549
- } else {
550
- pendingFirstCol = "";
551
- }
552
- uniqueRows.push(row);
553
- }
554
- if (uniqueRows.length === 0) return "";
555
- const md = [];
556
- md.push("| " + uniqueRows[0].join(" | ") + " |");
557
- md.push("| " + uniqueRows[0].map(() => "---").join(" | ") + " |");
558
- for (let i = 1; i < uniqueRows.length; i++) {
559
- md.push("| " + uniqueRows[i].join(" | ") + " |");
560
- }
561
- return md.join("\n");
562
- }
563
-
564
- // src/types.ts
565
- var HEADING_RATIO_H1 = 1.5;
566
- var HEADING_RATIO_H2 = 1.3;
567
- var HEADING_RATIO_H3 = 1.15;
568
-
569
- // src/hwpx/parser.ts
570
- init_page_range();
571
72
  var MAX_DECOMPRESS_SIZE = 100 * 1024 * 1024;
572
73
  var MAX_ZIP_ENTRIES = 500;
573
74
  function clampSpan(val, max) {
@@ -2382,7 +1883,6 @@ function parseLenientCfb(data) {
2382
1883
  }
2383
1884
 
2384
1885
  // src/hwp5/parser.ts
2385
- init_page_range();
2386
1886
  import { createRequire } from "module";
2387
1887
  var require2 = createRequire(import.meta.url);
2388
1888
  var CFB = require2("cfb");
@@ -3026,2308 +2526,56 @@ function arrangeCells(rows, cols, cells) {
3026
2526
  return grid.map((row) => row.map((c) => c || { text: "", colSpan: 1, rowSpan: 1 }));
3027
2527
  }
3028
2528
 
3029
- // src/pdf/parser.ts
3030
- init_page_range();
3031
-
3032
- // src/pdf/line-detector.ts
3033
- import { OPS } from "pdfjs-dist/legacy/build/pdf.mjs";
3034
- var ORIENTATION_TOL = 2;
3035
- var MIN_LINE_LENGTH = 15;
3036
- var MAX_LINE_WIDTH = 5;
3037
- var CONNECT_TOL = 5;
3038
- var CELL_PADDING = 2;
3039
- var MIN_COL_WIDTH = 15;
3040
- var MIN_ROW_HEIGHT = 6;
3041
- var VERTEX_MERGE_FACTOR = 4;
3042
- var MIN_COORD_MERGE_TOL = 8;
3043
- function extractLines(fnArray, argsArray) {
3044
- const horizontals = [];
3045
- const verticals = [];
3046
- let lineWidth = 1;
3047
- let currentPath = [];
3048
- let pathStartX = 0, pathStartY = 0;
3049
- let curX = 0, curY = 0;
3050
- function pushRectangle(path, rx, ry, rw, rh) {
3051
- if (Math.abs(rh) < ORIENTATION_TOL * 2) {
3052
- path.push({ x1: rx, y1: ry + rh / 2, x2: rx + rw, y2: ry + rh / 2 });
3053
- } else if (Math.abs(rw) < ORIENTATION_TOL * 2) {
3054
- path.push({ x1: rx + rw / 2, y1: ry, x2: rx + rw / 2, y2: ry + rh });
3055
- } else {
3056
- path.push(
3057
- { x1: rx, y1: ry, x2: rx + rw, y2: ry },
3058
- { x1: rx + rw, y1: ry, x2: rx + rw, y2: ry + rh },
3059
- { x1: rx + rw, y1: ry + rh, x2: rx, y2: ry + rh },
3060
- { x1: rx, y1: ry + rh, x2: rx, y2: ry }
3061
- );
3062
- }
3063
- }
3064
- function flushPath(isStroke) {
3065
- if (!isStroke) {
3066
- currentPath = [];
3067
- return;
3068
- }
3069
- for (const seg of currentPath) {
3070
- classifyAndAdd(seg, lineWidth, horizontals, verticals);
3071
- }
3072
- currentPath = [];
3073
- }
3074
- for (let i = 0; i < fnArray.length; i++) {
3075
- const op = fnArray[i];
3076
- const args = argsArray[i];
3077
- switch (op) {
3078
- case OPS.setLineWidth:
3079
- lineWidth = args[0] || 1;
3080
- break;
3081
- case OPS.constructPath: {
3082
- const arg0 = args[0];
3083
- if (Array.isArray(arg0)) {
3084
- const subOps = arg0;
3085
- const coords = args[1];
3086
- let ci = 0;
3087
- for (const subOp of subOps) {
3088
- if (subOp === OPS.moveTo) {
3089
- curX = coords[ci++];
3090
- curY = coords[ci++];
3091
- pathStartX = curX;
3092
- pathStartY = curY;
3093
- } else if (subOp === OPS.lineTo) {
3094
- const x2 = coords[ci++], y2 = coords[ci++];
3095
- currentPath.push({ x1: curX, y1: curY, x2, y2 });
3096
- curX = x2;
3097
- curY = y2;
3098
- } else if (subOp === OPS.rectangle) {
3099
- const rx = coords[ci++], ry = coords[ci++];
3100
- const rw = coords[ci++], rh = coords[ci++];
3101
- pushRectangle(currentPath, rx, ry, rw, rh);
3102
- } else if (subOp === OPS.closePath) {
3103
- if (curX !== pathStartX || curY !== pathStartY) {
3104
- currentPath.push({ x1: curX, y1: curY, x2: pathStartX, y2: pathStartY });
3105
- }
3106
- curX = pathStartX;
3107
- curY = pathStartY;
3108
- } else if (subOp === OPS.curveTo) {
3109
- ci += 6;
3110
- } else if (subOp === OPS.curveTo2 || subOp === OPS.curveTo3) {
3111
- ci += 4;
3112
- }
3113
- }
3114
- } else {
3115
- const afterOp = arg0;
3116
- const dataArr = args[1];
3117
- const pathData = dataArr?.[0];
3118
- if (pathData && typeof pathData === "object") {
3119
- const len = Object.keys(pathData).length;
3120
- let di = 0;
3121
- while (di < len) {
3122
- const drawOp = pathData[di++];
3123
- if (drawOp === 0 /* moveTo */) {
3124
- curX = pathData[di++];
3125
- curY = pathData[di++];
3126
- pathStartX = curX;
3127
- pathStartY = curY;
3128
- } else if (drawOp === 1 /* lineTo */) {
3129
- const x2 = pathData[di++], y2 = pathData[di++];
3130
- currentPath.push({ x1: curX, y1: curY, x2, y2 });
3131
- curX = x2;
3132
- curY = y2;
3133
- } else if (drawOp === 2 /* curveTo */) {
3134
- di += 6;
3135
- } else if (drawOp === 3 /* quadraticCurveTo */) {
3136
- di += 4;
3137
- } else if (drawOp === 4 /* closePath */) {
3138
- if (curX !== pathStartX || curY !== pathStartY) {
3139
- currentPath.push({ x1: curX, y1: curY, x2: pathStartX, y2: pathStartY });
3140
- }
3141
- curX = pathStartX;
3142
- curY = pathStartY;
3143
- } else {
3144
- break;
3145
- }
3146
- }
3147
- }
3148
- if (afterOp === OPS.stroke || afterOp === OPS.closeStroke) {
3149
- flushPath(true);
3150
- } else if (afterOp === OPS.fill || afterOp === OPS.eoFill || afterOp === OPS.fillStroke || afterOp === OPS.eoFillStroke || afterOp === OPS.closeFillStroke || afterOp === OPS.closeEOFillStroke) {
3151
- flushPath(true);
3152
- } else if (afterOp === OPS.endPath) {
3153
- flushPath(false);
3154
- }
3155
- }
3156
- break;
3157
- }
3158
- case OPS.stroke:
3159
- case OPS.closeStroke:
3160
- flushPath(true);
3161
- break;
3162
- case OPS.fill:
3163
- case OPS.eoFill:
3164
- case OPS.fillStroke:
3165
- case OPS.eoFillStroke:
3166
- case OPS.closeFillStroke:
3167
- case OPS.closeEOFillStroke:
3168
- flushPath(true);
3169
- break;
3170
- case OPS.endPath:
3171
- flushPath(false);
3172
- break;
3173
- }
3174
- }
3175
- return { horizontals, verticals };
3176
- }
3177
- function classifyAndAdd(seg, lineWidth, horizontals, verticals) {
3178
- const dx = Math.abs(seg.x2 - seg.x1);
3179
- const dy = Math.abs(seg.y2 - seg.y1);
3180
- const length = Math.sqrt(dx * dx + dy * dy);
3181
- if (length < MIN_LINE_LENGTH) return;
3182
- if (dy <= ORIENTATION_TOL) {
3183
- const y = (seg.y1 + seg.y2) / 2;
3184
- const x1 = Math.min(seg.x1, seg.x2);
3185
- const x2 = Math.max(seg.x1, seg.x2);
3186
- horizontals.push({ x1, y1: y, x2, y2: y, lineWidth });
3187
- } else if (dx <= ORIENTATION_TOL) {
3188
- const x = (seg.x1 + seg.x2) / 2;
3189
- const y1 = Math.min(seg.y1, seg.y2);
3190
- const y2 = Math.max(seg.y1, seg.y2);
3191
- verticals.push({ x1: x, y1, x2: x, y2, lineWidth });
3192
- }
3193
- }
3194
- function preprocessLines(horizontals, verticals) {
3195
- let h = horizontals.filter((l) => l.lineWidth <= MAX_LINE_WIDTH);
3196
- let v = verticals.filter((l) => l.lineWidth <= MAX_LINE_WIDTH);
3197
- h = mergeParallelLines(h, "h");
3198
- v = mergeParallelLines(v, "v");
3199
- return { horizontals: h, verticals: v };
3200
- }
3201
- function mergeParallelLines(lines, dir) {
3202
- if (lines.length <= 1) return lines;
3203
- const sorted = [...lines].sort((a, b) => {
3204
- const posA = dir === "h" ? a.y1 : a.x1;
3205
- const posB = dir === "h" ? b.y1 : b.x1;
3206
- if (Math.abs(posA - posB) > 0.1) return posA - posB;
3207
- return dir === "h" ? a.x1 - b.x1 : a.y1 - b.y1;
3208
- });
3209
- const MERGE_TOL = 3;
3210
- const result = [sorted[0]];
3211
- for (let i = 1; i < sorted.length; i++) {
3212
- const prev = result[result.length - 1];
3213
- const curr = sorted[i];
3214
- const prevPos = dir === "h" ? prev.y1 : prev.x1;
3215
- const currPos = dir === "h" ? curr.y1 : curr.x1;
3216
- if (Math.abs(prevPos - currPos) <= MERGE_TOL) {
3217
- const prevStart = dir === "h" ? prev.x1 : prev.y1;
3218
- const prevEnd = dir === "h" ? prev.x2 : prev.y2;
3219
- const currStart = dir === "h" ? curr.x1 : curr.y1;
3220
- const currEnd = dir === "h" ? curr.x2 : curr.y2;
3221
- const overlap = Math.min(prevEnd, currEnd) - Math.max(prevStart, currStart);
3222
- const minLen = Math.min(prevEnd - prevStart, currEnd - currStart);
3223
- if (overlap > minLen * 0.3) {
3224
- if (dir === "h") {
3225
- prev.x1 = Math.min(prev.x1, curr.x1);
3226
- prev.x2 = Math.max(prev.x2, curr.x2);
3227
- prev.y1 = (prev.y1 + curr.y1) / 2;
3228
- prev.y2 = prev.y1;
3229
- } else {
3230
- prev.y1 = Math.min(prev.y1, curr.y1);
3231
- prev.y2 = Math.max(prev.y2, curr.y2);
3232
- prev.x1 = (prev.x1 + curr.x1) / 2;
3233
- prev.x2 = prev.x1;
3234
- }
3235
- prev.lineWidth = Math.max(prev.lineWidth, curr.lineWidth);
3236
- continue;
3237
- }
3238
- }
3239
- result.push(curr);
3240
- }
3241
- return result;
3242
- }
3243
- function filterPageBorderLines(horizontals, verticals, pageWidth, pageHeight) {
3244
- const margin = 5;
3245
- return {
3246
- horizontals: horizontals.filter(
3247
- (l) => !(Math.abs(l.y1) < margin || Math.abs(l.y1 - pageHeight) < margin) || l.x2 - l.x1 < pageWidth * 0.9
3248
- ),
3249
- verticals: verticals.filter(
3250
- (l) => !(Math.abs(l.x1) < margin || Math.abs(l.x1 - pageWidth) < margin) || l.y2 - l.y1 < pageHeight * 0.9
3251
- )
3252
- };
2529
+ // src/xlsx/parser.ts
2530
+ import JSZip3 from "jszip";
2531
+ import { DOMParser as DOMParser2 } from "@xmldom/xmldom";
2532
+ var MAX_SHEETS = 100;
2533
+ var MAX_DECOMPRESS_SIZE3 = 100 * 1024 * 1024;
2534
+ var MAX_ROWS2 = 1e4;
2535
+ var MAX_COLS2 = 200;
2536
+ function cleanNumericValue(raw) {
2537
+ if (!/^-?\d+\.\d+$/.test(raw)) return raw;
2538
+ const num = parseFloat(raw);
2539
+ if (!isFinite(num)) return raw;
2540
+ const cleaned = parseFloat(num.toPrecision(15)).toString();
2541
+ return cleaned;
3253
2542
  }
3254
- function buildVertices(horizontals, verticals) {
3255
- const vertices = [];
3256
- const tol = CONNECT_TOL;
3257
- for (const h of horizontals) {
3258
- for (const v of verticals) {
3259
- if (v.x1 >= h.x1 - tol && v.x1 <= h.x2 + tol && h.y1 >= v.y1 - tol && h.y1 <= v.y2 + tol) {
3260
- const radius = Math.max(h.lineWidth, v.lineWidth, 1);
3261
- vertices.push({ x: v.x1, y: h.y1, radius });
3262
- }
3263
- }
3264
- }
3265
- return vertices;
3266
- }
3267
- function mergeVertices(vertices) {
3268
- if (vertices.length <= 1) return vertices;
3269
- const merged = [];
3270
- const used = new Array(vertices.length).fill(false);
3271
- for (let i = 0; i < vertices.length; i++) {
3272
- if (used[i]) continue;
3273
- let sumX = vertices[i].x, sumY = vertices[i].y;
3274
- let maxRadius = vertices[i].radius;
3275
- let count = 1;
3276
- for (let j = i + 1; j < vertices.length; j++) {
3277
- if (used[j]) continue;
3278
- const mergeTol = VERTEX_MERGE_FACTOR * Math.max(maxRadius, vertices[j].radius);
3279
- if (Math.abs(vertices[i].x - vertices[j].x) <= mergeTol && Math.abs(vertices[i].y - vertices[j].y) <= mergeTol) {
3280
- sumX += vertices[j].x;
3281
- sumY += vertices[j].y;
3282
- maxRadius = Math.max(maxRadius, vertices[j].radius);
3283
- count++;
3284
- used[j] = true;
3285
- }
3286
- }
3287
- merged.push({ x: sumX / count, y: sumY / count, radius: maxRadius });
3288
- }
3289
- return merged;
2543
+ function parseCellRef(ref) {
2544
+ const m = ref.match(/^([A-Z]+)(\d+)$/);
2545
+ if (!m) return null;
2546
+ let col = 0;
2547
+ for (const ch of m[1]) col = col * 26 + (ch.charCodeAt(0) - 64);
2548
+ return { col: col - 1, row: parseInt(m[2], 10) - 1 };
3290
2549
  }
3291
- function buildTableGrids(horizontals, verticals) {
3292
- if (horizontals.length < 2 || verticals.length < 2) return [];
3293
- const allVertices = buildVertices(horizontals, verticals);
3294
- const vertices = mergeVertices(allVertices);
3295
- if (vertices.length < 4) return [];
3296
- const globalRadius = vertices.reduce((max, v) => Math.max(max, v.radius), 1);
3297
- const allLines = [
3298
- ...horizontals.map((l, i) => ({ ...l, type: "h", id: i })),
3299
- ...verticals.map((l, i) => ({ ...l, type: "v", id: i + horizontals.length }))
3300
- ];
3301
- const groups = groupConnectedLines(allLines);
3302
- const grids = [];
3303
- for (const group of groups) {
3304
- const hLines = group.filter((l) => l.type === "h");
3305
- const vLines = group.filter((l) => l.type === "v");
3306
- if (hLines.length < 2 || vLines.length < 2) continue;
3307
- let gx1 = Infinity, gy1 = Infinity, gx2 = -Infinity, gy2 = -Infinity;
3308
- for (const l of vLines) {
3309
- if (l.x1 < gx1) gx1 = l.x1;
3310
- if (l.x1 > gx2) gx2 = l.x1;
3311
- }
3312
- for (const l of hLines) {
3313
- if (l.y1 < gy1) gy1 = l.y1;
3314
- if (l.y1 > gy2) gy2 = l.y1;
3315
- }
3316
- const groupBbox = {
3317
- x1: gx1 - CONNECT_TOL,
3318
- y1: gy1 - CONNECT_TOL,
3319
- x2: gx2 + CONNECT_TOL,
3320
- y2: gy2 + CONNECT_TOL
3321
- };
3322
- const groupVertices = vertices.filter(
3323
- (v) => v.x >= groupBbox.x1 && v.x <= groupBbox.x2 && v.y >= groupBbox.y1 && v.y <= groupBbox.y2
3324
- );
3325
- const groupRadius = groupVertices.length > 0 ? groupVertices.reduce((max, v) => Math.max(max, v.radius), 1) : globalRadius;
3326
- const coordMergeTol = Math.max(VERTEX_MERGE_FACTOR * groupRadius, MIN_COORD_MERGE_TOL);
3327
- const rawYs = [
3328
- ...hLines.map((l) => l.y1),
3329
- ...groupVertices.map((v) => v.y)
3330
- ];
3331
- const rowYs = clusterCoordinates(rawYs, coordMergeTol).sort((a, b) => b - a);
3332
- const rawXs = [
3333
- ...vLines.map((l) => l.x1),
3334
- ...groupVertices.map((v) => v.x)
3335
- ];
3336
- const colXs = clusterCoordinates(rawXs, coordMergeTol).sort((a, b) => a - b);
3337
- if (rowYs.length < 2 || colXs.length < 2) continue;
3338
- const validColXs = enforceMinWidth(colXs, MIN_COL_WIDTH);
3339
- const validRowYs = enforceMinHeight(rowYs, MIN_ROW_HEIGHT);
3340
- if (validRowYs.length < 2 || validColXs.length < 2) continue;
3341
- const bbox = {
3342
- x1: validColXs[0],
3343
- y1: validRowYs[validRowYs.length - 1],
3344
- x2: validColXs[validColXs.length - 1],
3345
- y2: validRowYs[0]
3346
- };
3347
- grids.push({ rowYs: validRowYs, colXs: validColXs, bbox, vertexRadius: groupRadius });
3348
- }
3349
- return mergeAdjacentGrids(grids);
2550
+ function parseMergeRef(ref) {
2551
+ const parts = ref.split(":");
2552
+ if (parts.length !== 2) return null;
2553
+ const start = parseCellRef(parts[0]);
2554
+ const end = parseCellRef(parts[1]);
2555
+ if (!start || !end) return null;
2556
+ return { startCol: start.col, startRow: start.row, endCol: end.col, endRow: end.row };
3350
2557
  }
3351
- function enforceMinWidth(colXs, minWidth) {
3352
- if (colXs.length <= 2) return colXs;
3353
- const result = [colXs[0]];
3354
- for (let i = 1; i < colXs.length; i++) {
3355
- const prevX = result[result.length - 1];
3356
- if (colXs[i] - prevX < minWidth && i < colXs.length - 1) {
3357
- continue;
3358
- }
3359
- result.push(colXs[i]);
3360
- }
2558
+ function getElements(parent, tagName) {
2559
+ const nodes = parent.getElementsByTagName(tagName);
2560
+ const result = [];
2561
+ for (let i = 0; i < nodes.length; i++) result.push(nodes[i]);
3361
2562
  return result;
3362
2563
  }
3363
- function enforceMinHeight(rowYs, minHeight) {
3364
- if (rowYs.length <= 2) return rowYs;
3365
- const result = [rowYs[0]];
3366
- for (let i = 1; i < rowYs.length; i++) {
3367
- const prevY = result[result.length - 1];
3368
- if (prevY - rowYs[i] < minHeight && i < rowYs.length - 1) {
3369
- continue;
3370
- }
3371
- result.push(rowYs[i]);
3372
- }
3373
- return result;
2564
+ function getTextContent(el) {
2565
+ return el.textContent?.trim() ?? "";
3374
2566
  }
3375
- function mergeAdjacentGrids(grids) {
3376
- if (grids.length <= 1) return grids;
3377
- const sorted = [...grids].sort((a, b) => b.bbox.y2 - a.bbox.y2);
3378
- const merged = [sorted[0]];
3379
- for (let i = 1; i < sorted.length; i++) {
3380
- const prev = merged[merged.length - 1];
3381
- const curr = sorted[i];
3382
- if (prev.colXs.length === curr.colXs.length) {
3383
- const mergeTol = Math.max(VERTEX_MERGE_FACTOR * Math.max(prev.vertexRadius, curr.vertexRadius), 6) * 3;
3384
- const colMatch = prev.colXs.every((x, ci) => Math.abs(x - curr.colXs[ci]) <= mergeTol);
3385
- const verticalGap = prev.bbox.y1 - curr.bbox.y2;
3386
- if (colMatch && verticalGap >= -CONNECT_TOL && verticalGap <= 20) {
3387
- const allRowYs = [.../* @__PURE__ */ new Set([...prev.rowYs, ...curr.rowYs])].sort((a, b) => b - a);
3388
- merged[merged.length - 1] = {
3389
- rowYs: allRowYs,
3390
- colXs: prev.colXs,
3391
- bbox: {
3392
- x1: Math.min(prev.bbox.x1, curr.bbox.x1),
3393
- y1: Math.min(prev.bbox.y1, curr.bbox.y1),
3394
- x2: Math.max(prev.bbox.x2, curr.bbox.x2),
3395
- y2: Math.max(prev.bbox.y2, curr.bbox.y2)
3396
- },
3397
- vertexRadius: Math.max(prev.vertexRadius, curr.vertexRadius)
3398
- };
3399
- continue;
3400
- }
3401
- }
3402
- merged.push(curr);
3403
- }
3404
- return merged;
2567
+ function parseXml(text) {
2568
+ return new DOMParser2().parseFromString(stripDtd(text), "text/xml");
3405
2569
  }
3406
- function clusterCoordinates(values, tolerance) {
3407
- if (values.length === 0) return [];
3408
- const sorted = [...values].sort((a, b) => a - b);
3409
- const clusters = [{ sum: sorted[0], count: 1 }];
3410
- for (let i = 1; i < sorted.length; i++) {
3411
- const last = clusters[clusters.length - 1];
3412
- const avg = last.sum / last.count;
3413
- if (Math.abs(sorted[i] - avg) <= tolerance) {
3414
- last.sum += sorted[i];
3415
- last.count++;
3416
- } else {
3417
- clusters.push({ sum: sorted[i], count: 1 });
3418
- }
2570
+ function parseSharedStrings(xml) {
2571
+ const doc = parseXml(xml);
2572
+ const strings = [];
2573
+ const siList = getElements(doc.documentElement, "si");
2574
+ for (const si of siList) {
2575
+ const tElements = getElements(si, "t");
2576
+ strings.push(tElements.map((t) => t.textContent ?? "").join(""));
3419
2577
  }
3420
- return clusters.map((c) => c.sum / c.count);
3421
- }
3422
- function groupConnectedLines(lines) {
3423
- const parent = lines.map((_, i) => i);
3424
- function find(x) {
3425
- while (parent[x] !== x) {
3426
- parent[x] = parent[parent[x]];
3427
- x = parent[x];
3428
- }
3429
- return x;
3430
- }
3431
- function union(a, b) {
3432
- const ra = find(a), rb = find(b);
3433
- if (ra !== rb) parent[ra] = rb;
3434
- }
3435
- for (let i = 0; i < lines.length; i++) {
3436
- for (let j = i + 1; j < lines.length; j++) {
3437
- if (linesIntersect(lines[i], lines[j])) {
3438
- union(i, j);
3439
- }
3440
- }
3441
- }
3442
- const groups = /* @__PURE__ */ new Map();
3443
- for (let i = 0; i < lines.length; i++) {
3444
- const root = find(i);
3445
- if (!groups.has(root)) groups.set(root, []);
3446
- groups.get(root).push(lines[i]);
3447
- }
3448
- return [...groups.values()];
3449
- }
3450
- function linesIntersect(a, b) {
3451
- if (a.type === b.type) {
3452
- if (a.type === "h") {
3453
- if (Math.abs(a.y1 - b.y1) > CONNECT_TOL) return false;
3454
- return Math.min(a.x2, b.x2) >= Math.max(a.x1, b.x1) - CONNECT_TOL;
3455
- } else {
3456
- if (Math.abs(a.x1 - b.x1) > CONNECT_TOL) return false;
3457
- return Math.min(a.y2, b.y2) >= Math.max(a.y1, b.y1) - CONNECT_TOL;
3458
- }
3459
- }
3460
- const h = a.type === "h" ? a : b;
3461
- const v = a.type === "h" ? b : a;
3462
- const tol = CONNECT_TOL;
3463
- return v.x1 >= h.x1 - tol && v.x1 <= h.x2 + tol && h.y1 >= v.y1 - tol && h.y1 <= v.y2 + tol;
3464
- }
3465
- function extractCells(grid, horizontals, verticals) {
3466
- const { rowYs, colXs } = grid;
3467
- const numRows = rowYs.length - 1;
3468
- const numCols = colXs.length - 1;
3469
- if (numRows <= 0 || numCols <= 0) return [];
3470
- const vBorders = Array.from(
3471
- { length: numRows },
3472
- (_, r) => Array.from(
3473
- { length: numCols + 1 },
3474
- (_2, c) => hasVerticalLine(verticals, colXs[c], rowYs[r], rowYs[r + 1], grid.vertexRadius)
3475
- )
3476
- );
3477
- const hBorders = Array.from(
3478
- { length: numRows + 1 },
3479
- (_, r) => Array.from(
3480
- { length: numCols },
3481
- (_2, c) => hasHorizontalLine(horizontals, rowYs[r], colXs[c], colXs[c + 1], grid.vertexRadius)
3482
- )
3483
- );
3484
- const occupied = Array.from({ length: numRows }, () => Array(numCols).fill(false));
3485
- const cells = [];
3486
- for (let r = 0; r < numRows; r++) {
3487
- for (let c = 0; c < numCols; c++) {
3488
- if (occupied[r][c]) continue;
3489
- let colSpan = 1;
3490
- let rowSpan = 1;
3491
- while (c + colSpan < numCols && !vBorders[r][c + colSpan]) {
3492
- let canExpand = true;
3493
- for (let dr = 0; dr < rowSpan; dr++) {
3494
- if (vBorders[r + dr][c + colSpan]) {
3495
- canExpand = false;
3496
- break;
3497
- }
3498
- }
3499
- if (!canExpand) break;
3500
- colSpan++;
3501
- }
3502
- while (r + rowSpan < numRows) {
3503
- let hasLine = false;
3504
- for (let dc = 0; dc < colSpan; dc++) {
3505
- if (hBorders[r + rowSpan][c + dc]) {
3506
- hasLine = true;
3507
- break;
3508
- }
3509
- }
3510
- if (hasLine) break;
3511
- rowSpan++;
3512
- }
3513
- for (let dr = 0; dr < rowSpan; dr++) {
3514
- for (let dc = 0; dc < colSpan; dc++) {
3515
- occupied[r + dr][c + dc] = true;
3516
- }
3517
- }
3518
- cells.push({
3519
- row: r,
3520
- col: c,
3521
- rowSpan,
3522
- colSpan,
3523
- bbox: {
3524
- x1: colXs[c],
3525
- y1: rowYs[r + rowSpan],
3526
- x2: colXs[c + colSpan],
3527
- y2: rowYs[r]
3528
- }
3529
- });
3530
- }
3531
- }
3532
- return cells;
3533
- }
3534
- function hasVerticalLine(verticals, x, topY, botY, vertexRadius) {
3535
- const tol = Math.max(VERTEX_MERGE_FACTOR * vertexRadius, 4);
3536
- for (const v of verticals) {
3537
- if (Math.abs(v.x1 - x) <= tol) {
3538
- const cellH = Math.abs(topY - botY);
3539
- if (cellH < 0.1) continue;
3540
- const overlapTop = Math.min(v.y2, topY);
3541
- const overlapBot = Math.max(v.y1, botY);
3542
- const overlap = overlapTop - overlapBot;
3543
- if (overlap >= cellH * 0.75) return true;
3544
- }
3545
- }
3546
- return false;
3547
- }
3548
- function hasHorizontalLine(horizontals, y, leftX, rightX, vertexRadius) {
3549
- const tol = Math.max(VERTEX_MERGE_FACTOR * vertexRadius, 4);
3550
- for (const h of horizontals) {
3551
- if (Math.abs(h.y1 - y) <= tol) {
3552
- const cellW = Math.abs(rightX - leftX);
3553
- if (cellW < 0.1) continue;
3554
- const overlapLeft = Math.max(h.x1, leftX);
3555
- const overlapRight = Math.min(h.x2, rightX);
3556
- const overlap = overlapRight - overlapLeft;
3557
- if (overlap >= cellW * 0.75) return true;
3558
- }
3559
- }
3560
- return false;
3561
- }
3562
- function mapTextToCells(items, cells) {
3563
- const result = /* @__PURE__ */ new Map();
3564
- for (const cell of cells) {
3565
- result.set(cell, []);
3566
- }
3567
- for (const item of items) {
3568
- const pad = CELL_PADDING;
3569
- let bestCell = null;
3570
- let bestScore = 0;
3571
- for (const cell of cells) {
3572
- const ix1 = Math.max(item.x, cell.bbox.x1 - pad);
3573
- const ix2 = Math.min(item.x + item.w, cell.bbox.x2 + pad);
3574
- const iy1 = Math.max(item.y, cell.bbox.y1 - pad);
3575
- const iy2 = Math.min(item.y + (item.h || item.fontSize), cell.bbox.y2 + pad);
3576
- if (ix1 >= ix2 || iy1 >= iy2) continue;
3577
- const intersectArea = (ix2 - ix1) * (iy2 - iy1);
3578
- const itemArea = Math.max(item.w, 1) * Math.max(item.h || item.fontSize, 1);
3579
- const score = intersectArea / itemArea;
3580
- if (score > bestScore) {
3581
- bestScore = score;
3582
- bestCell = cell;
3583
- }
3584
- }
3585
- if (bestCell && bestScore > 0.3) {
3586
- result.get(bestCell).push(item);
3587
- }
3588
- }
3589
- return result;
3590
- }
3591
- function cellTextToString(items) {
3592
- if (items.length === 0) return "";
3593
- if (items.length === 1) return items[0].text;
3594
- const sorted = [...items].sort((a, b) => b.y - a.y || a.x - b.x);
3595
- const lines = [];
3596
- let curLine = [sorted[0]];
3597
- let curY = sorted[0].y;
3598
- for (let i = 1; i < sorted.length; i++) {
3599
- const tol = Math.max(3, Math.min(sorted[i].fontSize, curLine[0].fontSize) * 0.6);
3600
- if (Math.abs(sorted[i].y - curY) <= tol) {
3601
- curLine.push(sorted[i]);
3602
- } else {
3603
- lines.push(curLine);
3604
- curLine = [sorted[i]];
3605
- curY = sorted[i].y;
3606
- }
3607
- }
3608
- lines.push(curLine);
3609
- const textLines = lines.map((line) => {
3610
- const s = line.sort((a, b) => a.x - b.x);
3611
- if (s.length === 1) return s[0].text;
3612
- const evenSpaced = detectEvenSpacedItems(s);
3613
- let result = s[0].text;
3614
- for (let j = 1; j < s.length; j++) {
3615
- if (evenSpaced[j]) {
3616
- result += s[j].text;
3617
- continue;
3618
- }
3619
- const gap = s[j].x - (s[j - 1].x + s[j - 1].w);
3620
- const avgFs = (s[j].fontSize + s[j - 1].fontSize) / 2;
3621
- const prevIsKorean = /[가-힣]$/.test(result);
3622
- const currIsKorean = /^[가-힣]/.test(s[j].text);
3623
- if (gap < avgFs * 0.15) {
3624
- result += s[j].text;
3625
- } else if (gap < avgFs * 0.35 && (prevIsKorean || currIsKorean)) {
3626
- result += s[j].text;
3627
- } else {
3628
- result += " " + s[j].text;
3629
- }
3630
- }
3631
- return result;
3632
- });
3633
- return mergeCellTextLines(textLines);
3634
- }
3635
- function detectEvenSpacedItems(items) {
3636
- const result = new Array(items.length).fill(false);
3637
- if (items.length < 3) return result;
3638
- let runStart = -1;
3639
- for (let i = 0; i < items.length; i++) {
3640
- const isShortKorean = /^[가-힣]{1}$/.test(items[i].text) || /^[\d]{1}$/.test(items[i].text);
3641
- if (isShortKorean && runStart >= 0 && i > 0) {
3642
- const gap = items[i].x - (items[i - 1].x + items[i - 1].w);
3643
- const maxRunGap = Math.max(items[i].fontSize * 3, 30);
3644
- if (gap > maxRunGap) {
3645
- if (i - runStart >= 3) markEvenRun(items, result, runStart, i);
3646
- runStart = i;
3647
- continue;
3648
- }
3649
- }
3650
- if (isShortKorean) {
3651
- if (runStart < 0) runStart = i;
3652
- } else {
3653
- if (runStart >= 0 && i - runStart >= 3) {
3654
- markEvenRun(items, result, runStart, i);
3655
- }
3656
- runStart = -1;
3657
- }
3658
- }
3659
- if (runStart >= 0 && items.length - runStart >= 3) {
3660
- markEvenRun(items, result, runStart, items.length);
3661
- }
3662
- return result;
3663
- }
3664
- function markEvenRun(items, result, start, end) {
3665
- const gaps = [];
3666
- for (let i = start + 1; i < end; i++) {
3667
- gaps.push(items[i].x - (items[i - 1].x + items[i - 1].w));
3668
- }
3669
- const posGaps = gaps.filter((g2) => g2 > 0);
3670
- if (posGaps.length < 2) return;
3671
- let minGap = Infinity, maxGap = -Infinity;
3672
- for (const g2 of posGaps) {
3673
- if (g2 < minGap) minGap = g2;
3674
- if (g2 > maxGap) maxGap = g2;
3675
- }
3676
- const avgFs = items[start].fontSize;
3677
- if (minGap >= avgFs * 0.1 && maxGap <= avgFs * 3 && maxGap / Math.max(minGap, 0.1) <= 3) {
3678
- for (let i = start + 1; i < end; i++) {
3679
- result[i] = true;
3680
- }
3681
- }
3682
- }
3683
- function mergeCellTextLines(textLines) {
3684
- if (textLines.length <= 1) return textLines[0] || "";
3685
- const merged = [textLines[0]];
3686
- for (let i = 1; i < textLines.length; i++) {
3687
- const prev = merged[merged.length - 1];
3688
- const curr = textLines[i];
3689
- if (/[가-힣]$/.test(prev) && /^[가-힣]+$/.test(curr) && curr.length <= 8 && !curr.includes(" ")) {
3690
- merged[merged.length - 1] = prev + curr;
3691
- } else if (curr.trim().length <= 3 && /^[)\]%}]/.test(curr.trim())) {
3692
- merged[merged.length - 1] = prev + curr.trim();
3693
- } else if (/[,(]$/.test(prev.trim()) && curr.trim().length <= 15) {
3694
- merged[merged.length - 1] = prev + curr.trim();
3695
- } else if (/[\d,]$/.test(prev) && /^[\d,]+[)\]]?$/.test(curr.trim()) && curr.trim().length <= 10) {
3696
- merged[merged.length - 1] = prev + curr.trim();
3697
- } else {
3698
- merged.push(curr);
3699
- }
3700
- }
3701
- return merged.join("\n");
3702
- }
3703
-
3704
- // src/pdf/cluster-detector.ts
3705
- var Y_TOL = 3;
3706
- var COL_CLUSTER_TOL = 15;
3707
- var MIN_ROWS = 3;
3708
- var MIN_COLS = 2;
3709
- var MIN_GAP_FACTOR = 2;
3710
- var MIN_GAP_ABSOLUTE = 20;
3711
- var MIN_COL_FILL_RATIO = 0.4;
3712
- function detectClusterTables(items, pageNum) {
3713
- if (items.length < MIN_ROWS * MIN_COLS) return [];
3714
- const { merged, originMap } = mergeEvenSpacedClusters(items);
3715
- const rows = groupByBaseline(merged);
3716
- if (rows.length < MIN_ROWS) return [];
3717
- const results = [];
3718
- const headerResult = detectHeaderRow(rows);
3719
- if (headerResult) {
3720
- const { columns, headerIdx } = headerResult;
3721
- const headerRow = rows[headerIdx];
3722
- const headerItems = [...headerRow.items].sort((a, b) => a.x - b.x);
3723
- const headerAndBelow = rows.slice(headerIdx);
3724
- const mergedRows = mergeMultiLineRows(headerAndBelow, columns);
3725
- const tableRegions = findTableRegionsByHeader(mergedRows, columns, headerItems);
3726
- for (const region of tableRegions) {
3727
- const table = buildClusterTable(region.rows, columns, pageNum);
3728
- if (table) {
3729
- expandUsedItems(table.usedItems, originMap);
3730
- results.push(table);
3731
- }
3732
- }
3733
- }
3734
- if (results.length === 0) {
3735
- const suspiciousRows = rows.filter((row) => hasSuspiciousGaps(row));
3736
- if (suspiciousRows.length >= MIN_ROWS) {
3737
- const columns = extractColumnClusters(suspiciousRows);
3738
- if (columns.length >= MIN_COLS) {
3739
- const tableRegions = findTableRegions(rows, columns);
3740
- for (const region of tableRegions) {
3741
- const mergedRows = mergeMultiLineRows(region.rows, columns);
3742
- const table = buildClusterTable(mergedRows, columns, pageNum);
3743
- if (table) {
3744
- expandUsedItems(table.usedItems, originMap);
3745
- results.push(table);
3746
- }
3747
- }
3748
- }
3749
- }
3750
- }
3751
- return results;
3752
- }
3753
- function mergeEvenSpacedClusters(items) {
3754
- const originMap = /* @__PURE__ */ new Map();
3755
- const rows = groupByBaseline(items);
3756
- const merged = [];
3757
- for (const row of rows) {
3758
- const sorted = [...row.items].sort((a, b) => a.x - b.x);
3759
- let i = 0;
3760
- while (i < sorted.length) {
3761
- if (/^[가-힣\d]$/.test(sorted[i].text)) {
3762
- let runEnd = i + 1;
3763
- while (runEnd < sorted.length && /^[가-힣\d]$/.test(sorted[runEnd].text)) {
3764
- const gap = sorted[runEnd].x - (sorted[runEnd - 1].x + sorted[runEnd - 1].w);
3765
- const fs = sorted[runEnd].fontSize;
3766
- if (gap < fs * 0.1 || gap > fs * 3) break;
3767
- runEnd++;
3768
- }
3769
- if (runEnd - i >= 3) {
3770
- const gaps = [];
3771
- for (let g2 = i + 1; g2 < runEnd; g2++) {
3772
- gaps.push(sorted[g2].x - (sorted[g2 - 1].x + sorted[g2 - 1].w));
3773
- }
3774
- let minG = Infinity, maxG = -Infinity;
3775
- for (const g2 of gaps) {
3776
- if (g2 < minG) minG = g2;
3777
- if (g2 > maxG) maxG = g2;
3778
- }
3779
- if (minG > 0 && maxG / minG <= 3) {
3780
- const run = sorted.slice(i, runEnd);
3781
- const text = run.map((r) => r.text).join("");
3782
- const first = run[0], last = run[runEnd - i - 1];
3783
- const item = {
3784
- text,
3785
- x: first.x,
3786
- y: first.y,
3787
- w: last.x + last.w - first.x,
3788
- h: first.h,
3789
- fontSize: first.fontSize,
3790
- fontName: first.fontName
3791
- };
3792
- originMap.set(item, run);
3793
- merged.push(item);
3794
- i = runEnd;
3795
- continue;
3796
- }
3797
- }
3798
- }
3799
- merged.push(sorted[i]);
3800
- i++;
3801
- }
3802
- }
3803
- return { merged, originMap };
3804
- }
3805
- function expandUsedItems(usedItems, originMap) {
3806
- const toAdd = [];
3807
- for (const item of usedItems) {
3808
- const origins = originMap.get(item);
3809
- if (origins) for (const o of origins) toAdd.push(o);
3810
- }
3811
- for (const a of toAdd) usedItems.add(a);
3812
- }
3813
- function detectHeaderRow(rows) {
3814
- const allItems = rows.flatMap((r) => r.items);
3815
- if (allItems.length === 0) return null;
3816
- let allMinX = Infinity, allMaxX = -Infinity;
3817
- for (const i of allItems) {
3818
- if (i.x < allMinX) allMinX = i.x;
3819
- const r = i.x + i.w;
3820
- if (r > allMaxX) allMaxX = r;
3821
- }
3822
- const pageSpan = allMaxX - allMinX;
3823
- if (pageSpan <= 0) return null;
3824
- for (let ri = 0; ri < rows.length; ri++) {
3825
- const row = rows[ri];
3826
- if (row.items.length < MIN_COLS || row.items.length > 6) continue;
3827
- if (row.items.some((i) => i.text.length > 8)) continue;
3828
- if (!row.items.some((i) => /[가-힣]/.test(i.text))) continue;
3829
- if (row.items.some((i) => /^[□■○●·※▶▷◆◇\-]/.test(i.text))) continue;
3830
- const sorted = [...row.items].sort((a, b) => a.x - b.x);
3831
- const xSpan = sorted[sorted.length - 1].x + sorted[sorted.length - 1].w - sorted[0].x;
3832
- if (xSpan / pageSpan < 0.4) continue;
3833
- const avgFs = sorted.reduce((s, i) => s + i.fontSize, 0) / sorted.length;
3834
- let hasLargeGap = false;
3835
- for (let i = 1; i < sorted.length; i++) {
3836
- const gap = sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w);
3837
- if (gap >= avgFs * 2.5) {
3838
- hasLargeGap = true;
3839
- break;
3840
- }
3841
- }
3842
- if (!hasLargeGap) continue;
3843
- const columns = sorted.map((item) => ({ x: item.x, count: 0 }));
3844
- let matchCount = 0;
3845
- for (let j = ri + 1; j < rows.length && matchCount < MIN_ROWS + 2; j++) {
3846
- const matched = countMatchedColumnsRange(rows[j], columns, sorted);
3847
- if (matched >= MIN_COLS) matchCount++;
3848
- }
3849
- if (matchCount < MIN_ROWS) continue;
3850
- return { columns, headerIdx: ri };
3851
- }
3852
- return null;
3853
- }
3854
- function mergeMultiLineRows(rows, columns) {
3855
- if (rows.length <= 1) return rows;
3856
- const result = [rows[0]];
3857
- const allFontSizes = rows.flatMap((r) => r.items).map((i) => i.fontSize);
3858
- const avgFontSize = allFontSizes.length > 0 ? allFontSizes.reduce((s, v) => s + v, 0) / allFontSizes.length : 12;
3859
- for (let i = 1; i < rows.length; i++) {
3860
- const prev = result[result.length - 1];
3861
- const curr = rows[i];
3862
- const yGap = Math.abs(prev.y - curr.y);
3863
- const matchedCols = countMatchedColumns(curr, columns);
3864
- if (yGap < avgFontSize * 1.8 && curr.items.length <= 2 && (matchedCols < MIN_COLS || curr.items.length === 1)) {
3865
- result[result.length - 1] = {
3866
- y: prev.y,
3867
- items: [...prev.items, ...curr.items]
3868
- };
3869
- } else {
3870
- result.push(curr);
3871
- }
3872
- }
3873
- return result;
3874
- }
3875
- function groupByBaseline(items) {
3876
- if (items.length === 0) return [];
3877
- const sorted = [...items].sort((a, b) => b.y - a.y || a.x - b.x);
3878
- const rows = [];
3879
- let curItems = [sorted[0]];
3880
- let curY = sorted[0].y;
3881
- for (let i = 1; i < sorted.length; i++) {
3882
- if (Math.abs(sorted[i].y - curY) <= Y_TOL) {
3883
- curItems.push(sorted[i]);
3884
- } else {
3885
- rows.push({ y: curY, items: curItems });
3886
- curItems = [sorted[i]];
3887
- curY = sorted[i].y;
3888
- }
3889
- }
3890
- if (curItems.length > 0) rows.push({ y: curY, items: curItems });
3891
- return rows;
3892
- }
3893
- function hasSuspiciousGaps(row) {
3894
- if (row.items.length < 2) return false;
3895
- const sorted = [...row.items].sort((a, b) => a.x - b.x);
3896
- if (sorted.length === 2 && sorted[1].text.length > 20) return false;
3897
- const avgFontSize = sorted.reduce((s, i) => s + i.fontSize, 0) / sorted.length;
3898
- const minGap = Math.max(avgFontSize * MIN_GAP_FACTOR, MIN_GAP_ABSOLUTE);
3899
- for (let i = 1; i < sorted.length; i++) {
3900
- const gap = sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w);
3901
- if (gap >= minGap) return true;
3902
- }
3903
- return false;
3904
- }
3905
- function extractColumnClusters(rows) {
3906
- const allX = [];
3907
- for (const row of rows) {
3908
- for (const item of row.items) allX.push(item.x);
3909
- }
3910
- if (allX.length === 0) return [];
3911
- allX.sort((a, b) => a - b);
3912
- const clusters = [];
3913
- let clusterStart = 0;
3914
- for (let i = 1; i <= allX.length; i++) {
3915
- if (i === allX.length || allX[i] - allX[i - 1] > COL_CLUSTER_TOL) {
3916
- const slice = allX.slice(clusterStart, i);
3917
- const avg = Math.round(slice.reduce((s, v) => s + v, 0) / slice.length);
3918
- clusters.push({ x: avg, count: slice.length });
3919
- clusterStart = i;
3920
- }
3921
- }
3922
- const minCount = Math.max(2, Math.floor(rows.length * MIN_COL_FILL_RATIO));
3923
- return clusters.filter((c) => c.count >= minCount).sort((a, b) => a.x - b.x);
3924
- }
3925
- function findTableRegionsByHeader(allRows, columns, headerItems) {
3926
- const regions = [];
3927
- let currentRegion = [];
3928
- let missStreak = 0;
3929
- for (const row of allRows) {
3930
- const matchedCols = countMatchedColumnsRange(row, columns, headerItems);
3931
- if (matchedCols >= MIN_COLS) {
3932
- currentRegion.push(row);
3933
- missStreak = 0;
3934
- } else if (currentRegion.length > 0 && (row.items.length <= 2 || missStreak === 0)) {
3935
- currentRegion.push(row);
3936
- missStreak++;
3937
- } else {
3938
- while (currentRegion.length > 0) {
3939
- const last = currentRegion[currentRegion.length - 1];
3940
- if (countMatchedColumnsRange(last, columns, headerItems) >= MIN_COLS) break;
3941
- currentRegion.pop();
3942
- }
3943
- if (currentRegion.length >= MIN_ROWS) {
3944
- regions.push({ rows: [...currentRegion] });
3945
- }
3946
- currentRegion = [];
3947
- missStreak = 0;
3948
- }
3949
- }
3950
- while (currentRegion.length > 0) {
3951
- const last = currentRegion[currentRegion.length - 1];
3952
- if (countMatchedColumnsRange(last, columns, headerItems) >= MIN_COLS) break;
3953
- currentRegion.pop();
3954
- }
3955
- if (currentRegion.length >= MIN_ROWS) {
3956
- regions.push({ rows: currentRegion });
3957
- }
3958
- return regions;
3959
- }
3960
- function findTableRegions(allRows, columns) {
3961
- const regions = [];
3962
- let currentRegion = [];
3963
- for (const row of allRows) {
3964
- const matchedCols = countMatchedColumns(row, columns);
3965
- if (matchedCols >= MIN_COLS) {
3966
- currentRegion.push(row);
3967
- } else if (row.items.length === 1) {
3968
- if (currentRegion.length > 0) {
3969
- currentRegion.push(row);
3970
- }
3971
- } else {
3972
- if (currentRegion.length >= MIN_ROWS) {
3973
- regions.push({ rows: [...currentRegion] });
3974
- }
3975
- currentRegion = [];
3976
- }
3977
- }
3978
- if (currentRegion.length >= MIN_ROWS) {
3979
- regions.push({ rows: currentRegion });
3980
- }
3981
- return regions;
3982
- }
3983
- function countMatchedColumns(row, columns) {
3984
- const matched = /* @__PURE__ */ new Set();
3985
- for (const item of row.items) {
3986
- for (let ci = 0; ci < columns.length; ci++) {
3987
- if (Math.abs(item.x - columns[ci].x) <= COL_CLUSTER_TOL * 2) {
3988
- matched.add(ci);
3989
- break;
3990
- }
3991
- }
3992
- }
3993
- return matched.size;
3994
- }
3995
- function countMatchedColumnsRange(row, columns, headerItems) {
3996
- const boundaries = [];
3997
- for (let ci = 0; ci < headerItems.length; ci++) {
3998
- const left = ci === 0 ? 0 : (headerItems[ci - 1].x + headerItems[ci - 1].w + headerItems[ci].x) / 2;
3999
- const right = ci === headerItems.length - 1 ? Infinity : (headerItems[ci].x + headerItems[ci].w + headerItems[ci + 1].x) / 2;
4000
- boundaries.push({ left, right });
4001
- }
4002
- const matched = /* @__PURE__ */ new Set();
4003
- for (const item of row.items) {
4004
- for (let ci = 0; ci < boundaries.length; ci++) {
4005
- if (item.x >= boundaries[ci].left && item.x < boundaries[ci].right) {
4006
- matched.add(ci);
4007
- break;
4008
- }
4009
- }
4010
- }
4011
- return matched.size;
4012
- }
4013
- function assignRowItems(items, columns, numCols) {
4014
- if (items.length === 0) return [];
4015
- const sorted = [...items].sort((a, b) => a.x - b.x);
4016
- const colCenters = columns.map((c) => c.x);
4017
- const gaps = [];
4018
- for (let i = 1; i < sorted.length; i++) {
4019
- gaps.push({ idx: i, size: sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w) });
4020
- }
4021
- const gapSizes = gaps.map((g2) => g2.size).sort((a, b) => a - b);
4022
- const medianGap = gapSizes.length > 0 ? gapSizes[Math.floor(gapSizes.length / 2)] : 0;
4023
- const gapThreshold = sorted.length <= numCols + 1 ? 12 : Math.max(medianGap * 2.5, 12);
4024
- const significantGaps = gaps.filter((g2) => g2.size >= gapThreshold).sort((a, b) => b.size - a.size).slice(0, numCols - 1).sort((a, b) => a.idx - b.idx);
4025
- const groups = [];
4026
- let start = 0;
4027
- for (const gap of significantGaps) {
4028
- groups.push(sorted.slice(start, gap.idx));
4029
- start = gap.idx;
4030
- }
4031
- groups.push(sorted.slice(start));
4032
- const result = [];
4033
- const usedCols = /* @__PURE__ */ new Set();
4034
- const groupCenters = groups.map((g2) => {
4035
- let minX = Infinity, maxX = -Infinity;
4036
- for (const i of g2) {
4037
- if (i.x < minX) minX = i.x;
4038
- const r = i.x + i.w;
4039
- if (r > maxX) maxX = r;
4040
- }
4041
- return (minX + maxX) / 2;
4042
- });
4043
- const assignments = [];
4044
- for (let gi = 0; gi < groups.length; gi++) {
4045
- for (let ci = 0; ci < numCols; ci++) {
4046
- assignments.push({ gi, ci, dist: Math.abs(groupCenters[gi] - colCenters[ci]) });
4047
- }
4048
- }
4049
- assignments.sort((a, b) => a.dist - b.dist);
4050
- const assignedGroups = /* @__PURE__ */ new Set();
4051
- for (const { gi, ci } of assignments) {
4052
- if (assignedGroups.has(gi) || usedCols.has(ci)) continue;
4053
- result.push({ col: ci, items: groups[gi] });
4054
- assignedGroups.add(gi);
4055
- usedCols.add(ci);
4056
- }
4057
- for (let gi = 0; gi < groups.length; gi++) {
4058
- if (assignedGroups.has(gi)) continue;
4059
- let bestCol = 0, bestDist = Infinity;
4060
- for (let ci = 0; ci < numCols; ci++) {
4061
- const d = Math.abs(groupCenters[gi] - colCenters[ci]);
4062
- if (d < bestDist) {
4063
- bestDist = d;
4064
- bestCol = ci;
4065
- }
4066
- }
4067
- result.push({ col: bestCol, items: groups[gi] });
4068
- }
4069
- return result;
4070
- }
4071
- function buildClusterTable(rows, columns, pageNum) {
4072
- const numCols = columns.length;
4073
- const numRows = rows.length;
4074
- if (numRows < MIN_ROWS || numCols < MIN_COLS) return null;
4075
- const cells = Array.from(
4076
- { length: numRows },
4077
- () => Array.from({ length: numCols }, () => ({ text: "", colSpan: 1, rowSpan: 1 }))
4078
- );
4079
- const usedItems = /* @__PURE__ */ new Set();
4080
- for (let r = 0; r < numRows; r++) {
4081
- const row = rows[r];
4082
- if (row.items.length === 1 && numCols > 1) {
4083
- cells[r][0] = { text: row.items[0].text, colSpan: numCols, rowSpan: 1 };
4084
- usedItems.add(row.items[0]);
4085
- continue;
4086
- }
4087
- const assignments = assignRowItems(row.items, columns, numCols);
4088
- for (const { col, items } of assignments) {
4089
- const text = items.map((i) => i.text).join(" ");
4090
- const existing = cells[r][col].text;
4091
- cells[r][col].text = existing ? existing + " " + text : text;
4092
- for (const item of items) usedItems.add(item);
4093
- }
4094
- }
4095
- let emptyRows = 0;
4096
- for (const row of cells) {
4097
- if (row.every((c) => c.text === "")) emptyRows++;
4098
- }
4099
- if (emptyRows > numRows * 0.5) return null;
4100
- for (let c = 0; c < numCols; c++) {
4101
- const hasValue = cells.some((row) => row[c].text !== "");
4102
- if (!hasValue) return null;
4103
- }
4104
- for (let r = numRows - 1; r >= 1; r--) {
4105
- const nonEmptyCols = cells[r].filter((c) => c.text.trim()).length;
4106
- if (nonEmptyCols !== 1) continue;
4107
- if (cells[r][0].text.trim() !== "") continue;
4108
- const contentText = cells[r].find((c) => c.text.trim())?.text.trim() || "";
4109
- if (/^[○●▶\-·]/.test(contentText)) continue;
4110
- for (let pr = r - 1; pr >= 0; pr--) {
4111
- if (cells[pr].some((c) => c.text.trim())) {
4112
- for (let c = 0; c < numCols; c++) {
4113
- const prev = cells[pr][c].text.trim();
4114
- const curr = cells[r][c].text.trim();
4115
- if (curr) cells[pr][c].text = prev ? prev + " " + curr : curr;
4116
- }
4117
- for (let c = 0; c < numCols; c++) cells[r][c].text = "";
4118
- break;
4119
- }
4120
- }
4121
- }
4122
- for (let r = 0; r < cells.length - 1; r++) {
4123
- const row = cells[r];
4124
- const hasCol0 = row[0].text.trim() !== "";
4125
- const hasColLast = numCols > 1 && row[numCols - 1].text.trim() !== "";
4126
- const midEmpty = row.slice(1, numCols - 1).every((c) => c.text.trim() === "");
4127
- if (hasCol0 && hasColLast && midEmpty) {
4128
- const next = cells[r + 1];
4129
- if (next[0].text.trim() === "" && next.some((c) => c.text.trim())) {
4130
- for (let c = 1; c < numCols; c++) {
4131
- const curr = next[c].text.trim();
4132
- if (curr) row[c].text = row[c].text.trim() ? row[c].text.trim() + " " + curr : curr;
4133
- }
4134
- for (let c = 0; c < numCols; c++) next[c].text = "";
4135
- }
4136
- }
4137
- }
4138
- const filteredCells = cells.filter((row) => row.some((c) => c.text.trim()));
4139
- const finalRowCount = filteredCells.length;
4140
- if (finalRowCount < MIN_ROWS) return null;
4141
- const irTable = {
4142
- rows: finalRowCount,
4143
- cols: numCols,
4144
- cells: filteredCells,
4145
- hasHeader: finalRowCount > 1
4146
- };
4147
- const allItems = rows.flatMap((r) => r.items);
4148
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
4149
- for (const i of allItems) {
4150
- if (i.x < minX) minX = i.x;
4151
- if (i.y < minY) minY = i.y;
4152
- if (i.x + i.w > maxX) maxX = i.x + i.w;
4153
- const h = i.h > 0 ? i.h : i.fontSize;
4154
- if (i.y + h > maxY) maxY = i.y + h;
4155
- }
4156
- return {
4157
- table: irTable,
4158
- bbox: { page: pageNum, x: minX, y: minY, width: maxX - minX, height: maxY - minY },
4159
- usedItems
4160
- };
4161
- }
4162
-
4163
- // src/pdf/polyfill.ts
4164
- import * as pdfjsWorker from "pdfjs-dist/legacy/build/pdf.worker.mjs";
4165
- var g = globalThis;
4166
- if (typeof g.DOMMatrix === "undefined") {
4167
- g.DOMMatrix = class DOMMatrix {
4168
- m = [1, 0, 0, 1, 0, 0];
4169
- constructor(init) {
4170
- if (init) this.m = init;
4171
- }
4172
- };
4173
- }
4174
- if (typeof g.Path2D === "undefined") {
4175
- g.Path2D = class Path2D {
4176
- };
4177
- }
4178
- g.pdfjsWorker = pdfjsWorker;
4179
-
4180
- // src/pdf/parser.ts
4181
- import { getDocument, GlobalWorkerOptions } from "pdfjs-dist/legacy/build/pdf.mjs";
4182
- GlobalWorkerOptions.workerSrc = "";
4183
- var MAX_PAGES = 5e3;
4184
- var MAX_TOTAL_TEXT = 100 * 1024 * 1024;
4185
- var PDF_LOAD_TIMEOUT_MS = 3e4;
4186
- async function loadPdfWithTimeout(buffer) {
4187
- const loadingTask = getDocument({
4188
- data: new Uint8Array(buffer),
4189
- useSystemFonts: true,
4190
- disableFontFace: true,
4191
- isEvalSupported: false
4192
- });
4193
- let timer;
4194
- try {
4195
- return await Promise.race([
4196
- loadingTask.promise,
4197
- new Promise((_, reject) => {
4198
- timer = setTimeout(() => {
4199
- loadingTask.destroy();
4200
- reject(new KordocError("PDF \uB85C\uB529 \uD0C0\uC784\uC544\uC6C3 (30\uCD08 \uCD08\uACFC)"));
4201
- }, PDF_LOAD_TIMEOUT_MS);
4202
- })
4203
- ]);
4204
- } finally {
4205
- if (timer !== void 0) clearTimeout(timer);
4206
- }
4207
- }
4208
- async function parsePdfDocument(buffer, options) {
4209
- const doc = await loadPdfWithTimeout(buffer);
4210
- try {
4211
- const pageCount = doc.numPages;
4212
- if (pageCount === 0) throw new KordocError("PDF\uC5D0 \uD398\uC774\uC9C0\uAC00 \uC5C6\uC2B5\uB2C8\uB2E4.");
4213
- const metadata = { pageCount };
4214
- await extractPdfMetadata(doc, metadata);
4215
- const blocks = [];
4216
- const warnings = [];
4217
- let totalChars = 0;
4218
- let totalTextBytes = 0;
4219
- const effectivePageCount = Math.min(pageCount, MAX_PAGES);
4220
- const pageFilter = options?.pages ? parsePageRange(options.pages, effectivePageCount) : null;
4221
- const totalTarget = pageFilter ? pageFilter.size : effectivePageCount;
4222
- const fontSizeFreq = /* @__PURE__ */ new Map();
4223
- const pageHeights = /* @__PURE__ */ new Map();
4224
- let parsedPages = 0;
4225
- for (let i = 1; i <= effectivePageCount; i++) {
4226
- if (pageFilter && !pageFilter.has(i)) continue;
4227
- try {
4228
- const page = await doc.getPage(i);
4229
- const tc = await page.getTextContent();
4230
- const viewport = page.getViewport({ scale: 1 });
4231
- pageHeights.set(i, viewport.height);
4232
- const rawItems = tc.items;
4233
- const items = normalizeItems(rawItems);
4234
- const { visible, hiddenCount } = filterHiddenText(items, viewport.width, viewport.height);
4235
- if (hiddenCount > 0) {
4236
- warnings.push({ page: i, message: `${hiddenCount}\uAC1C \uC228\uACA8\uC9C4 \uD14D\uC2A4\uD2B8 \uC694\uC18C \uD544\uD130\uB9C1\uB428`, code: "HIDDEN_TEXT_FILTERED" });
4237
- }
4238
- for (const item of visible) {
4239
- if (item.fontSize > 0) fontSizeFreq.set(item.fontSize, (fontSizeFreq.get(item.fontSize) || 0) + 1);
4240
- }
4241
- const opList = await page.getOperatorList();
4242
- const pageBlocks = extractPageBlocksWithLines(visible, i, opList, viewport.width, viewport.height);
4243
- for (const b of pageBlocks) blocks.push(b);
4244
- for (const b of pageBlocks) {
4245
- const t = b.text || "";
4246
- totalChars += t.replace(/\s/g, "").length;
4247
- totalTextBytes += t.length * 2;
4248
- }
4249
- if (totalTextBytes > MAX_TOTAL_TEXT) throw new KordocError("\uD14D\uC2A4\uD2B8 \uCD94\uCD9C \uD06C\uAE30 \uCD08\uACFC");
4250
- parsedPages++;
4251
- options?.onProgress?.(parsedPages, totalTarget);
4252
- } catch (pageErr) {
4253
- if (pageErr instanceof KordocError) throw pageErr;
4254
- warnings.push({ page: i, message: `\uD398\uC774\uC9C0 ${i} \uD30C\uC2F1 \uC2E4\uD328: ${pageErr instanceof Error ? pageErr.message : "\uC54C \uC218 \uC5C6\uB294 \uC624\uB958"}`, code: "PARTIAL_PARSE" });
4255
- }
4256
- }
4257
- const parsedPageCount = parsedPages || (pageFilter ? pageFilter.size : effectivePageCount);
4258
- if (totalChars / Math.max(parsedPageCount, 1) < 10) {
4259
- if (options?.ocr) {
4260
- try {
4261
- const { ocrPages: ocrPages2 } = await Promise.resolve().then(() => (init_provider(), provider_exports));
4262
- const ocrBlocks = await ocrPages2(doc, options.ocr, pageFilter, effectivePageCount);
4263
- if (ocrBlocks.length > 0) {
4264
- const ocrMarkdown = ocrBlocks.map((b) => b.text || "").filter(Boolean).join("\n\n");
4265
- return { markdown: ocrMarkdown, blocks: ocrBlocks, metadata, warnings, isImageBased: true };
4266
- }
4267
- } catch {
4268
- }
4269
- }
4270
- throw Object.assign(new KordocError(`\uC774\uBBF8\uC9C0 \uAE30\uBC18 PDF (${pageCount}\uD398\uC774\uC9C0, ${totalChars}\uC790)`), { isImageBased: true });
4271
- }
4272
- if (options?.removeHeaderFooter !== false && parsedPageCount >= 3) {
4273
- const removed = removeHeaderFooterBlocks(blocks, pageHeights, warnings);
4274
- for (let ri = removed.length - 1; ri >= 0; ri--) {
4275
- blocks.splice(removed[ri], 1);
4276
- }
4277
- }
4278
- const medianFontSize = computeMedianFontSizeFromFreq(fontSizeFreq);
4279
- if (medianFontSize > 0) {
4280
- detectHeadings(blocks, medianFontSize);
4281
- }
4282
- detectMarkerHeadings(blocks);
4283
- const outline = blocks.filter((b) => b.type === "heading" && b.level && b.text).map((b) => ({ level: b.level, text: b.text, pageNumber: b.pageNumber }));
4284
- let markdown = cleanPdfText(blocksToMarkdown(blocks));
4285
- return { markdown, blocks, metadata, outline: outline.length > 0 ? outline : void 0, warnings: warnings.length > 0 ? warnings : void 0 };
4286
- } finally {
4287
- await doc.destroy().catch(() => {
4288
- });
4289
- }
4290
- }
4291
- async function extractPdfMetadata(doc, metadata) {
4292
- try {
4293
- const result = await doc.getMetadata();
4294
- if (!result?.info) return;
4295
- const info = result.info;
4296
- if (typeof info.Title === "string" && info.Title.trim()) metadata.title = info.Title.trim();
4297
- if (typeof info.Author === "string" && info.Author.trim()) metadata.author = info.Author.trim();
4298
- if (typeof info.Creator === "string" && info.Creator.trim()) metadata.creator = info.Creator.trim();
4299
- if (typeof info.Subject === "string" && info.Subject.trim()) metadata.description = info.Subject.trim();
4300
- if (typeof info.Keywords === "string" && info.Keywords.trim()) {
4301
- metadata.keywords = info.Keywords.split(/[,;]/).map((k) => k.trim()).filter(Boolean);
4302
- }
4303
- if (typeof info.CreationDate === "string") metadata.createdAt = parsePdfDate(info.CreationDate);
4304
- if (typeof info.ModDate === "string") metadata.modifiedAt = parsePdfDate(info.ModDate);
4305
- } catch {
4306
- }
4307
- }
4308
- function parsePdfDate(dateStr) {
4309
- const m = dateStr.match(/D:(\d{4})(\d{2})?(\d{2})?(\d{2})?(\d{2})?(\d{2})?/);
4310
- if (!m) return void 0;
4311
- const [, year, month = "01", day = "01", hour = "00", min = "00", sec = "00"] = m;
4312
- return `${year}-${month}-${day}T${hour}:${min}:${sec}`;
4313
- }
4314
- function filterHiddenText(items, pageWidth, pageHeight) {
4315
- let hiddenCount = 0;
4316
- const visible = [];
4317
- for (const item of items) {
4318
- if (item.isHidden) {
4319
- hiddenCount++;
4320
- continue;
4321
- }
4322
- const margin = Math.max(pageWidth, pageHeight) * 0.1;
4323
- if (item.x < -margin || item.x > pageWidth + margin || item.y < -margin || item.y > pageHeight + margin) {
4324
- hiddenCount++;
4325
- continue;
4326
- }
4327
- visible.push(item);
4328
- }
4329
- return { visible, hiddenCount };
4330
- }
4331
- function computeMedianFontSizeFromFreq(freq) {
4332
- if (freq.size === 0) return 0;
4333
- let total = 0;
4334
- for (const count of freq.values()) total += count;
4335
- const sorted = [...freq.entries()].sort((a, b) => a[0] - b[0]);
4336
- const mid = Math.floor(total / 2);
4337
- let cumulative = 0;
4338
- for (const [size, count] of sorted) {
4339
- cumulative += count;
4340
- if (cumulative > mid) return size;
4341
- }
4342
- return sorted[sorted.length - 1][0];
4343
- }
4344
- function detectHeadings(blocks, medianFontSize) {
4345
- for (const block of blocks) {
4346
- if (block.type !== "paragraph" || !block.text || !block.style?.fontSize) continue;
4347
- const text = block.text.trim();
4348
- if (text.length === 0 || text.length > 200) continue;
4349
- if (/^\d+$/.test(text)) continue;
4350
- const ratio = block.style.fontSize / medianFontSize;
4351
- let level = 0;
4352
- if (ratio >= HEADING_RATIO_H1) level = 1;
4353
- else if (ratio >= HEADING_RATIO_H2) level = 2;
4354
- else if (ratio >= HEADING_RATIO_H3) level = 3;
4355
- if (level > 0) {
4356
- block.type = "heading";
4357
- block.level = level;
4358
- block.text = collapseEvenSpacing(text);
4359
- }
4360
- }
4361
- }
4362
- function collapseEvenSpacing(text) {
4363
- const tokens = text.split(" ");
4364
- const singleCharCount = tokens.filter((t) => t.length === 1).length;
4365
- if (tokens.length >= 3 && singleCharCount / tokens.length >= 0.7) {
4366
- return tokens.join("");
4367
- }
4368
- return text.replace(
4369
- /(?<![가-힣])[가-힣](?: [가-힣\d]){2,}(?![가-힣])/g,
4370
- (match) => match.replace(/ /g, "")
4371
- );
4372
- }
4373
- function shouldDemoteTable(table) {
4374
- const allCells = table.cells.flatMap((row) => row.map((c) => c.text.trim())).filter(Boolean);
4375
- const allText = allCells.join(" ");
4376
- if (table.rows <= 3 && table.cols <= 3) {
4377
- const totalCells2 = table.rows * table.cols;
4378
- const emptyCells2 = totalCells2 - allCells.length;
4379
- if (emptyCells2 >= totalCells2 * 0.3) return true;
4380
- if (/[□■◆○●▶ㅇ]/.test(allText)) return true;
4381
- if (/<[^>]+>/.test(allText)) return true;
4382
- }
4383
- if (allText.length > 200) return false;
4384
- if (/[□■◆○●▶]/.test(allText) && table.rows <= 3) return true;
4385
- const totalCells = table.rows * table.cols;
4386
- const emptyCells = totalCells - allCells.length;
4387
- if (table.rows <= 2 && emptyCells > totalCells * 0.5) return true;
4388
- if (table.rows === 1 && !/\d{2,}/.test(allText)) return true;
4389
- return false;
4390
- }
4391
- function demoteTableToText(table) {
4392
- const lines = [];
4393
- for (let r = 0; r < table.rows; r++) {
4394
- const cells = table.cells[r].map((c) => c.text.trim()).filter(Boolean);
4395
- if (cells.length === 0) continue;
4396
- if (table.cols === 2 && cells.length === 2) {
4397
- lines.push(`${cells[0]} : ${cells[1]}`);
4398
- } else {
4399
- lines.push(cells.join(" "));
4400
- }
4401
- }
4402
- return lines.join("\n");
4403
- }
4404
- function detectMarkerHeadings(blocks) {
4405
- for (let i = 0; i < blocks.length; i++) {
4406
- const block = blocks[i];
4407
- if (block.type !== "paragraph" || !block.text) continue;
4408
- const text = block.text.trim();
4409
- if (text.length < 50 && /^[□■◆◇▶]\s*[가-힣]/.test(text)) {
4410
- block.type = "heading";
4411
- block.level = 4;
4412
- continue;
4413
- }
4414
- if (/^[가-힣]{2,6}$/.test(text) && block.style?.fontSize) {
4415
- const prev = blocks[i - 1];
4416
- const next = blocks[i + 1];
4417
- const prevIsStructural = !prev || prev.type === "table" || prev.type === "heading" || prev.type === "separator";
4418
- const nextIsStructural = !next || next.type === "table" || next.type === "heading" || next.type === "paragraph" && next.text && /^[□■◆○●]/.test(next.text.trim());
4419
- if (prevIsStructural || nextIsStructural) {
4420
- block.type = "heading";
4421
- block.level = 3;
4422
- }
4423
- }
4424
- }
4425
- }
4426
- var MAX_XYCUT_DEPTH = 50;
4427
- function xyCutOrder(items, gapThreshold, depth = 0) {
4428
- if (items.length === 0) return [];
4429
- if (items.length <= 2 || depth >= MAX_XYCUT_DEPTH) return [items];
4430
- const region = computeRegion(items);
4431
- const ySplit = findYSplit(items, region, gapThreshold);
4432
- if (ySplit !== null) {
4433
- const upper = items.filter((i) => i.y > ySplit);
4434
- const lower = items.filter((i) => i.y <= ySplit);
4435
- if (upper.length > 0 && lower.length > 0 && upper.length < items.length) {
4436
- return [...xyCutOrder(upper, gapThreshold, depth + 1), ...xyCutOrder(lower, gapThreshold, depth + 1)];
4437
- }
4438
- }
4439
- const xSplit = findXSplit(items, region, gapThreshold);
4440
- if (xSplit !== null) {
4441
- const left = items.filter((i) => i.x + i.w / 2 < xSplit);
4442
- const right = items.filter((i) => i.x + i.w / 2 >= xSplit);
4443
- if (left.length > 0 && right.length > 0 && left.length < items.length) {
4444
- return [...xyCutOrder(left, gapThreshold, depth + 1), ...xyCutOrder(right, gapThreshold, depth + 1)];
4445
- }
4446
- }
4447
- return [items];
4448
- }
4449
- function computeRegion(items) {
4450
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
4451
- for (const i of items) {
4452
- if (i.x < minX) minX = i.x;
4453
- if (i.y < minY) minY = i.y;
4454
- if (i.x + i.w > maxX) maxX = i.x + i.w;
4455
- if (i.y + i.h > maxY) maxY = i.y + i.h;
4456
- }
4457
- return { items, minX, minY, maxX, maxY };
4458
- }
4459
- function findYSplit(items, _region, gapThreshold) {
4460
- const sorted = [...items].sort((a, b) => b.y - a.y);
4461
- let bestGap = gapThreshold;
4462
- let bestSplit = null;
4463
- for (let i = 1; i < sorted.length; i++) {
4464
- const prevBottom = sorted[i - 1].y - sorted[i - 1].h;
4465
- const currTop = sorted[i].y;
4466
- const gap = prevBottom - currTop;
4467
- if (gap > bestGap) {
4468
- bestGap = gap;
4469
- bestSplit = (prevBottom + currTop) / 2;
4470
- }
4471
- }
4472
- return bestSplit;
4473
- }
4474
- function findXSplit(items, _region, gapThreshold) {
4475
- const sorted = [...items].sort((a, b) => a.x - b.x);
4476
- let bestGap = gapThreshold;
4477
- let bestSplit = null;
4478
- for (let i = 1; i < sorted.length; i++) {
4479
- const prevRight = sorted[i - 1].x + sorted[i - 1].w;
4480
- const currLeft = sorted[i].x;
4481
- const gap = currLeft - prevRight;
4482
- if (gap > bestGap) {
4483
- bestGap = gap;
4484
- bestSplit = (prevRight + currLeft) / 2;
4485
- }
4486
- }
4487
- return bestSplit;
4488
- }
4489
- function extractPageBlocksWithLines(items, pageNum, opList, pageWidth, pageHeight) {
4490
- if (items.length === 0) return [];
4491
- let { horizontals, verticals } = extractLines(opList.fnArray, opList.argsArray);
4492
- ({ horizontals, verticals } = filterPageBorderLines(horizontals, verticals, pageWidth, pageHeight));
4493
- ({ horizontals, verticals } = preprocessLines(horizontals, verticals));
4494
- const grids = buildTableGrids(horizontals, verticals);
4495
- if (grids.length > 0) {
4496
- return extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals);
4497
- }
4498
- return extractPageBlocksFallback(items, pageNum);
4499
- }
4500
- function extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals) {
4501
- const blocks = [];
4502
- const usedItems = /* @__PURE__ */ new Set();
4503
- const sortedGrids = [...grids].sort((a, b) => b.bbox.y2 - a.bbox.y2);
4504
- for (const grid of sortedGrids) {
4505
- const numGridRows = grid.rowYs.length - 1;
4506
- const numGridCols = grid.colXs.length - 1;
4507
- if (numGridRows === 1 && numGridCols >= 2) continue;
4508
- const tableItems = [];
4509
- const pad = 3;
4510
- const gridW = grid.bbox.x2 - grid.bbox.x1;
4511
- for (const item of items) {
4512
- if (usedItems.has(item)) continue;
4513
- if (item.y < grid.bbox.y1 - pad || item.y > grid.bbox.y2 + pad) continue;
4514
- if (item.x < grid.bbox.x1 - pad || item.x + item.w > grid.bbox.x2 + pad) continue;
4515
- if (gridW < 120 && item.x + item.w > grid.bbox.x2 - 2) continue;
4516
- tableItems.push(item);
4517
- usedItems.add(item);
4518
- }
4519
- const cells = extractCells(grid, horizontals, verticals);
4520
- if (cells.length === 0) continue;
4521
- const textItems = tableItems.map((i) => ({
4522
- text: i.text,
4523
- x: i.x,
4524
- y: i.y,
4525
- w: i.w,
4526
- h: i.h,
4527
- fontSize: i.fontSize,
4528
- fontName: i.fontName
4529
- }));
4530
- const cellTextMap = mapTextToCells(textItems, cells);
4531
- const numRows = grid.rowYs.length - 1;
4532
- const numCols = grid.colXs.length - 1;
4533
- const irGrid = Array.from(
4534
- { length: numRows },
4535
- () => Array.from({ length: numCols }, () => ({ text: "", colSpan: 1, rowSpan: 1 }))
4536
- );
4537
- for (const cell of cells) {
4538
- const cellItems = cellTextMap.get(cell) || [];
4539
- let text = cellTextToString(cellItems);
4540
- text = text.replace(/^[\s]*[-–—]\s*\d+\s*[-–—][\s]*$/gm, "").trim();
4541
- text = text.split("\n").map((line) => collapseEvenSpacing(line)).join("\n");
4542
- irGrid[cell.row][cell.col] = {
4543
- text,
4544
- colSpan: cell.colSpan,
4545
- rowSpan: cell.rowSpan
4546
- };
4547
- }
4548
- const irTable = {
4549
- rows: numRows,
4550
- cols: numCols,
4551
- cells: irGrid,
4552
- hasHeader: numRows > 1
4553
- };
4554
- const hasContent = irGrid.some((row) => row.some((cell) => cell.text.trim() !== ""));
4555
- if (!hasContent) continue;
4556
- const tableBbox = {
4557
- page: pageNum,
4558
- x: grid.bbox.x1,
4559
- y: grid.bbox.y1,
4560
- width: grid.bbox.x2 - grid.bbox.x1,
4561
- height: grid.bbox.y2 - grid.bbox.y1
4562
- };
4563
- if (shouldDemoteTable(irTable)) {
4564
- const demoted = demoteTableToText(irTable);
4565
- if (demoted) {
4566
- const text = numGridRows === 1 ? "\n" + demoted + "\n" : demoted;
4567
- blocks.push({ type: "paragraph", text, pageNumber: pageNum, bbox: tableBbox, style: dominantStyle(tableItems) });
4568
- }
4569
- continue;
4570
- }
4571
- blocks.push({ type: "table", table: irTable, pageNumber: pageNum, bbox: tableBbox });
4572
- }
4573
- let remaining = items.filter((i) => !usedItems.has(i));
4574
- if (remaining.length > 0) {
4575
- remaining.sort((a, b) => b.y - a.y || a.x - b.x);
4576
- const clusterItems = remaining.map((i) => ({
4577
- text: i.text,
4578
- x: i.x,
4579
- y: i.y,
4580
- w: i.w,
4581
- h: i.h,
4582
- fontSize: i.fontSize,
4583
- fontName: i.fontName
4584
- }));
4585
- const clusterResults = detectClusterTables(clusterItems, pageNum);
4586
- if (clusterResults.length > 0) {
4587
- const ciToIdx = /* @__PURE__ */ new Map();
4588
- for (let ci = 0; ci < clusterItems.length; ci++) ciToIdx.set(clusterItems[ci], ci);
4589
- const usedClusterIndices = /* @__PURE__ */ new Set();
4590
- for (const cr of clusterResults) {
4591
- for (const ci of cr.usedItems) {
4592
- const idx = ciToIdx.get(ci);
4593
- if (idx !== void 0) usedClusterIndices.add(idx);
4594
- }
4595
- blocks.push({ type: "table", table: cr.table, pageNumber: pageNum, bbox: cr.bbox });
4596
- }
4597
- remaining = remaining.filter((_, idx) => !usedClusterIndices.has(idx));
4598
- }
4599
- if (remaining.length > 0) {
4600
- const allY = remaining.map((i) => i.y);
4601
- const pageH = safeMax(allY) - safeMin(allY);
4602
- const groups = xyCutOrder(remaining, Math.max(15, pageH * 0.03));
4603
- const textBlocks = [];
4604
- for (const group of groups) {
4605
- if (group.length === 0) continue;
4606
- const groupBlocks = extractPageBlocksFallback(group, pageNum);
4607
- for (const b of groupBlocks) textBlocks.push(b);
4608
- }
4609
- const finalTextBlocks = detectListBlocks(textBlocks);
4610
- for (const b of finalTextBlocks) blocks.push(b);
4611
- }
4612
- blocks.sort((a, b) => {
4613
- const ay = a.bbox ? a.bbox.y + a.bbox.height : 0;
4614
- const by = b.bbox ? b.bbox.y + b.bbox.height : 0;
4615
- return by - ay;
4616
- });
4617
- return mergeAdjacentTableBlocks(blocks);
4618
- }
4619
- return mergeAdjacentTableBlocks(blocks);
4620
- }
4621
- function mergeAdjacentTableBlocks(blocks) {
4622
- if (blocks.length <= 1) return blocks;
4623
- const result = [blocks[0]];
4624
- for (let i = 1; i < blocks.length; i++) {
4625
- const prev = result[result.length - 1];
4626
- const curr = blocks[i];
4627
- if (prev.type === "table" && curr.type === "table" && prev.table && curr.table && prev.table.cols === curr.table.cols) {
4628
- const merged = {
4629
- rows: prev.table.rows + curr.table.rows,
4630
- cols: prev.table.cols,
4631
- cells: [...prev.table.cells, ...curr.table.cells],
4632
- hasHeader: prev.table.hasHeader
4633
- };
4634
- result[result.length - 1] = { ...prev, table: merged };
4635
- } else {
4636
- result.push(curr);
4637
- }
4638
- }
4639
- return result;
4640
- }
4641
- function extractPageBlocksFallback(items, pageNum) {
4642
- if (items.length === 0) return [];
4643
- const blocks = [];
4644
- const clusterItems = items.map((i) => ({
4645
- text: i.text,
4646
- x: i.x,
4647
- y: i.y,
4648
- w: i.w,
4649
- h: i.h,
4650
- fontSize: i.fontSize,
4651
- fontName: i.fontName
4652
- }));
4653
- const clusterResults = detectClusterTables(clusterItems, pageNum);
4654
- if (clusterResults.length > 0) {
4655
- const ciToIdx = /* @__PURE__ */ new Map();
4656
- for (let ci = 0; ci < clusterItems.length; ci++) ciToIdx.set(clusterItems[ci], ci);
4657
- const usedIndices = /* @__PURE__ */ new Set();
4658
- for (const cr of clusterResults) {
4659
- for (const ci of cr.usedItems) {
4660
- const idx = ciToIdx.get(ci);
4661
- if (idx !== void 0) usedIndices.add(idx);
4662
- }
4663
- blocks.push({ type: "table", table: cr.table, pageNumber: pageNum, bbox: cr.bbox });
4664
- }
4665
- const remaining = items.filter((_, idx) => !usedIndices.has(idx));
4666
- if (remaining.length > 0) {
4667
- const yLines = groupByY(remaining);
4668
- for (const line of yLines) {
4669
- const text = mergeLineSimple(line);
4670
- if (!text.trim()) continue;
4671
- const bbox = computeBBox(line, pageNum);
4672
- blocks.push({ type: "paragraph", text, pageNumber: pageNum, bbox, style: dominantStyle(line) });
4673
- }
4674
- }
4675
- blocks.sort((a, b) => {
4676
- const ay = a.bbox ? a.bbox.y + a.bbox.height : 0;
4677
- const by = b.bbox ? b.bbox.y + b.bbox.height : 0;
4678
- return by - ay;
4679
- });
4680
- } else {
4681
- const allYLines = groupByY(items);
4682
- const columns = detectColumns(allYLines);
4683
- if (columns && columns.length >= 3) {
4684
- const tableText = extractWithColumns(allYLines, columns);
4685
- const bbox = computeBBox(items, pageNum);
4686
- blocks.push({ type: "paragraph", text: tableText, pageNumber: pageNum, bbox, style: dominantStyle(items) });
4687
- } else {
4688
- const allY = items.map((i) => i.y);
4689
- const pageHeight = safeMax(allY) - safeMin(allY);
4690
- const gapThreshold = Math.max(15, pageHeight * 0.03);
4691
- const orderedGroups = xyCutOrder(items, gapThreshold);
4692
- for (const group of orderedGroups) {
4693
- if (group.length === 0) continue;
4694
- const yLines = groupByY(group);
4695
- const groupColumns = detectColumns(yLines);
4696
- if (groupColumns && groupColumns.length >= 3) {
4697
- const tableText = extractWithColumns(yLines, groupColumns);
4698
- const bbox = computeBBox(group, pageNum);
4699
- blocks.push({ type: "paragraph", text: tableText, pageNumber: pageNum, bbox, style: dominantStyle(group) });
4700
- } else {
4701
- for (const line of yLines) {
4702
- const text = mergeLineSimple(line);
4703
- if (!text.trim()) continue;
4704
- const bbox = computeBBox(line, pageNum);
4705
- blocks.push({ type: "paragraph", text, pageNumber: pageNum, bbox, style: dominantStyle(line) });
4706
- }
4707
- }
4708
- }
4709
- }
4710
- }
4711
- return detectSpecialKoreanTables(blocks);
4712
- }
4713
- function computeBBox(items, pageNum) {
4714
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
4715
- for (const i of items) {
4716
- if (i.x < minX) minX = i.x;
4717
- if (i.y < minY) minY = i.y;
4718
- if (i.x + i.w > maxX) maxX = i.x + i.w;
4719
- const effectiveH = i.h > 0 ? i.h : i.fontSize;
4720
- if (i.y + effectiveH > maxY) maxY = i.y + effectiveH;
4721
- }
4722
- return { page: pageNum, x: minX, y: minY, width: maxX - minX, height: maxY - minY };
4723
- }
4724
- function dominantStyle(items) {
4725
- if (items.length === 0) return void 0;
4726
- const freq = /* @__PURE__ */ new Map();
4727
- let maxCount = 0, dominantSize = 0;
4728
- for (const i of items) {
4729
- if (i.fontSize <= 0) continue;
4730
- const count = (freq.get(i.fontSize) || 0) + 1;
4731
- freq.set(i.fontSize, count);
4732
- if (count > maxCount) {
4733
- maxCount = count;
4734
- dominantSize = i.fontSize;
4735
- }
4736
- }
4737
- if (dominantSize === 0) return void 0;
4738
- const fontName = items.find((i) => i.fontSize === dominantSize)?.fontName || void 0;
4739
- return { fontSize: dominantSize, fontName };
4740
- }
4741
- function normalizeItems(rawItems) {
4742
- const items = [];
4743
- const spacePositions = [];
4744
- for (const i of rawItems) {
4745
- if (typeof i.str !== "string") continue;
4746
- const x = Math.round(i.transform[4]);
4747
- const y = Math.round(i.transform[5]);
4748
- if (!i.str.trim()) {
4749
- spacePositions.push({ x, y });
4750
- continue;
4751
- }
4752
- const scaleY = Math.abs(i.transform[3]);
4753
- const scaleX = Math.abs(i.transform[0]);
4754
- const fontSize = Math.round(Math.max(scaleY, scaleX));
4755
- const w = Math.round(i.width);
4756
- const h = Math.round(i.height);
4757
- const isHidden = fontSize === 0 || i.width === 0 && i.str.trim().length > 0;
4758
- let text = i.str.trim();
4759
- if (/^[\d\s\-().·,☎]+$/.test(text) && /\d/.test(text) && / /.test(text)) {
4760
- text = text.replace(/ /g, "");
4761
- }
4762
- const split = splitEvenSpacedItem(text, x, w, fontSize);
4763
- if (split) {
4764
- for (const s of split) {
4765
- items.push({ text: s.text, x: s.x, y, w: s.w, h, fontSize, fontName: i.fontName || "", isHidden });
4766
- }
4767
- } else {
4768
- items.push({ text, x, y, w, h, fontSize, fontName: i.fontName || "", isHidden });
4769
- }
4770
- }
4771
- const sorted = items.sort((a, b) => b.y - a.y || a.x - b.x);
4772
- const deduped = [];
4773
- for (let i = 0; i < sorted.length; i++) {
4774
- let isDup = false;
4775
- for (let j = deduped.length - 1; j >= 0; j--) {
4776
- const prev = deduped[j];
4777
- if (prev.y - sorted[i].y > 3) break;
4778
- if (Math.abs(prev.y - sorted[i].y) <= 3 && prev.text === sorted[i].text && Math.abs(prev.x - sorted[i].x) <= 3) {
4779
- isDup = true;
4780
- break;
4781
- }
4782
- }
4783
- if (!isDup) deduped.push(sorted[i]);
4784
- }
4785
- if (spacePositions.length > 0) {
4786
- for (const item of deduped) {
4787
- for (const sp of spacePositions) {
4788
- if (Math.abs(sp.y - item.y) <= 3) {
4789
- const dist = item.x - sp.x;
4790
- if (dist >= 0 && dist <= 20) {
4791
- item.hasSpaceBefore = true;
4792
- break;
4793
- }
4794
- }
4795
- }
4796
- }
4797
- }
4798
- return deduped;
4799
- }
4800
- function splitEvenSpacedItem(text, itemX, itemW, fontSize) {
4801
- if (!/^[가-힣\d](?: [가-힣\d]){2,}$/.test(text)) return null;
4802
- const chars = text.split(" ");
4803
- if (chars.length < 3) return null;
4804
- const charW = itemW / chars.length;
4805
- if (charW > fontSize * 2) return null;
4806
- return chars.map((ch, idx) => ({
4807
- text: ch,
4808
- x: Math.round(itemX + idx * charW),
4809
- w: Math.round(charW * 0.8)
4810
- // 실제 글자 폭은 간격보다 좁음
4811
- }));
4812
- }
4813
- function groupByY(items) {
4814
- if (items.length === 0) return [];
4815
- const lines = [];
4816
- let curY = items[0].y;
4817
- let curLine = [items[0]];
4818
- for (let i = 1; i < items.length; i++) {
4819
- if (Math.abs(items[i].y - curY) > 3) {
4820
- lines.push(curLine);
4821
- curLine = [];
4822
- curY = items[i].y;
4823
- }
4824
- curLine.push(items[i]);
4825
- }
4826
- if (curLine.length > 0) lines.push(curLine);
4827
- return lines;
4828
- }
4829
- function isProseSpread(items) {
4830
- if (items.length < 4) return false;
4831
- const sorted = [...items].sort((a, b) => a.x - b.x);
4832
- const gaps = [];
4833
- for (let i = 1; i < sorted.length; i++) {
4834
- gaps.push(sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w));
4835
- }
4836
- const maxGap = safeMax(gaps);
4837
- const avgLen = items.reduce((s, i) => s + i.text.length, 0) / items.length;
4838
- return maxGap < 40 && avgLen < 5;
4839
- }
4840
- function detectColumns(yLines) {
4841
- const allItems = yLines.flat();
4842
- if (allItems.length === 0) return null;
4843
- const pageWidth = safeMax(allItems.map((i) => i.x + i.w)) - safeMin(allItems.map((i) => i.x));
4844
- if (pageWidth < 100) return null;
4845
- let bigoLineIdx = -1;
4846
- for (let i = 0; i < yLines.length; i++) {
4847
- if (yLines[i].length <= 2 && yLines[i].some((item) => item.text === "\uBE44\uACE0")) {
4848
- bigoLineIdx = i;
4849
- break;
4850
- }
4851
- }
4852
- const tableYLines = bigoLineIdx >= 0 ? yLines.slice(0, bigoLineIdx) : yLines;
4853
- const CLUSTER_TOL = 22;
4854
- const xClusters = [];
4855
- for (const line of tableYLines) {
4856
- if (isProseSpread(line)) continue;
4857
- for (const item of line) {
4858
- let found = false;
4859
- for (const c of xClusters) {
4860
- if (Math.abs(item.x - c.center) <= CLUSTER_TOL) {
4861
- c.center = Math.round((c.center * c.count + item.x) / (c.count + 1));
4862
- c.minX = Math.min(c.minX, item.x);
4863
- c.count++;
4864
- found = true;
4865
- break;
4866
- }
4867
- }
4868
- if (!found) {
4869
- xClusters.push({ center: item.x, count: 1, minX: item.x });
4870
- }
4871
- }
4872
- }
4873
- const peaks = xClusters.filter((c) => c.count >= 3).sort((a, b) => a.minX - b.minX);
4874
- if (peaks.length < 3) return null;
4875
- const MERGE_TOL = 40;
4876
- const merged = [peaks[0]];
4877
- for (let i = 1; i < peaks.length; i++) {
4878
- const prev = merged[merged.length - 1];
4879
- if (peaks[i].minX - prev.minX < MERGE_TOL) {
4880
- if (peaks[i].count > prev.count) {
4881
- prev.center = peaks[i].center;
4882
- }
4883
- prev.count += peaks[i].count;
4884
- prev.minX = Math.min(prev.minX, peaks[i].minX);
4885
- } else {
4886
- merged.push({ ...peaks[i] });
4887
- }
4888
- }
4889
- const rawColumns = merged.filter((c) => c.count >= 3).map((c) => c.minX);
4890
- if (rawColumns.length < 3) return null;
4891
- const MIN_DETECT_COL_WIDTH = 30;
4892
- const columns = [rawColumns[0]];
4893
- for (let i = 1; i < rawColumns.length; i++) {
4894
- if (rawColumns[i] - columns[columns.length - 1] < MIN_DETECT_COL_WIDTH) continue;
4895
- columns.push(rawColumns[i]);
4896
- }
4897
- return columns.length >= 3 ? columns : null;
4898
- }
4899
- function findColumn(x, columns) {
4900
- for (let i = columns.length - 1; i >= 0; i--) {
4901
- if (x >= columns[i] - 10) return i;
4902
- }
4903
- return 0;
4904
- }
4905
- function extractWithColumns(yLines, columns) {
4906
- const result = [];
4907
- const colMin = columns[0];
4908
- const colMax = columns[columns.length - 1];
4909
- let bigoIdx = -1;
4910
- for (let i = 0; i < yLines.length; i++) {
4911
- if (yLines[i].length <= 2 && yLines[i].some((item) => item.text === "\uBE44\uACE0")) {
4912
- bigoIdx = i;
4913
- break;
4914
- }
4915
- }
4916
- let tableStart = -1;
4917
- for (let i = 0; i < (bigoIdx >= 0 ? bigoIdx : yLines.length); i++) {
4918
- const usedCols = new Set(yLines[i].map((item) => findColumn(item.x, columns)));
4919
- if (usedCols.size >= 3) {
4920
- tableStart = i;
4921
- break;
4922
- }
4923
- }
4924
- const tableEnd = bigoIdx >= 0 ? bigoIdx : yLines.length;
4925
- for (let i = 0; i < (tableStart >= 0 ? tableStart : tableEnd); i++) {
4926
- result.push(mergeLineSimple(yLines[i]));
4927
- }
4928
- if (tableStart >= 0) {
4929
- const tableLines = yLines.slice(tableStart, tableEnd);
4930
- const gridLines = [];
4931
- for (const line of tableLines) {
4932
- const inRange = line.some(
4933
- (item) => item.x >= colMin - 20 && item.x <= colMax + 200
4934
- );
4935
- if (inRange && !isProseSpread(line)) {
4936
- gridLines.push(line);
4937
- } else {
4938
- if (gridLines.length > 0) {
4939
- result.push(buildGridTable(gridLines.splice(0), columns));
4940
- }
4941
- result.push(mergeLineSimple(line));
4942
- }
4943
- }
4944
- if (gridLines.length > 0) {
4945
- result.push(buildGridTable(gridLines, columns));
4946
- }
4947
- }
4948
- if (bigoIdx >= 0) {
4949
- result.push("");
4950
- for (let i = bigoIdx; i < yLines.length; i++) {
4951
- result.push(mergeLineSimple(yLines[i]));
4952
- }
4953
- }
4954
- return result.join("\n");
4955
- }
4956
- function buildGridTable(lines, columns) {
4957
- const numCols = columns.length;
4958
- const yRows = lines.map((items) => {
4959
- const row = Array(numCols).fill("");
4960
- for (const item of items) {
4961
- const col = findColumn(item.x, columns);
4962
- row[col] = row[col] ? row[col] + " " + item.text : item.text;
4963
- }
4964
- return row;
4965
- });
4966
- const dataColStart = Math.max(2, Math.floor(numCols / 2));
4967
- const merged = [];
4968
- for (const row of yRows) {
4969
- if (row.every((c) => c === "")) continue;
4970
- if (merged.length === 0) {
4971
- merged.push([...row]);
4972
- continue;
4973
- }
4974
- const prev = merged[merged.length - 1];
4975
- const filledCols = row.map((c, i) => c ? i : -1).filter((i) => i >= 0);
4976
- const filledCount = filledCols.length;
4977
- let isNewRow = false;
4978
- if (row[0] && row[0].length >= 3) {
4979
- isNewRow = true;
4980
- }
4981
- if (!isNewRow && numCols > 1 && row[1]) {
4982
- isNewRow = true;
4983
- }
4984
- if (!isNewRow) {
4985
- const hasData = row.slice(dataColStart).some((c) => c !== "");
4986
- const prevHasData = prev.slice(dataColStart).some((c) => c !== "");
4987
- if (hasData && prevHasData) {
4988
- isNewRow = true;
4989
- }
4990
- }
4991
- if (isNewRow && filledCount === 1 && row[0] && row[0].length <= 2) {
4992
- isNewRow = false;
4993
- }
4994
- if (isNewRow) {
4995
- merged.push([...row]);
4996
- } else {
4997
- for (let c = 0; c < numCols; c++) {
4998
- if (row[c]) {
4999
- prev[c] = prev[c] ? prev[c] + " " + row[c] : row[c];
5000
- }
5001
- }
5002
- }
5003
- }
5004
- if (merged.length < 2) {
5005
- return merged.map((r) => r.filter((c) => c).join(" ")).join("\n");
5006
- }
5007
- let headerEnd = 0;
5008
- for (let r = 0; r < merged.length; r++) {
5009
- const hasDataValues = merged[r].slice(dataColStart).some((c) => c && /\d/.test(c));
5010
- if (hasDataValues) break;
5011
- headerEnd = r + 1;
5012
- }
5013
- if (headerEnd > 1) {
5014
- const headerRow = Array(numCols).fill("");
5015
- for (let r = 0; r < headerEnd; r++) {
5016
- for (let c = 0; c < numCols; c++) {
5017
- if (merged[r][c]) {
5018
- headerRow[c] = headerRow[c] ? headerRow[c] + " " + merged[r][c] : merged[r][c];
5019
- }
5020
- }
5021
- }
5022
- merged.splice(0, headerEnd, headerRow);
5023
- }
5024
- for (const row of merged) {
5025
- for (let c = 0; c < row.length; c++) {
5026
- if (row[c]) row[c] = collapseEvenSpacing(row[c]);
5027
- }
5028
- }
5029
- const totalCells = merged.length * numCols;
5030
- const filledCells = merged.reduce((s, row) => s + row.filter((c) => c).length, 0);
5031
- if (filledCells < totalCells * 0.35 || merged.length < 2 || merged.length <= 3 && numCols >= 7) {
5032
- return merged.map((r) => r.filter((c) => c).join(" ")).join("\n");
5033
- }
5034
- const md = [];
5035
- md.push("| " + merged[0].join(" | ") + " |");
5036
- md.push("| " + merged[0].map(() => "---").join(" | ") + " |");
5037
- for (let r = 1; r < merged.length; r++) {
5038
- md.push("| " + merged[r].join(" | ") + " |");
5039
- }
5040
- return md.join("\n");
5041
- }
5042
- function mergeLineSimple(items) {
5043
- if (items.length <= 1) return items[0]?.text || "";
5044
- const sorted = [...items].sort((a, b) => a.x - b.x);
5045
- const isEvenSpaced = detectEvenSpacedItems(sorted);
5046
- let result = sorted[0].text;
5047
- for (let i = 1; i < sorted.length; i++) {
5048
- const gap = sorted[i].x - (sorted[i - 1].x + sorted[i - 1].w);
5049
- const avgFs = (sorted[i].fontSize + sorted[i - 1].fontSize) / 2;
5050
- const tabThreshold = Math.max(avgFs * 2, 30);
5051
- if (gap > tabThreshold) {
5052
- result += " ";
5053
- result += sorted[i].text;
5054
- continue;
5055
- }
5056
- if (isEvenSpaced[i]) {
5057
- result += sorted[i].text;
5058
- continue;
5059
- }
5060
- if (sorted[i].hasSpaceBefore && gap >= avgFs * 0.05) {
5061
- result += " ";
5062
- result += sorted[i].text;
5063
- continue;
5064
- }
5065
- if (/[□■○●▶◆◇ㅇ]$/.test(sorted[i - 1].text) && /^[가-힣]/.test(sorted[i].text) && gap > 1) {
5066
- result += " ";
5067
- result += sorted[i].text;
5068
- continue;
5069
- }
5070
- if (gap < avgFs * 0.15) {
5071
- } else if (gap < avgFs * 0.35 && (/[가-힣]$/.test(result) || /^[가-힣]/.test(sorted[i].text))) {
5072
- } else if (gap > 3) result += " ";
5073
- result += sorted[i].text;
5074
- }
5075
- return result;
5076
- }
5077
- function cleanPdfText(text) {
5078
- return mergeKoreanLines(
5079
- text.replace(/^\d{1,4}\n/, "").replace(/^[\s]*[-–—]\s*[-–—]?\d+[-–—]?[\s]*[-–—]?[\s]*$/gm, "").replace(/^\s*\d+\s*\/\s*\d+\s*$/gm, "").replace(/\n\d{1,4}\n/g, "\n").replace(/\n\d{1,4}$/, "").replace(/^#{1,6}\s*\d{1,4}\s*$/gm, "")
5080
- ).replace(/^(?!\| ---).*$/gm, (line) => collapseEvenSpacing(line)).replace(/([□■◆○●▶ㅇ])\s+([가-힣])\s+([가-힣])/g, "$1 $2$3").replace(/\n{3,}/g, "\n\n").trim();
5081
- }
5082
- function startsWithMarker(line) {
5083
- const t = line.trimStart();
5084
- return /^[가-힣ㄱ-ㅎ][.)]/.test(t) || /^\d+[.)]/.test(t) || /^\([가-힣ㄱ-ㅎ\d]+\)/.test(t) || /^[○●※▶▷◆◇■□★☆\-·]\s/.test(t) || /^제\d+[조항호장절]/.test(t);
5085
- }
5086
- function isStandaloneHeader(line) {
5087
- return /^제\d+[조항호장절](\([^)]*\))?(\s+\S+){0,7}$/.test(line.trim());
5088
- }
5089
- function detectListBlocks(blocks) {
5090
- const result = [];
5091
- for (let i = 0; i < blocks.length; i++) {
5092
- const block = blocks[i];
5093
- if (block.type === "paragraph" && block.text) {
5094
- const text = block.text.trim();
5095
- if (/^\d+\.\s/.test(text)) {
5096
- result.push({ ...block, type: "list", listType: "ordered", text: block.text });
5097
- continue;
5098
- }
5099
- if (/^[○●·※▶▷◆◇\-]\s/.test(text)) {
5100
- result.push({ ...block, type: "list", listType: "unordered", text: block.text });
5101
- continue;
5102
- }
5103
- }
5104
- result.push(block);
5105
- }
5106
- return result;
5107
- }
5108
- var KOREAN_TABLE_HEADER_RE = /^\(?(구분|항목|종류|분류|유형|대상|내용|기간|금액|비율|방법|절차|요건|조건|근거|목적|범위|기준)\)?[:\s]/;
5109
- var KV_FALSE_POSITIVE_RE = /\d{1,2}:\d{2}|:\/\/|\d+:\d+/;
5110
- function detectSpecialKoreanTables(blocks) {
5111
- const result = [];
5112
- let kvLines = [];
5113
- const flushKvTable = () => {
5114
- if (kvLines.length < 2) {
5115
- for (const kv of kvLines) result.push(kv.block);
5116
- kvLines = [];
5117
- return;
5118
- }
5119
- const cells = kvLines.map((kv) => {
5120
- if (kv.value) {
5121
- return [
5122
- { text: kv.key, colSpan: 1, rowSpan: 1 },
5123
- { text: kv.value, colSpan: 1, rowSpan: 1 }
5124
- ];
5125
- }
5126
- return [
5127
- { text: kv.key, colSpan: 2, rowSpan: 1 },
5128
- { text: "", colSpan: 1, rowSpan: 1 }
5129
- ];
5130
- });
5131
- const irTable = {
5132
- rows: cells.length,
5133
- cols: 2,
5134
- cells,
5135
- hasHeader: true
5136
- };
5137
- const firstBlock = kvLines[0].block;
5138
- result.push({
5139
- type: "table",
5140
- table: irTable,
5141
- pageNumber: firstBlock.pageNumber,
5142
- bbox: firstBlock.bbox
5143
- });
5144
- kvLines = [];
5145
- };
5146
- for (const block of blocks) {
5147
- if (block.type !== "paragraph" || !block.text) {
5148
- flushKvTable();
5149
- result.push(block);
5150
- continue;
5151
- }
5152
- const text = block.text.trim();
5153
- if (KOREAN_TABLE_HEADER_RE.test(text)) {
5154
- const colonIdx = text.indexOf(":");
5155
- if (colonIdx >= 0) {
5156
- kvLines.push({
5157
- key: text.slice(0, colonIdx).trim(),
5158
- value: text.slice(colonIdx + 1).trim(),
5159
- block
5160
- });
5161
- } else {
5162
- const spaceIdx = text.search(/\s/);
5163
- if (spaceIdx > 0) {
5164
- kvLines.push({
5165
- key: text.slice(0, spaceIdx).trim(),
5166
- value: text.slice(spaceIdx + 1).trim(),
5167
- block
5168
- });
5169
- } else {
5170
- kvLines.push({ key: text, value: "", block });
5171
- }
5172
- }
5173
- continue;
5174
- }
5175
- if (kvLines.length > 0 && text.includes(":")) {
5176
- if (!KV_FALSE_POSITIVE_RE.test(text) && !text.includes("(") && !text.includes(")")) {
5177
- const colonIdx = text.indexOf(":");
5178
- const key = text.slice(0, colonIdx).trim();
5179
- if (/^[가-힣]+$/.test(key) && key.length >= 2 && key.length <= 8) {
5180
- kvLines.push({
5181
- key,
5182
- value: text.slice(colonIdx + 1).trim(),
5183
- block
5184
- });
5185
- continue;
5186
- }
5187
- }
5188
- }
5189
- flushKvTable();
5190
- result.push(block);
5191
- }
5192
- flushKvTable();
5193
- return result;
5194
- }
5195
- function removeHeaderFooterBlocks(blocks, pageHeights, warnings) {
5196
- const ZONE_RATIO = 0.1;
5197
- const MIN_REPEAT = 3;
5198
- const headerTexts = /* @__PURE__ */ new Map();
5199
- const footerTexts = /* @__PURE__ */ new Map();
5200
- for (let bi = 0; bi < blocks.length; bi++) {
5201
- const b = blocks[bi];
5202
- if (!b.bbox || !b.pageNumber || !b.text?.trim()) continue;
5203
- const ph = pageHeights.get(b.bbox.page) || pageHeights.get(b.pageNumber);
5204
- if (!ph) continue;
5205
- const blockTop = ph - (b.bbox.y + b.bbox.height);
5206
- const blockBottom = ph - b.bbox.y;
5207
- if (blockBottom <= ph * ZONE_RATIO) {
5208
- const arr = footerTexts.get(b.pageNumber) || [];
5209
- arr.push(b.text.trim());
5210
- footerTexts.set(b.pageNumber, arr);
5211
- } else if (blockTop >= ph * (1 - ZONE_RATIO)) {
5212
- const arr = headerTexts.get(b.pageNumber) || [];
5213
- arr.push(b.text.trim());
5214
- headerTexts.set(b.pageNumber, arr);
5215
- }
5216
- }
5217
- const repeatedPatterns = /* @__PURE__ */ new Set();
5218
- for (const textsMap of [headerTexts, footerTexts]) {
5219
- const patternCount = /* @__PURE__ */ new Map();
5220
- for (const [, texts] of textsMap) {
5221
- for (const t of texts) {
5222
- const normalized = t.replace(/\d+/g, "#");
5223
- patternCount.set(normalized, (patternCount.get(normalized) || 0) + 1);
5224
- }
5225
- }
5226
- for (const [pattern, count] of patternCount) {
5227
- if (count >= MIN_REPEAT) repeatedPatterns.add(pattern);
5228
- }
5229
- }
5230
- if (repeatedPatterns.size === 0) return [];
5231
- const removeIndices = [];
5232
- for (let bi = 0; bi < blocks.length; bi++) {
5233
- const b = blocks[bi];
5234
- if (!b.bbox || !b.pageNumber || !b.text?.trim()) continue;
5235
- const ph = pageHeights.get(b.bbox.page) || pageHeights.get(b.pageNumber);
5236
- if (!ph) continue;
5237
- const blockTop = ph - (b.bbox.y + b.bbox.height);
5238
- const blockBottom = ph - b.bbox.y;
5239
- const inZone = blockBottom <= ph * ZONE_RATIO || blockTop >= ph * (1 - ZONE_RATIO);
5240
- if (!inZone) continue;
5241
- const normalized = b.text.trim().replace(/\d+/g, "#");
5242
- if (repeatedPatterns.has(normalized)) {
5243
- removeIndices.push(bi);
5244
- }
5245
- }
5246
- if (removeIndices.length > 0) {
5247
- warnings.push({ message: `${removeIndices.length}\uAC1C \uBA38\uB9AC\uAE00/\uBC14\uB2E5\uAE00 \uC694\uC18C \uC81C\uAC70\uB428`, code: "HIDDEN_TEXT_FILTERED" });
5248
- }
5249
- return removeIndices;
5250
- }
5251
- function mergeKoreanLines(text) {
5252
- if (!text) return "";
5253
- const lines = text.split("\n");
5254
- if (lines.length <= 1) return text;
5255
- const result = [lines[0]];
5256
- for (let i = 1; i < lines.length; i++) {
5257
- const prev = result[result.length - 1];
5258
- const curr = lines[i];
5259
- const currTrimmed = curr.trim();
5260
- if (/^#{1,6}\s/.test(prev) || /^#{1,6}\s/.test(curr) || /^\|/.test(currTrimmed) || /^---/.test(currTrimmed)) {
5261
- result.push(curr);
5262
- continue;
5263
- }
5264
- if (/,$/.test(prev.trim()) && currTrimmed.length > 0) {
5265
- result[result.length - 1] = prev + "\n" + curr;
5266
- continue;
5267
- }
5268
- if (/^\(※/.test(currTrimmed)) {
5269
- result[result.length - 1] = prev + " " + currTrimmed;
5270
- continue;
5271
- }
5272
- if (/[가-힣·,\-]$/.test(prev) && /^[가-힣(]/.test(curr) && !startsWithMarker(curr) && !isStandaloneHeader(prev) && !startsWithMarker(prev)) {
5273
- result[result.length - 1] = prev + " " + curr;
5274
- } else {
5275
- result.push(curr);
5276
- }
5277
- }
5278
- return result.join("\n");
5279
- }
5280
-
5281
- // src/xlsx/parser.ts
5282
- import JSZip3 from "jszip";
5283
- import { DOMParser as DOMParser2 } from "@xmldom/xmldom";
5284
- var MAX_SHEETS = 100;
5285
- var MAX_DECOMPRESS_SIZE3 = 100 * 1024 * 1024;
5286
- var MAX_ROWS2 = 1e4;
5287
- var MAX_COLS2 = 200;
5288
- function cleanNumericValue(raw) {
5289
- if (!/^-?\d+\.\d+$/.test(raw)) return raw;
5290
- const num = parseFloat(raw);
5291
- if (!isFinite(num)) return raw;
5292
- const cleaned = parseFloat(num.toPrecision(15)).toString();
5293
- return cleaned;
5294
- }
5295
- function parseCellRef(ref) {
5296
- const m = ref.match(/^([A-Z]+)(\d+)$/);
5297
- if (!m) return null;
5298
- let col = 0;
5299
- for (const ch of m[1]) col = col * 26 + (ch.charCodeAt(0) - 64);
5300
- return { col: col - 1, row: parseInt(m[2], 10) - 1 };
5301
- }
5302
- function parseMergeRef(ref) {
5303
- const parts = ref.split(":");
5304
- if (parts.length !== 2) return null;
5305
- const start = parseCellRef(parts[0]);
5306
- const end = parseCellRef(parts[1]);
5307
- if (!start || !end) return null;
5308
- return { startCol: start.col, startRow: start.row, endCol: end.col, endRow: end.row };
5309
- }
5310
- function getElements(parent, tagName) {
5311
- const nodes = parent.getElementsByTagName(tagName);
5312
- const result = [];
5313
- for (let i = 0; i < nodes.length; i++) result.push(nodes[i]);
5314
- return result;
5315
- }
5316
- function getTextContent(el) {
5317
- return el.textContent?.trim() ?? "";
5318
- }
5319
- function parseXml(text) {
5320
- return new DOMParser2().parseFromString(stripDtd(text), "text/xml");
5321
- }
5322
- function parseSharedStrings(xml) {
5323
- const doc = parseXml(xml);
5324
- const strings = [];
5325
- const siList = getElements(doc.documentElement, "si");
5326
- for (const si of siList) {
5327
- const tElements = getElements(si, "t");
5328
- strings.push(tElements.map((t) => t.textContent ?? "").join(""));
5329
- }
5330
- return strings;
2578
+ return strings;
5331
2579
  }
5332
2580
  function parseWorkbook(xml) {
5333
2581
  const doc = parseXml(xml);
@@ -5492,7 +2740,7 @@ async function parseXlsxDocument(buffer, options) {
5492
2740
  }
5493
2741
  let pageFilter = null;
5494
2742
  if (options?.pages) {
5495
- const { parsePageRange: parsePageRange2 } = await Promise.resolve().then(() => (init_page_range(), page_range_exports));
2743
+ const { parsePageRange: parsePageRange2 } = await import("./page-range-H35FN3OQ.js");
5496
2744
  pageFilter = parsePageRange2(options.pages, sheets.length);
5497
2745
  }
5498
2746
  const blocks = [];
@@ -7245,6 +4493,18 @@ async function parseHwp(buffer, options) {
7245
4493
  }
7246
4494
  }
7247
4495
  async function parsePdf(buffer, options) {
4496
+ let parsePdfDocument;
4497
+ try {
4498
+ const mod = await import("./parser-43IAQ5KE.js");
4499
+ parsePdfDocument = mod.parsePdfDocument;
4500
+ } catch {
4501
+ return {
4502
+ success: false,
4503
+ fileType: "pdf",
4504
+ error: "PDF \uD30C\uC2F1\uC5D0 pdfjs-dist\uAC00 \uD544\uC694\uD569\uB2C8\uB2E4. \uC124\uCE58: npm install pdfjs-dist",
4505
+ code: "MISSING_DEPENDENCY"
4506
+ };
4507
+ }
7248
4508
  try {
7249
4509
  const { markdown, blocks, metadata, outline, warnings, isImageBased } = await parsePdfDocument(buffer, options);
7250
4510
  return { success: true, fileType: "pdf", markdown, blocks, metadata, outline, warnings, isImageBased };