kordoc 4.22.14 → 4.22.16

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 (125) hide show
  1. package/README.md +6 -2
  2. package/dist/{-AF5OVQ2B.js → -3XSSQOP6.js} +11 -11
  3. package/dist/{chunk-PHULZF6L.js → chunk-2WAKSO2Z.js} +2 -2
  4. package/dist/{chunk-DWP5CLVR.js → chunk-2ZGVOGHM.js} +2 -2
  5. package/dist/{chunk-6LKB7UWJ.js → chunk-3BQYVNOW.js} +2 -2
  6. package/dist/{chunk-TMJJKLNC.cjs → chunk-3NYFGUOJ.cjs} +24 -24
  7. package/dist/{chunk-TMJJKLNC.cjs.map → chunk-3NYFGUOJ.cjs.map} +1 -1
  8. package/dist/{chunk-THAL2XH4.js → chunk-5EISWK2J.js} +2 -2
  9. package/dist/{chunk-H2J3WNB3.js → chunk-75YWX55A.js} +3 -3
  10. package/dist/{chunk-EMC23F72.js → chunk-C3XICONM.js} +4 -4
  11. package/dist/{chunk-F4IXPKBU.cjs → chunk-FFQLQLRE.cjs} +6 -6
  12. package/dist/{chunk-F4IXPKBU.cjs.map → chunk-FFQLQLRE.cjs.map} +1 -1
  13. package/dist/{chunk-LVKASU3G.js → chunk-GDTSPYWP.js} +2 -2
  14. package/dist/{chunk-DERMNCUV.js → chunk-HXV4V6IH.js} +2 -2
  15. package/dist/{chunk-M6LVRVKB.cjs → chunk-IUVFOBQM.cjs} +3 -3
  16. package/dist/{chunk-M6LVRVKB.cjs.map → chunk-IUVFOBQM.cjs.map} +1 -1
  17. package/dist/{chunk-SHJPQ3QB.cjs → chunk-JL3IR45L.cjs} +23 -23
  18. package/dist/{chunk-SHJPQ3QB.cjs.map → chunk-JL3IR45L.cjs.map} +1 -1
  19. package/dist/{chunk-NMA5P7SJ.js → chunk-JTRP3OO2.js} +2 -2
  20. package/dist/{chunk-WHNF67YI.cjs → chunk-JZCBDX34.cjs} +3 -3
  21. package/dist/{chunk-WHNF67YI.cjs.map → chunk-JZCBDX34.cjs.map} +1 -1
  22. package/dist/{chunk-JKSHF4LV.js → chunk-L5HFJV5O.js} +3 -3
  23. package/dist/{chunk-2HLWRP2W.js → chunk-NDO4XZMT.js} +2 -2
  24. package/dist/{chunk-FN462MNZ.js → chunk-RMNJTPSR.js} +4 -4
  25. package/dist/{chunk-WQWOCG53.js → chunk-RVZVCHUE.js} +2 -2
  26. package/dist/{chunk-PFVDY2EK.js → chunk-RWLGFHZO.js} +2 -2
  27. package/dist/{chunk-YDKJZF5L.js → chunk-SDIKLPMY.js} +2 -2
  28. package/dist/{chunk-SXSBYB4M.js → chunk-TI534EYY.js} +6 -6
  29. package/dist/{chunk-KNG6HPBO.cjs → chunk-UDG4L37H.cjs} +2 -2
  30. package/dist/{chunk-KNG6HPBO.cjs.map → chunk-UDG4L37H.cjs.map} +1 -1
  31. package/dist/{chunk-IE6LE6W3.js → chunk-VJ6UNTC7.js} +2 -2
  32. package/dist/{chunk-FJ5RGHZW.js → chunk-VTVPHZJ6.js} +476 -48
  33. package/dist/chunk-VTVPHZJ6.js.map +1 -0
  34. package/dist/{chunk-FNF3IGCT.js → chunk-VVOURQFY.js} +2 -2
  35. package/dist/{chunk-FNF3IGCT.js.map → chunk-VVOURQFY.js.map} +1 -1
  36. package/dist/{chunk-AA7VSDW7.js → chunk-W5SRR7W6.js} +3 -3
  37. package/dist/{chunk-5IWMX3MX.js → chunk-WMJPOOBJ.js} +2 -2
  38. package/dist/{chunk-634XJS2B.js → chunk-XD4B7B55.js} +2 -2
  39. package/dist/{chunk-634XJS2B.js.map → chunk-XD4B7B55.js.map} +1 -1
  40. package/dist/{chunk-EAUI4MGW.js → chunk-Z7URWVQH.js} +4 -4
  41. package/dist/chunks-XXPD5OV5.js +10 -0
  42. package/dist/cli.js +32 -32
  43. package/dist/cli.js.map +1 -1
  44. package/dist/{formula-HUREVQCI.js → formula-JMFME265.js} +4 -4
  45. package/dist/{formula-4AJAVAWT.cjs → formula-XSJKSTPU.cjs} +9 -9
  46. package/dist/{formula-4AJAVAWT.cjs.map → formula-XSJKSTPU.cjs.map} +1 -1
  47. package/dist/{formula-UBZCYRF4.js → formula-YWCFQN7M.js} +3 -3
  48. package/dist/{image-ocr-DTZWQOQL.cjs → image-ocr-2OWPVTWD.cjs} +17 -17
  49. package/dist/{image-ocr-DTZWQOQL.cjs.map → image-ocr-2OWPVTWD.cjs.map} +1 -1
  50. package/dist/{image-ocr-3R2YM67Q.js → image-ocr-HH4WLRMA.js} +9 -9
  51. package/dist/{image-ocr-PJT3Z4YE.js → image-ocr-W52R3U55.js} +6 -6
  52. package/dist/index.cjs +1015 -587
  53. package/dist/index.cjs.map +1 -1
  54. package/dist/index.d.cts +14 -3
  55. package/dist/index.d.ts +14 -3
  56. package/dist/index.js +471 -43
  57. package/dist/index.js.map +1 -1
  58. package/dist/mcp.js +41 -33
  59. package/dist/mcp.js.map +1 -1
  60. package/dist/{model-bundle-37BM65SF.js → model-bundle-LAMGDBR5.js} +5 -5
  61. package/dist/{models-SINSBMMS.js → models-UW6K53RI.js} +5 -5
  62. package/dist/{parse-worker-v2-XZE6G3YZ.js → parse-worker-v2-TCYTBP2P.js} +12 -12
  63. package/dist/{parser-BN6SMWKJ.js → parser-J37C64DT.js} +12 -12
  64. package/dist/{parser-7NEY6KUC.cjs → parser-JXAIWIOE.cjs} +157 -157
  65. package/dist/{parser-7NEY6KUC.cjs.map → parser-JXAIWIOE.cjs.map} +1 -1
  66. package/dist/{parser-PYLI2HSC.js → parser-Y2OKEGM4.js} +9 -9
  67. package/dist/{pdf-ocr-2O7FQ6JB.js → pdf-ocr-BDNBONES.js} +6 -6
  68. package/dist/pdf-ocr-HPC5BVB7.cjs +31 -0
  69. package/dist/{pdf-ocr-W5UAELPX.cjs.map → pdf-ocr-HPC5BVB7.cjs.map} +1 -1
  70. package/dist/{pdf-ocr-472YHMNQ.js → pdf-ocr-NUQHXJJR.js} +9 -9
  71. package/dist/{profile-io-KENW2S4U.js → profile-io-SET4RRP2.js} +3 -3
  72. package/dist/{rasterize-CDGVIXWB.js → rasterize-O7ZM2CRL.js} +3 -3
  73. package/dist/{redact-doc-LGIC27QL.js → redact-doc-Z3ISHGTJ.js} +11 -11
  74. package/dist/{render-4WB6VKAI.js → render-MLKOC2PN.js} +7 -7
  75. package/dist/seal-7QUWLHB6.js +10 -0
  76. package/dist/{setup-RNR35MKP.js → setup-CUB6RCSD.js} +3 -3
  77. package/dist/visual-QLWVIUQS.js +21 -0
  78. package/dist/{watch-VLHG2G5B.js → watch-YPDHDT4N.js} +12 -12
  79. package/package.json +1 -1
  80. package/dist/chunk-FJ5RGHZW.js.map +0 -1
  81. package/dist/chunks-75SH4S3P.js +0 -10
  82. package/dist/pdf-ocr-W5UAELPX.cjs +0 -31
  83. package/dist/seal-V6YQIAUC.js +0 -10
  84. package/dist/visual-ZZZ65FH6.js +0 -21
  85. /package/dist/{-AF5OVQ2B.js.map → -3XSSQOP6.js.map} +0 -0
  86. /package/dist/{chunk-PHULZF6L.js.map → chunk-2WAKSO2Z.js.map} +0 -0
  87. /package/dist/{chunk-DWP5CLVR.js.map → chunk-2ZGVOGHM.js.map} +0 -0
  88. /package/dist/{chunk-6LKB7UWJ.js.map → chunk-3BQYVNOW.js.map} +0 -0
  89. /package/dist/{chunk-THAL2XH4.js.map → chunk-5EISWK2J.js.map} +0 -0
  90. /package/dist/{chunk-H2J3WNB3.js.map → chunk-75YWX55A.js.map} +0 -0
  91. /package/dist/{chunk-EMC23F72.js.map → chunk-C3XICONM.js.map} +0 -0
  92. /package/dist/{chunk-LVKASU3G.js.map → chunk-GDTSPYWP.js.map} +0 -0
  93. /package/dist/{chunk-DERMNCUV.js.map → chunk-HXV4V6IH.js.map} +0 -0
  94. /package/dist/{chunk-NMA5P7SJ.js.map → chunk-JTRP3OO2.js.map} +0 -0
  95. /package/dist/{chunk-JKSHF4LV.js.map → chunk-L5HFJV5O.js.map} +0 -0
  96. /package/dist/{chunk-2HLWRP2W.js.map → chunk-NDO4XZMT.js.map} +0 -0
  97. /package/dist/{chunk-FN462MNZ.js.map → chunk-RMNJTPSR.js.map} +0 -0
  98. /package/dist/{chunk-WQWOCG53.js.map → chunk-RVZVCHUE.js.map} +0 -0
  99. /package/dist/{chunk-PFVDY2EK.js.map → chunk-RWLGFHZO.js.map} +0 -0
  100. /package/dist/{chunk-YDKJZF5L.js.map → chunk-SDIKLPMY.js.map} +0 -0
  101. /package/dist/{chunk-SXSBYB4M.js.map → chunk-TI534EYY.js.map} +0 -0
  102. /package/dist/{chunk-IE6LE6W3.js.map → chunk-VJ6UNTC7.js.map} +0 -0
  103. /package/dist/{chunk-AA7VSDW7.js.map → chunk-W5SRR7W6.js.map} +0 -0
  104. /package/dist/{chunk-5IWMX3MX.js.map → chunk-WMJPOOBJ.js.map} +0 -0
  105. /package/dist/{chunk-EAUI4MGW.js.map → chunk-Z7URWVQH.js.map} +0 -0
  106. /package/dist/{chunks-75SH4S3P.js.map → chunks-XXPD5OV5.js.map} +0 -0
  107. /package/dist/{formula-HUREVQCI.js.map → formula-JMFME265.js.map} +0 -0
  108. /package/dist/{formula-UBZCYRF4.js.map → formula-YWCFQN7M.js.map} +0 -0
  109. /package/dist/{image-ocr-3R2YM67Q.js.map → image-ocr-HH4WLRMA.js.map} +0 -0
  110. /package/dist/{image-ocr-PJT3Z4YE.js.map → image-ocr-W52R3U55.js.map} +0 -0
  111. /package/dist/{model-bundle-37BM65SF.js.map → model-bundle-LAMGDBR5.js.map} +0 -0
  112. /package/dist/{models-SINSBMMS.js.map → models-UW6K53RI.js.map} +0 -0
  113. /package/dist/{parse-worker-v2-XZE6G3YZ.js.map → parse-worker-v2-TCYTBP2P.js.map} +0 -0
  114. /package/dist/{parser-BN6SMWKJ.js.map → parser-J37C64DT.js.map} +0 -0
  115. /package/dist/{parser-PYLI2HSC.js.map → parser-Y2OKEGM4.js.map} +0 -0
  116. /package/dist/{pdf-ocr-2O7FQ6JB.js.map → pdf-ocr-BDNBONES.js.map} +0 -0
  117. /package/dist/{pdf-ocr-472YHMNQ.js.map → pdf-ocr-NUQHXJJR.js.map} +0 -0
  118. /package/dist/{profile-io-KENW2S4U.js.map → profile-io-SET4RRP2.js.map} +0 -0
  119. /package/dist/{rasterize-CDGVIXWB.js.map → rasterize-O7ZM2CRL.js.map} +0 -0
  120. /package/dist/{redact-doc-LGIC27QL.js.map → redact-doc-Z3ISHGTJ.js.map} +0 -0
  121. /package/dist/{render-4WB6VKAI.js.map → render-MLKOC2PN.js.map} +0 -0
  122. /package/dist/{seal-V6YQIAUC.js.map → seal-7QUWLHB6.js.map} +0 -0
  123. /package/dist/{setup-RNR35MKP.js.map → setup-CUB6RCSD.js.map} +0 -0
  124. /package/dist/{visual-ZZZ65FH6.js.map → visual-QLWVIUQS.js.map} +0 -0
  125. /package/dist/{watch-VLHG2G5B.js.map → watch-YPDHDT4N.js.map} +0 -0
