onionskin-cli 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +136 -0
- package/INSTALL.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE +46 -0
- package/README.md +88 -2
- package/docs/contracts/batch.schema.json +839 -0
- package/docs/contracts/history.schema.json +126 -0
- package/docs/contracts/ops.schema.json +541 -0
- package/package.json +54 -4
- package/scripts/checker/check.py +411 -0
- package/scripts/checker/requirements.txt +5 -0
- package/scripts/install-check.mjs +9 -0
- package/scripts/requirements.txt +5 -0
- package/scripts/rewriter/apple/main.swift +135 -0
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +6 -0
- package/skills/onionskin/SKILL.md +117 -0
- package/skills/onionskin/reference/batch.md +94 -0
- package/skills/onionskin/reference/formats.md +15 -0
- package/skills/onionskin/reference/ops.md +253 -0
- package/skills/onionskin/reference/troubleshooting.md +73 -0
- package/skills/onionskin/reference/voice-guide.md +86 -0
- package/skills/onionskin/voice.example.md +51 -0
- package/src/agent-detect.js +149 -0
- package/src/agent-events.js +159 -0
- package/src/agent-prompt.js +27 -0
- package/src/agent-runner.js +593 -0
- package/src/analyze.js +17 -0
- package/src/anchors.js +190 -0
- package/src/apply.js +387 -0
- package/src/auth.js +130 -0
- package/src/batch.js +322 -0
- package/src/check-worker.js +8 -0
- package/src/checker.js +293 -0
- package/src/cli.js +516 -0
- package/src/constants.js +20 -0
- package/src/diff.js +267 -0
- package/src/doctor.js +247 -0
- package/src/docx.js +438 -0
- package/src/fmt.js +30 -0
- package/src/fsutil.js +348 -0
- package/src/history-worker.js +16 -0
- package/src/history.js +576 -0
- package/src/lease.js +79 -0
- package/src/local.js +242 -0
- package/src/merge-pptx.js +198 -0
- package/src/merge-xlsx.js +453 -0
- package/src/merge.js +410 -0
- package/src/offload.js +34 -0
- package/src/ops/common.js +48 -0
- package/src/ops/docx-table.js +323 -0
- package/src/ops/docx.js +503 -0
- package/src/ops/engine.js +41 -0
- package/src/ops/pptx.js +336 -0
- package/src/ops/run.js +115 -0
- package/src/ops/runs.js +127 -0
- package/src/ops/schema.js +109 -0
- package/src/ops/xlsx.js +277 -0
- package/src/package.js +189 -0
- package/src/paths.js +64 -0
- package/src/person.js +66 -0
- package/src/pptx.js +194 -0
- package/src/question.js +47 -0
- package/src/quick-plan.js +107 -0
- package/src/quick.js +164 -0
- package/src/redline-check.js +251 -0
- package/src/redline-worker.js +29 -0
- package/src/redline.js +438 -0
- package/src/reply.js +410 -0
- package/src/review.js +274 -0
- package/src/rewrite-check.js +1014 -0
- package/src/rewriter-cleanup.js +105 -0
- package/src/rewriter-setup.js +104 -0
- package/src/rewriter.js +396 -0
- package/src/sanitize.js +42 -0
- package/src/sentence-span.js +25 -0
- package/src/server-entry.js +13 -0
- package/src/server.js +576 -0
- package/src/session.js +1910 -0
- package/src/settings.js +161 -0
- package/src/setup.js +160 -0
- package/src/stamp-words.js +10 -0
- package/src/stamps.js +59 -0
- package/src/style.js +110 -0
- package/src/suggest-reply.js +84 -0
- package/src/suggest.js +261 -0
- package/src/text.js +31 -0
- package/src/tracked-words.js +43 -0
- package/src/voice-guide.js +182 -0
- package/src/voice-learn.js +314 -0
- package/src/voice-sample-worker.js +95 -0
- package/src/voice-samples.js +329 -0
- package/src/voice-store.js +305 -0
- package/src/voice-worker.js +28 -0
- package/src/voice.js +297 -0
- package/src/xlsx.js +454 -0
- package/src/xml-tree.js +118 -0
- package/src/xml.js +127 -0
- package/src/zip-write.js +74 -0
- package/src/zip.js +88 -0
- package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
- package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
- package/vendor/engine/collab-BVynXMqc.js +2591 -0
- package/vendor/engine/collab.js +2 -0
- package/vendor/engine/core.js +4 -0
- package/vendor/engine/default-worker-DogNT09A.js +35625 -0
- package/vendor/engine/embed.js +107 -0
- package/vendor/engine/index.js +5 -0
- package/vendor/engine/office-engine.embed.js +184 -0
- package/vendor/engine/office-engine.worker.js +36030 -0
- package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
- package/vendor/engine-node/LICENSE +21 -0
- package/vendor/engine-node/engine-node.mjs +46618 -0
- package/vendor/engine.lock.json +26 -0
- package/web/dist/assets/index-CFISATkn.js +197 -0
- package/web/dist/assets/index-spXE4oTz.css +1 -0
- package/web/dist/index.html +14 -0
package/src/docx.js
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
import { engineNodePath } from "./ops/engine.js";
|
|
3
|
+
import { walkXml } from "./xml.js";
|
|
4
|
+
import { normText, similarity, textHash } from "./text.js";
|
|
5
|
+
import { openZip } from "./zip.js";
|
|
6
|
+
import { summarizeRuns } from "./fmt.js";
|
|
7
|
+
|
|
8
|
+
// One classifier for `protected`: the engine's own (default deny), bundled for Node. The batch, `apply` and the editor agree because they ask the same code.
|
|
9
|
+
const { protectReasons } = await import(pathToFileURL(engineNodePath()).href);
|
|
10
|
+
|
|
11
|
+
const OBJ = ""; // an inline object counts as one character, like the engine
|
|
12
|
+
|
|
13
|
+
const off = (v) => v === "0" || v === "false" || v === "off" || v === "none";
|
|
14
|
+
/** One direct run property (inside w:rPr) -> the normalized value History compares; "" = not set. */
|
|
15
|
+
function runProp(p, name, a) {
|
|
16
|
+
const v = a["w:val"];
|
|
17
|
+
if (name === "w:b" || name === "w:i" || name === "w:strike" || name === "w:dstrike") p[name === "w:b" ? "b" : name === "w:i" ? "i" : "strike"] = off(v) ? "" : "1";
|
|
18
|
+
else if (name === "w:u") p.u = off(v) ? "" : v || "single";
|
|
19
|
+
else if (name === "w:color") p.color = !v || v === "auto" ? "" : v.toUpperCase();
|
|
20
|
+
else if (name === "w:sz") p.size = v || "";
|
|
21
|
+
else if (name === "w:rFonts") p.font = a["w:ascii"] || a["w:hAnsi"] || "";
|
|
22
|
+
else if (name === "w:highlight") p.hl = off(v) ? "" : v || "";
|
|
23
|
+
else if (name === "w:rStyle") p.rs = v || "";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// ---- formatting as the merge reads it (src/merge.js), only collected with `detail` -------------------------------------------
|
|
27
|
+
// A run's direct properties in the values the engine's own formatting call takes, so a difference between two versions of a run is
|
|
28
|
+
// already the patch that turns one into the other. Whatever is not one of these (a character style, a theme color, shading, an
|
|
29
|
+
// attribute this file does not know) is kept as written in `rest`: it can be compared, never set.
|
|
30
|
+
const HIGHLIGHT = { yellow: "#ffff00", green: "#00ff00", cyan: "#00ffff", magenta: "#ff00ff", blue: "#0000ff", red: "#ff0000", darkBlue: "#000080", darkCyan: "#008080", darkGreen: "#008000", darkMagenta: "#800080", darkRed: "#800000", darkYellow: "#808000", darkGray: "#808080", lightGray: "#c0c0c0", black: "#000000", white: "#ffffff" };
|
|
31
|
+
const COMPANIONS = new Set(["w:bCs", "w:iCs", "w:szCs"]); // the engine writes these with b, i and sz
|
|
32
|
+
const only = (a, ...keys) => Object.keys(a).every((k) => keys.includes(k));
|
|
33
|
+
const written = (name, a) => `${name}{${Object.keys(a).sort().map((k) => `${k}=${a[k]}`).join(" ")}}`;
|
|
34
|
+
/** One direct child of a run's w:rPr -> [key, value] for the engine, or null when it has to be kept as written. */
|
|
35
|
+
function runSet(name, a) {
|
|
36
|
+
const v = a["w:val"];
|
|
37
|
+
if (name === "w:b" || name === "w:i" || name === "w:strike") return only(a, "w:val") ? [name.slice(2), !off(v)] : null;
|
|
38
|
+
if (name === "w:u") return only(a, "w:val") ? ["u", v === undefined ? "single" : off(v) ? "none" : v] : null;
|
|
39
|
+
if (name === "w:color") return only(a, "w:val") && (v === "auto" || /^[0-9a-fA-F]{6}$/.test(v || "")) ? ["color", v === "auto" ? "auto" : `#${v.toLowerCase()}`] : null;
|
|
40
|
+
if (name === "w:sz") return only(a, "w:val") && /^\d{1,4}$/.test(v || "") ? ["sz", Number(v) / 2] : null;
|
|
41
|
+
if (name === "w:rFonts") return a["w:ascii"] && only(a, "w:ascii", "w:hAnsi", "w:eastAsia", "w:cs") && Object.values(a).every((x) => x === a["w:ascii"]) ? ["font", a["w:ascii"]] : null;
|
|
42
|
+
if (name === "w:highlight") return only(a, "w:val") && HIGHLIGHT[v] ? ["hl", HIGHLIGHT[v]] : null;
|
|
43
|
+
if (name === "w:vertAlign") return only(a, "w:val") && (v === "superscript" || v === "subscript") ? ["va", v.slice(0, 3)] : null;
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const PPR_OWN = new Set(["w:pStyle", "w:jc", "w:numPr", "w:rPr", "w:sectPr"]); // read on their own, or not paragraph formatting the user sets
|
|
47
|
+
const RANGE = /^(.+)(Start|End)$/;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Walks word/document.xml and lists every paragraph in document order.
|
|
51
|
+
* `path` is the body ordinal path: [i] for a body-level paragraph (i counts body-level w:p / w:tbl, sdt content flattened)
|
|
52
|
+
* and [i,row,col,k] for a paragraph directly in a table cell. Nested tables are listed with path null.
|
|
53
|
+
* `text` keeps tabs and line breaks (so offsets line up with the engine); tracked deletions are excluded.
|
|
54
|
+
* With `detail` (the merge) a paragraph also carries: `spans` [{n, set, rest}], one per run that holds text, covering `text` in order
|
|
55
|
+
* (null when they do not add up to it); `list` {numId, ilvl} from its own w:numPr, else null; `ppr`, its other paragraph properties as
|
|
56
|
+
* written; `sect`, it ends a section; `loose`, a bookmark, comment or other range starts or ends inside it without its other end.
|
|
57
|
+
*/
|
|
58
|
+
export function parseDocxParagraphs(xml, { detail = false } = {}) {
|
|
59
|
+
const paragraphs = [];
|
|
60
|
+
const stack = []; // {name, ...}
|
|
61
|
+
let bodyIndex = -1;
|
|
62
|
+
let tbl = null; // current top-level table context
|
|
63
|
+
let tblDepth = 0;
|
|
64
|
+
let tableCount = 0; // 1-based order among top-level tables (the label users read), unlike body_path[0] which counts every body item
|
|
65
|
+
let cellK = 0;
|
|
66
|
+
let cur = null;
|
|
67
|
+
let delDepth = 0;
|
|
68
|
+
let inT = false;
|
|
69
|
+
let inInstr = false;
|
|
70
|
+
let inBody = false;
|
|
71
|
+
let run = null; // the open text run: {text, p: formatting} (History compares formatting; nothing else reads it)
|
|
72
|
+
let rprDepth = 0;
|
|
73
|
+
let pDepth = 0; // open w:p elements: 1 = the listed paragraph itself, more = a text box paragraph inside it
|
|
74
|
+
let pprTop = null; // detail: the open direct child of the paragraph's own w:pPr
|
|
75
|
+
let marks = null; // detail: range marks seen in the paragraph without their other end
|
|
76
|
+
const info = { tableIndex: -1, row: -1, col: -1 };
|
|
77
|
+
walkXml(xml, {
|
|
78
|
+
onOpen(name, attrs) {
|
|
79
|
+
stack.push(name);
|
|
80
|
+
if (name === "w:body") inBody = true;
|
|
81
|
+
if (!inBody) return;
|
|
82
|
+
if (name === "w:tbl") {
|
|
83
|
+
tblDepth++;
|
|
84
|
+
if (tblDepth === 1) {
|
|
85
|
+
bodyIndex++;
|
|
86
|
+
info.tableIndex = bodyIndex;
|
|
87
|
+
tableCount++;
|
|
88
|
+
info.row = -1;
|
|
89
|
+
info.col = -1;
|
|
90
|
+
}
|
|
91
|
+
} else if (name === "w:tr" && tblDepth === 1) {
|
|
92
|
+
info.row++;
|
|
93
|
+
info.col = -1;
|
|
94
|
+
} else if (name === "w:tc" && tblDepth === 1) {
|
|
95
|
+
info.col++;
|
|
96
|
+
cellK = 0;
|
|
97
|
+
} else if (name === "w:p") {
|
|
98
|
+
pDepth++;
|
|
99
|
+
const nested = stack.slice(0, -1).includes("w:p"); // a text box paragraph inside a paragraph
|
|
100
|
+
if (nested) return;
|
|
101
|
+
let path = null;
|
|
102
|
+
let tableNo = null;
|
|
103
|
+
if (tblDepth === 0) {
|
|
104
|
+
bodyIndex++;
|
|
105
|
+
path = [bodyIndex];
|
|
106
|
+
} else if (tblDepth === 1) {
|
|
107
|
+
path = [info.tableIndex, info.row, info.col, cellK++];
|
|
108
|
+
tableNo = tableCount;
|
|
109
|
+
}
|
|
110
|
+
cur = { ordinal: paragraphs.length, path, tableNo, paraId: /^[0-9A-Fa-f]{8}$/.test(attrs["w14:paraId"] || "") ? attrs["w14:paraId"].toUpperCase() : null, text: "", instr: "", protected: false, style: null, align: "", runs: [] };
|
|
111
|
+
if (detail) (Object.assign(cur, { spans: [], list: null, ppr: [], sect: false, loose: false }), (marks = new Set()), (pprTop = null));
|
|
112
|
+
paragraphs.push(cur);
|
|
113
|
+
} else if (cur) {
|
|
114
|
+
if (detail) {
|
|
115
|
+
const n = stack.length;
|
|
116
|
+
if (pDepth === 1 && stack[n - 2] === "w:pPr" && stack[n - 3] === "w:p") {
|
|
117
|
+
pprTop = name;
|
|
118
|
+
if (name === "w:sectPr") cur.sect = true;
|
|
119
|
+
}
|
|
120
|
+
if (pprTop && !PPR_OWN.has(pprTop)) cur.ppr.push(written(name, attrs));
|
|
121
|
+
else if (pprTop === "w:numPr" && pDepth === 1 && stack[n - 2] === "w:numPr" && (name === "w:numId" || name === "w:ilvl")) (cur.list ||= { numId: "", ilvl: 0 })[name === "w:numId" ? "numId" : "ilvl"] = name === "w:numId" ? attrs["w:val"] || "" : Number(attrs["w:val"]) || 0;
|
|
122
|
+
const mark = RANGE.exec(name);
|
|
123
|
+
if (mark && "w:id" in attrs) {
|
|
124
|
+
const key = `${mark[1]} ${attrs["w:id"]}`;
|
|
125
|
+
if (!marks.delete(key)) marks.add(key);
|
|
126
|
+
}
|
|
127
|
+
// the open run's own w:rPr: a direct child is one property; anything deeper belongs to a property kept as written
|
|
128
|
+
if (run && stack[run.depth] === "w:rPr" && n > run.depth + 1) {
|
|
129
|
+
const kv = n === run.depth + 2 ? runSet(name, attrs) : null;
|
|
130
|
+
if (kv) run.set[kv[0]] = kv[1];
|
|
131
|
+
else if (!COMPANIONS.has(name) || n !== run.depth + 2) run.rest.push(written(name, attrs));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (name === "w:pStyle" && cur.style === null) cur.style = attrs["w:val"] || null;
|
|
135
|
+
else if (name === "w:jc" && !cur.align && stack.includes("w:pPr")) cur.align = attrs["w:val"] || "";
|
|
136
|
+
else if (name === "w:r" && !run) run = { text: false, p: {}, at: cur.text.length, depth: stack.length, set: {}, rest: [] };
|
|
137
|
+
else if (name === "w:rPr") rprDepth++;
|
|
138
|
+
else if (run && rprDepth === 1 && stack.includes("w:rPr")) runProp(run.p, name, attrs);
|
|
139
|
+
if (name === "w:del") delDepth++;
|
|
140
|
+
else if (name === "w:t" && delDepth === 0) (inT = true, run && (run.text = true));
|
|
141
|
+
else if (name === "w:instrText") inInstr = true;
|
|
142
|
+
else if (name === "w:tab" && delDepth === 0) cur.text += "\t";
|
|
143
|
+
else if (name === "w:br" || name === "w:cr") {
|
|
144
|
+
if (delDepth === 0 && attrs["w:type"] !== "page" && attrs["w:type"] !== "column") cur.text += "\n";
|
|
145
|
+
} else if (name === "w:drawing" || name === "w:pict") {
|
|
146
|
+
if (delDepth === 0) cur.text += OBJ;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
onText(t) {
|
|
151
|
+
if (cur && inT) cur.text += t;
|
|
152
|
+
else if (cur && inInstr && cur.instr.length < 200) cur.instr += t;
|
|
153
|
+
},
|
|
154
|
+
onClose(name) {
|
|
155
|
+
stack.pop();
|
|
156
|
+
if (name === "w:t") inT = false;
|
|
157
|
+
else if (name === "w:instrText") inInstr = false;
|
|
158
|
+
else if (name === "w:del") delDepth = Math.max(0, delDepth - 1);
|
|
159
|
+
else if (name === "w:tbl") tblDepth--;
|
|
160
|
+
else if (name === "w:rPr") rprDepth = Math.max(0, rprDepth - 1);
|
|
161
|
+
else if (name === "w:r" && run) {
|
|
162
|
+
if (run.text && cur) cur.runs.push(run.p);
|
|
163
|
+
if (detail && cur && cur.text.length > run.at) cur.spans.push({ n: cur.text.length - run.at, set: run.set, rest: run.rest.join(" ") });
|
|
164
|
+
run = null;
|
|
165
|
+
} else if (name === "w:p") {
|
|
166
|
+
pDepth--;
|
|
167
|
+
if (stack.includes("w:p")) return;
|
|
168
|
+
if (cur) (cur.fmt = summarizeRuns(cur.runs), delete cur.runs);
|
|
169
|
+
if (cur && detail) {
|
|
170
|
+
if (cur.spans.reduce((n, x) => n + x.n, 0) !== cur.text.length) cur.spans = null;
|
|
171
|
+
cur.ppr = cur.ppr.join(" ");
|
|
172
|
+
cur.loose = marks.size > 0;
|
|
173
|
+
}
|
|
174
|
+
cur = null;
|
|
175
|
+
}
|
|
176
|
+
if (detail && name === pprTop && stack[stack.length - 1] === "w:pPr") pprTop = null;
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
for (const p of paragraphs) p.hash = textHash(p.text);
|
|
180
|
+
const reasons = protectReasons(xml);
|
|
181
|
+
// the engine lists paragraphs the same way; if the two ever disagree on the count, nothing can be trusted as editable
|
|
182
|
+
paragraphs.forEach((p, i) => {
|
|
183
|
+
const why = reasons.length === paragraphs.length ? reasons[i] : "unknown";
|
|
184
|
+
p.protected = !!why;
|
|
185
|
+
if (why) p.protectedReason = why;
|
|
186
|
+
});
|
|
187
|
+
return paragraphs;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const TOC_STYLE = /^toc\s?\d$/i;
|
|
191
|
+
/**
|
|
192
|
+
* Why a paragraph is protected, in short code + plain words for the batch (`field: table of contents`, `equation`, `tracked formatting`),
|
|
193
|
+
* and whether it is a generated table-of-contents entry. The engine's reason is the base; a field is named by its instruction keyword.
|
|
194
|
+
*/
|
|
195
|
+
export function protectInfo(p) {
|
|
196
|
+
if (!p.protected) return null;
|
|
197
|
+
const toc = /^\s*TOC\b/.test(p.instr || "") || TOC_STYLE.test(p.style || "");
|
|
198
|
+
let reason = String(p.protectedReason || "unknown").replace(/^(a|an)\s+/, "");
|
|
199
|
+
if (reason === "field") {
|
|
200
|
+
const kw = /^\s*([A-Za-z]{2,12})\b/.exec(p.instr || "")?.[1]?.toUpperCase();
|
|
201
|
+
reason = toc ? "field: table of contents" : kw ? `field: ${kw}` : "field";
|
|
202
|
+
} else if (toc) reason = `${reason} (table of contents entry)`;
|
|
203
|
+
return { reason, toc };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const HEADING = /^(heading\s?\d|title|subtitle)$/i;
|
|
207
|
+
/** The text of the nearest heading above a paragraph (by paragraph style), or "" when there is none. */
|
|
208
|
+
export function headingBefore(paragraphs, ordinal) {
|
|
209
|
+
for (let i = ordinal - 1; i >= 0; i--) if (paragraphs[i].style && HEADING.test(paragraphs[i].style) && paragraphs[i].text.trim()) return normText(paragraphs[i].text).slice(0, 80);
|
|
210
|
+
return "";
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** `detail` (the merge only): formatting per run and the paragraph's list, see parseDocxParagraphs; `list.kind` is "bullet", "number" or "other". */
|
|
214
|
+
export function readDocxParagraphs(bytes, { detail = false } = {}) {
|
|
215
|
+
const zip = openZip(bytes);
|
|
216
|
+
const xml = zip.readText("word/document.xml");
|
|
217
|
+
if (xml == null) throw new Error("word/document.xml missing");
|
|
218
|
+
const paragraphs = parseDocxParagraphs(xml, { detail });
|
|
219
|
+
const names = styleNames(zip.readText("word/styles.xml"));
|
|
220
|
+
if (names.size) for (const p of paragraphs) if (p.style && names.has(p.style)) p.styleName = names.get(p.style);
|
|
221
|
+
if (detail && paragraphs.some((p) => p.list)) {
|
|
222
|
+
const kinds = listKinds(zip.readText("word/numbering.xml"));
|
|
223
|
+
for (const p of paragraphs) if (p.list) p.list = p.list.numId && p.list.numId !== "0" ? { ...p.list, kind: kinds.get(p.list.numId) || "other" } : null; // numId 0 switches a style's list off
|
|
224
|
+
}
|
|
225
|
+
return paragraphs;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* w:numId -> "bullet" | "number" | "other", by the first level of its definition: the two kinds the editor's list buttons make and
|
|
230
|
+
* the engine can set again. A list with level overrides, or one this cannot read, is "other".
|
|
231
|
+
*/
|
|
232
|
+
function listKinds(xml) {
|
|
233
|
+
const out = new Map();
|
|
234
|
+
if (xml == null) return out;
|
|
235
|
+
const fmt = new Map(); // abstractNumId -> numFmt of level 0
|
|
236
|
+
const stack = [];
|
|
237
|
+
let abs = null;
|
|
238
|
+
let num = null;
|
|
239
|
+
let lvl = null;
|
|
240
|
+
try {
|
|
241
|
+
walkXml(xml, {
|
|
242
|
+
onOpen(name, a) {
|
|
243
|
+
stack.push(name);
|
|
244
|
+
const parent = stack[stack.length - 2];
|
|
245
|
+
if (name === "w:abstractNum") abs = a["w:abstractNumId"] ?? null;
|
|
246
|
+
else if (name === "w:lvl" && parent === "w:abstractNum") lvl = a["w:ilvl"];
|
|
247
|
+
else if (name === "w:numFmt" && parent === "w:lvl" && abs != null && lvl === "0" && !fmt.has(abs)) fmt.set(abs, a["w:val"] || "");
|
|
248
|
+
else if (name === "w:num") num = { id: a["w:numId"], abs: null, plain: true };
|
|
249
|
+
else if (num && name === "w:abstractNumId" && parent === "w:num") num.abs = a["w:val"];
|
|
250
|
+
else if (num && name === "w:lvlOverride") num.plain = false;
|
|
251
|
+
},
|
|
252
|
+
onClose(name) {
|
|
253
|
+
stack.pop();
|
|
254
|
+
if (name === "w:abstractNum") abs = null;
|
|
255
|
+
else if (name === "w:num" && num) {
|
|
256
|
+
const f = num.plain ? fmt.get(num.abs) : null;
|
|
257
|
+
if (num.id) out.set(num.id, f === "bullet" ? "bullet" : f === "decimal" ? "number" : "other");
|
|
258
|
+
num = null;
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
} catch {} // an unreadable numbering part: every list is "other", which is reported, never set
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** w:styleId -> the style's display name ("heading 2" is shown as "Heading 2"); empty when styles.xml is missing or unreadable. */
|
|
267
|
+
function styleNames(xml) {
|
|
268
|
+
const out = new Map();
|
|
269
|
+
if (xml == null) return out;
|
|
270
|
+
let id = null;
|
|
271
|
+
try {
|
|
272
|
+
walkXml(xml, {
|
|
273
|
+
onOpen(name, a) {
|
|
274
|
+
if (name === "w:style") id = a["w:styleId"] || null;
|
|
275
|
+
else if (name === "w:name" && id && !out.has(id) && a["w:val"]) out.set(id, a["w:val"].replace(/^./, (c) => c.toUpperCase()).slice(0, 60));
|
|
276
|
+
},
|
|
277
|
+
onClose(name) {
|
|
278
|
+
if (name === "w:style") id = null;
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
} catch {} // a damaged styles part only costs the readable style names
|
|
282
|
+
return out;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const CTX = 40;
|
|
286
|
+
const PARA_TEXT_KEEP = 2000;
|
|
287
|
+
|
|
288
|
+
// ---- engine location -> durable anchor ----------------------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
function locate(paragraphs, { path, paraId, quote, prefix, text, prevHash, nextHash, dupCount, allowPath = false }) {
|
|
291
|
+
const q = normText(quote || "");
|
|
292
|
+
const want = path ? path[0] : null;
|
|
293
|
+
const dist = (p) => (want == null || !p.path ? 1e6 : Math.abs(p.path[0] - want));
|
|
294
|
+
const nearest = (list) => list.slice().sort((a, b) => dist(a) - dist(b) || a.ordinal - b.ordinal)[0];
|
|
295
|
+
const nb = (p) => (prevHash && paragraphs[p.ordinal - 1]?.hash === prevHash ? 1 : 0) + (nextHash && paragraphs[p.ordinal + 1]?.hash === nextHash ? 1 : 0);
|
|
296
|
+
// 1. w14:paraId
|
|
297
|
+
if (paraId) {
|
|
298
|
+
const hit = paragraphs.filter((p) => p.paraId === paraId.toUpperCase());
|
|
299
|
+
if (hit.length === 1) return { p: hit[0], method: "id" };
|
|
300
|
+
}
|
|
301
|
+
// 2. identical text; among duplicates prefer the one whose neighbors are unchanged, then the nearest.
|
|
302
|
+
// Text that was not unique when the anchor was captured ("TBD") is only trusted when a hit has an agreeing neighbor: otherwise
|
|
303
|
+
// the comment would jump to another paragraph with the same words. Anchors from before dupCount was stored count short text as ambiguous.
|
|
304
|
+
let ambiguousHash = null;
|
|
305
|
+
if (text) {
|
|
306
|
+
const h = textHash(text);
|
|
307
|
+
const hits = paragraphs.filter((p) => p.hash === h);
|
|
308
|
+
const ambiguous = (dupCount != null ? dupCount > 1 : normText(text).length < 20) && (prevHash || nextHash);
|
|
309
|
+
if (hits.length && (!ambiguous || hits.some((p) => nb(p) > 0))) return { p: nearest(hits.filter((p) => nb(p) === Math.max(...hits.map(nb)))), method: "hash" };
|
|
310
|
+
if (hits.length) ambiguousHash = h;
|
|
311
|
+
}
|
|
312
|
+
// 3. same slot: both neighbors unchanged and exactly one paragraph sits between them (edited in place)
|
|
313
|
+
if (prevHash && nextHash) {
|
|
314
|
+
const slots = paragraphs.filter((p) => nb(p) === 2);
|
|
315
|
+
if (slots.length === 1 && (!text || similarity(slots[0].text, text) >= 0.3 || (q && normText(slots[0].text).includes(q)))) return { p: slots[0], method: "neighbors" };
|
|
316
|
+
}
|
|
317
|
+
// 4. exact quote. Ambiguous when the quote also occurs elsewhere and the prefix does not disambiguate: do not guess here.
|
|
318
|
+
if (q) {
|
|
319
|
+
const hits = paragraphs.filter((p) => normText(p.text).includes(q) && !(ambiguousHash && p.hash === ambiguousHash)); // look-alikes rejected in step 2 stay rejected
|
|
320
|
+
if (hits.length) {
|
|
321
|
+
const pre = normText(prefix || "");
|
|
322
|
+
const better = pre ? hits.filter((p) => normText(p.text).includes(pre + (pre.endsWith(" ") ? "" : " ") + q) || normText(p.text).includes(pre + q)) : [];
|
|
323
|
+
if (better.length) return { p: nearest(better), method: "quote" };
|
|
324
|
+
if (hits.length === 1 || (!pre && !text)) return { p: nearest(hits), method: "quote" };
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
// 5. fuzzy: similarity to the remembered text (or the quote), neighbors count in favor
|
|
328
|
+
const probe = text || q;
|
|
329
|
+
if (probe) {
|
|
330
|
+
// The best candidate must clearly beat the runner-up: in templated text a deleted paragraph looks like its siblings,
|
|
331
|
+
// and "moved or removed" is a better answer than attaching the comment to the wrong one.
|
|
332
|
+
const scored = [];
|
|
333
|
+
for (const p of paragraphs) {
|
|
334
|
+
if (ambiguousHash && p.hash === ambiguousHash) continue; // rejected above: a look-alike elsewhere is not this paragraph
|
|
335
|
+
const sim = text ? similarity(p.text, text) : normText(p.text).length ? similarity(normText(p.text).slice(0, q.length + 40), q) : 0;
|
|
336
|
+
if (sim >= 0.6) scored.push({ p, sim, score: sim + nb(p) * 0.1 - dist(p) * 1e-6 }); // distance only breaks ties
|
|
337
|
+
}
|
|
338
|
+
scored.sort((a, b) => b.score - a.score);
|
|
339
|
+
if (scored.length && (scored.length === 1 || scored[0].score - scored[1].score >= 0.04)) return { p: scored[0].p, method: "fuzzy", score: Math.round(scored[0].sim * 100) / 100 };
|
|
340
|
+
}
|
|
341
|
+
// At Send time the browser's own index is as fresh as the bytes, so the bare position is acceptable.
|
|
342
|
+
// After a reload it never is (an insert above shifts it): resolveDurable does not pass allowPath.
|
|
343
|
+
if (allowPath && path) {
|
|
344
|
+
const hit = paragraphs.find((p) => p.path && p.path.length === path.length && p.path.every((v, i) => v === path[i]));
|
|
345
|
+
if (hit) return { p: hit, method: "path" };
|
|
346
|
+
}
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Browser engine location (as sent at Send) -> durable anchor, resolved against the WRITTEN bytes.
|
|
352
|
+
* `loc` = {blockPath, paraId, start, end}; `quote/prefix/suffix` from the engine selection/hitTest.
|
|
353
|
+
* Returns null when the paragraph cannot be found.
|
|
354
|
+
*/
|
|
355
|
+
export function durableFromEngine(paragraphs, loc, ctx = {}) {
|
|
356
|
+
const hit = locate(paragraphs, { path: loc.blockPath, paraId: loc.paraId, quote: ctx.quote, prefix: ctx.prefix, text: ctx.paraText, allowPath: true });
|
|
357
|
+
if (!hit) return null;
|
|
358
|
+
return durableFor(paragraphs, hit.p, loc.blockPath ? loc.blockPath[0] : null, ctx, loc);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** Offsets of every non-overlapping occurrence of `q` in `text`. */
|
|
362
|
+
function occurrences(text, q) {
|
|
363
|
+
const out = [];
|
|
364
|
+
for (let i = text.indexOf(q); i >= 0; i = text.indexOf(q, i + q.length)) out.push(i);
|
|
365
|
+
return out;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Which occurrence of a repeated quote a selection or a stored anchor means. Prefix and suffix are the normalized text around it;
|
|
370
|
+
* `nth` is the occurrence index remembered at capture. Returns the offset, or -1 when the text does not occur.
|
|
371
|
+
*/
|
|
372
|
+
function pickOccurrence(text, q, { prefix = "", suffix = "", nth = null } = {}) {
|
|
373
|
+
const occ = occurrences(text, q);
|
|
374
|
+
if (!occ.length) return -1;
|
|
375
|
+
const guarded = occ.filter((at) => (!prefix || normText(text.slice(0, at)).endsWith(prefix)) && (!suffix || normText(text.slice(at + q.length)).startsWith(suffix)));
|
|
376
|
+
if (guarded.length === 1) return guarded[0];
|
|
377
|
+
if (nth != null && nth < occ.length && guarded.includes(occ[nth])) return occ[nth];
|
|
378
|
+
if (guarded.length) return guarded[0];
|
|
379
|
+
return nth != null && nth < occ.length ? occ[nth] : occ[0];
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function durableFor(paragraphs, p, engineTop, ctx, loc = {}) {
|
|
383
|
+
const norm = normText(p.text);
|
|
384
|
+
const q = normText(ctx.quote || "");
|
|
385
|
+
const prefix = normText(ctx.prefix || "").slice(-CTX);
|
|
386
|
+
const suffix = normText(ctx.suffix || "").slice(0, CTX);
|
|
387
|
+
let start = null;
|
|
388
|
+
let end = null;
|
|
389
|
+
let nth = null;
|
|
390
|
+
if (q) {
|
|
391
|
+
// the engine's own offsets when they point at the quote; else the occurrence whose surroundings match prefix and suffix
|
|
392
|
+
const at = Number.isInteger(loc.start) && Number.isInteger(loc.end) && p.text.slice(loc.start, loc.end) === ctx.quote ? loc.start : pickOccurrence(p.text, ctx.quote, { prefix, suffix });
|
|
393
|
+
if (at >= 0) {
|
|
394
|
+
start = at;
|
|
395
|
+
end = at + ctx.quote.length;
|
|
396
|
+
nth = occurrences(p.text, ctx.quote).indexOf(at);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
format: "docx",
|
|
401
|
+
paraId: p.paraId,
|
|
402
|
+
path: p.path,
|
|
403
|
+
tableNo: p.tableNo ?? null,
|
|
404
|
+
ordinal: p.ordinal,
|
|
405
|
+
protected: !!p.protected,
|
|
406
|
+
...(p.protected ? { protectInfo: protectInfo(p) } : {}),
|
|
407
|
+
textHash: p.hash,
|
|
408
|
+
prevHash: paragraphs[p.ordinal - 1]?.hash || null,
|
|
409
|
+
nextHash: paragraphs[p.ordinal + 1]?.hash || null,
|
|
410
|
+
dupCount: paragraphs.filter((x) => x.hash === p.hash).length,
|
|
411
|
+
paraText: norm.slice(0, PARA_TEXT_KEEP),
|
|
412
|
+
quote: q,
|
|
413
|
+
prefix,
|
|
414
|
+
suffix,
|
|
415
|
+
start,
|
|
416
|
+
end,
|
|
417
|
+
nth,
|
|
418
|
+
// difference between the engine's Block[] index and our body ordinal at capture time (section breaks etc.)
|
|
419
|
+
engineDelta: engineTop != null && p.path ? engineTop - p.path[0] : 0,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Re-resolve a durable anchor against new bytes: id, then hash, then exact quote, then fuzzy. */
|
|
424
|
+
export function resolveDurable(paragraphs, d) {
|
|
425
|
+
if (!d) return { method: "none" };
|
|
426
|
+
const hit = locate(paragraphs, { path: d.path, paraId: d.paraId, quote: d.quote, prefix: d.prefix, text: d.paraText, prevHash: d.prevHash, nextHash: d.nextHash, dupCount: d.dupCount });
|
|
427
|
+
if (!hit) return { method: "none" };
|
|
428
|
+
const p = hit.p;
|
|
429
|
+
const q = d.quote || "";
|
|
430
|
+
let start = null;
|
|
431
|
+
let end = null;
|
|
432
|
+
const at = q ? pickOccurrence(p.text, q, { prefix: d.prefix, suffix: d.suffix, nth: d.nth }) : -1;
|
|
433
|
+
if (at >= 0) {
|
|
434
|
+
start = at;
|
|
435
|
+
end = at + q.length;
|
|
436
|
+
}
|
|
437
|
+
return { method: hit.method, score: hit.score, ordinal: p.ordinal, path: p.path, tableNo: p.tableNo ?? null, paraId: p.paraId, textHash: p.hash, protected: !!p.protected, ...(p.protected ? { protectInfo: protectInfo(p) } : {}), dupCount: paragraphs.filter((x) => x.hash === p.hash).length, nth: at >= 0 ? occurrences(p.text, q).indexOf(at) : null, prevHash: paragraphs[p.ordinal - 1]?.hash || null, nextHash: paragraphs[p.ordinal + 1]?.hash || null, text: normText(p.text).slice(0, PARA_TEXT_KEEP), start, end, engineDelta: d.engineDelta || 0 };
|
|
438
|
+
}
|
package/src/fmt.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Formatting signatures for History. A paragraph (DOCX) or shape (PPTX) is reduced to one value per property over its text runs, so how a
|
|
2
|
+
// writer splits a paragraph into runs does not matter: "all runs bold" is the same signature however many runs there are. Two signatures
|
|
3
|
+
// are compared by property and reported in plain words. Pure functions; nothing here reads or writes a document.
|
|
4
|
+
|
|
5
|
+
/** run property key -> the word used in a note */
|
|
6
|
+
export const FMT_WORDS = { b: "bold", i: "italic", u: "underline", strike: "strikethrough", color: "color", size: "size", font: "font", hl: "highlight", rs: "character style" };
|
|
7
|
+
const NOTE_CAP = 4; // properties named in one note; the rest is "and more"
|
|
8
|
+
|
|
9
|
+
/** [{b:"1", color:"FF0000", ...}] (one object per text run, "" or missing = not set) -> {b: "1" | "" | "~1,", ...}; a mixed property is "~" plus its sorted values */
|
|
10
|
+
export function summarizeRuns(runs) {
|
|
11
|
+
const seen = {};
|
|
12
|
+
for (const run of runs) for (const k of Object.keys(FMT_WORDS)) (seen[k] ||= new Set()).add(run[k] || "");
|
|
13
|
+
if (!runs.length) return {};
|
|
14
|
+
const out = {};
|
|
15
|
+
for (const [k, set] of Object.entries(seen)) {
|
|
16
|
+
const v = [...set].sort();
|
|
17
|
+
if (v.length === 1 ? v[0] : true) out[k] = v.length === 1 ? v[0] : `~${v.join(",")}`;
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Plain words for what differs between two signatures: "bold, color", "style: Heading 2 → Heading 1, alignment"; null when nothing differs. `a` and `b` are {props, style, styleName, align}. */
|
|
23
|
+
export function fmtNote(a, b) {
|
|
24
|
+
const notes = [];
|
|
25
|
+
if ((a.style || "") !== (b.style || "")) notes.push(`style: ${a.styleName || a.style || "Normal"} → ${b.styleName || b.style || "Normal"}`);
|
|
26
|
+
if ((a.align || "") !== (b.align || "")) notes.push("alignment");
|
|
27
|
+
for (const [k, word] of Object.entries(FMT_WORDS)) if ((a.props?.[k] || "") !== (b.props?.[k] || "")) notes.push(word);
|
|
28
|
+
if (!notes.length) return null;
|
|
29
|
+
return notes.slice(0, NOTE_CAP).join(", ") + (notes.length > NOTE_CAP ? ", and more" : "");
|
|
30
|
+
}
|