docrev 0.11.1 → 0.11.3

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 (144) hide show
  1. package/.gitattributes +1 -1
  2. package/CHANGELOG.md +207 -197
  3. package/PLAN-tables-and-postprocess.md +850 -850
  4. package/README.md +433 -433
  5. package/bin/rev.js +11 -11
  6. package/bin/rev.ts +145 -145
  7. package/completions/rev.bash +127 -127
  8. package/completions/rev.ps1 +210 -210
  9. package/completions/rev.zsh +207 -207
  10. package/dist/lib/annotations.d.ts.map +1 -1
  11. package/dist/lib/annotations.js +28 -0
  12. package/dist/lib/annotations.js.map +1 -1
  13. package/dist/lib/build.d.ts +28 -0
  14. package/dist/lib/build.d.ts.map +1 -1
  15. package/dist/lib/build.js +81 -4
  16. package/dist/lib/build.js.map +1 -1
  17. package/dist/lib/commands/comments.d.ts.map +1 -1
  18. package/dist/lib/commands/comments.js +58 -28
  19. package/dist/lib/commands/comments.js.map +1 -1
  20. package/dist/lib/commands/context.d.ts +1 -0
  21. package/dist/lib/commands/context.d.ts.map +1 -1
  22. package/dist/lib/commands/context.js +1 -0
  23. package/dist/lib/commands/context.js.map +1 -1
  24. package/dist/lib/commands/core.d.ts.map +1 -1
  25. package/dist/lib/commands/core.js +25 -8
  26. package/dist/lib/commands/core.js.map +1 -1
  27. package/dist/lib/commands/response.js +1 -1
  28. package/dist/lib/commands/response.js.map +1 -1
  29. package/dist/lib/commands/utilities.js +164 -164
  30. package/dist/lib/commands/word-tools.js +8 -8
  31. package/dist/lib/grammar.js +3 -3
  32. package/dist/lib/import.d.ts +14 -0
  33. package/dist/lib/import.d.ts.map +1 -1
  34. package/dist/lib/import.js +23 -0
  35. package/dist/lib/import.js.map +1 -1
  36. package/dist/lib/input.d.ts +67 -0
  37. package/dist/lib/input.d.ts.map +1 -0
  38. package/dist/lib/input.js +164 -0
  39. package/dist/lib/input.js.map +1 -0
  40. package/dist/lib/macro-filter.lua +201 -201
  41. package/dist/lib/pdf-comments.js +44 -44
  42. package/dist/lib/plugins.js +57 -57
  43. package/dist/lib/pptx-color-filter.lua +37 -37
  44. package/dist/lib/pptx-themes.js +115 -115
  45. package/dist/lib/response.d.ts +1 -1
  46. package/dist/lib/response.d.ts.map +1 -1
  47. package/dist/lib/response.js +5 -2
  48. package/dist/lib/response.js.map +1 -1
  49. package/dist/lib/schema.d.ts.map +1 -1
  50. package/dist/lib/schema.js +8 -2
  51. package/dist/lib/schema.js.map +1 -1
  52. package/dist/lib/spelling.js +2 -2
  53. package/dist/lib/templates.js +387 -387
  54. package/dist/lib/themes.js +51 -51
  55. package/eslint.config.js +27 -27
  56. package/lib/anchor-match.ts +307 -307
  57. package/lib/annotations.ts +695 -664
  58. package/lib/build.ts +2047 -1956
  59. package/lib/citations.ts +160 -160
  60. package/lib/commands/build.ts +885 -885
  61. package/lib/commands/citations.ts +515 -515
  62. package/lib/commands/comments.ts +1077 -1050
  63. package/lib/commands/context.ts +185 -176
  64. package/lib/commands/core.ts +328 -309
  65. package/lib/commands/doi.ts +451 -451
  66. package/lib/commands/file-ops.ts +372 -372
  67. package/lib/commands/history.ts +320 -320
  68. package/lib/commands/index.ts +87 -87
  69. package/lib/commands/init.ts +259 -259
  70. package/lib/commands/merge-resolve.ts +378 -378
  71. package/lib/commands/preview.ts +178 -178
  72. package/lib/commands/project-info.ts +244 -244
  73. package/lib/commands/quality.ts +517 -517
  74. package/lib/commands/response.ts +454 -454
  75. package/lib/commands/section-boundaries.ts +82 -82
  76. package/lib/commands/sections.ts +451 -451
  77. package/lib/commands/sync.ts +689 -689
  78. package/lib/commands/text-ops.ts +449 -449
  79. package/lib/commands/utilities.ts +448 -448
  80. package/lib/commands/verify-anchors.ts +272 -272
  81. package/lib/commands/word-tools.ts +340 -340
  82. package/lib/comment-realign.ts +99 -99
  83. package/lib/config.ts +84 -84
  84. package/lib/crossref.ts +781 -781
  85. package/lib/csl.ts +191 -191
  86. package/lib/dependencies.ts +132 -132
  87. package/lib/diff-engine.ts +465 -465
  88. package/lib/doi-cache.ts +115 -115
  89. package/lib/doi.ts +879 -879
  90. package/lib/equations.ts +506 -506
  91. package/lib/errors.ts +350 -350
  92. package/lib/format.ts +541 -541
  93. package/lib/git.ts +326 -326
  94. package/lib/grammar.ts +303 -303
  95. package/lib/image-registry.ts +180 -180
  96. package/lib/import.ts +932 -906
  97. package/lib/input.ts +174 -0
  98. package/lib/journals.ts +543 -543
  99. package/lib/macro-filter.lua +201 -201
  100. package/lib/macros.ts +273 -273
  101. package/lib/merge.ts +633 -633
  102. package/lib/ooxml.ts +768 -768
  103. package/lib/orcid.ts +144 -144
  104. package/lib/pdf-comments.ts +263 -263
  105. package/lib/pdf-import.ts +524 -524
  106. package/lib/plugins.ts +362 -362
  107. package/lib/postprocess.ts +188 -188
  108. package/lib/pptx-color-filter.lua +37 -37
  109. package/lib/pptx-template.ts +469 -469
  110. package/lib/pptx-themes.ts +483 -483
  111. package/lib/protect-restore.ts +520 -520
  112. package/lib/rate-limiter.ts +127 -127
  113. package/lib/response.ts +200 -197
  114. package/lib/restore-references.ts +240 -240
  115. package/lib/review.ts +327 -327
  116. package/lib/schema.ts +494 -488
  117. package/lib/scientific-words.ts +73 -73
  118. package/lib/sections.ts +428 -428
  119. package/lib/slides.ts +756 -756
  120. package/lib/spelling.ts +334 -334
  121. package/lib/templates.ts +526 -526
  122. package/lib/themes.ts +742 -742
  123. package/lib/trackchanges.ts +166 -166
  124. package/lib/tui.ts +450 -450
  125. package/lib/types.ts +546 -546
  126. package/lib/undo.ts +250 -250
  127. package/lib/utils.ts +69 -69
  128. package/lib/variables.ts +179 -179
  129. package/lib/word-extraction.ts +525 -525
  130. package/lib/word.ts +526 -526
  131. package/lib/wordcomments.ts +789 -789
  132. package/package.json +1 -1
  133. package/scripts/postbuild.js +47 -47
  134. package/skill/REFERENCE.md +550 -550
  135. package/skill/SKILL.md +302 -302
  136. package/tsconfig.json +26 -26
  137. package/types/index.d.ts +531 -531
  138. package/issues.md +0 -180
  139. package/site/assets/extra.css +0 -208
  140. package/site/commands.html +0 -926
  141. package/site/configuration.html +0 -469
  142. package/site/index.html +0 -288
  143. package/site/troubleshooting.html +0 -461
  144. package/site/workflow.html +0 -518
