docrev 0.9.5 → 0.9.7

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 (99) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dev_notes/bug_repro_comment_parser.md +71 -0
  3. package/dist/lib/anchor-match.d.ts +41 -0
  4. package/dist/lib/anchor-match.d.ts.map +1 -0
  5. package/dist/lib/anchor-match.js +192 -0
  6. package/dist/lib/anchor-match.js.map +1 -0
  7. package/dist/lib/annotations.d.ts.map +1 -1
  8. package/dist/lib/annotations.js +8 -5
  9. package/dist/lib/annotations.js.map +1 -1
  10. package/dist/lib/commands/file-ops.d.ts +11 -0
  11. package/dist/lib/commands/file-ops.d.ts.map +1 -0
  12. package/dist/lib/commands/file-ops.js +301 -0
  13. package/dist/lib/commands/file-ops.js.map +1 -0
  14. package/dist/lib/commands/index.d.ts +10 -1
  15. package/dist/lib/commands/index.d.ts.map +1 -1
  16. package/dist/lib/commands/index.js +19 -1
  17. package/dist/lib/commands/index.js.map +1 -1
  18. package/dist/lib/commands/merge-resolve.d.ts +12 -0
  19. package/dist/lib/commands/merge-resolve.d.ts.map +1 -0
  20. package/dist/lib/commands/merge-resolve.js +318 -0
  21. package/dist/lib/commands/merge-resolve.js.map +1 -0
  22. package/dist/lib/commands/preview.d.ts +11 -0
  23. package/dist/lib/commands/preview.d.ts.map +1 -0
  24. package/dist/lib/commands/preview.js +138 -0
  25. package/dist/lib/commands/preview.js.map +1 -0
  26. package/dist/lib/commands/project-info.d.ts +11 -0
  27. package/dist/lib/commands/project-info.d.ts.map +1 -0
  28. package/dist/lib/commands/project-info.js +187 -0
  29. package/dist/lib/commands/project-info.js.map +1 -0
  30. package/dist/lib/commands/quality.d.ts +11 -0
  31. package/dist/lib/commands/quality.d.ts.map +1 -0
  32. package/dist/lib/commands/quality.js +384 -0
  33. package/dist/lib/commands/quality.js.map +1 -0
  34. package/dist/lib/commands/section-boundaries.d.ts +22 -0
  35. package/dist/lib/commands/section-boundaries.d.ts.map +1 -0
  36. package/dist/lib/commands/section-boundaries.js +53 -0
  37. package/dist/lib/commands/section-boundaries.js.map +1 -0
  38. package/dist/lib/commands/sections.d.ts +3 -2
  39. package/dist/lib/commands/sections.d.ts.map +1 -1
  40. package/dist/lib/commands/sections.js +4 -736
  41. package/dist/lib/commands/sections.js.map +1 -1
  42. package/dist/lib/commands/sync.d.ts +11 -0
  43. package/dist/lib/commands/sync.d.ts.map +1 -0
  44. package/dist/lib/commands/sync.js +576 -0
  45. package/dist/lib/commands/sync.js.map +1 -0
  46. package/dist/lib/commands/text-ops.d.ts +11 -0
  47. package/dist/lib/commands/text-ops.d.ts.map +1 -0
  48. package/dist/lib/commands/text-ops.js +357 -0
  49. package/dist/lib/commands/text-ops.js.map +1 -0
  50. package/dist/lib/commands/utilities.d.ts +2 -4
  51. package/dist/lib/commands/utilities.d.ts.map +1 -1
  52. package/dist/lib/commands/utilities.js +3 -1572
  53. package/dist/lib/commands/utilities.js.map +1 -1
  54. package/dist/lib/commands/verify-anchors.d.ts +17 -0
  55. package/dist/lib/commands/verify-anchors.d.ts.map +1 -0
  56. package/dist/lib/commands/verify-anchors.js +215 -0
  57. package/dist/lib/commands/verify-anchors.js.map +1 -0
  58. package/dist/lib/commands/word-tools.d.ts +11 -0
  59. package/dist/lib/commands/word-tools.d.ts.map +1 -0
  60. package/dist/lib/commands/word-tools.js +272 -0
  61. package/dist/lib/commands/word-tools.js.map +1 -0
  62. package/dist/lib/diff-engine.d.ts +25 -0
  63. package/dist/lib/diff-engine.d.ts.map +1 -0
  64. package/dist/lib/diff-engine.js +354 -0
  65. package/dist/lib/diff-engine.js.map +1 -0
  66. package/dist/lib/import.d.ts +44 -118
  67. package/dist/lib/import.d.ts.map +1 -1
  68. package/dist/lib/import.js +25 -1173
  69. package/dist/lib/import.js.map +1 -1
  70. package/dist/lib/restore-references.d.ts +35 -0
  71. package/dist/lib/restore-references.d.ts.map +1 -0
  72. package/dist/lib/restore-references.js +188 -0
  73. package/dist/lib/restore-references.js.map +1 -0
  74. package/dist/lib/word-extraction.d.ts +100 -0
  75. package/dist/lib/word-extraction.d.ts.map +1 -0
  76. package/dist/lib/word-extraction.js +594 -0
  77. package/dist/lib/word-extraction.js.map +1 -0
  78. package/lib/anchor-match.ts +238 -0
  79. package/lib/annotations.ts +9 -5
  80. package/lib/commands/file-ops.ts +372 -0
  81. package/lib/commands/index.ts +27 -0
  82. package/lib/commands/merge-resolve.ts +378 -0
  83. package/lib/commands/preview.ts +178 -0
  84. package/lib/commands/project-info.ts +244 -0
  85. package/lib/commands/quality.ts +517 -0
  86. package/lib/commands/section-boundaries.ts +72 -0
  87. package/lib/commands/sections.ts +3 -870
  88. package/lib/commands/sync.ts +701 -0
  89. package/lib/commands/text-ops.ts +449 -0
  90. package/lib/commands/utilities.ts +62 -2043
  91. package/lib/commands/verify-anchors.ts +261 -0
  92. package/lib/commands/word-tools.ts +340 -0
  93. package/lib/diff-engine.ts +465 -0
  94. package/lib/import.ts +108 -1504
  95. package/lib/restore-references.ts +240 -0
  96. package/lib/word-extraction.ts +759 -0
  97. package/package.json +1 -1
  98. package/skill/REFERENCE.md +29 -2
  99. package/skill/SKILL.md +12 -2