@@ -50,10 +50,10 @@ import {
50
50
  resolveParaHeading,
51
51
  simulateWrap,
52
52
  takeTocLeaders
53
- } from "./chunk-SXSBYB4M.js";
53
+ } from "./chunk-TI534EYY.js";
54
54
  import {
55
55
  blocksToPages
56
- } from "./chunk-WQWOCG53.js";
56
+ } from "./chunk-RVZVCHUE.js";
57
57
  import {
58
58
  bmpToPng,
59
59
  inlineImagesIntoMarkdown
@@ -75,7 +75,7 @@ import {
75
75
  tidyScriptTags,
76
76
  unframeLayoutTables,
77
77
  wrapScript
78
- } from "./chunk-FNF3IGCT.js";
78
+ } from "./chunk-VVOURQFY.js";
79
79
  import {
80
80
  MAX_COLS,
81
81
  MAX_ROWS,
@@ -97,7 +97,7 @@ import {
97
97
  noteTableHtml,
98
98
  sanitizeText,
99
99
  visibleText
100
- } from "./chunk-DERMNCUV.js";
100
+ } from "./chunk-HXV4V6IH.js";
101
101
  import {
102
102
  ANNOTATION_BLANK_RE,
103
103
  ValueCursor,
@@ -130,7 +130,7 @@ import {
130
130
  resolveUnmatched,
131
131
  scanInlineSegments,
132
132
  scanSectionXml
133
- } from "./chunk-JKSHF4LV.js";
133
+ } from "./chunk-L5HFJV5O.js";
134
134
  import {
135
135
  MAX_DECOMPRESS_SIZE,
136
136
  MAX_XML_DEPTH,
@@ -147,7 +147,7 @@ import {
147
147
  localName,
148
148
  rawTextContent,
149
149
  readZipEntry
150
- } from "./chunk-IE6LE6W3.js";
150
+ } from "./chunk-VJ6UNTC7.js";
151
151
  import {
152
152
  KordocError,
153
153
  classifyError,
@@ -163,7 +163,7 @@ import {
163
163
  toArrayBuffer,
164
164
  unescapeHtml,
165
165
  unzipLimitBytes
166
- } from "./chunk-PHULZF6L.js";
166
+ } from "./chunk-2WAKSO2Z.js";
167
167
 
168
168
  // src/table/visual.ts
169
169
  import { readFile as readFile2 } from "fs/promises";
