kordoc 4.0.8 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +25 -11
  2. package/dist/{-LE4RLXVA.js → -AS4IABL2.js} +23 -10
  3. package/dist/{chunk-2DZQF6YJ.js → chunk-37HJHCPA.js} +24 -15
  4. package/dist/chunk-37HJHCPA.js.map +1 -0
  5. package/dist/{chunk-6XAAYAWL.cjs → chunk-37VKMUST.cjs} +47 -37
  6. package/dist/chunk-37VKMUST.cjs.map +1 -0
  7. package/dist/{chunk-OQPILS7B.js → chunk-4KI23VHA.js} +33 -23
  8. package/dist/chunk-4KI23VHA.js.map +1 -0
  9. package/dist/{chunk-D35VBACN.js → chunk-AX2R5Q2N.js} +7 -4
  10. package/dist/chunk-AX2R5Q2N.js.map +1 -0
  11. package/dist/chunk-DZIXKL7E.js +145 -0
  12. package/dist/chunk-DZIXKL7E.js.map +1 -0
  13. package/dist/chunk-F2ZU3IZG.js +82 -0
  14. package/dist/chunk-F2ZU3IZG.js.map +1 -0
  15. package/dist/{chunk-BNU5QGIZ.js → chunk-GQVSHGG4.js} +12 -4
  16. package/dist/chunk-GQVSHGG4.js.map +1 -0
  17. package/dist/{chunk-7S3M4N4E.js → chunk-L2XQQTOY.js} +566 -189
  18. package/dist/chunk-L2XQQTOY.js.map +1 -0
  19. package/dist/{chunk-HMUEU7D7.js → chunk-O5VS7RIR.js} +2 -2
  20. package/dist/{chunk-UEIYETCQ.js → chunk-SSTK6IKK.js} +18 -160
  21. package/dist/chunk-SSTK6IKK.js.map +1 -0
  22. package/dist/chunk-UOO7LSDG.js +152 -0
  23. package/dist/chunk-UOO7LSDG.js.map +1 -0
  24. package/dist/{chunk-RROH5WHO.js → chunk-WO52HVQJ.js} +2 -2
  25. package/dist/{chunk-MEPHGCPQ.js → chunk-YOO6ET6M.js} +7 -3
  26. package/dist/chunk-YOO6ET6M.js.map +1 -0
  27. package/dist/chunks-WVMGF7JL.js +10 -0
  28. package/dist/cli.js +170 -35
  29. package/dist/cli.js.map +1 -1
  30. package/dist/{detect-RI2MQ33K.js → detect-YEZX2XCF.js} +2 -2
  31. package/dist/index.cjs +1142 -531
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.d.cts +105 -1
  34. package/dist/index.d.ts +105 -1
  35. package/dist/index.js +802 -191
  36. package/dist/index.js.map +1 -1
  37. package/dist/mcp.js +273 -65
  38. package/dist/mcp.js.map +1 -1
  39. package/dist/{parser-ITMU2WN5.cjs → parser-N4A7UGDK.cjs} +127 -45
  40. package/dist/parser-N4A7UGDK.cjs.map +1 -0
  41. package/dist/{parser-TCTCSBYZ.js → parser-PEOYBXBO.js} +118 -34
  42. package/dist/parser-PEOYBXBO.js.map +1 -0
  43. package/dist/{parser-KMMUS73Q.js → parser-XX4QTDFQ.js} +114 -32
  44. package/dist/parser-XX4QTDFQ.js.map +1 -0
  45. package/dist/{profile-io-SLN4P76T.js → profile-io-PLDQFPJA.js} +3 -3
  46. package/dist/{provider-G4C2V2PD.cjs → provider-4FAYHJ6N.cjs} +3 -3
  47. package/dist/provider-4FAYHJ6N.cjs.map +1 -0
  48. package/dist/{provider-4ZJKV3DC.js → provider-C6IOGIS5.js} +3 -3
  49. package/dist/provider-C6IOGIS5.js.map +1 -0
  50. package/dist/{provider-AKROB7WQ.js → provider-UIBW2MIH.js} +3 -3
  51. package/dist/provider-UIBW2MIH.js.map +1 -0
  52. package/dist/rasterize-WWNQLKYW.js +40 -0
  53. package/dist/rasterize-WWNQLKYW.js.map +1 -0
  54. package/dist/redact-7ILEAUTS.js +12 -0
  55. package/dist/render-T7S6H6AN.js +10 -0
  56. package/dist/render-T7S6H6AN.js.map +1 -0
  57. package/dist/seal-7PTL6MXH.js +10 -0
  58. package/dist/seal-7PTL6MXH.js.map +1 -0
  59. package/dist/{setup-57FB3LSP.js → setup-Q2PRE7UA.js} +5 -3
  60. package/dist/setup-Q2PRE7UA.js.map +1 -0
  61. package/dist/{watch-OHQWSVPE.js → watch-SFHXZALC.js} +77 -23
  62. package/dist/watch-SFHXZALC.js.map +1 -0
  63. package/package.json +1 -1
  64. package/dist/chunk-2DZQF6YJ.js.map +0 -1
  65. package/dist/chunk-6XAAYAWL.cjs.map +0 -1
  66. package/dist/chunk-7S3M4N4E.js.map +0 -1
  67. package/dist/chunk-BNU5QGIZ.js.map +0 -1
  68. package/dist/chunk-D35VBACN.js.map +0 -1
  69. package/dist/chunk-MEPHGCPQ.js.map +0 -1
  70. package/dist/chunk-OQPILS7B.js.map +0 -1
  71. package/dist/chunk-UEIYETCQ.js.map +0 -1
  72. package/dist/parser-ITMU2WN5.cjs.map +0 -1
  73. package/dist/parser-KMMUS73Q.js.map +0 -1
  74. package/dist/parser-TCTCSBYZ.js.map +0 -1
  75. package/dist/provider-4ZJKV3DC.js.map +0 -1
  76. package/dist/provider-AKROB7WQ.js.map +0 -1
  77. package/dist/provider-G4C2V2PD.cjs.map +0 -1
  78. package/dist/render-25BT623I.js +0 -10
  79. package/dist/seal-R4TETA4C.js +0 -10
  80. package/dist/setup-57FB3LSP.js.map +0 -1
  81. package/dist/watch-OHQWSVPE.js.map +0 -1
  82. /package/dist/{-LE4RLXVA.js.map → -AS4IABL2.js.map} +0 -0
  83. /package/dist/{chunk-HMUEU7D7.js.map → chunk-O5VS7RIR.js.map} +0 -0
  84. /package/dist/{chunk-RROH5WHO.js.map → chunk-WO52HVQJ.js.map} +0 -0
  85. /package/dist/{detect-RI2MQ33K.js.map → chunks-WVMGF7JL.js.map} +0 -0
  86. /package/dist/{profile-io-SLN4P76T.js.map → detect-YEZX2XCF.js.map} +0 -0
  87. /package/dist/{render-25BT623I.js.map → profile-io-PLDQFPJA.js.map} +0 -0
  88. /package/dist/{seal-R4TETA4C.js.map → redact-7ILEAUTS.js.map} +0 -0
