docrev 0.9.11 → 0.9.14

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