@@ -19327,7 +19327,7 @@ var MIN_SIDE = 24;
19327
19327
  async function ocrEmbeddedImages(blocks, options, warnings) {
19328
19328
  const ocr = options?.ocr;
19329
19329
  if (ocr !== true && ocr !== "force" && typeof ocr !== "function") return false;
19330
- const { parseImageDocument } = await import("./image-ocr-3R2YM67Q.js");
19330
+ const { parseImageDocument } = await import("./image-ocr-HH4WLRMA.js");
19331
19331
  let inserted = false, unreadable = 0;
19332
19332
  let failed = null;
19333
19333
  const walk = async (list, depth) => {
@@ -27908,6 +27908,400 @@ function minimalTextSplices(para2, xml, origPlain, newPlain, origSegs, newSegs)
27908
27908
  return out;
27909
27909
  }
27910
27910
 
27911
+ // src/roundtrip/structure-edit.ts
27912
+ var LINESEG_RE2 = /<(\w+:)?linesegarray\b[^>]*?(?:\/>|>[\s\S]*?<\/\1linesegarray>)/g;
27913
+ var OBJECT_RE = /<(?:\w+:)?(?:secPr|ctrl|tbl|pic|container|rect|ellipse|arc|polygon|curve|line|connectLine|textart|ole|equation|chart|video|drawText|switch)\b/;
27914
+ var SYMBOL_FAMILY = {
27915
+ "\u25CB": "\u25CB",
27916
+ "\u3147": "\u25CB",
27917
+ "\u25E6": "\u25CB",
27918
+ "\u274D": "\u25CB",
27919
+ "o": "\u25CB",
27920
+ "\u25CF": "\u25CF",
27921
+ "-": "-",
27922
+ "\u2013": "-",
27923
+ "\u2015": "-",
27924
+ "\u2014": "-",
27925
+ "\uFF0D": "-",
27926
+ "\xB7": "\u318D",
27927
+ "\u318D": "\u318D",
27928
+ "\u2219": "\u318D",
27929
+ "\u2022": "\u318D",
27930
+ "\u25A1": "\u25A1",
27931
+ "\u25A0": "\u25A0",
27932
+ "\u25C7": "\u25C7",
27933
+ "\u25C6": "\u25C6",
27934
+ "\u25A3": "\u25A3",
27935
+ "\u203B": "\u203B",
27936
+ "*": "*",
27937
+ "\u25B6": "\u25B6",
27938
+ "\u25B7": "\u25B7",
27939
+ "\u25BA": "\u25B6",
27940
+ "\u25B8": "\u25B8",
27941
+ "\u25AA": "\u25AA",
27942
+ "\u2713": "\u2713",
27943
+ "\u21D2": "\u21D2",
27944
+ "\u21B3": "\u21B3"
27945
+ };
27946
+ function markerClass(plain4, headingLevel) {
27947
+ const t = plain4.replace(/^\s+/, "");
27948
+ let cls = "";
27949
+ const sym = SYMBOL_FAMILY[t[0]];
27950
+ if (sym && (t.length === 1 || /\s/.test(t[1]))) cls = sym;
27951
+ else if (/^붙\s*임(?:\s|$)/.test(t)) cls = "\uBD99\uC784";
27952
+ else if (/^[❶-❿]/.test(t)) cls = "\u2776";
27953
+ else if (/^[①-⑳]/.test(t)) cls = "\u2460";
27954
+ else if (/^[㉮-㉻]/.test(t)) cls = "\u326E";
27955
+ else if (/^[Ⅰ-Ⅻ][.\s]/.test(t)) cls = "\u2160.";
27956
+ else {
27957
+ const m = /^(\(?)(\d{1,2}|[가나다라마바사아자차카타파하])([.)])(?=\s|$)/.exec(t);
27958
+ if (m) cls = `${m[1]}${/\d/.test(m[2]) ? "N" : "K"}${m[3]}`;
27959
+ }
27960
+ return headingLevel ? `h${headingLevel}:${cls}` : cls;
27961
+ }
27962
+ function topLevelParagraphs(xml) {
27963
+ const root = /<(?:\w+:)?sec\b[^>]*>/.exec(xml);
27964
+ if (!root) return [];
27965
+ const out = [];
27966
+ let pos = root.index + root[0].length;
27967
+ const pRe = /<(?:\w+:)?p(?=[\s>/])/y;
27968
+ for (; ; ) {
27969
+ const lt = xml.indexOf("<", pos);
27970
+ if (lt < 0 || xml.startsWith("</", lt)) break;
27971
+ const end = findElementEnd(xml, lt);
27972
+ if (end < 0) break;
27973
+ pRe.lastIndex = lt;
27974
+ if (pRe.test(xml)) out.push({ start: lt, end });
27975
+ pos = end;
27976
+ }
27977
+ return out;
27978
+ }
27979
+ function containingTopParagraph(tops, pos) {
27980
+ let lo = 0, hi = tops.length - 1;
27981
+ while (lo <= hi) {
27982
+ const mid = lo + hi >> 1;
27983
+ if (tops[mid].end <= pos) lo = mid + 1;
27984
+ else if (tops[mid].start > pos) hi = mid - 1;
27985
+ else return tops[mid];
27986
+ }
27987
+ return void 0;
27988
+ }
27989
+ function isRemovableParagraph(fragment) {
27990
+ const open = fragment.slice(0, fragment.indexOf(">") + 1);
27991
+ if (/\b(?:pageBreak|columnBreak)="1"/.test(open)) return false;
27992
+ return !OBJECT_RE.test(fragment.slice(open.length));
27993
+ }
27994
+ function keepBreaks(clone, targetOpen) {
27995
+ let out = clone;
27996
+ for (const attr of ["pageBreak", "columnBreak"]) {
27997
+ if (!new RegExp(`\\b${attr}="1"`).test(targetOpen)) continue;
27998
+ out = new RegExp(`^<[^>]*?\\b${attr}=`).test(out) ? out.replace(new RegExp(`^(<[^>]*?)\\b${attr}="0"`), `$1${attr}="1"`) : out.replace(/^<([\w:]+)/, `<$1 ${attr}="1"`);
27999
+ }
28000
+ return out;
28001
+ }
28002
+ function pickDonor(donors, cls, near, exclude) {
28003
+ let best;
28004
+ for (const d of donors) {
28005
+ if (d.cls !== cls || d.unitIdx === exclude) continue;
28006
+ if (!best || Math.abs(d.unitIdx - near) < Math.abs(best.unitIdx - near)) best = d;
28007
+ }
28008
+ return best;
28009
+ }
28010
+ function cloneParagraphXml(xml, donor, newPlain, newSegs) {
28011
+ const end = findElementEnd(xml, donor.para.start);
28012
+ if (end < 0) return null;
28013
+ const frag = xml.slice(donor.para.start, end);
28014
+ if (!isRemovableParagraph(frag.replace(/\b(?:pageBreak|columnBreak)="1"/, ""))) return null;
28015
+ const para2 = scanSectionXml(frag, donor.sectionIndex).bodyParagraphs[0];
28016
+ if (!para2 || para2.start !== 0) return null;
28017
+ const splices = minimalTextSplices(para2, frag, donor.plain, newPlain, donor.segs, newSegs) ?? markerRunSplices(para2, frag, newPlain) ?? buildParagraphSplices(para2, newPlain, frag);
28018
+ if (splices === null) return null;
28019
+ return applySplices(frag, splices).replace(LINESEG_RE2, "").replace(/^(<[^>]*?)\bpageBreak="1"/, '$1pageBreak="0"').replace(/^(<[^>]*?)\bcolumnBreak="1"/, '$1columnBreak="0"');
28020
+ }
28021
+ function editedInline(raw) {
28022
+ let text = raw.split("\n").map((l) => l.trim()).filter(Boolean).join(" ");
28023
+ const hm = /^(#{1,6})\s+/.exec(text);
28024
+ if (hm) text = text.slice(hm[0].length);
28025
+ return { ...parseEmphasis(text), level: hm ? hm[1].length : void 0 };
28026
+ }
28027
+ function createStructureEditor(env) {
28028
+ const tops = env.scans.map((s) => topLevelParagraphs(s.xml));
28029
+ const removed = env.scans.map(() => []);
28030
+ const loc = (oi) => {
28031
+ const u = env.origUnits[oi];
28032
+ const block = env.origBlocks[u.blockIdx];
28033
+ if (!block) return void 0;
28034
+ if (block.type === "table") {
28035
+ const ord = env.obTableOrdinals.get(u.blockIdx);
28036
+ const t = ord !== void 0 && env.obTableOrdinals.size === env.scanTables.length ? env.scanTables[ord] : void 0;
28037
+ return t ? { sectionIndex: t.sectionIndex, pos: t.start } : void 0;
28038
+ }
28039
+ const para2 = env.paraMap.get(u.blockIdx)?.para;
28040
+ return para2 ? { sectionIndex: para2.sectionIndex, pos: para2.start } : void 0;
28041
+ };
28042
+ const topOf = (oi) => {
28043
+ const l = loc(oi);
28044
+ const top = l && containingTopParagraph(tops[l.sectionIndex], l.pos);
28045
+ return top && { sectionIndex: l.sectionIndex, ...top };
28046
+ };
28047
+ const isRemoved = (sectionIndex, pos) => removed[sectionIndex].some((r) => pos >= r.start && pos < r.end);
28048
+ const touched = (sectionIndex, start, end) => env.sectionSplices[sectionIndex].some((s) => s.start < end && s.end > start && !(s.start === s.end && (s.start === start || s.start === end)));
28049
+ const donors = [];
28050
+ env.origUnits.forEach((u, oi) => {
28051
+ if (u.kind !== "text" || u.fragment || u.role) return;
28052
+ const block = env.origBlocks[u.blockIdx];
28053
+ if (!block || block.type !== "paragraph" && block.type !== "heading" || block.footnoteText || block.text?.includes("\n")) return;
28054
+ const m = env.paraMap.get(u.blockIdx);
28055
+ if (!m?.para || m.para.kind !== "body" || m.prefixStripped || m.noteMarks?.length) return;
28056
+ const top = topOf(oi);
28057
+ const xml = env.scans[m.para.sectionIndex].xml;
28058
+ if (!top || top.start !== m.para.start || !isRemovableParagraph(xml.slice(top.start, top.end).replace(/\b(?:pageBreak|columnBreak)="1"/, ""))) return;
28059
+ const { plain: plain4, segs } = parseEmphasis(env.inlineOf(oi));
28060
+ if (!plain4.trim()) return;
28061
+ donors.push({ cls: markerClass(plain4, block.type === "heading" ? block.level : void 0), unitIdx: oi, sectionIndex: m.para.sectionIndex, para: m.para, plain: plain4, segs });
28062
+ });
28063
+ const donorOf = new Map(donors.map((d) => [d.unitIdx, d]));
28064
+ const chooseDonor = (cls, near) => {
28065
+ const same = pickDonor(donors, cls, near) ?? (cls.startsWith("h") ? pickDonor(donors, cls.slice(cls.indexOf(":") + 1), near) : void 0);
28066
+ if (same) return same;
28067
+ let before;
28068
+ for (const d of donors) {
28069
+ if (d.unitIdx < near) before = d;
28070
+ else return before ?? d;
28071
+ }
28072
+ return before;
28073
+ };
28074
+ const skip = (reason, before, after) => {
28075
+ env.skipped.push({ reason, ...before !== void 0 ? { before: summarize(before) } : {}, ...after !== void 0 ? { after: summarize(after) } : {} });
28076
+ return 0;
28077
+ };
28078
+ const firstEdit = env.scans.map(() => Infinity);
28079
+ const mark = (sectionIndex, pos) => {
28080
+ firstEdit[sectionIndex] = Math.min(firstEdit[sectionIndex], pos);
28081
+ };
28082
+ return {
28083
+ /**
28084
+ * 구조 편집 뒤 최상위 문단의 줄 배치 캐시 제거 splice — 문단이 늘거나 줄면 뒤 문단 캐시의 세로 위치(vertpos)가 낡는다.
28085
+ * 캐시를 믿고 그리는 뷰어(kordoc render)가 낡은 자리에 그려 붙임 줄이 다음 쪽 한가운데로 밀렸다(견본 재작성 실측).
28086
+ * 첫 구조 편집 앞 문단과 글을 고친 문단(패처가 따로 지운다)은 건드리지 않는다
28087
+ */
28088
+ staleCacheSplices(sectionIndex) {
28089
+ const from = firstEdit[sectionIndex];
28090
+ if (from === Infinity) return [];
28091
+ const xml = env.scans[sectionIndex].xml;
28092
+ const out = [];
28093
+ for (const top of tops[sectionIndex]) {
28094
+ if (top.start < from || touched(sectionIndex, top.start, top.end)) continue;
28095
+ const c = ownCacheSplice(xml, top.start, top.end);
28096
+ if (c) out.push(c);
28097
+ }
28098
+ return out;
28099
+ },
28100
+ /** 부호가 바뀐 글 수정 — 새 부호를 단 견본 문단을 복제해 그 자리를 갈아 끼운다. 해당 없으면 null(일반 수정으로) */
28101
+ restyle(oi, edited) {
28102
+ const own = donorOf.get(oi);
28103
+ if (!own || edited.kind !== "text") return null;
28104
+ const ed = editedInline(edited.raw);
28105
+ const cls = markerClass(ed.plain, ed.level);
28106
+ if (cls === own.cls) return null;
28107
+ const donor = pickDonor(donors, cls, oi, oi);
28108
+ if (!donor) return null;
28109
+ const top = topOf(oi);
28110
+ if (touched(top.sectionIndex, top.start, top.end)) return null;
28111
+ const clone = cloneParagraphXml(env.scans[donor.sectionIndex].xml, donor, ed.plain, ed.segs);
28112
+ if (clone === null) return null;
28113
+ const xml = env.scans[top.sectionIndex].xml;
28114
+ env.sectionSplices[top.sectionIndex].push({ start: top.start, end: top.end, replacement: keepBreaks(clone, xml.slice(top.start, xml.indexOf(">", top.start) + 1)) });
28115
+ removed[top.sectionIndex].push(top);
28116
+ mark(top.sectionIndex, top.start);
28117
+ return 1;
28118
+ },
28119
+ remove(oi) {
28120
+ const u = env.origUnits[oi];
28121
+ if (u.kind === "image" || u.kind === "separator") return skip("\uC774\uBBF8\uC9C0/\uAD6C\uBD84\uC120 \uC0AD\uC81C\uB294 \uBBF8\uC9C0\uC6D0 \u2014 \uC6D0\uBCF8 \uC720\uC9C0", u.raw);
28122
+ if (u.fragment || u.role) return skip("\uBB38\uB2E8 \uC77C\uBD80(\uAC15\uC81C \uC904\uBC14\uAFC8 \uC870\uAC01\xB7\uD45C \uCEA1\uC158) \uC0AD\uC81C\uB294 \uBBF8\uC9C0\uC6D0 \u2014 \uC6D0\uBCF8 \uC720\uC9C0", u.raw);
28123
+ const top = topOf(oi);
28124
+ if (!top) return skip("\uC0AD\uC81C\uD560 \uBB38\uB2E8 \uC704\uCE58\uB97C \uCC3E\uC9C0 \uBABB\uD568 (\uBA38\uB9AC\uB9D0\xB7\uAE00\uC0C1\uC790\xB7\uD45C \uC548 \uC601\uC5ED) \u2014 \uC6D0\uBCF8 \uC720\uC9C0", u.raw);
28125
+ if (isRemoved(top.sectionIndex, top.start)) return 0;
28126
+ const xml = env.scans[top.sectionIndex].xml;
28127
+ const block = env.origBlocks[u.blockIdx];
28128
+ let rest = xml.slice(top.start, top.end);
28129
+ if (block?.type === "table") {
28130
+ const l = loc(oi);
28131
+ const tEnd = findElementEnd(xml, l.pos);
28132
+ rest = xml.slice(top.start, l.pos) + xml.slice(tEnd, top.end);
28133
+ if (/<(?:\w+:)?t>[^<]*\S/.test(rest)) return skip("\uD45C \uC0AD\uC81C \uBBF8\uC9C0\uC6D0 \u2014 \uD45C\uC640 \uAC19\uC740 \uBB38\uB2E8\uC5D0 \uAE00\uC774 \uC788\uC74C", u.raw);
28134
+ } else {
28135
+ const para2 = env.paraMap.get(u.blockIdx)?.para;
28136
+ if (!para2 || para2.kind !== "body" || para2.start !== top.start) return skip("\uBCF8\uBB38 \uBC16(\uD45C \uCE78\xB7\uAE00\uC0C1\uC790) \uBB38\uB2E8 \uC0AD\uC81C\uB294 \uBBF8\uC9C0\uC6D0 \u2014 \uC6D0\uBCF8 \uC720\uC9C0", u.raw);
28137
+ }
28138
+ if (!isRemovableParagraph(rest) || touched(top.sectionIndex, top.start, top.end)) {
28139
+ const para2 = block?.type !== "table" ? env.paraMap.get(u.blockIdx)?.para : void 0;
28140
+ const splices = para2 && !touched(top.sectionIndex, top.start, top.end) ? buildParagraphSplices(para2, "", xml) : null;
28141
+ if (!splices) return skip("\uAC1C\uCCB4\xB7\uAD6C\uC5ED \uC815\uC758\uAC00 \uB4E0 \uBB38\uB2E8\uC774\uB77C \uC0AD\uC81C \uBBF8\uC9C0\uC6D0 \u2014 \uC6D0\uBCF8 \uC720\uC9C0", u.raw);
28142
+ env.sectionSplices[top.sectionIndex].push(...splices);
28143
+ env.skipped.push({ reason: "\uAC1C\uCCB4\xB7\uAD6C\uC5ED \uC815\uC758\xB7\uCABD \uB098\uB214\uC774 \uB4E0 \uBB38\uB2E8 \u2014 \uBB38\uB2E8\uC740 \uB450\uACE0 \uAE00\uB9CC \uBE44\uC6C0", before: summarize(u.raw), partial: true });
28144
+ return 1;
28145
+ }
28146
+ env.sectionSplices[top.sectionIndex].push({ start: top.start, end: top.end, replacement: "" });
28147
+ removed[top.sectionIndex].push(top);
28148
+ mark(top.sectionIndex, top.start);
28149
+ return 1;
28150
+ },
28151
+ insert(edited, lastOi) {
28152
+ if (edited.kind === "image" || edited.kind === "separator") return skip("\uC774\uBBF8\uC9C0/\uAD6C\uBD84\uC120 \uCD94\uAC00\uB294 \uBBF8\uC9C0\uC6D0", void 0, edited.raw);
28153
+ if (edited.kind === "html-table") return skip("\uBCD1\uD569\uD45C(HTML) \uCD94\uAC00\uB294 \uBBF8\uC9C0\uC6D0 \u2014 GFM \uD45C(| \uD5E4\uB354 | \u2026 |)\uB85C \uC4F0\uAC70\uB098 \uC6D0\uBCF8 \uD45C\uB97C \uACE0\uCCD0 \uC4F0\uC138\uC694", void 0, edited.raw);
28154
+ let at;
28155
+ for (let k = lastOi ?? -1; k >= 0 && !at; k--) {
28156
+ const top = topOf(k);
28157
+ if (top) at = { sectionIndex: top.sectionIndex, pos: top.end, near: k + 0.5 };
28158
+ }
28159
+ if (!at) {
28160
+ for (let k = 0; k < env.origUnits.length && !at; k++) {
28161
+ const top = topOf(k);
28162
+ if (!top) continue;
28163
+ if (OBJECT_RE.test(env.scans[top.sectionIndex].xml.slice(top.start, top.end))) return skip("\uBB38\uC11C \uB9E8 \uC55E(\uAD6C\uC5ED \uC815\uC758 \uBB38\uB2E8 \uC55E) \uCD94\uAC00\uB294 \uBBF8\uC9C0\uC6D0 \u2014 \uCCAB \uBE14\uB85D \uB4A4\uC5D0 \uC4F0\uC138\uC694", void 0, edited.raw);
28164
+ at = { sectionIndex: top.sectionIndex, pos: top.start, near: k - 0.5 };
28165
+ }
28166
+ }
28167
+ if (!at) return skip("\uCD94\uAC00\uD560 \uC790\uB9AC\uB97C \uCC3E\uC9C0 \uBABB\uD568", void 0, edited.raw);
28168
+ const xml = env.scans[at.sectionIndex].xml;
28169
+ let fragment;
28170
+ if (edited.kind === "gfm-table") {
28171
+ const near = chooseDonor("", at.near);
28172
+ const prId = near ? Number(/paraPrIDRef="(\d+)"/.exec(xml.slice(near.para.start, near.para.start + 400))?.[1] ?? 0) : 0;
28173
+ const built = env.tableXml(edited, prId);
28174
+ if (typeof built !== "string") return skip(built.reason, void 0, edited.raw);
28175
+ fragment = built;
28176
+ } else {
28177
+ const ed = editedInline(edited.raw);
28178
+ if (!ed.plain.trim()) return 0;
28179
+ const donor = chooseDonor(markerClass(ed.plain, ed.level), at.near);
28180
+ if (!donor) return skip("\uC11C\uC2DD\uC744 \uBE4C\uB9B4 \uBCF8\uBB38 \uBB38\uB2E8\uC774 \uC5C6\uC5B4 \uCD94\uAC00 \uBBF8\uC9C0\uC6D0", void 0, edited.raw);
28181
+ fragment = cloneParagraphXml(env.scans[donor.sectionIndex].xml, donor, ed.plain, ed.segs);
28182
+ if (fragment === null) return skip("\uACAC\uBCF8 \uBB38\uB2E8 \uBCF5\uC81C \uC2E4\uD328 \u2014 \uCD94\uAC00 \uBBF8\uC9C0\uC6D0", void 0, edited.raw);
28183
+ }
28184
+ env.sectionSplices[at.sectionIndex].push({ start: at.pos, end: at.pos, replacement: fragment });
28185
+ mark(at.sectionIndex, at.pos);
28186
+ return 1;
28187
+ }
28188
+ };
28189
+ }
28190
+ function pairTablesInGaps(pairs, a, b) {
28191
+ const isTable = (k) => k === "gfm-table" || k === "html-table";
28192
+ const out = [];
28193
+ let gap = [];
28194
+ const flush = () => {
28195
+ const A = gap.filter((p) => p[0] !== null).map((p) => p[0]);
28196
+ const B = gap.filter((p) => p[1] !== null).map((p) => p[1]);
28197
+ gap = [];
28198
+ const matches = [];
28199
+ let bj = 0;
28200
+ for (const ai of A) {
28201
+ if (!isTable(a[ai].kind)) continue;
28202
+ let k = bj;
28203
+ while (k < B.length && b[B[k]].kind !== a[ai].kind) k++;
28204
+ if (k < B.length) {
28205
+ matches.push([ai, B[k]]);
28206
+ bj = k + 1;
28207
+ }
28208
+ }
28209
+ let ia = 0, ib = 0;
28210
+ for (const [ma, mb] of matches) {
28211
+ while (A[ia] !== ma) out.push([A[ia++], null]);
28212
+ while (B[ib] !== mb) out.push([null, B[ib++]]);
28213
+ out.push([A[ia++], B[ib++]]);
28214
+ }
28215
+ while (ia < A.length) out.push([A[ia++], null]);
28216
+ while (ib < B.length) out.push([null, B[ib++]]);
28217
+ };
28218
+ for (const p of pairs) {
28219
+ if (p[0] !== null && p[1] !== null) {
28220
+ flush();
28221
+ out.push(p);
28222
+ } else gap.push(p);
28223
+ }
28224
+ flush();
28225
+ return out;
28226
+ }
28227
+ function ownCacheSplice(xml, start, end) {
28228
+ const head = xml.slice(start, xml.lastIndexOf("</", end - 1)).trimEnd();
28229
+ const close = /<\/(\w+:)?linesegarray>$/.exec(head);
28230
+ const open = close ? head.lastIndexOf(`<${close[1] ?? ""}linesegarray`) : -1;
28231
+ return open >= 0 ? { start: start + open, end: start + head.length, replacement: "" } : null;
28232
+ }
28233
+ function restructureCellLines(cellParas, newLines, scans, sectionSplices, norm2) {
28234
+ cellParas = cellParas.filter((p) => !p.inTextbox);
28235
+ if (cellParas.length === 0) return null;
28236
+ const sectionIndex = cellParas[0].sectionIndex;
28237
+ const xml = scans[sectionIndex].xml;
28238
+ const nonEmpty = cellParas.filter((p) => norm2(p.text) !== "");
28239
+ const olds = nonEmpty.length ? nonEmpty : [cellParas[0]];
28240
+ const range = (p) => ({ start: p.start, end: findElementEnd(xml, p.start) });
28241
+ if (olds.some((p) => range(p).end < 0)) return null;
28242
+ const donors = olds.flatMap((p, i) => {
28243
+ const r = range(p);
28244
+ if (!isRemovableParagraph(xml.slice(r.start, r.end).replace(/\b(?:pageBreak|columnBreak)="1"/, ""))) return [];
28245
+ const plain4 = norm2(p.text);
28246
+ return [{ cls: markerClass(plain4), unitIdx: i, sectionIndex, para: p, plain: plain4, segs: [plain4] }];
28247
+ });
28248
+ if (donors.length === 0) return null;
28249
+ const pairs = nonEmpty.length ? alignUnits(olds.map((p) => norm2(p.text)), newLines.map(norm2)) : newLines.map((_, k) => k === 0 ? [0, 0] : [null, k]);
28250
+ const out = [];
28251
+ let first = Infinity, remaining = cellParas.length, lastOld = null;
28252
+ for (const [oi, ei] of pairs) {
28253
+ if (oi !== null && ei !== null) {
28254
+ lastOld = oi;
28255
+ const p = olds[oi], line = newLines[ei];
28256
+ if (norm2(p.text) === norm2(line)) continue;
28257
+ const cls = markerClass(line);
28258
+ const r = range(p);
28259
+ const donor = cls !== markerClass(norm2(p.text)) && isRemovableParagraph(xml.slice(r.start, r.end).replace(/\b(?:pageBreak|columnBreak)="1"/, "")) ? pickDonor(donors, cls, oi, oi) : void 0;
28260
+ const clone = donor ? cloneParagraphXml(xml, donor, line, [line]) : null;
28261
+ if (clone !== null) {
28262
+ out.push({ start: r.start, end: r.end, replacement: keepBreaks(clone, xml.slice(r.start, xml.indexOf(">", r.start) + 1)) });
28263
+ first = Math.min(first, r.start);
28264
+ continue;
28265
+ }
28266
+ const sp = minimalTextSplices(p, xml, norm2(p.text), line) ?? buildParagraphSplices(p, line, xml);
28267
+ if (sp === null) return null;
28268
+ out.push(...sp);
28269
+ } else if (oi !== null) {
28270
+ lastOld = oi;
28271
+ const p = olds[oi], r = range(p);
28272
+ if (remaining > 1 && isRemovableParagraph(xml.slice(r.start, r.end))) {
28273
+ out.push({ start: r.start, end: r.end, replacement: "" });
28274
+ remaining--;
28275
+ first = Math.min(first, r.start);
28276
+ } else {
28277
+ const sp = buildParagraphSplices(p, "", xml);
28278
+ if (sp === null) return null;
28279
+ out.push(...sp);
28280
+ }
28281
+ } else if (ei !== null) {
28282
+ const line = newLines[ei];
28283
+ if (!norm2(line)) continue;
28284
+ const near = lastOld === null ? -0.5 : lastOld + 0.5;
28285
+ const donor = pickDonor(donors, markerClass(line), near) ?? pickDonor(donors, "", near) ?? pickDonor(donors, donors[0].cls, near);
28286
+ const clone = donor && cloneParagraphXml(xml, donor, line, [line]);
28287
+ if (!clone) return null;
28288
+ const at = lastOld === null ? olds[0].start : range(olds[lastOld]).end;
28289
+ out.push({ start: at, end: at, replacement: clone });
28290
+ first = Math.min(first, at);
28291
+ }
28292
+ }
28293
+ if (first !== Infinity) {
28294
+ for (const p of cellParas) {
28295
+ const r = range(p);
28296
+ if (r.start < first || out.some((s) => s.start < r.end && s.end > r.start)) continue;
28297
+ const c = ownCacheSplice(xml, r.start, r.end);
28298
+ if (c) out.push(c);
28299
+ }
28300
+ }
28301
+ sectionSplices[sectionIndex].push(...out);
28302
+ return 1;
28303
+ }
28304
+
27911
28305
  // src/roundtrip/table-patch.ts
27912
28306
  function patchGfmTable(table2, scanTable, orig, edited, ctx, skip) {
27913
28307
  const replica = replicateGfmTable(table2);
@@ -28185,6 +28579,10 @@ function applyCellEdit(table2, scanTable, gridR, gridC, newLines, ctx, before, a
28185
28579
  let sectionIndex = -1;
28186
28580
  const unstable = newLines.find((l) => sanitizeText(l) !== l);
28187
28581
  if (unstable !== void 0) return skip("\uACF5\uBC31 \uC815\uADDC\uD654 \uBD88\uC548\uC815 \uD14D\uC2A4\uD2B8 \u2014 \uD328\uCE58 \uC2DC \uC6D0\uBB38 \uBCF4\uC874 \uBD88\uAC00\uB85C \uBBF8\uC9C0\uC6D0");
28582
+ if (ctx.structure && newLines.length > Math.max(nonEmpty.length, 1) || ctx.structure && nonEmpty.length > 1 && newLines.length < nonEmpty.length) {
28583
+ const done = restructureCellLines(cell2.paragraphs, newLines, ctx.scans, ctx.sectionSplices, normForMatch);
28584
+ if (done !== null) return done;
28585
+ }
28188
28586
  if (nonEmpty.length === 0) {
28189
28587
  if (newLines.length === 0) return 0;
28190
28588
  const target = cell2.paragraphs[0];
@@ -28736,7 +29134,8 @@ function serializeRecords(recs, repl, inserts) {
28736
29134
  };
28737
29135
  for (let i = 0; i < recs.length; i++) {
28738
29136
  for (const ins of inserts?.get(i) ?? []) push(ins.tagId, ins.level, ins.data);
28739
- push(recs[i].tagId, recs[i].level, repl?.get(i) ?? recs[i].data);
29137
+ const data = repl?.get(i);
29138
+ if (data !== null) push(recs[i].tagId, recs[i].level, data ?? recs[i].data);
28740
29139
  }
28741
29140
  return Buffer.concat(parts);
28742
29141
  }
@@ -28912,7 +29311,8 @@ async function patchHwp(original, editedMarkdown, options) {
28912
29311
  paraMap,
28913
29312
  scans,
28914
29313
  tableMap,
28915
- skipped
29314
+ skipped,
29315
+ keepLineseg: options?.keepLineseg === true
28916
29316
  });
28917
29317
  } else if (oi !== null) {
28918
29318
  skipped.push({ reason: "\uBE14\uB85D \uC0AD\uC81C\uB294 \uBBF8\uC9C0\uC6D0 (v1) \u2014 \uC6D0\uBCF8 \uC720\uC9C0", before: summarize(origUnits[oi].raw) });
@@ -29108,7 +29508,7 @@ function patchParagraph(block, orig, edited, ctx, skip) {
29108
29508
  const origPrefix = block.text.split(" ", 1)[0];
29109
29509
  if (origMd.startsWith(origPrefix + " ")) origMd = origMd.slice(origPrefix.length + 1);
29110
29510
  }
29111
- return stageParaPatch(ctx.scans[mapping.para.sectionIndex], mapping.para, newPlain, skip, origMd);
29511
+ return stageParaPatch(ctx.scans[mapping.para.sectionIndex], mapping.para, newPlain, skip, ctx.keepLineseg, origMd);
29112
29512
  }
29113
29513
  function patchGfmCells(scanTable, orig, edited, ctx, skip) {
29114
29514
  const origRows = parseGfmTable(orig.lines);
@@ -29150,7 +29550,7 @@ function patchGfmCells(scanTable, orig, edited, ctx, skip) {
29150
29550
  cellSkip("\uACF5\uBC31 \uC815\uADDC\uD654 \uBD88\uC548\uC815 \uD14D\uC2A4\uD2B8 \u2014 \uBBF8\uC9C0\uC6D0");
29151
29551
  continue;
29152
29552
  }
29153
- applied += stageParaPatch(ctx.scans[para2.sectionIndex], para2, after, cellSkip);
29553
+ applied += stageParaPatch(ctx.scans[para2.sectionIndex], para2, after, cellSkip, ctx.keepLineseg);
29154
29554
  continue;
29155
29555
  }
29156
29556
  if (beforeParts.length !== cell2.paras.length || afterParts.length !== cell2.paras.length) {
@@ -29178,7 +29578,7 @@ function patchGfmCells(scanTable, orig, edited, ctx, skip) {
29178
29578
  cellSkip("\uACF5\uBC31 \uC815\uADDC\uD654 \uBD88\uC548\uC815 \uD14D\uC2A4\uD2B8 \u2014 \uBBF8\uC9C0\uC6D0");
29179
29579
  break;
29180
29580
  }
29181
- applied += stageParaPatch(ctx.scans[para2.sectionIndex], para2, after, cellSkip);
29581
+ applied += stageParaPatch(ctx.scans[para2.sectionIndex], para2, after, cellSkip, ctx.keepLineseg);
29182
29582
  }
29183
29583
  }
29184
29584
  }