package/lib/ooxml.ts CHANGED
@@ -1,768 +1,768 @@
1
- /**
2
- * Parser-backed OOXML layer.
3
- *
4
- * A `.docx` is a zip of XML parts. Reading those parts with regexes breaks on
5
- * the variability real Word emits: marker elements carry attributes beyond
6
- * `w:id`, anchor text spans many runs, the WordprocessingML namespace can be
7
- * bound to a prefix other than `w`, runs are separated by `<w:tab/>`/`<w:br/>`,
8
- * and prose lives in parts other than `document.xml` (footnotes, endnotes,
9
- * headers, footers).
10
- *
11
- * This module tokenizes the XML structurally and walks it once into an ordered
12
- * flow of paragraphs, runs, text, track-change spans, and comment-range
13
- * markers. Every reader (text extraction, comment anchors, headings) and the
14
- * comment injector derive from that single walk — there is no second engine to
15
- * drift from.
16
- */
17
-
18
- import AdmZip from 'adm-zip';
19
-
20
- export const WML_NS = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
21
-
22
- // =============================================================================
23
- // Package access
24
- // =============================================================================
25
-
26
- /** Open a `.docx` package. Throws a clear error if it is not a valid zip. */
27
- export function openDocx(docxPath: string): AdmZip {
28
- try {
29
- return new AdmZip(docxPath);
30
- } catch (err: any) {
31
- throw new Error(`Invalid Word document (not a valid .docx file): ${err.message}`);
32
- }
33
- }
34
-
35
- /** Read a single part as UTF-8 text, or `null` when the part is absent. */
36
- export function readPartText(zip: AdmZip, partName: string): string | null {
37
- const entry = zip.getEntry(partName);
38
- if (!entry) return null;
39
- return entry.getData().toString('utf8');
40
- }
41
-
42
- /**
43
- * The WordprocessingML parts that carry running prose, in reading order:
44
- * the main story first, then footnotes, endnotes, headers, and footers.
45
- * A reader that opens only `document.xml` loses everything else.
46
- */
47
- export function listProseParts(zip: AdmZip): string[] {
48
- const parts: string[] = [];
49
- if (zip.getEntry('word/document.xml')) parts.push('word/document.xml');
50
- for (const fixed of ['word/footnotes.xml', 'word/endnotes.xml']) {
51
- if (zip.getEntry(fixed)) parts.push(fixed);
52
- }
53
- const headerFooter = zip
54
- .getEntries()
55
- .map((e) => e.entryName)
56
- .filter((name) => /^word\/(header|footer)\d*\.xml$/.test(name))
57
- .sort();
58
- parts.push(...headerFooter);
59
- return parts;
60
- }
61
-
62
- // =============================================================================
63
- // Entity coding
64
- // =============================================================================
65
-
66
- /** Decode the XML entities that appear in `.docx` text and attribute values. */
67
- export function decodeXmlEntities(text: string): string {
68
- return text
69
- .replace(/&lt;/g, '<')
70
- .replace(/&gt;/g, '>')
71
- .replace(/&quot;/g, '"')
72
- .replace(/&apos;/g, "'")
73
- .replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(parseInt(code, 10)))
74
- .replace(/&#x([0-9a-fA-F]+);/g, (_, code) => String.fromCodePoint(parseInt(code, 16)))
75
- .replace(/&amp;/g, '&'); // ampersand last so decoded text is not re-decoded
76
- }
77
-
78
- /** Encode text for placement inside an XML element (`<w:t>`). */
79
- export function encodeXmlText(text: string): string {
80
- return text
81
- .replace(/&/g, '&amp;')
82
- .replace(/</g, '&lt;')
83
- .replace(/>/g, '&gt;');
84
- }
85
-
86
- /** Encode text for placement inside a double-quoted XML attribute value. */
87
- export function encodeXmlAttr(text: string): string {
88
- return encodeXmlText(text).replace(/"/g, '&quot;');
89
- }
90
-
91
- // =============================================================================
92
- // Tokenizer
93
- // =============================================================================
94
-
95
- export interface XmlAttr {
96
- /** Qualified name as written, e.g. `w:id`. */
97
- name: string;
98
- prefix: string;
99
- local: string;
100
- /** Entity-decoded value. */
101
- value: string;
102
- }
103
-
104
- export type XmlTokenKind =
105
- | 'open'
106
- | 'close'
107
- | 'selfclose'
108
- | 'text'
109
- | 'comment'
110
- | 'cdata'
111
- | 'decl'
112
- | 'pi';
113
-
114
- export interface XmlToken {
115
- kind: XmlTokenKind;
116
- /** Byte offset of `<` (or text start) in the source. */
117
- start: number;
118
- /** Byte offset just past `>` (or text end) in the source. */
119
- end: number;
120
- /** Exact source slice [start, end). */
121
- raw: string;
122
- name?: string;
123
- prefix?: string;
124
- local?: string;
125
- attrs?: XmlAttr[];
126
- /** Entity-decoded text, for `text` and `cdata` tokens. */
127
- text?: string;
128
- }
129
-
130
- function splitQName(name: string): { prefix: string; local: string } {
131
- const colon = name.indexOf(':');
132
- return colon === -1
133
- ? { prefix: '', local: name }
134
- : { prefix: name.slice(0, colon), local: name.slice(colon + 1) };
135
- }
136
-
137
- const WS = /\s/;
138
-
139
- function parseTag(raw: string, start: number, end: number): XmlToken {
140
- const isClose = raw.startsWith('</');
141
- const selfClose = !isClose && raw.endsWith('/>');
142
- const body = raw.slice(isClose ? 2 : 1, selfClose ? raw.length - 2 : raw.length - 1);
143
-
144
- let p = 0;
145
- while (p < body.length && !WS.test(body[p]!) && body[p] !== '/') p++;
146
- const name = body.slice(0, p);
147
- const { prefix, local } = splitQName(name);
148
-
149
- const attrs: XmlAttr[] = [];
150
- while (p < body.length) {
151
- while (p < body.length && WS.test(body[p]!)) p++;
152
- if (p >= body.length) break;
153
- const nameStart = p;
154
- while (p < body.length && body[p] !== '=' && !WS.test(body[p]!)) p++;
155
- const attrName = body.slice(nameStart, p);
156
- while (p < body.length && WS.test(body[p]!)) p++;
157
- if (body[p] !== '=') {
158
- // Valueless attribute (not used by WML, but keep the lexer honest).
159
- if (attrName) {
160
- const q = splitQName(attrName);
161
- attrs.push({ name: attrName, prefix: q.prefix, local: q.local, value: '' });
162
- }
163
- continue;
164
- }
165
- p++; // skip '='
166
- while (p < body.length && WS.test(body[p]!)) p++;
167
- const quote = body[p];
168
- let value = '';
169
- if (quote === '"' || quote === "'") {
170
- p++;
171
- const valueStart = p;
172
- while (p < body.length && body[p] !== quote) p++;
173
- value = body.slice(valueStart, p);
174
- p++; // skip closing quote
175
- } else {
176
- const valueStart = p;
177
- while (p < body.length && !WS.test(body[p]!)) p++;
178
- value = body.slice(valueStart, p);
179
- }
180
- const q = splitQName(attrName);
181
- attrs.push({ name: attrName, prefix: q.prefix, local: q.local, value: decodeXmlEntities(value) });
182
- }
183
-
184
- return {
185
- kind: isClose ? 'close' : selfClose ? 'selfclose' : 'open',
186
- start,
187
- end,
188
- raw,
189
- name,
190
- prefix,
191
- local,
192
- attrs,
193
- };
194
- }
195
-
196
- /**
197
- * Tokenize an XML string into an ordered, offset-tagged token stream.
198
- * Quote-aware so a raw `>` inside an attribute value does not end a tag early.
199
- */
200
- export function tokenizeXml(xml: string): XmlToken[] {
201
- const tokens: XmlToken[] = [];
202
- const n = xml.length;
203
- let i = 0;
204
-
205
- while (i < n) {
206
- const lt = xml.indexOf('<', i);
207
- if (lt === -1) {
208
- tokens.push({ kind: 'text', start: i, end: n, raw: xml.slice(i, n), text: decodeXmlEntities(xml.slice(i, n)) });
209
- break;
210
- }
211
- if (lt > i) {
212
- tokens.push({ kind: 'text', start: i, end: lt, raw: xml.slice(i, lt), text: decodeXmlEntities(xml.slice(i, lt)) });
213
- }
214
-
215
- if (xml.startsWith('<!--', lt)) {
216
- const close = xml.indexOf('-->', lt + 4);
217
- const end = close === -1 ? n : close + 3;
218
- tokens.push({ kind: 'comment', start: lt, end, raw: xml.slice(lt, end) });
219
- i = end;
220
- continue;
221
- }
222
- if (xml.startsWith('<![CDATA[', lt)) {
223
- const close = xml.indexOf(']]>', lt + 9);
224
- const end = close === -1 ? n : close + 3;
225
- tokens.push({
226
- kind: 'cdata',
227
- start: lt,
228
- end,
229
- raw: xml.slice(lt, end),
230
- text: xml.slice(lt + 9, close === -1 ? n : close),
231
- });
232
- i = end;
233
- continue;
234
- }
235
- if (xml.startsWith('<?', lt)) {
236
- const close = xml.indexOf('?>', lt + 2);
237
- const end = close === -1 ? n : close + 2;
238
- tokens.push({ kind: xml.startsWith('<?xml', lt) ? 'decl' : 'pi', start: lt, end, raw: xml.slice(lt, end) });
239
- i = end;
240
- continue;
241
- }
242
-
243
- // Element tag: find the closing '>' that is not inside a quoted value.
244
- let j = lt + 1;
245
- let quote = '';
246
- while (j < n) {
247
- const ch = xml[j];
248
- if (quote) {
249
- if (ch === quote) quote = '';
250
- } else if (ch === '"' || ch === "'") {
251
- quote = ch;
252
- } else if (ch === '>') {
253
- break;
254
- }
255
- j++;
256
- }
257
- const end = j < n ? j + 1 : n;
258
- tokens.push(parseTag(xml.slice(lt, end), lt, end));
259
- i = end;
260
- }
261
-
262
- return tokens;
263
- }
264
-
265
- // =============================================================================
266
- // Namespace resolution
267
- // =============================================================================
268
-
269
- export interface NsContext {
270
- uriForPrefix(prefix: string): string | undefined;
271
- /** True when an element token lives in the WordprocessingML namespace. */
272
- isWml(token: XmlToken): boolean;
273
- /** Value of the WML-namespaced attribute with the given local name, if present. */
274
- wmlAttr(token: XmlToken, local: string): string | undefined;
275
- }
276
-
277
- /**
278
- * Build a namespace context from a token stream. Declarations are read from
279
- * every element (the root carries them in practice), so the WML namespace is
280
- * matched by URI rather than by assuming the `w` prefix.
281
- */
282
- export function resolveNamespaces(tokens: XmlToken[]): NsContext {
283
- const prefixToUri = new Map<string, string>();
284
- for (const tok of tokens) {
285
- if (tok.kind !== 'open' && tok.kind !== 'selfclose') continue;
286
- for (const attr of tok.attrs ?? []) {
287
- if (attr.name === 'xmlns') prefixToUri.set('', attr.value);
288
- else if (attr.prefix === 'xmlns') prefixToUri.set(attr.local, attr.value);
289
- }
290
- }
291
-
292
- const uriForPrefix = (prefix: string) => prefixToUri.get(prefix);
293
- const isWml = (token: XmlToken) => prefixToUri.get(token.prefix ?? '') === WML_NS;
294
- const wmlAttr = (token: XmlToken, local: string) => {
295
- for (const attr of token.attrs ?? []) {
296
- if (attr.local !== local) continue;
297
- // An unprefixed attribute is in no namespace; a prefixed one resolves
298
- // through the same table. Marker ids share their element's prefix.
299
- if (attr.prefix === '' || prefixToUri.get(attr.prefix) === WML_NS) return attr.value;
300
- }
301
- return undefined;
302
- };
303
-
304
- return { uriForPrefix, isWml, wmlAttr };
305
- }
306
-
307
- // =============================================================================
308
- // Ordered document flow
309
- // =============================================================================
310
-
311
- export type FlowItem =
312
- | { kind: 'text'; text: string }
313
- | { kind: 'paraStart'; style: string | null; level: number; xmlStart: number }
314
- | { kind: 'paraEnd' }
315
- | { kind: 'runStart'; xmlStart: number; xmlContentStart: number }
316
- | { kind: 'runEnd'; xmlEnd: number }
317
- | { kind: 'commentStart'; id: string; xmlStart: number; xmlEnd: number }
318
- | { kind: 'commentEnd'; id: string; xmlStart: number; xmlEnd: number }
319
- | { kind: 'commentRef'; id: string }
320
- | { kind: 'insStart' }
321
- | { kind: 'insEnd' }
322
- | { kind: 'delStart' }
323
- | { kind: 'delEnd' };
324
-
325
- /**
326
- * Walk a part's XML once into an ordered flow. Text-bearing items appear in
327
- * document order interleaved with paragraph, run, track-change, and
328
- * comment-range boundaries, each tagged with the source offsets a later
329
- * injection step needs.
330
- */
331
- export function walkBody(xml: string, ns?: NsContext): FlowItem[] {
332
- const tokens = tokenizeXml(xml);
333
- const nsCtx = ns ?? resolveNamespaces(tokens);
334
- const flow: FlowItem[] = [];
335
-
336
- // Track nesting so content is read precisely: literal text only counts
337
- // inside `<w:t>`/`<w:delText>` (never `<w:instrText>` field codes), and
338
- // `<w:tab/>`/`<w:br/>` only count inside a run (never a `<w:pPr>` tab-stop
339
- // definition).
340
- let runDepth = 0;
341
- let inPPr = 0;
342
- let textDepth = 0;
343
- let currentPara: Extract<FlowItem, { kind: 'paraStart' }> | null = null;
344
-
345
- const local = (t: XmlToken) => (nsCtx.isWml(t) ? t.local : undefined);
346
-
347
- for (let idx = 0; idx < tokens.length; idx++) {
348
- const tok = tokens[idx]!;
349
-
350
- if (tok.kind === 'text') {
351
- if (textDepth > 0 && tok.text) flow.push({ kind: 'text', text: tok.text });
352
- continue;
353
- }
354
- if (tok.kind === 'cdata') {
355
- if (textDepth > 0 && tok.text) flow.push({ kind: 'text', text: tok.text });
356
- continue;
357
- }
358
- if (tok.kind !== 'open' && tok.kind !== 'close' && tok.kind !== 'selfclose') continue;
359
-
360
- const ln = local(tok);
361
- if (ln === undefined) continue;
362
-
363
- if (tok.kind === 'open') {
364
- switch (ln) {
365
- case 'p':
366
- currentPara = { kind: 'paraStart', style: null, level: 0, xmlStart: tok.start };
367
- flow.push(currentPara);
368
- break;
369
- case 'pPr':
370
- inPPr++;
371
- break;
372
- case 't':
373
- case 'delText':
374
- textDepth++;
375
- break;
376
- case 'r':
377
- runDepth++;
378
- flow.push({ kind: 'runStart', xmlStart: tok.start, xmlContentStart: tok.end });
379
- break;
380
- case 'ins':
381
- flow.push({ kind: 'insStart' });
382
- break;
383
- case 'del':
384
- flow.push({ kind: 'delStart' });
385
- break;
386
- default:
387
- break;
388
- }
389
- } else if (tok.kind === 'close') {
390
- switch (ln) {
391
- case 'p':
392
- flow.push({ kind: 'paraEnd' });
393
- currentPara = null;
394
- break;
395
- case 'pPr':
396
- if (inPPr > 0) inPPr--;
397
- break;
398
- case 't':
399
- case 'delText':
400
- if (textDepth > 0) textDepth--;
401
- break;
402
- case 'r':
403
- if (runDepth > 0) runDepth--;
404
- flow.push({ kind: 'runEnd', xmlEnd: tok.start });
405
- break;
406
- case 'ins':
407
- flow.push({ kind: 'insEnd' });
408
- break;
409
- case 'del':
410
- flow.push({ kind: 'delEnd' });
411
- break;
412
- default:
413
- break;
414
- }
415
- } else {
416
- // self-close
417
- switch (ln) {
418
- case 'pStyle': {
419
- const val = nsCtx.wmlAttr(tok, 'val');
420
- if (currentPara && inPPr > 0 && val) {
421
- currentPara.style = val;
422
- const m = val.match(/(\d+)/);
423
- if (/heading/i.test(val)) currentPara.level = m ? parseInt(m[1]!, 10) : 0;
424
- }
425
- break;
426
- }
427
- case 'tab':
428
- if (runDepth > 0 && inPPr === 0) flow.push({ kind: 'text', text: '\t' });
429
- break;
430
- case 'br':
431
- case 'cr':
432
- if (runDepth > 0 && inPPr === 0) flow.push({ kind: 'text', text: '\n' });
433
- break;
434
- case 'commentRangeStart': {
435
- const id = nsCtx.wmlAttr(tok, 'id');
436
- if (id !== undefined) flow.push({ kind: 'commentStart', id, xmlStart: tok.start, xmlEnd: tok.end });
437
- break;
438
- }
439
- case 'commentRangeEnd': {
440
- const id = nsCtx.wmlAttr(tok, 'id');
441
- if (id !== undefined) flow.push({ kind: 'commentEnd', id, xmlStart: tok.start, xmlEnd: tok.end });
442
- break;
443
- }
444
- case 'commentReference': {
445
- const id = nsCtx.wmlAttr(tok, 'id');
446
- if (id !== undefined) flow.push({ kind: 'commentRef', id });
447
- break;
448
- }
449
- default:
450
- break;
451
- }
452
- }
453
- }
454
-
455
- return flow;
456
- }
457
-
458
- // =============================================================================
459
- // Derived readers
460
- // =============================================================================
461
-
462
- export interface CommentRange {
463
- id: string;
464
- /** Concatenated anchor text between the range markers. */
465
- anchor: string;
466
- /** Start offset of the anchor in `text`. */
467
- start: number;
468
- /** End offset of the anchor in `text`. */
469
- end: number;
470
- /** True when the range encloses no text (a zero-width / point anchor). */
471
- isEmpty: boolean;
472
- }
473
-
474
- export interface DocTextModel {
475
- /** Plain text: runs concatenated, paragraphs joined with nothing between. */
476
- text: string;
477
- /** Comment ranges with offsets into `text`. */
478
- comments: CommentRange[];
479
- /** Heading paragraphs with their offset into `text`. */
480
- headings: Array<{ style: string; level: number; text: string; position: number }>;
481
- }
482
-
483
- /**
484
- * Build the plain-text model used for comment-anchor matching. Runs are
485
- * concatenated and paragraphs are joined with no separator, matching the
486
- * coordinate system the placement engine expects, but namespace-aware and
487
- * robust to marker-attribute variation and multi-run anchors.
488
- */
489
- export function buildDocTextModel(xml: string): DocTextModel {
490
- const flow = walkBody(xml);
491
- let text = '';
492
- const startOffsets = new Map<string, number>();
493
- const endOffsets = new Map<string, number>();
494
- const comments: CommentRange[] = [];
495
- const headings: DocTextModel['headings'] = [];
496
-
497
- let paraStartOffset = 0;
498
- let paraStyle: string | null = null;
499
- let paraLevel = 0;
500
- let paraText = '';
501
-
502
- for (const item of flow) {
503
- switch (item.kind) {
504
- case 'text':
505
- text += item.text;
506
- paraText += item.text;
507
- break;
508
- case 'paraStart':
509
- paraStartOffset = text.length;
510
- paraStyle = item.style;
511
- paraLevel = item.level;
512
- paraText = '';
513
- break;
514
- case 'paraEnd':
515
- if (paraStyle && /heading/i.test(paraStyle) && paraText.trim()) {
516
- headings.push({ style: paraStyle, level: paraLevel, text: paraText.trim(), position: paraStartOffset });
517
- }
518
- break;
519
- case 'commentStart':
520
- if (!startOffsets.has(item.id)) startOffsets.set(item.id, text.length);
521
- break;
522
- case 'commentEnd':
523
- if (!endOffsets.has(item.id)) endOffsets.set(item.id, text.length);
524
- break;
525
- default:
526
- break;
527
- }
528
- }
529
-
530
- // The paragraph style can be set after `paraStart` (pStyle lives early in
531
- // the paragraph), so headings are recorded at paraEnd above.
532
-
533
- for (const [id, start] of startOffsets) {
534
- const end = endOffsets.get(id);
535
- if (end === undefined) continue;
536
- const anchor = text.slice(start, Math.max(start, end));
537
- comments.push({ id, anchor: anchor.trim(), start, end: Math.max(start, end), isEmpty: !anchor.trim() });
538
- }
539
-
540
- return { text, comments, headings };
541
- }
542
-
543
- // =============================================================================
544
- // Run index (for comment injection)
545
- // =============================================================================
546
-
547
- export interface TextRunSlot {
548
- /** Source offset of the enclosing run's `<w:r>`. */
549
- runOpenStart: number;
550
- /** Source offset just past the enclosing run's `</w:r>`. */
551
- runCloseEnd: number;
552
- /** Raw `<w:rPr>...</w:rPr>` of the enclosing run, or '' when it has none. */
553
- rPr: string;
554
- /** Raw opening `<w:t ...>` tag of this text element. */
555
- tOpenTag: string;
556
- /** Source offset just past `<w:t ...>`. */
557
- contentStart: number;
558
- /** Source offset of `</w:t>`. */
559
- contentEnd: number;
560
- /** Raw (still XML-encoded) text between the `<w:t>` tags. */
561
- content: string;
562
- }
563
-
564
- interface RunFrame {
565
- openStart: number;
566
- rPr: string;
567
- rPrOpenStart: number;
568
- pending: TextRunSlot[];
569
- }
570
-
571
- /**
572
- * Index every `<w:t>` text element with its enclosing run's exact boundaries
573
- * and run properties, derived from the token nesting rather than by scanning
574
- * backwards for the nearest `<w:r`. This locates the correct run even when the
575
- * text sits inside a hyperlink, a field, or an `mc:AlternateContent` block, and
576
- * naturally ignores marker-like text that lives in attributes (it is not inside
577
- * a `<w:t>`).
578
- */
579
- export function indexTextRuns(xml: string): TextRunSlot[] {
580
- const tokens = tokenizeXml(xml);
581
- const ns = resolveNamespaces(tokens);
582
- const slots: TextRunSlot[] = [];
583
- const runStack: RunFrame[] = [];
584
-
585
- let tOpenTag = '';
586
- let tContentStart = -1;
587
- let inT = false;
588
-
589
- for (const tok of tokens) {
590
- if (tok.kind === 'open' || tok.kind === 'close' || tok.kind === 'selfclose') {
591
- const ln = ns.isWml(tok) ? tok.local : undefined;
592
- const top = runStack[runStack.length - 1];
593
-
594
- if (tok.kind === 'open') {
595
- if (ln === 'r') {
596
- runStack.push({ openStart: tok.start, rPr: '', rPrOpenStart: -1, pending: [] });
597
- } else if (ln === 'rPr' && top) {
598
- top.rPrOpenStart = tok.start;
599
- } else if (ln === 't' && top) {
600
- tOpenTag = tok.raw;
601
- tContentStart = tok.end;
602
- inT = true;
603
- }
604
- } else if (tok.kind === 'close') {
605
- if (ln === 'rPr' && top && top.rPrOpenStart >= 0) {
606
- top.rPr = xml.slice(top.rPrOpenStart, tok.end);
607
- top.rPrOpenStart = -1;
608
- } else if (ln === 't' && top && inT) {
609
- top.pending.push({
610
- runOpenStart: top.openStart,
611
- runCloseEnd: -1,
612
- rPr: top.rPr,
613
- tOpenTag,
614
- contentStart: tContentStart,
615
- contentEnd: tok.start,
616
- content: xml.slice(tContentStart, tok.start),
617
- });
618
- inT = false;
619
- } else if (ln === 'r') {
620
- const frame = runStack.pop();
621
- if (frame) {
622
- for (const slot of frame.pending) {
623
- slot.runCloseEnd = tok.end;
624
- slots.push(slot);
625
- }
626
- }
627
- }
628
- }
629
- }
630
- }
631
-
632
- slots.sort((a, b) => a.contentStart - b.contentStart);
633
- return slots;
634
- }
635
-
636
- // =============================================================================
637
- // Comment bodies + threading
638
- // =============================================================================
639
-
640
- export interface ExtractedComment {
641
- id: string;
642
- author: string;
643
- /** Full ISO date string as written, or '' when absent. */
644
- date: string;
645
- text: string;
646
- /** Parent comment id for a reply, resolved from commentsExtended.xml. */
647
- parentId?: string;
648
- }
649
-
650
- /** First attribute with the given local name, regardless of namespace prefix. */
651
- function attrByLocal(token: XmlToken, local: string): string | undefined {
652
- for (const attr of token.attrs ?? []) {
653
- if (attr.local === local) return attr.value;
654
- }
655
- return undefined;
656
- }
657
-
658
- /**
659
- * Extract comment bodies from `word/comments.xml` and reply threading from
660
- * `word/commentsExtended.xml`. Structural elements are matched by local name
661
- * in the WordprocessingML namespace, so a document that binds WML to a prefix
662
- * other than `w` still yields its comments.
663
- */
664
- export function extractComments(zip: AdmZip): ExtractedComment[] {
665
- const commentsXml = readPartText(zip, 'word/comments.xml');
666
- if (commentsXml === null) return [];
667
-
668
- const tokens = tokenizeXml(commentsXml);
669
- const ns = resolveNamespaces(tokens);
670
- const wmlLocal = (t: XmlToken) => (ns.isWml(t) ? t.local : undefined);
671
-
672
- const comments: ExtractedComment[] = [];
673
- const paraIdToCommentId = new Map<string, string>();
674
-
675
- let current: ExtractedComment | null = null;
676
- let textDepth = 0;
677
-
678
- for (const tok of tokens) {
679
- if (tok.kind === 'text' || tok.kind === 'cdata') {
680
- if (current && textDepth > 0 && tok.text) current.text += tok.text;
681
- continue;
682
- }
683
- const ln =
684
- tok.kind === 'open' || tok.kind === 'close' || tok.kind === 'selfclose'
685
- ? wmlLocal(tok)
686
- : undefined;
687
- if (ln === undefined) continue;
688
-
689
- // Word stores the threading key (w14:paraId) on each comment paragraph;
690
- // a reply's second paragraph is an empty self-closed <w:p/>, so read it on
691
- // open and self-close alike.
692
- if ((tok.kind === 'open' || tok.kind === 'selfclose') && ln === 'p' && current) {
693
- const paraId = attrByLocal(tok, 'paraId');
694
- if (paraId && current.id) paraIdToCommentId.set(paraId, current.id);
695
- }
696
-
697
- if (tok.kind === 'open') {
698
- if (ln === 'comment') {
699
- current = {
700
- id: ns.wmlAttr(tok, 'id') ?? '',
701
- author: ns.wmlAttr(tok, 'author') ?? 'Unknown',
702
- date: ns.wmlAttr(tok, 'date') ?? '',
703
- text: '',
704
- };
705
- } else if ((ln === 't' || ln === 'delText') && current) {
706
- textDepth++;
707
- }
708
- } else if (tok.kind === 'close') {
709
- if (ln === 'comment' && current) {
710
- current.text = current.text.trim();
711
- comments.push(current);
712
- current = null;
713
- textDepth = 0;
714
- } else if ((ln === 't' || ln === 'delText') && textDepth > 0) {
715
- textDepth--;
716
- }
717
- }
718
- }
719
-
720
- // Resolve reply links from commentsExtended.xml (absent for non-Word sources).
721
- const extendedXml = readPartText(zip, 'word/commentsExtended.xml');
722
- if (extendedXml && paraIdToCommentId.size > 0) {
723
- const parentByCommentId = new Map<string, string>();
724
- for (const tok of tokenizeXml(extendedXml)) {
725
- if (tok.kind !== 'selfclose' && tok.kind !== 'open') continue;
726
- if (tok.local !== 'commentEx') continue;
727
- const paraId = attrByLocal(tok, 'paraId');
728
- const parentParaId = attrByLocal(tok, 'paraIdParent');
729
- if (!paraId || !parentParaId) continue;
730
- const childId = paraIdToCommentId.get(paraId);
731
- const parentId = paraIdToCommentId.get(parentParaId);
732
- if (childId && parentId && childId !== parentId) {
733
- parentByCommentId.set(childId, parentId);
734
- }
735
- }
736
- for (const c of comments) {
737
- const parent = parentByCommentId.get(c.id);
738
- if (parent) c.parentId = parent;
739
- }
740
- }
741
-
742
- return comments;
743
- }
744
-
745
- /** Parts that can carry comment ranges, in reading order. */
746
- export const COMMENT_PARTS = ['word/document.xml', 'word/footnotes.xml', 'word/endnotes.xml'];
747
-
748
- /**
749
- * Comment ranges across every part that can hold them — not just
750
- * `document.xml`, so a comment anchored in a footnote or endnote is found
751
- * rather than silently lost. Positions are offsets into the concatenated
752
- * `fullDocText`.
753
- */
754
- export function buildCommentAnchorModel(zip: AdmZip): { fullDocText: string; comments: CommentRange[] } {
755
- let fullDocText = '';
756
- const comments: CommentRange[] = [];
757
- for (const part of COMMENT_PARTS) {
758
- const xml = readPartText(zip, part);
759
- if (xml === null) continue;
760
- const model = buildDocTextModel(xml);
761
- const offset = fullDocText.length;
762
- fullDocText += model.text;
763
- for (const c of model.comments) {
764
- comments.push({ id: c.id, anchor: c.anchor, start: c.start + offset, end: c.end + offset, isEmpty: c.isEmpty });
765
- }
766
- }
767
- return { fullDocText, comments };
768
- }
1
+ /**
2
+ * Parser-backed OOXML layer.
3
+ *
4
+ * A `.docx` is a zip of XML parts. Reading those parts with regexes breaks on
5
+ * the variability real Word emits: marker elements carry attributes beyond
6
+ * `w:id`, anchor text spans many runs, the WordprocessingML namespace can be
7
+ * bound to a prefix other than `w`, runs are separated by `<w:tab/>`/`<w:br/>`,
8
+ * and prose lives in parts other than `document.xml` (footnotes, endnotes,
9
+ * headers, footers).
10
+ *
11
+ * This module tokenizes the XML structurally and walks it once into an ordered
12
+ * flow of paragraphs, runs, text, track-change spans, and comment-range
13
+ * markers. Every reader (text extraction, comment anchors, headings) and the
14
+ * comment injector derive from that single walk — there is no second engine to
15
+ * drift from.
16
+ */
17
+
18
+ import AdmZip from 'adm-zip';
19
+
20
+ export const WML_NS = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
21
+
22
+ // =============================================================================
23
+ // Package access
24
+ // =============================================================================
25
+
26
+ /** Open a `.docx` package. Throws a clear error if it is not a valid zip. */
27
+ export function openDocx(docxPath: string): AdmZip {
28
+ try {
29
+ return new AdmZip(docxPath);
30
+ } catch (err: any) {
31
+ throw new Error(`Invalid Word document (not a valid .docx file): ${err.message}`);
32
+ }
33
+ }
34
+
35
+ /** Read a single part as UTF-8 text, or `null` when the part is absent. */
36
+ export function readPartText(zip: AdmZip, partName: string): string | null {
37
+ const entry = zip.getEntry(partName);
38
+ if (!entry) return null;
39
+ return entry.getData().toString('utf8');
40
+ }
41
+
42
+ /**
43
+ * The WordprocessingML parts that carry running prose, in reading order:
44
+ * the main story first, then footnotes, endnotes, headers, and footers.
45
+ * A reader that opens only `document.xml` loses everything else.
46
+ */
47
+ export function listProseParts(zip: AdmZip): string[] {
48
+ const parts: string[] = [];
49
+ if (zip.getEntry('word/document.xml')) parts.push('word/document.xml');
50
+ for (const fixed of ['word/footnotes.xml', 'word/endnotes.xml']) {
51
+ if (zip.getEntry(fixed)) parts.push(fixed);
52
+ }
53
+ const headerFooter = zip
54
+ .getEntries()
55
+ .map((e) => e.entryName)
56
+ .filter((name) => /^word\/(header|footer)\d*\.xml$/.test(name))
57
+ .sort();
58
+ parts.push(...headerFooter);
59
+ return parts;
60
+ }
61
+
62
+ // =============================================================================
63
+ // Entity coding
64
+ // =============================================================================
65
+
66
+ /** Decode the XML entities that appear in `.docx` text and attribute values. */
67
+ export function decodeXmlEntities(text: string): string {
68
+ return text
69
+ .replace(/&lt;/g, '<')
70
+ .replace(/&gt;/g, '>')
71
+ .replace(/&quot;/g, '"')
72
+ .replace(/&apos;/g, "'")
73
+ .replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(parseInt(code, 10)))
74
+ .replace(/&#x([0-9a-fA-F]+);/g, (_, code) => String.fromCodePoint(parseInt(code, 16)))
75
+ .replace(/&amp;/g, '&'); // ampersand last so decoded text is not re-decoded
76
+ }
77
+
78
+ /** Encode text for placement inside an XML element (`<w:t>`). */
79
+ export function encodeXmlText(text: string): string {
80
+ return text
81
+ .replace(/&/g, '&amp;')
82
+ .replace(/</g, '&lt;')
83
+ .replace(/>/g, '&gt;');
84
+ }
85
+
86
+ /** Encode text for placement inside a double-quoted XML attribute value. */
87
+ export function encodeXmlAttr(text: string): string {
88
+ return encodeXmlText(text).replace(/"/g, '&quot;');
89
+ }
90
+
91
+ // =============================================================================
92
+ // Tokenizer
93
+ // =============================================================================
94
+
95
+ export interface XmlAttr {
96
+ /** Qualified name as written, e.g. `w:id`. */
97
+ name: string;
98
+ prefix: string;
99
+ local: string;
100
+ /** Entity-decoded value. */
101
+ value: string;
102
+ }
103
+
104
+ export type XmlTokenKind =
105
+ | 'open'
106
+ | 'close'
107
+ | 'selfclose'
108
+ | 'text'
109
+ | 'comment'
110
+ | 'cdata'
111
+ | 'decl'
112
+ | 'pi';
113
+
114
+ export interface XmlToken {
115
+ kind: XmlTokenKind;
116
+ /** Byte offset of `<` (or text start) in the source. */
117
+ start: number;
118
+ /** Byte offset just past `>` (or text end) in the source. */
119
+ end: number;
120
+ /** Exact source slice [start, end). */
121
+ raw: string;
122
+ name?: string;
123
+ prefix?: string;
124
+ local?: string;
125
+ attrs?: XmlAttr[];
126
+ /** Entity-decoded text, for `text` and `cdata` tokens. */
127
+ text?: string;
128
+ }
129
+
130
+ function splitQName(name: string): { prefix: string; local: string } {
131
+ const colon = name.indexOf(':');
132
+ return colon === -1
133
+ ? { prefix: '', local: name }
134
+ : { prefix: name.slice(0, colon), local: name.slice(colon + 1) };
135
+ }
136
+
137
+ const WS = /\s/;
138
+
139
+ function parseTag(raw: string, start: number, end: number): XmlToken {
140
+ const isClose = raw.startsWith('</');
141
+ const selfClose = !isClose && raw.endsWith('/>');
142
+ const body = raw.slice(isClose ? 2 : 1, selfClose ? raw.length - 2 : raw.length - 1);
143
+
144
+ let p = 0;
145
+ while (p < body.length && !WS.test(body[p]!) && body[p] !== '/') p++;
146
+ const name = body.slice(0, p);
147
+ const { prefix, local } = splitQName(name);
148
+
149
+ const attrs: XmlAttr[] = [];
150
+ while (p < body.length) {
151
+ while (p < body.length && WS.test(body[p]!)) p++;
152
+ if (p >= body.length) break;
153
+ const nameStart = p;
154
+ while (p < body.length && body[p] !== '=' && !WS.test(body[p]!)) p++;
155
+ const attrName = body.slice(nameStart, p);
156
+ while (p < body.length && WS.test(body[p]!)) p++;
157
+ if (body[p] !== '=') {
158
+ // Valueless attribute (not used by WML, but keep the lexer honest).
159
+ if (attrName) {
160
+ const q = splitQName(attrName);
161
+ attrs.push({ name: attrName, prefix: q.prefix, local: q.local, value: '' });
162
+ }
163
+ continue;
164
+ }
165
+ p++; // skip '='
166
+ while (p < body.length && WS.test(body[p]!)) p++;
167
+ const quote = body[p];
168
+ let value = '';
169
+ if (quote === '"' || quote === "'") {
170
+ p++;
171
+ const valueStart = p;
172
+ while (p < body.length && body[p] !== quote) p++;
173
+ value = body.slice(valueStart, p);
174
+ p++; // skip closing quote
175
+ } else {
176
+ const valueStart = p;
177
+ while (p < body.length && !WS.test(body[p]!)) p++;
178
+ value = body.slice(valueStart, p);
179
+ }
180
+ const q = splitQName(attrName);
181
+ attrs.push({ name: attrName, prefix: q.prefix, local: q.local, value: decodeXmlEntities(value) });
182
+ }
183
+
184
+ return {
185
+ kind: isClose ? 'close' : selfClose ? 'selfclose' : 'open',
186
+ start,
187
+ end,
188
+ raw,
189
+ name,
190
+ prefix,
191
+ local,
192
+ attrs,
193
+ };
194
+ }
195
+
196
+ /**
197
+ * Tokenize an XML string into an ordered, offset-tagged token stream.
198
+ * Quote-aware so a raw `>` inside an attribute value does not end a tag early.
199
+ */
200
+ export function tokenizeXml(xml: string): XmlToken[] {
201
+ const tokens: XmlToken[] = [];
202
+ const n = xml.length;
203
+ let i = 0;
204
+
205
+ while (i < n) {
206
+ const lt = xml.indexOf('<', i);
207
+ if (lt === -1) {
208
+ tokens.push({ kind: 'text', start: i, end: n, raw: xml.slice(i, n), text: decodeXmlEntities(xml.slice(i, n)) });
209
+ break;
210
+ }
211
+ if (lt > i) {
212
+ tokens.push({ kind: 'text', start: i, end: lt, raw: xml.slice(i, lt), text: decodeXmlEntities(xml.slice(i, lt)) });
213
+ }
214
+
215
+ if (xml.startsWith('<!--', lt)) {
216
+ const close = xml.indexOf('-->', lt + 4);
217
+ const end = close === -1 ? n : close + 3;
218
+ tokens.push({ kind: 'comment', start: lt, end, raw: xml.slice(lt, end) });
219
+ i = end;
220
+ continue;
221
+ }
222
+ if (xml.startsWith('<![CDATA[', lt)) {
223
+ const close = xml.indexOf(']]>', lt + 9);
224
+ const end = close === -1 ? n : close + 3;
225
+ tokens.push({
226
+ kind: 'cdata',
227
+ start: lt,
228
+ end,
229
+ raw: xml.slice(lt, end),
230
+ text: xml.slice(lt + 9, close === -1 ? n : close),
231
+ });
232
+ i = end;
233
+ continue;
234
+ }
235
+ if (xml.startsWith('<?', lt)) {
236
+ const close = xml.indexOf('?>', lt + 2);
237
+ const end = close === -1 ? n : close + 2;
238
+ tokens.push({ kind: xml.startsWith('<?xml', lt) ? 'decl' : 'pi', start: lt, end, raw: xml.slice(lt, end) });
239
+ i = end;
240
+ continue;
241
+ }
242
+
243
+ // Element tag: find the closing '>' that is not inside a quoted value.
244
+ let j = lt + 1;
245
+ let quote = '';
246
+ while (j < n) {
247
+ const ch = xml[j];
248
+ if (quote) {
249
+ if (ch === quote) quote = '';
250
+ } else if (ch === '"' || ch === "'") {
251
+ quote = ch;
252
+ } else if (ch === '>') {
253
+ break;
254
+ }
255
+ j++;
256
+ }
257
+ const end = j < n ? j + 1 : n;
258
+ tokens.push(parseTag(xml.slice(lt, end), lt, end));
259
+ i = end;
260
+ }
261
+
262
+ return tokens;
263
+ }
264
+
265
+ // =============================================================================
266
+ // Namespace resolution
267
+ // =============================================================================
268
+
269
+ export interface NsContext {
270
+ uriForPrefix(prefix: string): string | undefined;
271
+ /** True when an element token lives in the WordprocessingML namespace. */
272
+ isWml(token: XmlToken): boolean;
273
+ /** Value of the WML-namespaced attribute with the given local name, if present. */
274
+ wmlAttr(token: XmlToken, local: string): string | undefined;
275
+ }
276
+
277
+ /**
278
+ * Build a namespace context from a token stream. Declarations are read from
279
+ * every element (the root carries them in practice), so the WML namespace is
280
+ * matched by URI rather than by assuming the `w` prefix.
281
+ */
282
+ export function resolveNamespaces(tokens: XmlToken[]): NsContext {
283
+ const prefixToUri = new Map<string, string>();
284
+ for (const tok of tokens) {
285
+ if (tok.kind !== 'open' && tok.kind !== 'selfclose') continue;
286
+ for (const attr of tok.attrs ?? []) {
287
+ if (attr.name === 'xmlns') prefixToUri.set('', attr.value);
288
+ else if (attr.prefix === 'xmlns') prefixToUri.set(attr.local, attr.value);
289
+ }
290
+ }
291
+
292
+ const uriForPrefix = (prefix: string) => prefixToUri.get(prefix);
293
+ const isWml = (token: XmlToken) => prefixToUri.get(token.prefix ?? '') === WML_NS;
294
+ const wmlAttr = (token: XmlToken, local: string) => {
295
+ for (const attr of token.attrs ?? []) {
296
+ if (attr.local !== local) continue;
297
+ // An unprefixed attribute is in no namespace; a prefixed one resolves
298
+ // through the same table. Marker ids share their element's prefix.
299
+ if (attr.prefix === '' || prefixToUri.get(attr.prefix) === WML_NS) return attr.value;
300
+ }
301
+ return undefined;
302
+ };
303
+
304
+ return { uriForPrefix, isWml, wmlAttr };
305
+ }
306
+
307
+ // =============================================================================
308
+ // Ordered document flow
309
+ // =============================================================================
310
+
311
+ export type FlowItem =
312
+ | { kind: 'text'; text: string }
313
+ | { kind: 'paraStart'; style: string | null; level: number; xmlStart: number }
314
+ | { kind: 'paraEnd' }
315
+ | { kind: 'runStart'; xmlStart: number; xmlContentStart: number }
316
+ | { kind: 'runEnd'; xmlEnd: number }
317
+ | { kind: 'commentStart'; id: string; xmlStart: number; xmlEnd: number }
318
+ | { kind: 'commentEnd'; id: string; xmlStart: number; xmlEnd: number }
319
+ | { kind: 'commentRef'; id: string }
320
+ | { kind: 'insStart' }
321
+ | { kind: 'insEnd' }
322
+ | { kind: 'delStart' }
323
+ | { kind: 'delEnd' };
324
+
325
+ /**
326
+ * Walk a part's XML once into an ordered flow. Text-bearing items appear in
327
+ * document order interleaved with paragraph, run, track-change, and
328
+ * comment-range boundaries, each tagged with the source offsets a later
329
+ * injection step needs.
330
+ */
331
+ export function walkBody(xml: string, ns?: NsContext): FlowItem[] {
332
+ const tokens = tokenizeXml(xml);
333
+ const nsCtx = ns ?? resolveNamespaces(tokens);
334
+ const flow: FlowItem[] = [];
335
+
336
+ // Track nesting so content is read precisely: literal text only counts
337
+ // inside `<w:t>`/`<w:delText>` (never `<w:instrText>` field codes), and
338
+ // `<w:tab/>`/`<w:br/>` only count inside a run (never a `<w:pPr>` tab-stop
339
+ // definition).
340
+ let runDepth = 0;
341
+ let inPPr = 0;
342
+ let textDepth = 0;
343
+ let currentPara: Extract<FlowItem, { kind: 'paraStart' }> | null = null;
344
+
345
+ const local = (t: XmlToken) => (nsCtx.isWml(t) ? t.local : undefined);
346
+
347
+ for (let idx = 0; idx < tokens.length; idx++) {
348
+ const tok = tokens[idx]!;
349
+
350
+ if (tok.kind === 'text') {
351
+ if (textDepth > 0 && tok.text) flow.push({ kind: 'text', text: tok.text });
352
+ continue;
353
+ }
354
+ if (tok.kind === 'cdata') {
355
+ if (textDepth > 0 && tok.text) flow.push({ kind: 'text', text: tok.text });
356
+ continue;
357
+ }
358
+ if (tok.kind !== 'open' && tok.kind !== 'close' && tok.kind !== 'selfclose') continue;
359
+
360
+ const ln = local(tok);
361
+ if (ln === undefined) continue;
362
+
363
+ if (tok.kind === 'open') {
364
+ switch (ln) {
365
+ case 'p':
366
+ currentPara = { kind: 'paraStart', style: null, level: 0, xmlStart: tok.start };
367
+ flow.push(currentPara);
368
+ break;
369
+ case 'pPr':
370
+ inPPr++;
371
+ break;
372
+ case 't':
373
+ case 'delText':
374
+ textDepth++;
375
+ break;
376
+ case 'r':
377
+ runDepth++;
378
+ flow.push({ kind: 'runStart', xmlStart: tok.start, xmlContentStart: tok.end });
379
+ break;
380
+ case 'ins':
381
+ flow.push({ kind: 'insStart' });
382
+ break;
383
+ case 'del':
384
+ flow.push({ kind: 'delStart' });
385
+ break;
386
+ default:
387
+ break;
388
+ }
389
+ } else if (tok.kind === 'close') {
390
+ switch (ln) {
391
+ case 'p':
392
+ flow.push({ kind: 'paraEnd' });
393
+ currentPara = null;
394
+ break;
395
+ case 'pPr':
396
+ if (inPPr > 0) inPPr--;
397
+ break;
398
+ case 't':
399
+ case 'delText':
400
+ if (textDepth > 0) textDepth--;
401
+ break;
402
+ case 'r':
403
+ if (runDepth > 0) runDepth--;
404
+ flow.push({ kind: 'runEnd', xmlEnd: tok.start });
405
+ break;
406
+ case 'ins':
407
+ flow.push({ kind: 'insEnd' });
408
+ break;
409
+ case 'del':
410
+ flow.push({ kind: 'delEnd' });
411
+ break;
412
+ default:
413
+ break;
414
+ }
415
+ } else {
416
+ // self-close
417
+ switch (ln) {
418
+ case 'pStyle': {
419
+ const val = nsCtx.wmlAttr(tok, 'val');
420
+ if (currentPara && inPPr > 0 && val) {
421
+ currentPara.style = val;
422
+ const m = val.match(/(\d+)/);
423
+ if (/heading/i.test(val)) currentPara.level = m ? parseInt(m[1]!, 10) : 0;
424
+ }
425
+ break;
426
+ }
427
+ case 'tab':
428
+ if (runDepth > 0 && inPPr === 0) flow.push({ kind: 'text', text: '\t' });
429
+ break;
430
+ case 'br':
431
+ case 'cr':
432
+ if (runDepth > 0 && inPPr === 0) flow.push({ kind: 'text', text: '\n' });
433
+ break;
434
+ case 'commentRangeStart': {
435
+ const id = nsCtx.wmlAttr(tok, 'id');
436
+ if (id !== undefined) flow.push({ kind: 'commentStart', id, xmlStart: tok.start, xmlEnd: tok.end });
437
+ break;
438
+ }
439
+ case 'commentRangeEnd': {
440
+ const id = nsCtx.wmlAttr(tok, 'id');
441
+ if (id !== undefined) flow.push({ kind: 'commentEnd', id, xmlStart: tok.start, xmlEnd: tok.end });
442
+ break;
443
+ }
444
+ case 'commentReference': {
445
+ const id = nsCtx.wmlAttr(tok, 'id');
446
+ if (id !== undefined) flow.push({ kind: 'commentRef', id });
447
+ break;
448
+ }
449
+ default:
450
+ break;
451
+ }
452
+ }
453
+ }
454
+
455
+ return flow;
456
+ }
457
+
458
+ // =============================================================================
459
+ // Derived readers
460
+ // =============================================================================
461
+
462
+ export interface CommentRange {
463
+ id: string;
464
+ /** Concatenated anchor text between the range markers. */
465
+ anchor: string;
466
+ /** Start offset of the anchor in `text`. */
467
+ start: number;
468
+ /** End offset of the anchor in `text`. */
469
+ end: number;
470
+ /** True when the range encloses no text (a zero-width / point anchor). */
471
+ isEmpty: boolean;
472
+ }
473
+
474
+ export interface DocTextModel {
475
+ /** Plain text: runs concatenated, paragraphs joined with nothing between. */
476
+ text: string;
477
+ /** Comment ranges with offsets into `text`. */
478
+ comments: CommentRange[];
479
+ /** Heading paragraphs with their offset into `text`. */
480
+ headings: Array<{ style: string; level: number; text: string; position: number }>;
481
+ }
482
+
483
+ /**
484
+ * Build the plain-text model used for comment-anchor matching. Runs are
485
+ * concatenated and paragraphs are joined with no separator, matching the
486
+ * coordinate system the placement engine expects, but namespace-aware and
487
+ * robust to marker-attribute variation and multi-run anchors.
488
+ */
489
+ export function buildDocTextModel(xml: string): DocTextModel {
490
+ const flow = walkBody(xml);
491
+ let text = '';
492
+ const startOffsets = new Map<string, number>();
493
+ const endOffsets = new Map<string, number>();
494
+ const comments: CommentRange[] = [];
495
+ const headings: DocTextModel['headings'] = [];
496
+
497
+ let paraStartOffset = 0;
498
+ let paraStyle: string | null = null;
499
+ let paraLevel = 0;
500
+ let paraText = '';
501
+
502
+ for (const item of flow) {
503
+ switch (item.kind) {
504
+ case 'text':
505
+ text += item.text;
506
+ paraText += item.text;
507
+ break;
508
+ case 'paraStart':
509
+ paraStartOffset = text.length;
510
+ paraStyle = item.style;
511
+ paraLevel = item.level;
512
+ paraText = '';
513
+ break;
514
+ case 'paraEnd':
515
+ if (paraStyle && /heading/i.test(paraStyle) && paraText.trim()) {
516
+ headings.push({ style: paraStyle, level: paraLevel, text: paraText.trim(), position: paraStartOffset });
517
+ }
518
+ break;
519
+ case 'commentStart':
520
+ if (!startOffsets.has(item.id)) startOffsets.set(item.id, text.length);
521
+ break;
522
+ case 'commentEnd':
523
+ if (!endOffsets.has(item.id)) endOffsets.set(item.id, text.length);
524
+ break;
525
+ default:
526
+ break;
527
+ }
528
+ }
529
+
530
+ // The paragraph style can be set after `paraStart` (pStyle lives early in
531
+ // the paragraph), so headings are recorded at paraEnd above.
532
+
533
+ for (const [id, start] of startOffsets) {
534
+ const end = endOffsets.get(id);
535
+ if (end === undefined) continue;
536
+ const anchor = text.slice(start, Math.max(start, end));
537
+ comments.push({ id, anchor: anchor.trim(), start, end: Math.max(start, end), isEmpty: !anchor.trim() });
538
+ }
539
+
540
+ return { text, comments, headings };
541
+ }
542
+
543
+ // =============================================================================
544
+ // Run index (for comment injection)
545
+ // =============================================================================
546
+
547
+ export interface TextRunSlot {
548
+ /** Source offset of the enclosing run's `<w:r>`. */
549
+ runOpenStart: number;
550
+ /** Source offset just past the enclosing run's `</w:r>`. */
551
+ runCloseEnd: number;
552
+ /** Raw `<w:rPr>...</w:rPr>` of the enclosing run, or '' when it has none. */
553
+ rPr: string;
554
+ /** Raw opening `<w:t ...>` tag of this text element. */
555
+ tOpenTag: string;
556
+ /** Source offset just past `<w:t ...>`. */
557
+ contentStart: number;
558
+ /** Source offset of `</w:t>`. */
559
+ contentEnd: number;
560
+ /** Raw (still XML-encoded) text between the `<w:t>` tags. */
561
+ content: string;
562
+ }
563
+
564
+ interface RunFrame {
565
+ openStart: number;
566
+ rPr: string;
567
+ rPrOpenStart: number;
568
+ pending: TextRunSlot[];
569
+ }
570
+
571
+ /**
572
+ * Index every `<w:t>` text element with its enclosing run's exact boundaries
573
+ * and run properties, derived from the token nesting rather than by scanning
574
+ * backwards for the nearest `<w:r`. This locates the correct run even when the
575
+ * text sits inside a hyperlink, a field, or an `mc:AlternateContent` block, and
576
+ * naturally ignores marker-like text that lives in attributes (it is not inside
577
+ * a `<w:t>`).
578
+ */
579
+ export function indexTextRuns(xml: string): TextRunSlot[] {
580
+ const tokens = tokenizeXml(xml);
581
+ const ns = resolveNamespaces(tokens);
582
+ const slots: TextRunSlot[] = [];
583
+ const runStack: RunFrame[] = [];
584
+
585
+ let tOpenTag = '';
586
+ let tContentStart = -1;
587
+ let inT = false;
588
+
589
+ for (const tok of tokens) {
590
+ if (tok.kind === 'open' || tok.kind === 'close' || tok.kind === 'selfclose') {
591
+ const ln = ns.isWml(tok) ? tok.local : undefined;
592
+ const top = runStack[runStack.length - 1];
593
+
594
+ if (tok.kind === 'open') {
595
+ if (ln === 'r') {
596
+ runStack.push({ openStart: tok.start, rPr: '', rPrOpenStart: -1, pending: [] });
597
+ } else if (ln === 'rPr' && top) {
598
+ top.rPrOpenStart = tok.start;
599
+ } else if (ln === 't' && top) {
600
+ tOpenTag = tok.raw;
601
+ tContentStart = tok.end;
602
+ inT = true;
603
+ }
604
+ } else if (tok.kind === 'close') {
605
+ if (ln === 'rPr' && top && top.rPrOpenStart >= 0) {
606
+ top.rPr = xml.slice(top.rPrOpenStart, tok.end);
607
+ top.rPrOpenStart = -1;
608
+ } else if (ln === 't' && top && inT) {
609
+ top.pending.push({
610
+ runOpenStart: top.openStart,
611
+ runCloseEnd: -1,
612
+ rPr: top.rPr,
613
+ tOpenTag,
614
+ contentStart: tContentStart,
615
+ contentEnd: tok.start,
616
+ content: xml.slice(tContentStart, tok.start),
617
+ });
618
+ inT = false;
619
+ } else if (ln === 'r') {
620
+ const frame = runStack.pop();
621
+ if (frame) {
622
+ for (const slot of frame.pending) {
623
+ slot.runCloseEnd = tok.end;
624
+ slots.push(slot);
625
+ }
626
+ }
627
+ }
628
+ }
629
+ }
630
+ }
631
+
632
+ slots.sort((a, b) => a.contentStart - b.contentStart);
633
+ return slots;
634
+ }
635
+
636
+ // =============================================================================
637
+ // Comment bodies + threading
638
+ // =============================================================================
639
+
640
+ export interface ExtractedComment {
641
+ id: string;
642
+ author: string;
643
+ /** Full ISO date string as written, or '' when absent. */
644
+ date: string;
645
+ text: string;
646
+ /** Parent comment id for a reply, resolved from commentsExtended.xml. */
647
+ parentId?: string;
648
+ }
649
+
650
+ /** First attribute with the given local name, regardless of namespace prefix. */
651
+ function attrByLocal(token: XmlToken, local: string): string | undefined {
652
+ for (const attr of token.attrs ?? []) {
653
+ if (attr.local === local) return attr.value;
654
+ }
655
+ return undefined;
656
+ }
657
+
658
+ /**
659
+ * Extract comment bodies from `word/comments.xml` and reply threading from
660
+ * `word/commentsExtended.xml`. Structural elements are matched by local name
661
+ * in the WordprocessingML namespace, so a document that binds WML to a prefix
662
+ * other than `w` still yields its comments.
663
+ */
664
+ export function extractComments(zip: AdmZip): ExtractedComment[] {
665
+ const commentsXml = readPartText(zip, 'word/comments.xml');
666
+ if (commentsXml === null) return [];
667
+
668
+ const tokens = tokenizeXml(commentsXml);
669
+ const ns = resolveNamespaces(tokens);
670
+ const wmlLocal = (t: XmlToken) => (ns.isWml(t) ? t.local : undefined);
671
+
672
+ const comments: ExtractedComment[] = [];
673
+ const paraIdToCommentId = new Map<string, string>();
674
+
675
+ let current: ExtractedComment | null = null;
676
+ let textDepth = 0;
677
+
678
+ for (const tok of tokens) {
679
+ if (tok.kind === 'text' || tok.kind === 'cdata') {
680
+ if (current && textDepth > 0 && tok.text) current.text += tok.text;
681
+ continue;
682
+ }
683
+ const ln =
684
+ tok.kind === 'open' || tok.kind === 'close' || tok.kind === 'selfclose'
685
+ ? wmlLocal(tok)
686
+ : undefined;
687
+ if (ln === undefined) continue;
688
+
689
+ // Word stores the threading key (w14:paraId) on each comment paragraph;
690
+ // a reply's second paragraph is an empty self-closed <w:p/>, so read it on
691
+ // open and self-close alike.
692
+ if ((tok.kind === 'open' || tok.kind === 'selfclose') && ln === 'p' && current) {
693
+ const paraId = attrByLocal(tok, 'paraId');
694
+ if (paraId && current.id) paraIdToCommentId.set(paraId, current.id);
695
+ }
696
+
697
+ if (tok.kind === 'open') {
698
+ if (ln === 'comment') {
699
+ current = {
700
+ id: ns.wmlAttr(tok, 'id') ?? '',
701
+ author: ns.wmlAttr(tok, 'author') ?? 'Unknown',
702
+ date: ns.wmlAttr(tok, 'date') ?? '',
703
+ text: '',
704
+ };
705
+ } else if ((ln === 't' || ln === 'delText') && current) {
706
+ textDepth++;
707
+ }
708
+ } else if (tok.kind === 'close') {
709
+ if (ln === 'comment' && current) {
710
+ current.text = current.text.trim();
711
+ comments.push(current);
712
+ current = null;
713
+ textDepth = 0;
714
+ } else if ((ln === 't' || ln === 'delText') && textDepth > 0) {
715
+ textDepth--;
716
+ }
717
+ }
718
+ }
719
+
720
+ // Resolve reply links from commentsExtended.xml (absent for non-Word sources).
721
+ const extendedXml = readPartText(zip, 'word/commentsExtended.xml');
722
+ if (extendedXml && paraIdToCommentId.size > 0) {
723
+ const parentByCommentId = new Map<string, string>();
724
+ for (const tok of tokenizeXml(extendedXml)) {
725
+ if (tok.kind !== 'selfclose' && tok.kind !== 'open') continue;
726
+ if (tok.local !== 'commentEx') continue;
727
+ const paraId = attrByLocal(tok, 'paraId');
728
+ const parentParaId = attrByLocal(tok, 'paraIdParent');
729
+ if (!paraId || !parentParaId) continue;
730
+ const childId = paraIdToCommentId.get(paraId);
731
+ const parentId = paraIdToCommentId.get(parentParaId);
732
+ if (childId && parentId && childId !== parentId) {
733
+ parentByCommentId.set(childId, parentId);
734
+ }
735
+ }
736
+ for (const c of comments) {
737
+ const parent = parentByCommentId.get(c.id);
738
+ if (parent) c.parentId = parent;
739
+ }
740
+ }
741
+
742
+ return comments;
743
+ }
744
+
745
+ /** Parts that can carry comment ranges, in reading order. */
746
+ export const COMMENT_PARTS = ['word/document.xml', 'word/footnotes.xml', 'word/endnotes.xml'];
747
+
748
+ /**
749
+ * Comment ranges across every part that can hold them — not just
750
+ * `document.xml`, so a comment anchored in a footnote or endnote is found
751
+ * rather than silently lost. Positions are offsets into the concatenated
752
+ * `fullDocText`.
753
+ */
754
+ export function buildCommentAnchorModel(zip: AdmZip): { fullDocText: string; comments: CommentRange[] } {
755
+ let fullDocText = '';
756
+ const comments: CommentRange[] = [];
757
+ for (const part of COMMENT_PARTS) {
758
+ const xml = readPartText(zip, part);
759
+ if (xml === null) continue;
760
+ const model = buildDocTextModel(xml);
761
+ const offset = fullDocText.length;
762
+ fullDocText += model.text;
763
+ for (const c of model.comments) {
764
+ comments.push({ id: c.id, anchor: c.anchor, start: c.start + offset, end: c.end + offset, isEmpty: c.isEmpty });
765
+ }
766
+ }
767
+ return { fullDocText, comments };
768
+ }