@@ -0,0 +1,759 @@
1
+ /**
2
+ * Word document data extraction - raw extraction from .docx files
3
+ */
4
+
5
+ import * as fs from 'fs';
6
+ import * as path from 'path';
7
+ import { exec } from 'child_process';
8
+ import { promisify } from 'util';
9
+
10
+ const execAsync = promisify(exec);
11
+
12
+ // ============================================
13
+ // Type Definitions
14
+ // ============================================
15
+
16
+ export interface WordComment {
17
+ id: string;
18
+ author: string;
19
+ date: string;
20
+ text: string;
21
+ }
22
+
23
+ export interface TextNode {
24
+ xmlStart: number;
25
+ xmlEnd: number;
26
+ textStart: number;
27
+ textEnd: number;
28
+ text: string;
29
+ }
30
+
31
+ export interface CommentAnchorData {
32
+ anchor: string;
33
+ before: string;
34
+ after: string;
35
+ docPosition: number;
36
+ docLength: number;
37
+ isEmpty: boolean;
38
+ }
39
+
40
+ export interface CommentAnchorsResult {
41
+ anchors: Map<string, CommentAnchorData>;
42
+ fullDocText: string;
43
+ }
44
+
45
+ export interface DocxHeading {
46
+ /** Heading style name from `<w:pStyle>`, e.g. "Heading1" */
47
+ style: string;
48
+ /** Heading depth: 1, 2, 3, ... (parsed from style name; 0 if unknown) */
49
+ level: number;
50
+ /** Concatenated text content of the heading paragraph */
51
+ text: string;
52
+ /** Position in fullDocText (same coordinate system as CommentAnchorData.docPosition) */
53
+ docPosition: number;
54
+ }
55
+
56
+ export interface WordTable {
57
+ markdown: string;
58
+ rowCount: number;
59
+ colCount: number;
60
+ }
61
+
62
+ export interface ParsedRow {
63
+ cells: string[];
64
+ colSpans: number[];
65
+ }
66
+
67
+ export interface ExtractFromWordOptions {
68
+ mediaDir?: string;
69
+ skipMediaExtraction?: boolean;
70
+ }
71
+
72
+ export interface ExtractMessage {
73
+ type: 'info' | 'warning';
74
+ message: string;
75
+ }
76
+
77
+ export interface ExtractFromWordResult {
78
+ text: string;
79
+ comments: WordComment[];
80
+ anchors: Map<string, CommentAnchorData>;
81
+ messages: ExtractMessage[];
82
+ extractedMedia: string[];
83
+ tables: WordTable[];
84
+ hasTrackChanges: boolean;
85
+ trackChangeStats: { insertions: number; deletions: number };
86
+ }
87
+
88
+ // ============================================
89
+ // Functions
90
+ // ============================================
91
+
92
+ /**
93
+ * Extract comments directly from Word docx comments.xml
94
+ */
95
+ export async function extractWordComments(docxPath: string): Promise<WordComment[]> {
96
+ const AdmZip = (await import('adm-zip')).default;
97
+ const { parseStringPromise } = await import('xml2js');
98
+
99
+ const comments: WordComment[] = [];
100
+
101
+ // Validate file exists
102
+ if (!fs.existsSync(docxPath)) {
103
+ throw new Error(`File not found: ${docxPath}`);
104
+ }
105
+
106
+ try {
107
+ let zip;
108
+ try {
109
+ zip = new AdmZip(docxPath);
110
+ } catch (err: any) {
111
+ throw new Error(`Invalid Word document (not a valid .docx file): ${err.message}`);
112
+ }
113
+
114
+ const commentsEntry = zip.getEntry('word/comments.xml');
115
+
116
+ if (!commentsEntry) {
117
+ return comments;
118
+ }
119
+
120
+ let commentsXml;
121
+ try {
122
+ commentsXml = commentsEntry.getData().toString('utf8');
123
+ } catch (err: any) {
124
+ throw new Error(`Failed to read comments from document: ${err.message}`);
125
+ }
126
+
127
+ const parsed = await parseStringPromise(commentsXml, { explicitArray: false });
128
+
129
+ const ns = 'w:';
130
+ const commentsRoot = parsed['w:comments'];
131
+ if (!commentsRoot || !commentsRoot['w:comment']) {
132
+ return comments;
133
+ }
134
+
135
+ // Ensure it's an array
136
+ const commentNodes = Array.isArray(commentsRoot['w:comment'])
137
+ ? commentsRoot['w:comment']
138
+ : [commentsRoot['w:comment']];
139
+
140
+ for (const comment of commentNodes) {
141
+ const id = comment.$?.['w:id'] || '';
142
+ const author = comment.$?.['w:author'] || 'Unknown';
143
+ const date = comment.$?.['w:date'] || '';
144
+
145
+ // Extract text from nested w:p/w:r/w:t elements
146
+ let text = '';
147
+ const extractText = (node: any): void => {
148
+ if (!node) return;
149
+ if (typeof node === 'string') {
150
+ text += node;
151
+ return;
152
+ }
153
+ if (node['w:t']) {
154
+ const t = node['w:t'];
155
+ text += typeof t === 'string' ? t : (t._ || t);
156
+ }
157
+ if (node['w:r']) {
158
+ const runs = Array.isArray(node['w:r']) ? node['w:r'] : [node['w:r']];
159
+ runs.forEach(extractText);
160
+ }
161
+ if (node['w:p']) {
162
+ const paras = Array.isArray(node['w:p']) ? node['w:p'] : [node['w:p']];
163
+ paras.forEach(extractText);
164
+ }
165
+ };
166
+ extractText(comment);
167
+
168
+ comments.push({ id, author, date: date.slice(0, 10), text: text.trim() });
169
+ }
170
+ } catch (err: any) {
171
+ // Re-throw with more context if it's already an Error we created
172
+ if (err.message.includes('Invalid Word document') || err.message.includes('File not found')) {
173
+ throw err;
174
+ }
175
+ throw new Error(`Error extracting comments from ${path.basename(docxPath)}: ${err.message}`);
176
+ }
177
+
178
+ return comments;
179
+ }
180
+
181
+ /**
182
+ * Extract comment anchor texts from document.xml with surrounding context
183
+ * Returns map of comment ID -> {anchor, before, after, docPosition, isEmpty} for better matching
184
+ * Also returns fullDocText for section boundary matching
185
+ */
186
+ export async function extractCommentAnchors(docxPath: string): Promise<CommentAnchorsResult> {
187
+ const AdmZip = (await import('adm-zip')).default;
188
+ const anchors = new Map<string, CommentAnchorData>();
189
+ let fullDocText = '';
190
+
191
+ try {
192
+ const zip = new AdmZip(docxPath);
193
+ const docEntry = zip.getEntry('word/document.xml');
194
+
195
+ if (!docEntry) {
196
+ return { anchors, fullDocText };
197
+ }
198
+
199
+ const docXml = docEntry.getData().toString('utf8');
200
+
201
+ // ========================================
202
+ // STEP 1: Build text position mapping
203
+ // ========================================
204
+ const textNodePattern = /<w:t[^>]*>([^<]*)<\/w:t>/g;
205
+ const textNodes: TextNode[] = [];
206
+ let textPosition = 0;
207
+ let nodeMatch;
208
+
209
+ while ((nodeMatch = textNodePattern.exec(docXml)) !== null) {
210
+ const rawText = nodeMatch[1] ?? '';
211
+ const decodedText = decodeXmlEntities(rawText);
212
+ textNodes.push({
213
+ xmlStart: nodeMatch.index,
214
+ xmlEnd: nodeMatch.index + nodeMatch[0].length,
215
+ textStart: textPosition,
216
+ textEnd: textPosition + decodedText.length,
217
+ text: decodedText
218
+ });
219
+ textPosition += decodedText.length;
220
+ }
221
+
222
+ fullDocText = textNodes.map(n => n.text).join('');
223
+
224
+ // Helper: convert XML position to text position
225
+ function xmlPosToTextPos(xmlPos: number): number {
226
+ for (let i = 0; i < textNodes.length; i++) {
227
+ const node = textNodes[i];
228
+ if (!node) continue;
229
+ if (xmlPos >= node.xmlStart && xmlPos < node.xmlEnd) {
230
+ return node.textStart;
231
+ }
232
+ if (xmlPos < node.xmlStart) {
233
+ return node.textStart;
234
+ }
235
+ }
236
+ const lastNode = textNodes[textNodes.length - 1];
237
+ return lastNode ? lastNode.textEnd : 0;
238
+ }
239
+
240
+ // Helper: extract context before a position
241
+ function getContextBefore(position: number, maxLength: number = 150): string {
242
+ const beforeText = fullDocText.slice(Math.max(0, position - maxLength), position);
243
+ const sentenceStart = beforeText.search(/[.!?]\s+[A-Z][^.!?]*$/);
244
+ return sentenceStart >= 0
245
+ ? beforeText.slice(sentenceStart + 2).trim()
246
+ : beforeText.slice(-80).trim();
247
+ }
248
+
249
+ // Helper: extract context after a position
250
+ function getContextAfter(position: number, maxLength: number = 150): string {
251
+ const afterText = fullDocText.slice(position, position + maxLength);
252
+ const sentenceEnd = afterText.search(/[.!?]\s/);
253
+ return sentenceEnd >= 0
254
+ ? afterText.slice(0, sentenceEnd + 1).trim()
255
+ : afterText.slice(0, 80).trim();
256
+ }
257
+
258
+ // ========================================
259
+ // STEP 2: Collect all start/end markers separately
260
+ // ========================================
261
+ const startPattern = /<w:commentRangeStart[^>]*w:id="(\d+)"[^>]*\/?>/g;
262
+ const endPattern = /<w:commentRangeEnd[^>]*w:id="(\d+)"[^>]*\/?>/g;
263
+
264
+ const starts = new Map<string, number>(); // id -> position after start tag
265
+ const ends = new Map<string, number>(); // id -> position before end tag
266
+
267
+ let match;
268
+ while ((match = startPattern.exec(docXml)) !== null) {
269
+ const id = match[1];
270
+ if (!starts.has(id)) {
271
+ starts.set(id, match.index + match[0].length);
272
+ }
273
+ }
274
+
275
+ while ((match = endPattern.exec(docXml)) !== null) {
276
+ const id = match[1];
277
+ if (!ends.has(id)) {
278
+ ends.set(id, match.index);
279
+ }
280
+ }
281
+
282
+ // ========================================
283
+ // STEP 3: Process each comment range by ID
284
+ // ========================================
285
+ for (const [id, startXmlPos] of starts) {
286
+ const endXmlPos = ends.get(id);
287
+
288
+ // Missing end marker - skip with warning
289
+ if (endXmlPos === undefined) {
290
+ console.warn(`Comment ${id}: missing end marker`);
291
+ continue;
292
+ }
293
+
294
+ // Calculate text position
295
+ const docPosition = xmlPosToTextPos(startXmlPos);
296
+
297
+ // Handle empty or inverted ranges
298
+ if (endXmlPos <= startXmlPos) {
299
+ anchors.set(id, {
300
+ anchor: '',
301
+ before: getContextBefore(docPosition),
302
+ after: getContextAfter(docPosition),
303
+ docPosition,
304
+ docLength: fullDocText.length,
305
+ isEmpty: true
306
+ });
307
+ continue;
308
+ }
309
+
310
+ // Extract XML segment between markers
311
+ const segment = docXml.slice(startXmlPos, endXmlPos);
312
+
313
+ // Extract text from w:t (regular) AND w:delText (deleted text in track changes)
314
+ const textInRangePattern = /<w:t[^>]*>([^<]*)<\/w:t>|<w:delText[^>]*>([^<]*)<\/w:delText>/g;
315
+ let anchorText = '';
316
+ let tm;
317
+ while ((tm = textInRangePattern.exec(segment)) !== null) {
318
+ anchorText += tm[1] || tm[2] || '';
319
+ }
320
+ anchorText = decodeXmlEntities(anchorText);
321
+
322
+ // Get context
323
+ const anchorLength = anchorText.length;
324
+ const before = getContextBefore(docPosition);
325
+ const after = getContextAfter(docPosition + anchorLength);
326
+
327
+ // ALWAYS add entry (even if anchor is empty)
328
+ anchors.set(id, {
329
+ anchor: anchorText.trim(),
330
+ before,
331
+ after,
332
+ docPosition,
333
+ docLength: fullDocText.length,
334
+ isEmpty: !anchorText.trim()
335
+ });
336
+ }
337
+ } catch (err: any) {
338
+ console.error('Error extracting comment anchors:', err.message);
339
+ return { anchors, fullDocText: '' };
340
+ }
341
+
342
+ return { anchors, fullDocText };
343
+ }
344
+
345
+ /**
346
+ * Extract heading paragraphs from a docx, with their text positions in the
347
+ * same coordinate system as `extractCommentAnchors`'s `fullDocText` and
348
+ * `CommentAnchorData.docPosition`.
349
+ *
350
+ * Headings are paragraphs whose `<w:pStyle>` is a Heading style. Reading
351
+ * styles directly is more reliable than keyword-matching the concatenated
352
+ * body text — there, paragraph boundaries are gone, so the literal string
353
+ * "Methods" can appear inside prose ("results across countries") and the
354
+ * structured-abstract label "Methods:" loses its colon when text runs are
355
+ * concatenated.
356
+ */
357
+ export async function extractHeadings(docxPath: string): Promise<DocxHeading[]> {
358
+ const AdmZip = (await import('adm-zip')).default;
359
+
360
+ if (!fs.existsSync(docxPath)) {
361
+ throw new Error(`File not found: ${docxPath}`);
362
+ }
363
+
364
+ const zip = new AdmZip(docxPath);
365
+ const docEntry = zip.getEntry('word/document.xml');
366
+ if (!docEntry) return [];
367
+ const xml = docEntry.getData().toString('utf8');
368
+
369
+ // Build the same xml-pos → text-pos mapping that extractCommentAnchors does
370
+ const textNodePattern = /<w:t[^>]*>([^<]*)<\/w:t>/g;
371
+ const nodes: Array<{ xmlStart: number; xmlEnd: number; textStart: number; textEnd: number }> = [];
372
+ let textPos = 0;
373
+ let m;
374
+ while ((m = textNodePattern.exec(xml)) !== null) {
375
+ const decoded = decodeXmlEntities(m[1] ?? '');
376
+ nodes.push({
377
+ xmlStart: m.index,
378
+ xmlEnd: m.index + m[0].length,
379
+ textStart: textPos,
380
+ textEnd: textPos + decoded.length,
381
+ });
382
+ textPos += decoded.length;
383
+ }
384
+
385
+ function xmlToTextPos(xmlPos: number): number {
386
+ for (const n of nodes) {
387
+ if (xmlPos >= n.xmlStart && xmlPos < n.xmlEnd) return n.textStart;
388
+ if (xmlPos < n.xmlStart) return n.textStart;
389
+ }
390
+ return nodes.length ? nodes[nodes.length - 1].textEnd : 0;
391
+ }
392
+
393
+ const headings: DocxHeading[] = [];
394
+ const paraPattern = /<w:p\b[^>]*>([\s\S]*?)<\/w:p>/g;
395
+ let pm;
396
+ while ((pm = paraPattern.exec(xml)) !== null) {
397
+ const inner = pm[1];
398
+ const styleMatch = inner.match(/<w:pStyle[^>]*w:val="([^"]+)"/);
399
+ if (!styleMatch) continue;
400
+ const style = styleMatch[1];
401
+ if (!/heading/i.test(style)) continue;
402
+
403
+ // Concatenate text runs; include w:delText so a heading inside a tracked
404
+ // deletion is still surfaced (verifying anchors against an original draft)
405
+ const textInRange = /<w:t[^>]*>([^<]*)<\/w:t>|<w:delText[^>]*>([^<]*)<\/w:delText>/g;
406
+ let txt = '';
407
+ let tm;
408
+ while ((tm = textInRange.exec(inner)) !== null) {
409
+ txt += decodeXmlEntities(tm[1] || tm[2] || '');
410
+ }
411
+ const trimmed = txt.trim();
412
+ if (!trimmed) continue;
413
+
414
+ const levelMatch = style.match(/(\d+)/);
415
+ const level = levelMatch ? parseInt(levelMatch[1], 10) : 0;
416
+ headings.push({
417
+ style,
418
+ level,
419
+ text: trimmed,
420
+ docPosition: xmlToTextPos(pm.index),
421
+ });
422
+ }
423
+
424
+ return headings;
425
+ }
426
+
427
+ /**
428
+ * Decode XML entities in text
429
+ */
430
+ function decodeXmlEntities(text: string): string {
431
+ return text
432
+ .replace(/&amp;/g, '&')
433
+ .replace(/&lt;/g, '<')
434
+ .replace(/&gt;/g, '>')
435
+ .replace(/&quot;/g, '"')
436
+ .replace(/&apos;/g, "'")
437
+ .replace(/&#(\d+);/g, (_, code) => String.fromCharCode(parseInt(code, 10)))
438
+ .replace(/&#x([0-9a-fA-F]+);/g, (_, code) => String.fromCharCode(parseInt(code, 16)));
439
+ }
440
+
441
+ /**
442
+ * Extract text content from a Word XML cell
443
+ */
444
+ function extractCellText(cellXml: string): string {
445
+ const parts: string[] = [];
446
+
447
+ // Check for OMML math - replace with [math] placeholder
448
+ if (cellXml.includes('<m:oMath')) {
449
+ // Try to extract the text representation of math
450
+ const mathTextMatches = cellXml.match(/<m:t>([^<]*)<\/m:t>/g) || [];
451
+ if (mathTextMatches.length > 0) {
452
+ const mathText = mathTextMatches.map((t) => t.replace(/<[^>]+>/g, '')).join('');
453
+ parts.push(mathText);
454
+ } else {
455
+ parts.push('[math]');
456
+ }
457
+ }
458
+
459
+ // Extract regular text from w:t elements
460
+ const textMatches = cellXml.match(/<w:t[^>]*>([^<]*)<\/w:t>/g) || [];
461
+ for (const match of textMatches) {
462
+ const text = match.replace(/<[^>]+>/g, '');
463
+ if (text) {
464
+ parts.push(text);
465
+ }
466
+ }
467
+
468
+ let result = parts.join('').trim();
469
+ result = decodeXmlEntities(result);
470
+
471
+ // Escape pipe characters in cell content (would break table)
472
+ result = result.replace(/\|/g, '\\|');
473
+
474
+ return result;
475
+ }
476
+
477
+ /**
478
+ * Parse a table row, handling merged cells (gridSpan)
479
+ */
480
+ function parseTableRow(rowXml: string, expectedCols: number): ParsedRow {
481
+ // Match cells - handle both <w:tc> and <w:tc ...>
482
+ const cellMatches = rowXml.match(/<w:tc(?:\s[^>]*)?>[\s\S]*?<\/w:tc>/g) || [];
483
+ const cells: string[] = [];
484
+ const colSpans: number[] = [];
485
+
486
+ for (const cellXml of cellMatches) {
487
+ // Check for horizontal merge (gridSpan)
488
+ const gridSpanMatch = cellXml.match(/<w:gridSpan\s+w:val="(\d+)"/);
489
+ const span = gridSpanMatch ? parseInt(gridSpanMatch[1], 10) : 1;
490
+
491
+ // Check for vertical merge continuation (vMerge without restart)
492
+ // If vMerge is present without w:val="restart", it's a continuation - use empty
493
+ const vMergeMatch = cellXml.match(/<w:vMerge(?:\s+w:val="([^"]+)")?/);
494
+ const isVMergeContinuation = vMergeMatch && vMergeMatch[1] !== 'restart';
495
+
496
+ const cellText = isVMergeContinuation ? '' : extractCellText(cellXml);
497
+
498
+ // Add the cell content
499
+ cells.push(cellText);
500
+ colSpans.push(span);
501
+
502
+ // For gridSpan > 1, add empty cells to maintain column alignment
503
+ for (let i = 1; i < span; i++) {
504
+ cells.push('');
505
+ colSpans.push(0); // 0 indicates this is a spanned cell
506
+ }
507
+ }
508
+
509
+ return { cells, colSpans };
510
+ }
511
+
512
+ /**
513
+ * Determine table grid column count from table XML
514
+ */
515
+ function getTableGridCols(tableXml: string): number {
516
+ // Try to get from tblGrid
517
+ const gridColMatches = tableXml.match(/<w:gridCol/g) || [];
518
+ if (gridColMatches.length > 0) {
519
+ return gridColMatches.length;
520
+ }
521
+
522
+ // Fallback: count max cells in any row
523
+ const rowMatches = tableXml.match(/<w:tr[\s\S]*?<\/w:tr>/g) || [];
524
+ let maxCols = 0;
525
+ for (const rowXml of rowMatches) {
526
+ const { cells } = parseTableRow(rowXml, 0);
527
+ maxCols = Math.max(maxCols, cells.length);
528
+ }
529
+ return maxCols;
530
+ }
531
+
532
+ /**
533
+ * Extract tables directly from Word document XML and convert to markdown pipe tables
534
+ */
535
+ export async function extractWordTables(docxPath: string): Promise<WordTable[]> {
536
+ const AdmZip = (await import('adm-zip')).default;
537
+ const tables: WordTable[] = [];
538
+
539
+ try {
540
+ const zip = new AdmZip(docxPath);
541
+ const docEntry = zip.getEntry('word/document.xml');
542
+
543
+ if (!docEntry) {
544
+ return tables;
545
+ }
546
+
547
+ const xml = docEntry.getData().toString('utf8');
548
+
549
+ // Find all table elements
550
+ const tableMatches = xml.match(/<w:tbl>[\s\S]*?<\/w:tbl>/g) || [];
551
+
552
+ for (const tableXml of tableMatches) {
553
+ // Determine expected column count from grid
554
+ const expectedCols = getTableGridCols(tableXml);
555
+
556
+ // Extract rows
557
+ const rowMatches = tableXml.match(/<w:tr[\s\S]*?<\/w:tr>/g) || [];
558
+ const rows: string[][] = [];
559
+
560
+ for (const rowXml of rowMatches) {
561
+ const { cells } = parseTableRow(rowXml, expectedCols);
562
+ if (cells.length > 0) {
563
+ rows.push(cells);
564
+ }
565
+ }
566
+
567
+ if (rows.length > 0) {
568
+ // Convert to markdown pipe table
569
+ const markdown = convertRowsToMarkdownTable(rows);
570
+ tables.push({ markdown, rowCount: rows.length, colCount: expectedCols || rows[0]?.length || 0 });
571
+ }
572
+ }
573
+ } catch (err: any) {
574
+ console.error('Error extracting tables from Word:', err.message);
575
+ }
576
+
577
+ return tables;
578
+ }
579
+
580
+ /**
581
+ * Convert array of rows (each row is array of cell strings) to markdown pipe table
582
+ */
583
+ function convertRowsToMarkdownTable(rows: string[][]): string {
584
+ if (rows.length === 0) return '';
585
+
586
+ // Normalize column count (use max across all rows)
587
+ const colCount = Math.max(...rows.map((r) => r.length));
588
+
589
+ // Pad rows to have consistent column count
590
+ const normalizedRows = rows.map((row) => {
591
+ while (row.length < colCount) {
592
+ row.push('');
593
+ }
594
+ return row;
595
+ });
596
+
597
+ // Build markdown table
598
+ const lines: string[] = [];
599
+
600
+ // Header row
601
+ const header = normalizedRows[0];
602
+ lines.push('| ' + header.join(' | ') + ' |');
603
+
604
+ // Separator row
605
+ lines.push('|' + header.map(() => '---').join('|') + '|');
606
+
607
+ // Data rows
608
+ for (let i = 1; i < normalizedRows.length; i++) {
609
+ lines.push('| ' + normalizedRows[i].join(' | ') + ' |');
610
+ }
611
+
612
+ return lines.join('\n');
613
+ }
614
+
615
+ /**
616
+ * Extract text from Word document using pandoc with track changes preserved
617
+ */
618
+ export async function extractFromWord(
619
+ docxPath: string,
620
+ options: ExtractFromWordOptions = {}
621
+ ): Promise<ExtractFromWordResult> {
622
+ let text: string;
623
+ let messages: ExtractMessage[] = [];
624
+ let extractedMedia: string[] = [];
625
+ let hasTrackChanges = false;
626
+ let trackChangeStats = { insertions: 0, deletions: 0 };
627
+
628
+ // Determine media extraction directory
629
+ const docxDir = path.dirname(docxPath);
630
+ const mediaDir = options.mediaDir || path.join(docxDir, 'media');
631
+
632
+ // Skip media extraction if figures already exist (e.g., when re-importing with existing source)
633
+ const skipMediaExtraction = options.skipMediaExtraction || false;
634
+
635
+ // Extract tables directly from Word XML (reliable, no heuristics)
636
+ const wordTables = await extractWordTables(docxPath);
637
+
638
+ // Try pandoc first with --track-changes=all to preserve reviewer edits
639
+ try {
640
+ // Build pandoc command
641
+ let pandocCmd = `pandoc "${docxPath}" -t markdown --wrap=none --track-changes=all`;
642
+ if (!skipMediaExtraction) {
643
+ pandocCmd += ` --extract-media="${mediaDir}"`;
644
+ }
645
+
646
+ const { stdout } = await execAsync(pandocCmd, { maxBuffer: 50 * 1024 * 1024 });
647
+ text = stdout;
648
+
649
+ // Convert pandoc's track change format to CriticMarkup
650
+ const origLength = text.length;
651
+
652
+ // Use a more robust pattern that handles nested content
653
+ text = text.replace(/\[([^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*)\]\{\.insertion[^}]*\}/g, (match, content) => {
654
+ if (content.trim()) {
655
+ trackChangeStats.insertions++;
656
+ return `{++${content}++}`;
657
+ }
658
+ return ''; // Empty insertions are removed
659
+ });
660
+
661
+ text = text.replace(/\[([^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*)\]\{\.deletion[^}]*\}/g, (match, content) => {
662
+ if (content.trim()) {
663
+ trackChangeStats.deletions++;
664
+ return `{--${content}--}`;
665
+ }
666
+ return ''; // Empty deletions are removed
667
+ });
668
+
669
+ // Handle any remaining pandoc track change patterns
670
+ let prevText;
671
+ do {
672
+ prevText = text;
673
+ text = text.replace(/\[([^\]]*)\]\{\.insertion[^}]*\}/g, (match, content) => {
674
+ if (content.trim()) {
675
+ trackChangeStats.insertions++;
676
+ return `{++${content}++}`;
677
+ }
678
+ return '';
679
+ });
680
+ text = text.replace(/\[([^\]]*)\]\{\.deletion[^}]*\}/g, (match, content) => {
681
+ if (content.trim()) {
682
+ trackChangeStats.deletions++;
683
+ return `{--${content}--}`;
684
+ }
685
+ return '';
686
+ });
687
+ } while (text !== prevText);
688
+
689
+ // Handle pandoc comment patterns - remove comment text from body
690
+ text = text.replace(/\[[^\]]*\]\{\.comment-start[^}]*\}/g, '');
691
+ text = text.replace(/\[\]\{\.comment-end[^}]*\}/g, '');
692
+
693
+ // Also handle {.mark} spans
694
+ text = text.replace(/\[([^\]]*)\]\{\.mark\}/g, '$1');
695
+
696
+ hasTrackChanges = trackChangeStats.insertions > 0 || trackChangeStats.deletions > 0;
697
+
698
+ if (hasTrackChanges) {
699
+ messages.push({
700
+ type: 'info',
701
+ message: `Found ${trackChangeStats.insertions} insertion(s) and ${trackChangeStats.deletions} deletion(s) from track changes`
702
+ });
703
+ }
704
+
705
+ // Find extracted media files
706
+ const mediaSubdir = path.join(mediaDir, 'media');
707
+ if (fs.existsSync(mediaSubdir)) {
708
+ extractedMedia = fs.readdirSync(mediaSubdir)
709
+ .filter(f => /\.(png|jpg|jpeg|gif|svg|emf|wmf|tiff?)$/i.test(f))
710
+ .map(f => path.join(mediaSubdir, f));
711
+
712
+ if (extractedMedia.length > 0) {
713
+ messages.push({
714
+ type: 'info',
715
+ message: `Extracted ${extractedMedia.length} image(s) to ${mediaSubdir}`
716
+ });
717
+ }
718
+ }
719
+ } catch (pandocErr: any) {
720
+ // Pandoc not available — use XML-based extraction with track change support
721
+ const { extractPlainTextWithTrackChanges } = await import('./word.js');
722
+ const { getInstallInstructions } = await import('./dependencies.js');
723
+ const installCmd = getInstallInstructions('pandoc');
724
+
725
+ const xmlResult = await extractPlainTextWithTrackChanges(docxPath);
726
+ text = xmlResult.text;
727
+ hasTrackChanges = xmlResult.hasTrackChanges;
728
+ trackChangeStats = xmlResult.stats;
729
+
730
+ if (hasTrackChanges) {
731
+ messages.push({
732
+ type: 'warning',
733
+ message: `Pandoc not installed. Using built-in XML extractor (${trackChangeStats.insertions} insertions, ${trackChangeStats.deletions} deletions preserved). Formatting may differ. Install pandoc for best results: ${installCmd}`
734
+ });
735
+ } else {
736
+ messages.push({
737
+ type: 'warning',
738
+ message: `Pandoc not installed. Using built-in XML extractor (no track changes found). Install pandoc for better formatting: ${installCmd}`
739
+ });
740
+ }
741
+ }
742
+
743
+ // Extract comments directly from docx XML
744
+ const comments = await extractWordComments(docxPath);
745
+
746
+ // Extract comment anchor texts
747
+ const { anchors } = await extractCommentAnchors(docxPath);
748
+
749
+ return {
750
+ text,
751
+ comments,
752
+ anchors,
753
+ messages,
754
+ extractedMedia,
755
+ tables: wordTables,
756
+ hasTrackChanges,
757
+ trackChangeStats,
758
+ };
759
+ }