@@ -29310,7 +29710,7 @@ function applyCellEdit5(table2, scanTable, gridR, gridC, newLines, ctx, before,
29310
29710
  let staged = 0;
29311
29711
  for (let i = 0; i < targets.length; i++) {
29312
29712
  if (assigned[i] === targets[i].rawText || normForMatch(assigned[i]) === normForMatch(targets[i].rawText)) continue;
29313
- staged += stageParaPatch(ctx.scans[targets[i].sectionIndex], targets[i], assigned[i], skip);
29713
+ staged += stageParaPatch(ctx.scans[targets[i].sectionIndex], targets[i], assigned[i], skip, ctx.keepLineseg);
29314
29714
  }
29315
29715
  return staged > 0 ? 1 : 0;
29316
29716
  }
@@ -29495,11 +29895,11 @@ function synthesizeLineSegs(lineSegData, newRaw, startUnits) {
29495
29895
  }
29496
29896
  return { buf: Buffer.concat(segs), count: lines.length };
29497
29897
  }
29498
- function stageParaPatch(scan, para2, newPlain, skip, origPlain) {
29898
+ function stageParaPatch(scan, para2, newPlain, skip, keepLineseg, origPlain) {
29499
29899
  if (!scan.safe) return skip("\uC139\uC158 \uB808\uCF54\uB4DC \uC7AC\uC9C1\uB82C\uD654 \uBD88\uC77C\uCE58 \u2014 \uC548\uC804\uC744 \uC704\uD574 \uC774 \uC139\uC158\uC740 \uBBF8\uC9C0\uC6D0");
29500
29900
  if (para2.textIdx === -2) return skip("\uBCF5\uC218 PARA_TEXT \uB808\uCF54\uB4DC \uBB38\uB2E8 \u2014 \uBBF8\uC9C0\uC6D0 (v1)");
29501
29901
  if (para2.rangeTagCount > 0) return skip("\uBC94\uC704 \uD0DC\uADF8(\uD615\uAD11\uD39C/\uAD50\uC815\uBD80\uD638) \uBB38\uB2E8 \u2014 \uBBF8\uC9C0\uC6D0 (v1)");
29502
- if (para2.charShapeIdx < 0 || para2.lineSegIdx < 0) return skip("\uBB38\uB2E8 \uB808\uCF54\uB4DC \uAD6C\uC131 \uBE44\uC815\uD615 \u2014 \uBBF8\uC9C0\uC6D0");
29902
+ if (para2.charShapeIdx < 0) return skip("\uBB38\uB2E8 \uB808\uCF54\uB4DC \uAD6C\uC131 \uBE44\uC815\uD615 \u2014 \uBBF8\uC9C0\uC6D0");
29503
29903
  if (scan.repl.has(para2.headerIdx)) return skip("\uB3D9\uC77C \uBB38\uB2E8 \uC911\uBCF5 \uC218\uC815 \u2014 \uCCAB \uC218\uC815\uB9CC \uC801\uC6A9");
29504
29904
  if (/[\u0000-\u0009\u000b-\u001f]/.test(newPlain)) return skip("\uC0C8 \uD14D\uC2A4\uD2B8\uC5D0 \uC81C\uC5B4\uBB38\uC790 \uD3EC\uD568 \u2014 \uBBF8\uC9C0\uC6D0");
29505
29905
  const records = scan.records;
@@ -29521,13 +29921,7 @@ function stageParaPatch(scan, para2, newPlain, skip, origPlain) {
29521
29921
  const cs2 = rebuildCharShape(charShapeRec.data, 0);
29522
29922
  scan.repl.set(para2.charShapeIdx, cs2.buf);
29523
29923
  newHeader2.writeUInt16LE(cs2.count, 12);
29524
- if (newPlain.includes("\n")) {
29525
- const synth = synthesizeLineSegs(records[para2.lineSegIdx].data, newPlain, 0);
29526
- if (synth) {
29527
- scan.repl.set(para2.lineSegIdx, synth.buf);
29528
- newHeader2.writeUInt16LE(synth.count, 16);
29529
- }
29530
- }
29924
+ stageLineSegs(scan, para2, newHeader2, newPlain, 0, newPlain.length + nCharsLow, keepLineseg);
29531
29925
  scan.repl.set(para2.headerIdx, newHeader2);
29532
29926
  return 1;
29533
29927
  }
@@ -29549,14 +29943,23 @@ function stageParaPatch(scan, para2, newPlain, skip, origPlain) {
29549
29943
  const cs = edit ? shiftCharShape(charShapeRec.data, seg.prefixUnits + edit.start, seg.prefixUnits + edit.end, edit.text.length) : rebuildCharShape(charShapeRec.data, seg.prefixUnits);
29550
29944
  scan.repl.set(para2.charShapeIdx, cs.buf);
29551
29945
  newHeader.writeUInt16LE(cs.count, 12);
29552
- if (newRaw.includes("\n")) {
29553
- const synth = synthesizeLineSegs(records[para2.lineSegIdx].data, newRaw, seg.prefixUnits);
29946
+ stageLineSegs(scan, para2, newHeader, newRaw, seg.prefixUnits, nChars, keepLineseg);
29947
+ scan.repl.set(para2.headerIdx, newHeader);
29948
+ return 1;
29949
+ }
29950
+ function stageLineSegs(scan, para2, newHeader, newRaw, startUnits, nChars, keepLineseg) {
29951
+ if (para2.lineSegIdx < 0) return;
29952
+ const lineSegData = scan.records[para2.lineSegIdx].data;
29953
+ if (!keepLineseg) {
29954
+ scan.repl.set(para2.lineSegIdx, null);
29955
+ newHeader.writeUInt16LE(0, 16);
29956
+ } else if (newRaw.includes("\n")) {
29957
+ const synth = synthesizeLineSegs(lineSegData, newRaw, startUnits);
29554
29958
  if (synth) {
29555
29959
  scan.repl.set(para2.lineSegIdx, synth.buf);
29556
29960
  newHeader.writeUInt16LE(synth.count, 16);
29557
29961
  }
29558
29962
  } else {
29559
- const lineSegData = records[para2.lineSegIdx].data;
29560
29963
  const segCount = Math.floor(lineSegData.length / 36);
29561
29964
  let keep = 1;
29562
29965
  while (keep < segCount && lineSegData.readUInt32LE(keep * 36) < nChars) keep++;
@@ -29565,8 +29968,6 @@ function stageParaPatch(scan, para2, newPlain, skip, origPlain) {
29565
29968
  newHeader.writeUInt16LE(keep, 16);
29566
29969
  }
29567
29970
  }
29568
- scan.repl.set(para2.headerIdx, newHeader);
29569
- return 1;
29570
29971
  }
29571
29972
 
29572
29973
  // src/roundtrip/patcher.ts
@@ -29619,32 +30020,44 @@ async function patchHwpx(original, editedMarkdown, options) {
29619
30020
  }
29620
30021
  const origUnits = buildOrigUnits(origBlocks);
29621
30022
  const editedUnits = splitMarkdownUnits(editedMarkdown);
29622
- const pairs = alignUnits(origUnits.map((u) => u.raw), editedUnits.map((u) => u.raw));
30023
+ const pairs = alignUnits(origUnits.map((u) => looseRaw(u.raw)), editedUnits.map((u) => looseRaw(u.raw)));
29623
30024
  const paraMap = resolveParagraphMappings(origBlocks, scans);
29624
30025
  const scanTables = scans.flatMap((s) => s.tables.filter((t) => t.rows.length > 0));
29625
30026
  const obTableOrdinals = buildTableOrdinals(origBlocks);
29626
30027
  const sectionSplices = scans.map(() => []);
29627
- for (const [oi, ei] of pairs) {
30028
+ const structural = options?.structure !== false && origUnits.length * editedUnits.length <= 4e6;
30029
+ const ctx = { origBlocks, paraMap, scans, scanTables, obTableOrdinals, sectionSplices, skipped, tableInsert, structure: structural };
30030
+ const struct = structural ? createStructureEditor({
30031
+ origUnits,
30032
+ origBlocks,
30033
+ paraMap,
30034
+ scans,
30035
+ scanTables,
30036
+ obTableOrdinals,
30037
+ sectionSplices,
30038
+ skipped,
30039
+ inlineOf: (oi) => textUnitInline(origUnits[oi].raw, origBlocks[origUnits[oi].blockIdx]),
30040
+ tableXml: (edited2, outerParaPrId) => newTableParagraph(edited2, outerParaPrId, tableInsert)
30041
+ }) : null;
30042
+ let lastOi = null;
30043
+ for (const [oi, ei] of struct ? pairTablesInGaps(pairs, origUnits, editedUnits) : pairs) {
29628
30044
  if (oi !== null && ei !== null) {
30045
+ lastOi = oi;
29629
30046
  const orig = origUnits[oi];
29630
30047
  const edited2 = editedUnits[ei];
29631
- if (orig.raw === edited2.raw) continue;
29632
- applied += handleModifiedUnit(orig, edited2, {
29633
- origBlocks,
29634
- paraMap,
29635
- scans,
29636
- scanTables,
29637
- obTableOrdinals,
29638
- sectionSplices,
29639
- skipped,
29640
- tableInsert
29641
- });
30048
+ if (looseRaw(orig.raw) === looseRaw(edited2.raw)) continue;
30049
+ const restyled = struct?.restyle(oi, edited2) ?? null;
30050
+ applied += restyled ?? handleModifiedUnit(orig, edited2, ctx);
29642
30051
  } else if (oi !== null) {
29643
- skipped.push({ reason: "\uBE14\uB85D \uC0AD\uC81C\uB294 \uBBF8\uC9C0\uC6D0 (v1) \u2014 \uC6D0\uBCF8 \uC720\uC9C0", before: summarize(origUnits[oi].raw) });
30052
+ lastOi = oi;
30053
+ if (struct) applied += struct.remove(oi);
30054
+ else skipped.push({ reason: "\uBE14\uB85D \uC0AD\uC81C\uB294 \uBBF8\uC9C0\uC6D0 (structure: false) \u2014 \uC6D0\uBCF8 \uC720\uC9C0", before: summarize(origUnits[oi].raw) });
29644
30055
  } else if (ei !== null) {
29645
- skipped.push({ reason: "\uBE14\uB85D \uCD94\uAC00\uB294 \uBBF8\uC9C0\uC6D0 (v1)", after: summarize(editedUnits[ei].raw) });
30056
+ if (struct) applied += struct.insert(editedUnits[ei], lastOi);
30057
+ else skipped.push({ reason: "\uBE14\uB85D \uCD94\uAC00\uB294 \uBBF8\uC9C0\uC6D0 (structure: false)", after: summarize(editedUnits[ei].raw) });
29646
30058
  }
29647
30059
  }
30060
+ if (struct) scans.forEach((_, i) => sectionSplices[i].push(...struct.staleCacheSplices(i)));
29648
30061
  const replacements = /* @__PURE__ */ new Map();
29649
30062
  const edited = [];
29650
30063
  const encoder = new TextEncoder();
@@ -29939,6 +30352,18 @@ function convertParagraphToTable(block, orig, edited, ctx, skip) {
29939
30352
  ctx.sectionSplices[para2.sectionIndex].push({ start: para2.start, end: pEnd, replacement: tableXml });
29940
30353
  return 1;
29941
30354
  }
30355
+ function newTableParagraph(edited, outerParaPrId, ti) {
30356
+ if (!ti) return { reason: "\uD45C \uCD94\uAC00 \uBD88\uAC00 \u2014 header \uC5D4\uD2B8\uB9AC(borderFills) \uD574\uC11D \uC2E4\uD328" };
30357
+ const rows = parseGfmTable(edited.lines);
30358
+ if (rows.length === 0 || rows.every((r) => r.length === 0)) return { reason: "\uD45C \uB0B4\uC6A9\uC774 \uBE44\uC5B4 \uC788\uC74C" };
30359
+ if (ti.borderFillId === null) {
30360
+ const inj = injectCellBorderFill(ti.headerXml, ti.nextId++);
30361
+ if (!inj) return { reason: "header <hh:borderFills> \uAD6C\uC870\uB97C \uCC3E\uC744 \uC218 \uC5C6\uC5B4 \uD45C \uD14C\uB450\uB9AC \uC0DD\uC131 \uBD88\uAC00" };
30362
+ ti.borderFillId = inj.borderFillId;
30363
+ ti.headerSplices.push(...inj.headerSplices);
30364
+ }
30365
+ return buildTableParagraphXml(rows, { borderFillId: ti.borderFillId, outerParaPrId, cellParaPrId: 0, cellCharPrId: 0, tableId: ti.nextId++ });
30366
+ }
29942
30367
  function patchParagraphUnit(block, orig, edited, ctx, skip) {
29943
30368
  const mapping = ctx.paraMap.get(orig.blockIdx);
29944
30369
  if (!mapping?.para) return skip("\uBB38\uB2E8 \uC18C\uC2A4\uB9F5 \uB9E4\uD551 \uC2E4\uD328 (\uBA38\uB9AC\uB9D0/\uAE00\uC0C1\uC790/\uCEA1\uC158 \uC601\uC5ED\uC774\uAC70\uB098 \uD14D\uC2A4\uD2B8 \uBD88\uC77C\uCE58)");
@@ -30011,12 +30436,12 @@ function textUnitInline(raw, block) {
30011
30436
  return text;
30012
30437
  }
30013
30438
  function diffUnitLists(a, b) {
30014
- const pairs = alignUnits(a.map((u) => u.raw), b.map((u) => u.raw));
30439
+ const pairs = alignUnits(a.map((u) => looseRaw(u.raw)), b.map((u) => looseRaw(u.raw)));
30015
30440
  const stats = { added: 0, removed: 0, modified: 0, unchanged: 0 };
30016
30441
  const diffs = [];
30017
30442
  for (const [ai, bi] of pairs) {
30018
30443
  if (ai !== null && bi !== null) {
30019
- if (a[ai].raw === b[bi].raw) {
30444
+ if (looseRaw(a[ai].raw) === looseRaw(b[bi].raw)) {
30020
30445
  stats.unchanged++;
30021
30446
  continue;
30022
30447
  }
@@ -30032,6 +30457,9 @@ function diffUnitLists(a, b) {
30032
30457
  }
30033
30458
  return { stats, diffs };
30034
30459
  }
30460
+ function looseRaw(raw) {
30461
+ return raw.replace(/\\~/g, "~");
30462
+ }
30035
30463
  function unitToBlock(u) {
30036
30464
  return { type: "paragraph", text: u.raw };
30037
30465
  }
@@ -30597,7 +31025,7 @@ function scriptsOff(r, off) {
30597
31025
  }
30598
31026
  async function parseImage(buffer, options) {
30599
31027
  try {
30600
- const { parseImageDocument } = await import("./image-ocr-3R2YM67Q.js");
31028
+ const { parseImageDocument } = await import("./image-ocr-HH4WLRMA.js");
30601
31029
  const { blocks, warnings, lines } = await parseImageDocument(buffer, options);
30602
31030
  stripScriptTags({ blocks });
30603
31031
  return {
@@ -30661,7 +31089,7 @@ async function parseHwp(buffer, options) {
30661
31089
  async function parsePdf(buffer, options) {
30662
31090
  let parsePdfDocument;
30663
31091
  try {
30664
- const mod = await import("./parser-BN6SMWKJ.js");
31092
+ const mod = await import("./parser-J37C64DT.js");
30665
31093
  parsePdfDocument = mod.parsePdfDocument;
30666
31094
  } catch {
30667
31095
  return {
@@ -30884,4 +31312,4 @@ export {
30884
31312
  parseHwpml,
30885
31313
  fillForm
30886
31314
  };
30887
- //# sourceMappingURL=chunk-FJ5RGHZW.js.map
31315
+ //# sourceMappingURL=chunk-VTVPHZJ6.js.map