docrev 0.11.1 → 0.11.2

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 (116) hide show
  1. package/.gitattributes +1 -1
  2. package/CHANGELOG.md +202 -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/build.d.ts +28 -0
  11. package/dist/lib/build.d.ts.map +1 -1
  12. package/dist/lib/build.js +81 -4
  13. package/dist/lib/build.js.map +1 -1
  14. package/dist/lib/commands/utilities.js +164 -164
  15. package/dist/lib/commands/word-tools.js +8 -8
  16. package/dist/lib/grammar.js +3 -3
  17. package/dist/lib/macro-filter.lua +201 -201
  18. package/dist/lib/pdf-comments.js +44 -44
  19. package/dist/lib/plugins.js +57 -57
  20. package/dist/lib/pptx-color-filter.lua +37 -37
  21. package/dist/lib/pptx-themes.js +115 -115
  22. package/dist/lib/schema.d.ts.map +1 -1
  23. package/dist/lib/schema.js +8 -2
  24. package/dist/lib/schema.js.map +1 -1
  25. package/dist/lib/spelling.js +2 -2
  26. package/dist/lib/templates.js +387 -387
  27. package/dist/lib/themes.js +51 -51
  28. package/eslint.config.js +27 -27
  29. package/lib/anchor-match.ts +307 -307
  30. package/lib/annotations.ts +664 -664
  31. package/lib/build.ts +2047 -1956
  32. package/lib/citations.ts +160 -160
  33. package/lib/commands/build.ts +885 -885
  34. package/lib/commands/citations.ts +515 -515
  35. package/lib/commands/comments.ts +1050 -1050
  36. package/lib/commands/context.ts +176 -176
  37. package/lib/commands/core.ts +309 -309
  38. package/lib/commands/doi.ts +451 -451
  39. package/lib/commands/file-ops.ts +372 -372
  40. package/lib/commands/history.ts +320 -320
  41. package/lib/commands/index.ts +87 -87
  42. package/lib/commands/init.ts +259 -259
  43. package/lib/commands/merge-resolve.ts +378 -378
  44. package/lib/commands/preview.ts +178 -178
  45. package/lib/commands/project-info.ts +244 -244
  46. package/lib/commands/quality.ts +517 -517
  47. package/lib/commands/response.ts +454 -454
  48. package/lib/commands/section-boundaries.ts +82 -82
  49. package/lib/commands/sections.ts +451 -451
  50. package/lib/commands/sync.ts +689 -689
  51. package/lib/commands/text-ops.ts +449 -449
  52. package/lib/commands/utilities.ts +448 -448
  53. package/lib/commands/verify-anchors.ts +272 -272
  54. package/lib/commands/word-tools.ts +340 -340
  55. package/lib/comment-realign.ts +99 -99
  56. package/lib/config.ts +84 -84
  57. package/lib/crossref.ts +781 -781
  58. package/lib/csl.ts +191 -191
  59. package/lib/dependencies.ts +132 -132
  60. package/lib/diff-engine.ts +465 -465
  61. package/lib/doi-cache.ts +115 -115
  62. package/lib/doi.ts +879 -879
  63. package/lib/equations.ts +506 -506
  64. package/lib/errors.ts +350 -350
  65. package/lib/format.ts +541 -541
  66. package/lib/git.ts +326 -326
  67. package/lib/grammar.ts +303 -303
  68. package/lib/image-registry.ts +180 -180
  69. package/lib/import.ts +906 -906
  70. package/lib/journals.ts +543 -543
  71. package/lib/macro-filter.lua +201 -201
  72. package/lib/macros.ts +273 -273
  73. package/lib/merge.ts +633 -633
  74. package/lib/ooxml.ts +768 -768
  75. package/lib/orcid.ts +144 -144
  76. package/lib/pdf-comments.ts +263 -263
  77. package/lib/pdf-import.ts +524 -524
  78. package/lib/plugins.ts +362 -362
  79. package/lib/postprocess.ts +188 -188
  80. package/lib/pptx-color-filter.lua +37 -37
  81. package/lib/pptx-template.ts +469 -469
  82. package/lib/pptx-themes.ts +483 -483
  83. package/lib/protect-restore.ts +520 -520
  84. package/lib/rate-limiter.ts +127 -127
  85. package/lib/response.ts +197 -197
  86. package/lib/restore-references.ts +240 -240
  87. package/lib/review.ts +327 -327
  88. package/lib/schema.ts +494 -488
  89. package/lib/scientific-words.ts +73 -73
  90. package/lib/sections.ts +428 -428
  91. package/lib/slides.ts +756 -756
  92. package/lib/spelling.ts +334 -334
  93. package/lib/templates.ts +526 -526
  94. package/lib/themes.ts +742 -742
  95. package/lib/trackchanges.ts +166 -166
  96. package/lib/tui.ts +450 -450
  97. package/lib/types.ts +546 -546
  98. package/lib/undo.ts +250 -250
  99. package/lib/utils.ts +69 -69
  100. package/lib/variables.ts +179 -179
  101. package/lib/word-extraction.ts +525 -525
  102. package/lib/word.ts +526 -526
  103. package/lib/wordcomments.ts +789 -789
  104. package/package.json +1 -1
  105. package/scripts/postbuild.js +47 -47
  106. package/skill/REFERENCE.md +550 -550
  107. package/skill/SKILL.md +302 -302
  108. package/tsconfig.json +26 -26
  109. package/types/index.d.ts +531 -531
  110. package/issues.md +0 -180
  111. package/site/assets/extra.css +0 -208
  112. package/site/commands.html +0 -926
  113. package/site/configuration.html +0 -469
  114. package/site/index.html +0 -288
  115. package/site/troubleshooting.html +0 -461
  116. package/site/workflow.html +0 -518