@@ -7,7 +7,7 @@ import {
7
7
  encodePng,
8
8
  safeMax,
9
9
  safeMin
10
- } from "./chunk-OQPILS7B.js";
10
+ } from "./chunk-4KI23VHA.js";
11
11
  import {
12
12
  parsePageRange
13
13
  } from "./chunk-GE43BE46.js";
@@ -578,6 +578,18 @@ function buildVertices(horizontals, verticals) {
578
578
  }
579
579
  function mergeVertices(vertices) {
580
580
  if (vertices.length <= 1) return vertices;
581
+ let maxRadiusAll = 1;
582
+ for (const v of vertices) {
583
+ if (v.radius > maxRadiusAll) maxRadiusAll = v.radius;
584
+ }
585
+ const cell = Math.max(VERTEX_MERGE_FACTOR * maxRadiusAll, 1);
586
+ const buckets = /* @__PURE__ */ new Map();
587
+ for (let i = 0; i < vertices.length; i++) {
588
+ const key = Math.floor(vertices[i].x / cell) + "," + Math.floor(vertices[i].y / cell);
589
+ const arr = buckets.get(key);
590
+ if (arr) arr.push(i);
591
+ else buckets.set(key, [i]);
592
+ }
581
593
  const merged = [];
582
594
  const used = new Array(vertices.length).fill(false);
583
595
  for (let i = 0; i < vertices.length; i++) {
@@ -585,7 +597,20 @@ function mergeVertices(vertices) {
585
597
  let sumX = vertices[i].x, sumY = vertices[i].y;
586
598
  let maxRadius = vertices[i].radius;
587
599
  let count = 1;
588
- for (let j = i + 1; j < vertices.length; j++) {
600
+ const cx = Math.floor(vertices[i].x / cell);
601
+ const cy = Math.floor(vertices[i].y / cell);
602
+ const candidates = [];
603
+ for (let dx = -1; dx <= 1; dx++) {
604
+ for (let dy = -1; dy <= 1; dy++) {
605
+ const arr = buckets.get(cx + dx + "," + (cy + dy));
606
+ if (!arr) continue;
607
+ for (const j of arr) {
608
+ if (j > i && !used[j]) candidates.push(j);
609
+ }
610
+ }
611
+ }
612
+ candidates.sort((a, b) => a - b);
613
+ for (const j of candidates) {
589
614
  if (used[j]) continue;
590
615
  const mergeTol = VERTEX_MERGE_FACTOR * Math.max(maxRadius, vertices[j].radius);
591
616
  if (Math.abs(vertices[i].x - vertices[j].x) <= mergeTol && Math.abs(vertices[i].y - vertices[j].y) <= mergeTol) {
@@ -812,6 +837,7 @@ function splitStackedGroup(group) {
812
837
  }
813
838
  return bands.filter((b) => b.length > 0);
814
839
  }
840
+ var GROUP_BUCKET_CELL = 100;
815
841
  function groupConnectedLines(lines) {
816
842
  const parent = lines.map((_, i) => i);
817
843
  function find(x) {
@@ -825,10 +851,34 @@ function groupConnectedLines(lines) {
825
851
  const ra = find(a), rb = find(b);
826
852
  if (ra !== rb) parent[ra] = rb;
827
853
  }
854
+ const cellMap = /* @__PURE__ */ new Map();
828
855
  for (let i = 0; i < lines.length; i++) {
829
- for (let j = i + 1; j < lines.length; j++) {
830
- if (linesIntersect(lines[i], lines[j])) {
831
- union(i, j);
856
+ const l = lines[i];
857
+ const cx1 = Math.floor((Math.min(l.x1, l.x2) - CONNECT_TOL) / GROUP_BUCKET_CELL);
858
+ const cx2 = Math.floor((Math.max(l.x1, l.x2) + CONNECT_TOL) / GROUP_BUCKET_CELL);
859
+ const cy1 = Math.floor((Math.min(l.y1, l.y2) - CONNECT_TOL) / GROUP_BUCKET_CELL);
860
+ const cy2 = Math.floor((Math.max(l.y1, l.y2) + CONNECT_TOL) / GROUP_BUCKET_CELL);
861
+ for (let cx = cx1; cx <= cx2; cx++) {
862
+ for (let cy = cy1; cy <= cy2; cy++) {
863
+ const key = cx + "," + cy;
864
+ const arr = cellMap.get(key);
865
+ if (arr) arr.push(i);
866
+ else cellMap.set(key, [i]);
867
+ }
868
+ }
869
+ }
870
+ const tested = /* @__PURE__ */ new Set();
871
+ for (const arr of cellMap.values()) {
872
+ for (let a = 0; a < arr.length; a++) {
873
+ for (let b = a + 1; b < arr.length; b++) {
874
+ const i = Math.min(arr[a], arr[b]);
875
+ const j = Math.max(arr[a], arr[b]);
876
+ const key = i * lines.length + j;
877
+ if (tested.has(key)) continue;
878
+ tested.add(key);
879
+ if (linesIntersect(lines[i], lines[j])) {
880
+ union(i, j);
881
+ }
832
882
  }
833
883
  }
834
884
  }
@@ -1316,12 +1366,21 @@ async function extractPageImages(page, fnArray, argsArray, pageNumber, state, wa
1316
1366
  if (typeof objId !== "string" || pageSeenIds.has(objId)) continue;
1317
1367
  pageSeenIds.add(objId);
1318
1368
  dedupeId = objId;
1319
- imgData = await resolveImgData(page, objId);
1320
- } else if (op === OPS3.paintInlineImageXObject) {
1321
- imgData = argsArray[i]?.[0];
1322
- } else {
1369
+ } else if (op !== OPS3.paintInlineImageXObject) {
1323
1370
  continue;
1324
1371
  }
1372
+ if (state.imageIndex >= MAX_IMAGES_PER_DOC || state.totalBytes >= MAX_TOTAL_IMAGE_BYTES) {
1373
+ if (!state.capWarned) {
1374
+ state.capWarned = true;
1375
+ warnings.push({ page: pageNumber, message: `\uC774\uBBF8\uC9C0 \uCD94\uCD9C \uC0C1\uD55C \uB3C4\uB2EC (${MAX_IMAGES_PER_DOC}\uAC1C/128MB) \u2014 \uC774\uD6C4 \uC774\uBBF8\uC9C0 \uC0DD\uB7B5`, code: "SKIPPED_IMAGE" });
1376
+ }
1377
+ continue;
1378
+ }
1379
+ if (dedupeId) {
1380
+ imgData = await resolveImgData(page, dedupeId);
1381
+ } else {
1382
+ imgData = argsArray[i]?.[0];
1383
+ }
1325
1384
  if (!imgData?.data || !imgData.width || !imgData.height) continue;
1326
1385
  const { width: w, height: h } = imgData;
1327
1386
  if (w < MIN_DIM || h < MIN_DIM) continue;
@@ -1331,13 +1390,6 @@ async function extractPageImages(page, fnArray, argsArray, pageNumber, state, wa
1331
1390
  }
1332
1391
  const hash = `${w}x${h}k${imgData.kind ?? "?"}h${fnv1a(imgData.data)}${dedupeId ? "" : "inline"}`;
1333
1392
  if (state.seen.has(hash)) continue;
1334
- if (state.imageIndex >= MAX_IMAGES_PER_DOC || state.totalBytes >= MAX_TOTAL_IMAGE_BYTES) {
1335
- if (!state.capWarned) {
1336
- state.capWarned = true;
1337
- warnings.push({ page: pageNumber, message: `\uC774\uBBF8\uC9C0 \uCD94\uCD9C \uC0C1\uD55C \uB3C4\uB2EC (${MAX_IMAGES_PER_DOC}\uAC1C/128MB) \u2014 \uC774\uD6C4 \uC774\uBBF8\uC9C0 \uC0DD\uB7B5`, code: "SKIPPED_IMAGE" });
1338
- }
1339
- continue;
1340
- }
1341
1393
  const rgba = toRgba(imgData);
1342
1394
  if (!rgba) continue;
1343
1395
  const png = encodePng(w, h, rgba);
@@ -2680,11 +2732,12 @@ function buildGridTable(lines, columns) {
2680
2732
  if (filledCells < totalCells * 0.35 || merged.length < 2 || merged.length <= 3 && numCols >= 7) {
2681
2733
  return merged.map((r) => r.filter((c) => c).join(" ")).join("\n");
2682
2734
  }
2735
+ const escCell = (c) => c.replace(/\t/g, " ").replace(/\|/g, "\\|");
2683
2736
  const md = [];
2684
- md.push("| " + merged[0].join(" | ") + " |");
2737
+ md.push("| " + merged[0].map(escCell).join(" | ") + " |");
2685
2738
  md.push("| " + merged[0].map(() => "---").join(" | ") + " |");
2686
2739
  for (let r = 1; r < merged.length; r++) {
2687
- md.push("| " + merged[r].join(" | ") + " |");
2740
+ md.push("| " + merged[r].map(escCell).join(" | ") + " |");
2688
2741
  }
2689
2742
  return md.join("\n");
2690
2743
  }
@@ -2864,6 +2917,7 @@ function detectKoreanListBlocks(blocks) {
2864
2917
  }
2865
2918
  let familyStack = [];
2866
2919
  let lastTopLevelList = null;
2920
+ let prevListIdx = -2;
2867
2921
  const toRemove = /* @__PURE__ */ new Set();
2868
2922
  for (let i = 0; i < blocks.length; i++) {
2869
2923
  const b = blocks[i];
@@ -2892,7 +2946,7 @@ function detectKoreanListBlocks(blocks) {
2892
2946
  if (depth === 0) {
2893
2947
  blocks[i] = listBlock;
2894
2948
  lastTopLevelList = listBlock;
2895
- } else if (lastTopLevelList) {
2949
+ } else if (lastTopLevelList && i === prevListIdx + 1) {
2896
2950
  if (!lastTopLevelList.children) lastTopLevelList.children = [];
2897
2951
  lastTopLevelList.children.push(listBlock);
2898
2952
  toRemove.add(i);
@@ -2900,6 +2954,7 @@ function detectKoreanListBlocks(blocks) {
2900
2954
  blocks[i] = listBlock;
2901
2955
  lastTopLevelList = listBlock;
2902
2956
  }
2957
+ prevListIdx = i;
2903
2958
  }
2904
2959
  if (toRemove.size > 0) {
2905
2960
  const sorted = [...toRemove].sort((a, b) => b - a);
@@ -3025,8 +3080,8 @@ function removeHeaderFooterBlocks(blocks, pageHeights, warnings) {
3025
3080
  const blockTop = ph - (b.bbox.y + b.bbox.height);
3026
3081
  const blockBottom = ph - b.bbox.y;
3027
3082
  const entry = { blockIdx: bi, page: b.pageNumber, text: b.text.trim() };
3028
- if (blockBottom <= ph * ZONE_RATIO) bottomEntries.push(entry);
3029
- else if (blockTop >= ph * (1 - ZONE_RATIO)) topEntries.push(entry);
3083
+ if (blockBottom <= ph * ZONE_RATIO) topEntries.push(entry);
3084
+ else if (blockTop >= ph * (1 - ZONE_RATIO)) bottomEntries.push(entry);
3030
3085
  }
3031
3086
  const removeSet = /* @__PURE__ */ new Set();
3032
3087
  for (const entries of [topEntries, bottomEntries]) {
@@ -3199,6 +3254,8 @@ function extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals) {
3199
3254
  hasSpaceBefore: i.hasSpaceBefore
3200
3255
  }));
3201
3256
  const cellTextMap = mapTextToCells(textItems, cells);
3257
+ const assignedItems = /* @__PURE__ */ new Set();
3258
+ for (const arr of cellTextMap.values()) for (const it of arr) assignedItems.add(it);
3202
3259
  const numRows = grid.rowYs.length - 1;
3203
3260
  const numCols = grid.colXs.length - 1;
3204
3261
  const irGrid = Array.from(
@@ -3218,9 +3275,11 @@ function extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals) {
3218
3275
  }
3219
3276
  let finalGrid = irGrid;
3220
3277
  let finalRows = numRows;
3278
+ let rebuiltUsed = false;
3221
3279
  if (numRows <= 2 && numCols >= 3) {
3222
3280
  const rebuilt = normalizeUndersegmentedTable(irGrid, grid.colXs, textItems);
3223
3281
  if (rebuilt) {
3282
+ rebuiltUsed = true;
3224
3283
  finalGrid = rebuilt.map((row) => row.map((rawText) => {
3225
3284
  const cleaned = rawText.replace(/^[\s]*[-–—]\s*\d+\s*[-–—][\s]*$/gm, "").trim();
3226
3285
  return {
@@ -3232,6 +3291,11 @@ function extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals) {
3232
3291
  finalRows = finalGrid.length;
3233
3292
  }
3234
3293
  }
3294
+ if (!rebuiltUsed) {
3295
+ for (let ti = 0; ti < textItems.length; ti++) {
3296
+ if (!assignedItems.has(textItems[ti])) usedItems.delete(tableItems[ti]);
3297
+ }
3298
+ }
3235
3299
  const irTable = {
3236
3300
  rows: finalRows,
3237
3301
  cols: numCols,
@@ -3288,6 +3352,8 @@ function extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals) {
3288
3352
  }
3289
3353
  remaining = remaining.filter((_, idx) => !usedClusterIndices.has(idx));
3290
3354
  }
3355
+ const groupSizes = [];
3356
+ let finalTextBlocks = [];
3291
3357
  if (remaining.length > 0) {
3292
3358
  const allY = remaining.map((i) => i.y);
3293
3359
  const pageH = safeMax(allY) - safeMin(allY);
@@ -3297,16 +3363,28 @@ function extractBlocksWithGrids(items, pageNum, grids, horizontals, verticals) {
3297
3363
  if (group.length === 0) continue;
3298
3364
  const groupBlocks = extractPageBlocksFallback(group, pageNum);
3299
3365
  for (const b of groupBlocks) textBlocks.push(b);
3366
+ groupSizes.push(groupBlocks.length);
3300
3367
  }
3301
- const finalTextBlocks = detectListBlocks(textBlocks);
3302
- for (const b of finalTextBlocks) blocks.push(b);
3368
+ finalTextBlocks = detectListBlocks(textBlocks);
3303
3369
  }
3304
- blocks.sort((a, b) => {
3305
- const ay = a.bbox ? a.bbox.y + a.bbox.height : 0;
3306
- const by = b.bbox ? b.bbox.y + b.bbox.height : 0;
3307
- return by - ay;
3308
- });
3309
- return mergeAdjacentTableBlocks(blocks);
3370
+ const units = blocks.map((b) => [b]);
3371
+ let off = 0;
3372
+ for (const size of groupSizes) {
3373
+ const unit = finalTextBlocks.slice(off, off + size);
3374
+ off += size;
3375
+ if (unit.length > 0) units.push(unit);
3376
+ }
3377
+ const unitTopY = (u) => {
3378
+ let top = 0;
3379
+ for (const b of u) {
3380
+ if (b.bbox && b.bbox.y + b.bbox.height > top) top = b.bbox.y + b.bbox.height;
3381
+ }
3382
+ return top;
3383
+ };
3384
+ units.sort((a, b) => unitTopY(b) - unitTopY(a));
3385
+ const ordered = [];
3386
+ for (const u of units) for (const b of u) ordered.push(b);
3387
+ return mergeAdjacentTableBlocks(ordered);
3310
3388
  }
3311
3389
  return mergeAdjacentTableBlocks(blocks);
3312
3390
  }
@@ -3795,7 +3873,11 @@ async function parsePdfDocument(buffer, options) {
3795
3873
  }
3796
3874
  let pageText = "";
3797
3875
  for (const b of pageBlocks) {
3798
- const t = b.text || "";
3876
+ let t = b.text || "";
3877
+ if (b.type === "table" && b.table) {
3878
+ const cellText = b.table.cells.map((row) => row.map((c) => c.text).join(" ")).join("\n");
3879
+ t = t ? t + "\n" + cellText : cellText;
3880
+ }
3799
3881
  totalChars += t.replace(/\s/g, "").length;
3800
3882
  totalTextBytes += t.length * 2;
3801
3883
  pageText += pageText ? "\n" + t : t;
@@ -3814,7 +3896,7 @@ async function parsePdfDocument(buffer, options) {
3814
3896
  if (totalChars / Math.max(parsedPageCount, 1) < 10) {
3815
3897
  if (options?.ocr) {
3816
3898
  try {
3817
- const { ocrPages } = await import("./provider-4ZJKV3DC.js");
3899
+ const { ocrPages } = await import("./provider-C6IOGIS5.js");
3818
3900
  const ocrBlocks = await ocrPages(doc, options.ocr, pageFilter, effectivePageCount);
3819
3901
  if (ocrBlocks.length > 0) {
3820
3902
  const ocrMarkdown = ocrBlocks.map((b) => b.text || "").filter(Boolean).join("\n\n");
@@ -3935,4 +4017,4 @@ export {
3935
4017
  parsePdfDocument,
3936
4018
  removeHeaderFooterBlocks
3937
4019
  };
3938
- //# sourceMappingURL=parser-KMMUS73Q.js.map
4020
+ //# sourceMappingURL=parser-XX4QTDFQ.js.map