package/lib/word.ts CHANGED
@@ -1,526 +1,526 @@
1
- /**
2
- * Word document extraction utilities
3
- * Handle reading text, comments, and anchors from .docx files
4
- */
5
-
6
- import * as fs from 'fs';
7
- import * as path from 'path';
8
- import AdmZip from 'adm-zip';
9
- import type { WordComment, CommentAnchor, WordMetadata, TrackChangesResult } from './types.js';
10
- import {
11
- openDocx,
12
- readPartText,
13
- buildCommentAnchorModel,
14
- extractComments,
15
- walkBody,
16
- type FlowItem,
17
- } from './ooxml.js';
18
-
19
- // =============================================================================
20
- // Constants
21
- // =============================================================================
22
-
23
- /** Characters of context to extract around comment anchors */
24
- const ANCHOR_CONTEXT_SIZE = 100;
25
-
26
- /** Characters of context before comment range start */
27
- const CONTEXT_BEFORE_SIZE = 500;
28
-
29
- // =============================================================================
30
- // Public API
31
- // =============================================================================
32
-
33
- /**
34
- * Extract comments from Word document's comments.xml
35
- * @param docxPath - Path to .docx file
36
- * @returns Array of extracted comments
37
- * @throws {TypeError} If docxPath is not a string
38
- * @throws {Error} If file not found or invalid docx
39
- */
40
- export async function extractWordComments(docxPath: string): Promise<WordComment[]> {
41
- if (typeof docxPath !== 'string') {
42
- throw new TypeError(`docxPath must be a string, got ${typeof docxPath}`);
43
- }
44
- if (!fs.existsSync(docxPath)) {
45
- throw new Error(`File not found: ${docxPath}`);
46
- }
47
-
48
- const zip = openDocx(docxPath);
49
- return extractComments(zip)
50
- .filter((c) => c.id && c.text)
51
- .map((c) => ({ id: c.id, author: c.author, date: c.date, text: c.text }));
52
- }
53
-
54
- /**
55
- * Extract comment anchors (where comments are attached) from document.xml
56
- * Returns mapping of comment ID to the text they're anchored to
57
- * @param docxPath - Path to .docx file
58
- * @returns Map of comment ID to anchor info
59
- * @throws {TypeError} If docxPath is not a string
60
- * @throws {Error} If invalid docx structure
61
- */
62
- export async function extractCommentAnchors(docxPath: string): Promise<Map<string, CommentAnchor>> {
63
- if (typeof docxPath !== 'string') {
64
- throw new TypeError(`docxPath must be a string, got ${typeof docxPath}`);
65
- }
66
-
67
- const zip = openDocx(docxPath);
68
- if (!zip.getEntry('word/document.xml')) {
69
- throw new Error('Invalid docx: no document.xml');
70
- }
71
-
72
- const { fullDocText, comments } = buildCommentAnchorModel(zip);
73
- const anchors = new Map<string, CommentAnchor>();
74
-
75
- for (const range of comments) {
76
- anchors.set(range.id, {
77
- text: range.anchor,
78
- context: fullDocText.slice(Math.max(0, range.start - CONTEXT_BEFORE_SIZE), range.start).slice(-ANCHOR_CONTEXT_SIZE),
79
- });
80
- }
81
-
82
- return anchors;
83
- }
84
-
85
- /**
86
- * Extract plain text from Word document (strips track change markup)
87
- * @param docxPath - Path to .docx file
88
- * @returns Extracted plain text (accepted changes applied)
89
- * @throws {TypeError} If docxPath is not a string
90
- * @throws {Error} If file not found
91
- */
92
- export async function extractTextFromWord(docxPath: string): Promise<string> {
93
- if (typeof docxPath !== 'string') {
94
- throw new TypeError(`docxPath must be a string, got ${typeof docxPath}`);
95
- }
96
- const result = await extractPlainTextWithTrackChanges(docxPath);
97
- // Strip CriticMarkup: accept insertions, remove deletions, apply substitutions
98
- let text = result.text;
99
- text = text.replace(/\{~~[^~]*~>([^~]*)~~\}/g, '$1'); // substitutions → new
100
- text = text.replace(/\{\+\+([^+]*)\+\+\}/g, '$1'); // insertions → keep
101
- text = text.replace(/\{--[^}]*--\}/g, ''); // deletions → remove
102
- return text;
103
- }
104
-
105
- /**
106
- * Get document metadata from Word file
107
- * @param docxPath - Path to .docx file
108
- * @returns Document metadata
109
- * @throws {TypeError} If docxPath is not a string
110
- */
111
- export async function getWordMetadata(docxPath: string): Promise<WordMetadata> {
112
- if (typeof docxPath !== 'string') {
113
- throw new TypeError(`docxPath must be a string, got ${typeof docxPath}`);
114
- }
115
-
116
- const zip = new AdmZip(docxPath);
117
- const coreEntry = zip.getEntry('docProps/core.xml');
118
-
119
- if (!coreEntry) {
120
- return {};
121
- }
122
-
123
- const coreXml = zip.readAsText(coreEntry);
124
- const metadata: WordMetadata = {};
125
-
126
- // Extract common metadata fields
127
- const patterns: Record<string, RegExp> = {
128
- title: /<dc:title>([^<]*)<\/dc:title>/,
129
- author: /<dc:creator>([^<]*)<\/dc:creator>/,
130
- created: /<dcterms:created[^>]*>([^<]*)<\/dcterms:created>/,
131
- modified: /<dcterms:modified[^>]*>([^<]*)<\/dcterms:modified>/,
132
- };
133
-
134
- for (const [key, pattern] of Object.entries(patterns)) {
135
- const match = coreXml.match(pattern);
136
- if (match) {
137
- (metadata as any)[key] = match[1];
138
- }
139
- }
140
-
141
- return metadata;
142
- }
143
-
144
- /**
145
- * Check if file is a valid Word document
146
- * @param filePath - Path to file to check
147
- * @returns True if valid .docx file
148
- */
149
- export function isWordDocument(filePath: string): boolean {
150
- if (typeof filePath !== 'string') return false;
151
- if (!fs.existsSync(filePath)) return false;
152
- if (!filePath.toLowerCase().endsWith('.docx')) return false;
153
-
154
- try {
155
- const zip = new AdmZip(filePath);
156
- return zip.getEntry('word/document.xml') !== null;
157
- } catch {
158
- return false;
159
- }
160
- }
161
-
162
- /**
163
- * Extract text content from XML element, handling nested elements
164
- * @param xml - XML string
165
- * @returns Plain text content
166
- */
167
- function extractTextFromXml(xml: string): string {
168
- let text = '';
169
- // Match w:t elements (regular text)
170
- const textPattern = /<w:t[^>]*>([^<]*)<\/w:t>/g;
171
- let match: RegExpExecArray | null;
172
- while ((match = textPattern.exec(xml)) !== null) {
173
- text += match[1];
174
- }
175
- // Also match w:delText (deleted text)
176
- const delTextPattern = /<w:delText[^>]*>([^<]*)<\/w:delText>/g;
177
- while ((match = delTextPattern.exec(xml)) !== null) {
178
- text += match[1];
179
- }
180
- return text;
181
- }
182
-
183
- /**
184
- * Extract track changes (insertions and deletions) from Word document
185
- * Converts Word's w:ins and w:del elements to CriticMarkup format
186
- *
187
- * @param docxPath - Path to Word document
188
- * @returns Track changes result with content and stats
189
- */
190
- export async function extractTrackChanges(docxPath: string): Promise<TrackChangesResult> {
191
- if (!fs.existsSync(docxPath)) {
192
- throw new Error(`File not found: ${docxPath}`);
193
- }
194
-
195
- const zip = new AdmZip(docxPath);
196
- const documentEntry = zip.getEntry('word/document.xml');
197
-
198
- if (!documentEntry) {
199
- throw new Error('Invalid docx: no document.xml');
200
- }
201
-
202
- let xml = zip.readAsText(documentEntry);
203
- let insertions = 0;
204
- let deletions = 0;
205
-
206
- // Check if there are any track changes
207
- const hasInsertions = xml.includes('<w:ins ');
208
- const hasDeletions = xml.includes('<w:del ');
209
-
210
- if (!hasInsertions && !hasDeletions) {
211
- return { hasTrackChanges: false, content: null, stats: { insertions: 0, deletions: 0 } };
212
- }
213
-
214
- // Process insertions: <w:ins ...>...</w:ins> -> {++...++}
215
- // Match the full w:ins element including nested content
216
- xml = xml.replace(/<w:ins\b[^>]*>([\s\S]*?)<\/w:ins>/g, (match, content) => {
217
- const text = extractTextFromXml(content);
218
- if (text.trim()) {
219
- insertions++;
220
- return `{++${text}++}`;
221
- }
222
- return text;
223
- });
224
-
225
- // Process deletions: <w:del ...>...</w:del> -> {--...--}
226
- xml = xml.replace(/<w:del\b[^>]*>([\s\S]*?)<\/w:del>/g, (match, content) => {
227
- const text = extractTextFromXml(content);
228
- if (text.trim()) {
229
- deletions++;
230
- return `{--${text}--}`;
231
- }
232
- return '';
233
- });
234
-
235
- return {
236
- hasTrackChanges: true,
237
- content: xml,
238
- stats: { insertions, deletions },
239
- };
240
- }
241
-
242
- /**
243
- * Extract a single marker's content starting at position i.
244
- * Returns { content, end } where end is the position after the closing marker,
245
- * or null if no valid closing marker found.
246
- */
247
- function extractMarker(text: string, i: number, open: string, close: string): { content: string; end: number } | null {
248
- if (!text.startsWith(open, i)) return null;
249
- const start = i + open.length;
250
- const closeIdx = text.indexOf(close, start);
251
- if (closeIdx === -1) return null;
252
- return { content: text.slice(start, closeIdx), end: closeIdx + close.length };
253
- }
254
-
255
- /**
256
- * Greedily collect consecutive markers of the same type.
257
- * E.g. {++a++}{++b++}{++c++} → "abc", advancing past all three.
258
- */
259
- function collectConsecutive(text: string, i: number, open: string, close: string): { content: string; end: number } | null {
260
- const first = extractMarker(text, i, open, close);
261
- if (!first) return null;
262
-
263
- let content = first.content;
264
- let end = first.end;
265
-
266
- while (end < text.length) {
267
- const next = extractMarker(text, end, open, close);
268
- if (!next) break;
269
- content += next.content;
270
- end = next.end;
271
- }
272
-
273
- return { content, end };
274
- }
275
-
276
- /**
277
- * Scan text for adjacent CriticMarkup markers and:
278
- * 1. Merge consecutive same-type markers: {++a++}{++b++} → {++ab++}
279
- * 2. Merge adjacent del+ins or ins+del into substitutions: {--old--}{++new++} → {~~old~>new~~}
280
- *
281
- * Uses a linear scanner — no regex backtracking, no ambiguity.
282
- */
283
- function mergeAdjacentMarkers(text: string): string {
284
- let result = '';
285
- let i = 0;
286
-
287
- while (i < text.length) {
288
- // --- Deletion block ---
289
- if (text.startsWith('{--', i)) {
290
- const del = collectConsecutive(text, i, '{--', '--}');
291
- if (!del) { result += text[i]; i++; continue; }
292
-
293
- // Skip spaces, then check for adjacent insertion
294
- let j = del.end;
295
- while (j < text.length && text[j] === ' ') j++;
296
-
297
- const ins = collectConsecutive(text, j, '{++', '++}');
298
- if (ins) {
299
- // Merge into substitution
300
- const trailing = del.content.endsWith(' ') || ins.content.endsWith(' ');
301
- result += `{~~${del.content.trimEnd()}~>${ins.content.trimEnd()}~~}${trailing ? ' ' : ''}`;
302
- i = ins.end;
303
- } else {
304
- // Emit merged deletion
305
- result += `{--${del.content}--}`;
306
- i = del.end;
307
- }
308
- continue;
309
- }
310
-
311
- // --- Insertion block ---
312
- if (text.startsWith('{++', i)) {
313
- const ins = collectConsecutive(text, i, '{++', '++}');
314
- if (!ins) { result += text[i]; i++; continue; }
315
-
316
- // Skip spaces, then check for adjacent deletion
317
- let j = ins.end;
318
- while (j < text.length && text[j] === ' ') j++;
319
-
320
- const del = collectConsecutive(text, j, '{--', '--}');
321
- if (del) {
322
- // Merge into substitution (del → ins order in output)
323
- const trailing = del.content.endsWith(' ') || ins.content.endsWith(' ');
324
- result += `{~~${del.content.trimEnd()}~>${ins.content.trimEnd()}~~}${trailing ? ' ' : ''}`;
325
- i = del.end;
326
- } else {
327
- // Emit merged insertion
328
- result += `{++${ins.content}++}`;
329
- i = ins.end;
330
- }
331
- continue;
332
- }
333
-
334
- result += text[i];
335
- i++;
336
- }
337
-
338
- return result;
339
- }
340
-
341
- /**
342
- * Extract plain text from Word XML with track changes preserved as CriticMarkup.
343
- * This is a pandoc-free fallback that reads document.xml directly.
344
- *
345
- * Converts:
346
- * <w:ins> content </w:ins> → {++text++}
347
- * <w:del> content </w:del> → {--text--}
348
- *
349
- * Also detects headings (w:pStyle Heading1-6) and outputs markdown # syntax.
350
- *
351
- * @param docxPath - Path to Word document
352
- * @returns Plain text with CriticMarkup and stats
353
- */
354
- export async function extractPlainTextWithTrackChanges(docxPath: string): Promise<{
355
- text: string;
356
- hasTrackChanges: boolean;
357
- stats: { insertions: number; deletions: number };
358
- }> {
359
- if (!fs.existsSync(docxPath)) {
360
- throw new Error(`File not found: ${docxPath}`);
361
- }
362
-
363
- const zip = openDocx(docxPath);
364
- const docXml = readPartText(zip, 'word/document.xml');
365
- if (docXml === null) {
366
- throw new Error('Invalid docx: no document.xml');
367
- }
368
-
369
- let insertions = 0;
370
- let deletions = 0;
371
- const paragraphs: string[] = [];
372
-
373
- // One ordered walk drives everything: paragraph and heading boundaries,
374
- // run text (entities already decoded, tabs/breaks rendered), and the
375
- // track-change spans that become CriticMarkup. Field codes (w:instrText)
376
- // never reach the text because the walker only reads w:t / w:delText.
377
- let paraOut = '';
378
- let headingLevel = 0;
379
- let mode: 'normal' | 'ins' | 'del' = 'normal';
380
- let buffer = '';
381
-
382
- const flushSpan = (open: string, close: string, isIns: boolean) => {
383
- if (buffer.trim()) {
384
- if (isIns) insertions++;
385
- else deletions++;
386
- paraOut += `${open}${buffer}${close}`;
387
- } else if (buffer.length > 0) {
388
- // Whitespace-only edits are kept as plain text to preserve spacing.
389
- paraOut += buffer;
390
- }
391
- buffer = '';
392
- };
393
-
394
- const endParagraph = () => {
395
- let text = mergeAdjacentMarkers(paraOut);
396
- text = text.replace(/ {2,}/g, ' ');
397
- if (text.trim()) {
398
- paragraphs.push(
399
- headingLevel >= 1 && headingLevel <= 6 ? '#'.repeat(headingLevel) + ' ' + text.trim() : text,
400
- );
401
- }
402
- paraOut = '';
403
- headingLevel = 0;
404
- mode = 'normal';
405
- buffer = '';
406
- };
407
-
408
- for (const item of walkBody(docXml) as FlowItem[]) {
409
- switch (item.kind) {
410
- case 'paraStart':
411
- paraOut = '';
412
- headingLevel = item.level;
413
- mode = 'normal';
414
- buffer = '';
415
- break;
416
- case 'paraEnd':
417
- endParagraph();
418
- break;
419
- case 'text':
420
- if (mode === 'normal') paraOut += item.text;
421
- else buffer += item.text;
422
- break;
423
- case 'insStart':
424
- mode = 'ins';
425
- buffer = '';
426
- break;
427
- case 'insEnd':
428
- flushSpan('{++', '++}', true);
429
- mode = 'normal';
430
- break;
431
- case 'delStart':
432
- mode = 'del';
433
- buffer = '';
434
- break;
435
- case 'delEnd':
436
- flushSpan('{--', '--}', false);
437
- mode = 'normal';
438
- break;
439
- default:
440
- break;
441
- }
442
- }
443
-
444
- return {
445
- text: paragraphs.join('\n\n'),
446
- hasTrackChanges: insertions > 0 || deletions > 0,
447
- stats: { insertions, deletions },
448
- };
449
- }
450
-
451
- interface ExtractWithTrackChangesOptions {
452
- mediaDir?: string;
453
- }
454
-
455
- /**
456
- * Extract Word document content with track changes preserved as CriticMarkup
457
- * Uses pandoc with track-changes=all option to preserve insertions/deletions
458
- *
459
- * @param docxPath - Path to Word document
460
- * @param options - Options
461
- * @returns Track changes result with text and stats
462
- */
463
- export async function extractWithTrackChanges(
464
- docxPath: string,
465
- options: ExtractWithTrackChangesOptions = {}
466
- ): Promise<{ text: string; hasTrackChanges: boolean; stats: { insertions: number; deletions: number } }> {
467
- const { mediaDir } = options;
468
-
469
- if (!fs.existsSync(docxPath)) {
470
- throw new Error(`File not found: ${docxPath}`);
471
- }
472
-
473
- const { execSync } = await import('child_process');
474
-
475
- // Use pandoc with --track-changes=all to preserve track changes
476
- // This outputs insertions as [insertion]{.insertion} and deletions as [deletion]{.deletion}
477
- let pandocArgs = `"${docxPath}" -t markdown --wrap=none --track-changes=all`;
478
- if (mediaDir) {
479
- pandocArgs += ` --extract-media="${mediaDir}"`;
480
- }
481
-
482
- let text: string;
483
- try {
484
- text = execSync(`pandoc ${pandocArgs}`, {
485
- encoding: 'utf-8',
486
- maxBuffer: 50 * 1024 * 1024,
487
- });
488
- } catch (err: any) {
489
- throw new Error(`Pandoc extraction failed: ${err.message}`);
490
- }
491
-
492
- // Count track changes from pandoc output
493
- let insertions = 0;
494
- let deletions = 0;
495
-
496
- // Pandoc outputs track changes as:
497
- // [inserted text]{.insertion author="..."}
498
- // [deleted text]{.deletion author="..."}
499
-
500
- // Convert pandoc's track change format to CriticMarkup
501
- // Insertions: [text]{.insertion ...} -> {++text++}
502
- text = text.replace(/\[([^\]]*)\]\{\.insertion[^}]*\}/g, (match, content) => {
503
- if (content.trim()) {
504
- insertions++;
505
- return `{++${content}++}`;
506
- }
507
- return '';
508
- });
509
-
510
- // Deletions: [text]{.deletion ...} -> {--text--}
511
- text = text.replace(/\[([^\]]*)\]\{\.deletion[^}]*\}/g, (match, content) => {
512
- if (content.trim()) {
513
- deletions++;
514
- return `{--${content}--}`;
515
- }
516
- return '';
517
- });
518
-
519
- const hasTrackChanges = insertions > 0 || deletions > 0;
520
-
521
- return {
522
- text,
523
- hasTrackChanges,
524
- stats: { insertions, deletions },
525
- };
526
- }
1
+ /**
2
+ * Word document extraction utilities
3
+ * Handle reading text, comments, and anchors from .docx files
4
+ */
5
+
6
+ import * as fs from 'fs';
7
+ import * as path from 'path';
8
+ import AdmZip from 'adm-zip';
9
+ import type { WordComment, CommentAnchor, WordMetadata, TrackChangesResult } from './types.js';
10
+ import {
11
+ openDocx,
12
+ readPartText,
13
+ buildCommentAnchorModel,
14
+ extractComments,
15
+ walkBody,
16
+ type FlowItem,
17
+ } from './ooxml.js';
18
+
19
+ // =============================================================================
20
+ // Constants
21
+ // =============================================================================
22
+
23
+ /** Characters of context to extract around comment anchors */
24
+ const ANCHOR_CONTEXT_SIZE = 100;
25
+
26
+ /** Characters of context before comment range start */
27
+ const CONTEXT_BEFORE_SIZE = 500;
28
+
29
+ // =============================================================================
30
+ // Public API
31
+ // =============================================================================
32
+
33
+ /**
34
+ * Extract comments from Word document's comments.xml
35
+ * @param docxPath - Path to .docx file
36
+ * @returns Array of extracted comments
37
+ * @throws {TypeError} If docxPath is not a string
38
+ * @throws {Error} If file not found or invalid docx
39
+ */
40
+ export async function extractWordComments(docxPath: string): Promise<WordComment[]> {
41
+ if (typeof docxPath !== 'string') {
42
+ throw new TypeError(`docxPath must be a string, got ${typeof docxPath}`);
43
+ }
44
+ if (!fs.existsSync(docxPath)) {
45
+ throw new Error(`File not found: ${docxPath}`);
46
+ }
47
+
48
+ const zip = openDocx(docxPath);
49
+ return extractComments(zip)
50
+ .filter((c) => c.id && c.text)
51
+ .map((c) => ({ id: c.id, author: c.author, date: c.date, text: c.text }));
52
+ }
53
+
54
+ /**
55
+ * Extract comment anchors (where comments are attached) from document.xml
56
+ * Returns mapping of comment ID to the text they're anchored to
57
+ * @param docxPath - Path to .docx file
58
+ * @returns Map of comment ID to anchor info
59
+ * @throws {TypeError} If docxPath is not a string
60
+ * @throws {Error} If invalid docx structure
61
+ */
62
+ export async function extractCommentAnchors(docxPath: string): Promise<Map<string, CommentAnchor>> {
63
+ if (typeof docxPath !== 'string') {
64
+ throw new TypeError(`docxPath must be a string, got ${typeof docxPath}`);
65
+ }
66
+
67
+ const zip = openDocx(docxPath);
68
+ if (!zip.getEntry('word/document.xml')) {
69
+ throw new Error('Invalid docx: no document.xml');
70
+ }
71
+
72
+ const { fullDocText, comments } = buildCommentAnchorModel(zip);
73
+ const anchors = new Map<string, CommentAnchor>();
74
+
75
+ for (const range of comments) {
76
+ anchors.set(range.id, {
77
+ text: range.anchor,
78
+ context: fullDocText.slice(Math.max(0, range.start - CONTEXT_BEFORE_SIZE), range.start).slice(-ANCHOR_CONTEXT_SIZE),
79
+ });
80
+ }
81
+
82
+ return anchors;
83
+ }
84
+
85
+ /**
86
+ * Extract plain text from Word document (strips track change markup)
87
+ * @param docxPath - Path to .docx file
88
+ * @returns Extracted plain text (accepted changes applied)
89
+ * @throws {TypeError} If docxPath is not a string
90
+ * @throws {Error} If file not found
91
+ */
92
+ export async function extractTextFromWord(docxPath: string): Promise<string> {
93
+ if (typeof docxPath !== 'string') {
94
+ throw new TypeError(`docxPath must be a string, got ${typeof docxPath}`);
95
+ }
96
+ const result = await extractPlainTextWithTrackChanges(docxPath);
97
+ // Strip CriticMarkup: accept insertions, remove deletions, apply substitutions
98
+ let text = result.text;
99
+ text = text.replace(/\{~~[^~]*~>([^~]*)~~\}/g, '$1'); // substitutions → new
100
+ text = text.replace(/\{\+\+([^+]*)\+\+\}/g, '$1'); // insertions → keep
101
+ text = text.replace(/\{--[^}]*--\}/g, ''); // deletions → remove
102
+ return text;
103
+ }
104
+
105
+ /**
106
+ * Get document metadata from Word file
107
+ * @param docxPath - Path to .docx file
108
+ * @returns Document metadata
109
+ * @throws {TypeError} If docxPath is not a string
110
+ */
111
+ export async function getWordMetadata(docxPath: string): Promise<WordMetadata> {
112
+ if (typeof docxPath !== 'string') {
113
+ throw new TypeError(`docxPath must be a string, got ${typeof docxPath}`);
114
+ }
115
+
116
+ const zip = new AdmZip(docxPath);
117
+ const coreEntry = zip.getEntry('docProps/core.xml');
118
+
119
+ if (!coreEntry) {
120
+ return {};
121
+ }
122
+
123
+ const coreXml = zip.readAsText(coreEntry);
124
+ const metadata: WordMetadata = {};
125
+
126
+ // Extract common metadata fields
127
+ const patterns: Record<string, RegExp> = {
128
+ title: /<dc:title>([^<]*)<\/dc:title>/,
129
+ author: /<dc:creator>([^<]*)<\/dc:creator>/,
130
+ created: /<dcterms:created[^>]*>([^<]*)<\/dcterms:created>/,
131
+ modified: /<dcterms:modified[^>]*>([^<]*)<\/dcterms:modified>/,
132
+ };
133
+
134
+ for (const [key, pattern] of Object.entries(patterns)) {
135
+ const match = coreXml.match(pattern);
136
+ if (match) {
137
+ (metadata as any)[key] = match[1];
138
+ }
139
+ }
140
+
141
+ return metadata;
142
+ }
143
+
144
+ /**
145
+ * Check if file is a valid Word document
146
+ * @param filePath - Path to file to check
147
+ * @returns True if valid .docx file
148
+ */
149
+ export function isWordDocument(filePath: string): boolean {
150
+ if (typeof filePath !== 'string') return false;
151
+ if (!fs.existsSync(filePath)) return false;
152
+ if (!filePath.toLowerCase().endsWith('.docx')) return false;
153
+
154
+ try {
155
+ const zip = new AdmZip(filePath);
156
+ return zip.getEntry('word/document.xml') !== null;
157
+ } catch {
158
+ return false;
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Extract text content from XML element, handling nested elements
164
+ * @param xml - XML string
165
+ * @returns Plain text content
166
+ */
167
+ function extractTextFromXml(xml: string): string {
168
+ let text = '';
169
+ // Match w:t elements (regular text)
170
+ const textPattern = /<w:t[^>]*>([^<]*)<\/w:t>/g;
171
+ let match: RegExpExecArray | null;
172
+ while ((match = textPattern.exec(xml)) !== null) {
173
+ text += match[1];
174
+ }
175
+ // Also match w:delText (deleted text)
176
+ const delTextPattern = /<w:delText[^>]*>([^<]*)<\/w:delText>/g;
177
+ while ((match = delTextPattern.exec(xml)) !== null) {
178
+ text += match[1];
179
+ }
180
+ return text;
181
+ }
182
+
183
+ /**
184
+ * Extract track changes (insertions and deletions) from Word document
185
+ * Converts Word's w:ins and w:del elements to CriticMarkup format
186
+ *
187
+ * @param docxPath - Path to Word document
188
+ * @returns Track changes result with content and stats
189
+ */
190
+ export async function extractTrackChanges(docxPath: string): Promise<TrackChangesResult> {
191
+ if (!fs.existsSync(docxPath)) {
192
+ throw new Error(`File not found: ${docxPath}`);
193
+ }
194
+
195
+ const zip = new AdmZip(docxPath);
196
+ const documentEntry = zip.getEntry('word/document.xml');
197
+
198
+ if (!documentEntry) {
199
+ throw new Error('Invalid docx: no document.xml');
200
+ }
201
+
202
+ let xml = zip.readAsText(documentEntry);
203
+ let insertions = 0;
204
+ let deletions = 0;
205
+
206
+ // Check if there are any track changes
207
+ const hasInsertions = xml.includes('<w:ins ');
208
+ const hasDeletions = xml.includes('<w:del ');
209
+
210
+ if (!hasInsertions && !hasDeletions) {
211
+ return { hasTrackChanges: false, content: null, stats: { insertions: 0, deletions: 0 } };
212
+ }
213
+
214
+ // Process insertions: <w:ins ...>...</w:ins> -> {++...++}
215
+ // Match the full w:ins element including nested content
216
+ xml = xml.replace(/<w:ins\b[^>]*>([\s\S]*?)<\/w:ins>/g, (match, content) => {
217
+ const text = extractTextFromXml(content);
218
+ if (text.trim()) {
219
+ insertions++;
220
+ return `{++${text}++}`;
221
+ }
222
+ return text;
223
+ });
224
+
225
+ // Process deletions: <w:del ...>...</w:del> -> {--...--}
226
+ xml = xml.replace(/<w:del\b[^>]*>([\s\S]*?)<\/w:del>/g, (match, content) => {
227
+ const text = extractTextFromXml(content);
228
+ if (text.trim()) {
229
+ deletions++;
230
+ return `{--${text}--}`;
231
+ }
232
+ return '';
233
+ });
234
+
235
+ return {
236
+ hasTrackChanges: true,
237
+ content: xml,
238
+ stats: { insertions, deletions },
239
+ };
240
+ }
241
+
242
+ /**
243
+ * Extract a single marker's content starting at position i.
244
+ * Returns { content, end } where end is the position after the closing marker,
245
+ * or null if no valid closing marker found.
246
+ */
247
+ function extractMarker(text: string, i: number, open: string, close: string): { content: string; end: number } | null {
248
+ if (!text.startsWith(open, i)) return null;
249
+ const start = i + open.length;
250
+ const closeIdx = text.indexOf(close, start);
251
+ if (closeIdx === -1) return null;
252
+ return { content: text.slice(start, closeIdx), end: closeIdx + close.length };
253
+ }
254
+
255
+ /**
256
+ * Greedily collect consecutive markers of the same type.
257
+ * E.g. {++a++}{++b++}{++c++} → "abc", advancing past all three.
258
+ */
259
+ function collectConsecutive(text: string, i: number, open: string, close: string): { content: string; end: number } | null {
260
+ const first = extractMarker(text, i, open, close);
261
+ if (!first) return null;
262
+
263
+ let content = first.content;
264
+ let end = first.end;
265
+
266
+ while (end < text.length) {
267
+ const next = extractMarker(text, end, open, close);
268
+ if (!next) break;
269
+ content += next.content;
270
+ end = next.end;
271
+ }
272
+
273
+ return { content, end };
274
+ }
275
+
276
+ /**
277
+ * Scan text for adjacent CriticMarkup markers and:
278
+ * 1. Merge consecutive same-type markers: {++a++}{++b++} → {++ab++}
279
+ * 2. Merge adjacent del+ins or ins+del into substitutions: {--old--}{++new++} → {~~old~>new~~}
280
+ *
281
+ * Uses a linear scanner — no regex backtracking, no ambiguity.
282
+ */
283
+ function mergeAdjacentMarkers(text: string): string {
284
+ let result = '';
285
+ let i = 0;
286
+
287
+ while (i < text.length) {
288
+ // --- Deletion block ---
289
+ if (text.startsWith('{--', i)) {
290
+ const del = collectConsecutive(text, i, '{--', '--}');
291
+ if (!del) { result += text[i]; i++; continue; }
292
+
293
+ // Skip spaces, then check for adjacent insertion
294
+ let j = del.end;
295
+ while (j < text.length && text[j] === ' ') j++;
296
+
297
+ const ins = collectConsecutive(text, j, '{++', '++}');
298
+ if (ins) {
299
+ // Merge into substitution
300
+ const trailing = del.content.endsWith(' ') || ins.content.endsWith(' ');
301
+ result += `{~~${del.content.trimEnd()}~>${ins.content.trimEnd()}~~}${trailing ? ' ' : ''}`;
302
+ i = ins.end;
303
+ } else {
304
+ // Emit merged deletion
305
+ result += `{--${del.content}--}`;
306
+ i = del.end;
307
+ }
308
+ continue;
309
+ }
310
+
311
+ // --- Insertion block ---
312
+ if (text.startsWith('{++', i)) {
313
+ const ins = collectConsecutive(text, i, '{++', '++}');
314
+ if (!ins) { result += text[i]; i++; continue; }
315
+
316
+ // Skip spaces, then check for adjacent deletion
317
+ let j = ins.end;
318
+ while (j < text.length && text[j] === ' ') j++;
319
+
320
+ const del = collectConsecutive(text, j, '{--', '--}');
321
+ if (del) {
322
+ // Merge into substitution (del → ins order in output)
323
+ const trailing = del.content.endsWith(' ') || ins.content.endsWith(' ');
324
+ result += `{~~${del.content.trimEnd()}~>${ins.content.trimEnd()}~~}${trailing ? ' ' : ''}`;
325
+ i = del.end;
326
+ } else {
327
+ // Emit merged insertion
328
+ result += `{++${ins.content}++}`;
329
+ i = ins.end;
330
+ }
331
+ continue;
332
+ }
333
+
334
+ result += text[i];
335
+ i++;
336
+ }
337
+
338
+ return result;
339
+ }
340
+
341
+ /**
342
+ * Extract plain text from Word XML with track changes preserved as CriticMarkup.
343
+ * This is a pandoc-free fallback that reads document.xml directly.
344
+ *
345
+ * Converts:
346
+ * <w:ins> content </w:ins> → {++text++}
347
+ * <w:del> content </w:del> → {--text--}
348
+ *
349
+ * Also detects headings (w:pStyle Heading1-6) and outputs markdown # syntax.
350
+ *
351
+ * @param docxPath - Path to Word document
352
+ * @returns Plain text with CriticMarkup and stats
353
+ */
354
+ export async function extractPlainTextWithTrackChanges(docxPath: string): Promise<{
355
+ text: string;
356
+ hasTrackChanges: boolean;
357
+ stats: { insertions: number; deletions: number };
358
+ }> {
359
+ if (!fs.existsSync(docxPath)) {
360
+ throw new Error(`File not found: ${docxPath}`);
361
+ }
362
+
363
+ const zip = openDocx(docxPath);
364
+ const docXml = readPartText(zip, 'word/document.xml');
365
+ if (docXml === null) {
366
+ throw new Error('Invalid docx: no document.xml');
367
+ }
368
+
369
+ let insertions = 0;
370
+ let deletions = 0;
371
+ const paragraphs: string[] = [];
372
+
373
+ // One ordered walk drives everything: paragraph and heading boundaries,
374
+ // run text (entities already decoded, tabs/breaks rendered), and the
375
+ // track-change spans that become CriticMarkup. Field codes (w:instrText)
376
+ // never reach the text because the walker only reads w:t / w:delText.
377
+ let paraOut = '';
378
+ let headingLevel = 0;
379
+ let mode: 'normal' | 'ins' | 'del' = 'normal';
380
+ let buffer = '';
381
+
382
+ const flushSpan = (open: string, close: string, isIns: boolean) => {
383
+ if (buffer.trim()) {
384
+ if (isIns) insertions++;
385
+ else deletions++;
386
+ paraOut += `${open}${buffer}${close}`;
387
+ } else if (buffer.length > 0) {
388
+ // Whitespace-only edits are kept as plain text to preserve spacing.
389
+ paraOut += buffer;
390
+ }
391
+ buffer = '';
392
+ };
393
+
394
+ const endParagraph = () => {
395
+ let text = mergeAdjacentMarkers(paraOut);
396
+ text = text.replace(/ {2,}/g, ' ');
397
+ if (text.trim()) {
398
+ paragraphs.push(
399
+ headingLevel >= 1 && headingLevel <= 6 ? '#'.repeat(headingLevel) + ' ' + text.trim() : text,
400
+ );
401
+ }
402
+ paraOut = '';
403
+ headingLevel = 0;
404
+ mode = 'normal';
405
+ buffer = '';
406
+ };
407
+
408
+ for (const item of walkBody(docXml) as FlowItem[]) {
409
+ switch (item.kind) {
410
+ case 'paraStart':
411
+ paraOut = '';
412
+ headingLevel = item.level;
413
+ mode = 'normal';
414
+ buffer = '';
415
+ break;
416
+ case 'paraEnd':
417
+ endParagraph();
418
+ break;
419
+ case 'text':
420
+ if (mode === 'normal') paraOut += item.text;
421
+ else buffer += item.text;
422
+ break;
423
+ case 'insStart':
424
+ mode = 'ins';
425
+ buffer = '';
426
+ break;
427
+ case 'insEnd':
428
+ flushSpan('{++', '++}', true);
429
+ mode = 'normal';
430
+ break;
431
+ case 'delStart':
432
+ mode = 'del';
433
+ buffer = '';
434
+ break;
435
+ case 'delEnd':
436
+ flushSpan('{--', '--}', false);
437
+ mode = 'normal';
438
+ break;
439
+ default:
440
+ break;
441
+ }
442
+ }
443
+
444
+ return {
445
+ text: paragraphs.join('\n\n'),
446
+ hasTrackChanges: insertions > 0 || deletions > 0,
447
+ stats: { insertions, deletions },
448
+ };
449
+ }
450
+
451
+ interface ExtractWithTrackChangesOptions {
452
+ mediaDir?: string;
453
+ }
454
+
455
+ /**
456
+ * Extract Word document content with track changes preserved as CriticMarkup
457
+ * Uses pandoc with track-changes=all option to preserve insertions/deletions
458
+ *
459
+ * @param docxPath - Path to Word document
460
+ * @param options - Options
461
+ * @returns Track changes result with text and stats
462
+ */
463
+ export async function extractWithTrackChanges(
464
+ docxPath: string,
465
+ options: ExtractWithTrackChangesOptions = {}
466
+ ): Promise<{ text: string; hasTrackChanges: boolean; stats: { insertions: number; deletions: number } }> {
467
+ const { mediaDir } = options;
468
+
469
+ if (!fs.existsSync(docxPath)) {
470
+ throw new Error(`File not found: ${docxPath}`);
471
+ }
472
+
473
+ const { execSync } = await import('child_process');
474
+
475
+ // Use pandoc with --track-changes=all to preserve track changes
476
+ // This outputs insertions as [insertion]{.insertion} and deletions as [deletion]{.deletion}
477
+ let pandocArgs = `"${docxPath}" -t markdown --wrap=none --track-changes=all`;
478
+ if (mediaDir) {
479
+ pandocArgs += ` --extract-media="${mediaDir}"`;
480
+ }
481
+
482
+ let text: string;
483
+ try {
484
+ text = execSync(`pandoc ${pandocArgs}`, {
485
+ encoding: 'utf-8',
486
+ maxBuffer: 50 * 1024 * 1024,
487
+ });
488
+ } catch (err: any) {
489
+ throw new Error(`Pandoc extraction failed: ${err.message}`);
490
+ }
491
+
492
+ // Count track changes from pandoc output
493
+ let insertions = 0;
494
+ let deletions = 0;
495
+
496
+ // Pandoc outputs track changes as:
497
+ // [inserted text]{.insertion author="..."}
498
+ // [deleted text]{.deletion author="..."}
499
+
500
+ // Convert pandoc's track change format to CriticMarkup
501
+ // Insertions: [text]{.insertion ...} -> {++text++}
502
+ text = text.replace(/\[([^\]]*)\]\{\.insertion[^}]*\}/g, (match, content) => {
503
+ if (content.trim()) {
504
+ insertions++;
505
+ return `{++${content}++}`;
506
+ }
507
+ return '';
508
+ });
509
+
510
+ // Deletions: [text]{.deletion ...} -> {--text--}
511
+ text = text.replace(/\[([^\]]*)\]\{\.deletion[^}]*\}/g, (match, content) => {
512
+ if (content.trim()) {
513
+ deletions++;
514
+ return `{--${content}--}`;
515
+ }
516
+ return '';
517
+ });
518
+
519
+ const hasTrackChanges = insertions > 0 || deletions > 0;
520
+
521
+ return {
522
+ text,
523
+ hasTrackChanges,
524
+ stats: { insertions, deletions },
525
+ };
526
+ }