docrev 0.11.2 → 0.12.0

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 (205) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/LICENSE +21 -0
  3. package/dist/bin/rev.js +29 -21
  4. package/dist/bin/rev.js.map +1 -1
  5. package/dist/lib/annotations.d.ts.map +1 -1
  6. package/dist/lib/annotations.js +28 -0
  7. package/dist/lib/annotations.js.map +1 -1
  8. package/dist/lib/build.d.ts +43 -0
  9. package/dist/lib/build.d.ts.map +1 -1
  10. package/dist/lib/build.js +175 -5
  11. package/dist/lib/build.js.map +1 -1
  12. package/dist/lib/commands/build.d.ts.map +1 -1
  13. package/dist/lib/commands/build.js +39 -125
  14. package/dist/lib/commands/build.js.map +1 -1
  15. package/dist/lib/commands/comments.d.ts.map +1 -1
  16. package/dist/lib/commands/comments.js +61 -31
  17. package/dist/lib/commands/comments.js.map +1 -1
  18. package/dist/lib/commands/context.d.ts +12 -0
  19. package/dist/lib/commands/context.d.ts.map +1 -1
  20. package/dist/lib/commands/context.js +34 -0
  21. package/dist/lib/commands/context.js.map +1 -1
  22. package/dist/lib/commands/core.d.ts.map +1 -1
  23. package/dist/lib/commands/core.js +25 -8
  24. package/dist/lib/commands/core.js.map +1 -1
  25. package/dist/lib/commands/merge-resolve.js +1 -1
  26. package/dist/lib/commands/merge-resolve.js.map +1 -1
  27. package/dist/lib/commands/quality.js +2 -2
  28. package/dist/lib/commands/quality.js.map +1 -1
  29. package/dist/lib/commands/response.js +1 -1
  30. package/dist/lib/commands/response.js.map +1 -1
  31. package/dist/lib/commands/sections.js +1 -1
  32. package/dist/lib/commands/sections.js.map +1 -1
  33. package/dist/lib/commands/text-ops.js +1 -1
  34. package/dist/lib/commands/text-ops.js.map +1 -1
  35. package/dist/lib/commands/utilities.d.ts.map +1 -1
  36. package/dist/lib/commands/utilities.js +5 -2
  37. package/dist/lib/commands/utilities.js.map +1 -1
  38. package/dist/lib/commands/word-tools.js +2 -2
  39. package/dist/lib/commands/word-tools.js.map +1 -1
  40. package/dist/lib/crossref.d.ts.map +1 -1
  41. package/dist/lib/crossref.js +12 -41
  42. package/dist/lib/crossref.js.map +1 -1
  43. package/dist/lib/csl.d.ts.map +1 -1
  44. package/dist/lib/csl.js +14 -22
  45. package/dist/lib/csl.js.map +1 -1
  46. package/dist/lib/diff-engine.js +1 -1
  47. package/dist/lib/diff-engine.js.map +1 -1
  48. package/dist/lib/equations.d.ts.map +1 -1
  49. package/dist/lib/equations.js +12 -3
  50. package/dist/lib/equations.js.map +1 -1
  51. package/dist/lib/errors.d.ts.map +1 -1
  52. package/dist/lib/errors.js +1 -30
  53. package/dist/lib/errors.js.map +1 -1
  54. package/dist/lib/image-registry.d.ts.map +1 -1
  55. package/dist/lib/image-registry.js +4 -0
  56. package/dist/lib/image-registry.js.map +1 -1
  57. package/dist/lib/import.d.ts +15 -1
  58. package/dist/lib/import.d.ts.map +1 -1
  59. package/dist/lib/import.js +24 -1
  60. package/dist/lib/import.js.map +1 -1
  61. package/dist/lib/input.d.ts +56 -0
  62. package/dist/lib/input.d.ts.map +1 -0
  63. package/dist/lib/input.js +129 -0
  64. package/dist/lib/input.js.map +1 -0
  65. package/dist/lib/merge.d.ts.map +1 -1
  66. package/dist/lib/merge.js +9 -1
  67. package/dist/lib/merge.js.map +1 -1
  68. package/dist/lib/ooxml.d.ts +22 -0
  69. package/dist/lib/ooxml.d.ts.map +1 -1
  70. package/dist/lib/ooxml.js +130 -0
  71. package/dist/lib/ooxml.js.map +1 -1
  72. package/dist/lib/orcid.d.ts.map +1 -1
  73. package/dist/lib/orcid.js +16 -7
  74. package/dist/lib/orcid.js.map +1 -1
  75. package/dist/lib/plugins.d.ts.map +1 -1
  76. package/dist/lib/plugins.js +8 -4
  77. package/dist/lib/plugins.js.map +1 -1
  78. package/dist/lib/protect-restore.d.ts.map +1 -1
  79. package/dist/lib/protect-restore.js +7 -5
  80. package/dist/lib/protect-restore.js.map +1 -1
  81. package/dist/lib/response.d.ts +1 -1
  82. package/dist/lib/response.d.ts.map +1 -1
  83. package/dist/lib/response.js +5 -2
  84. package/dist/lib/response.js.map +1 -1
  85. package/dist/lib/restore-references.d.ts.map +1 -1
  86. package/dist/lib/restore-references.js +25 -3
  87. package/dist/lib/restore-references.js.map +1 -1
  88. package/dist/lib/schema.d.ts.map +1 -1
  89. package/dist/lib/schema.js +1 -19
  90. package/dist/lib/schema.js.map +1 -1
  91. package/dist/lib/slides.js +1 -1
  92. package/dist/lib/slides.js.map +1 -1
  93. package/dist/lib/spelling.js +1 -1
  94. package/dist/lib/spelling.js.map +1 -1
  95. package/dist/lib/tui.js +0 -1
  96. package/dist/lib/tui.js.map +1 -1
  97. package/dist/lib/utils.d.ts +14 -0
  98. package/dist/lib/utils.d.ts.map +1 -1
  99. package/dist/lib/utils.js +41 -0
  100. package/dist/lib/utils.js.map +1 -1
  101. package/dist/lib/word-extraction.d.ts +5 -5
  102. package/dist/lib/word-extraction.d.ts.map +1 -1
  103. package/dist/lib/word-extraction.js +25 -106
  104. package/dist/lib/word-extraction.js.map +1 -1
  105. package/dist/lib/word.d.ts +14 -38
  106. package/dist/lib/word.d.ts.map +1 -1
  107. package/dist/lib/word.js +24 -179
  108. package/dist/lib/word.js.map +1 -1
  109. package/dist/lib/wordcomments.d.ts.map +1 -1
  110. package/dist/lib/wordcomments.js +9 -7
  111. package/dist/lib/wordcomments.js.map +1 -1
  112. package/package.json +17 -6
  113. package/.gitattributes +0 -1
  114. package/PLAN-tables-and-postprocess.md +0 -850
  115. package/bin/rev.ts +0 -145
  116. package/docs-src/build.py +0 -113
  117. package/docs-src/extra.css +0 -208
  118. package/docs-src/md-to-html.lua +0 -6
  119. package/docs-src/template.html +0 -116
  120. package/eslint.config.js +0 -27
  121. package/lib/anchor-match.ts +0 -307
  122. package/lib/annotations.ts +0 -664
  123. package/lib/build.ts +0 -2047
  124. package/lib/citations.ts +0 -160
  125. package/lib/commands/build.ts +0 -885
  126. package/lib/commands/citations.ts +0 -515
  127. package/lib/commands/comments.ts +0 -1050
  128. package/lib/commands/context.ts +0 -176
  129. package/lib/commands/core.ts +0 -309
  130. package/lib/commands/doi.ts +0 -451
  131. package/lib/commands/file-ops.ts +0 -372
  132. package/lib/commands/history.ts +0 -320
  133. package/lib/commands/index.ts +0 -87
  134. package/lib/commands/init.ts +0 -259
  135. package/lib/commands/merge-resolve.ts +0 -378
  136. package/lib/commands/preview.ts +0 -178
  137. package/lib/commands/project-info.ts +0 -244
  138. package/lib/commands/quality.ts +0 -517
  139. package/lib/commands/response.ts +0 -454
  140. package/lib/commands/section-boundaries.ts +0 -82
  141. package/lib/commands/sections.ts +0 -451
  142. package/lib/commands/sync.ts +0 -689
  143. package/lib/commands/text-ops.ts +0 -449
  144. package/lib/commands/utilities.ts +0 -448
  145. package/lib/commands/verify-anchors.ts +0 -272
  146. package/lib/commands/word-tools.ts +0 -340
  147. package/lib/comment-realign.ts +0 -99
  148. package/lib/config.ts +0 -84
  149. package/lib/crossref.ts +0 -781
  150. package/lib/csl.ts +0 -191
  151. package/lib/dependencies.ts +0 -132
  152. package/lib/diff-engine.ts +0 -465
  153. package/lib/doi-cache.ts +0 -115
  154. package/lib/doi.ts +0 -879
  155. package/lib/equations.ts +0 -506
  156. package/lib/errors.ts +0 -350
  157. package/lib/format.ts +0 -541
  158. package/lib/git.ts +0 -326
  159. package/lib/grammar.ts +0 -303
  160. package/lib/image-registry.ts +0 -180
  161. package/lib/import.ts +0 -906
  162. package/lib/journals.ts +0 -543
  163. package/lib/macro-filter.lua +0 -201
  164. package/lib/macros.ts +0 -273
  165. package/lib/merge.ts +0 -633
  166. package/lib/ooxml.ts +0 -768
  167. package/lib/orcid.ts +0 -144
  168. package/lib/pdf-comments.ts +0 -263
  169. package/lib/pdf-import.ts +0 -524
  170. package/lib/plugins.ts +0 -362
  171. package/lib/postprocess.ts +0 -188
  172. package/lib/pptx-color-filter.lua +0 -37
  173. package/lib/pptx-template.ts +0 -469
  174. package/lib/pptx-themes/academic.pptx +0 -0
  175. package/lib/pptx-themes/corporate.pptx +0 -0
  176. package/lib/pptx-themes/dark.pptx +0 -0
  177. package/lib/pptx-themes/default.pptx +0 -0
  178. package/lib/pptx-themes/minimal.pptx +0 -0
  179. package/lib/pptx-themes/plant.pptx +0 -0
  180. package/lib/pptx-themes.ts +0 -483
  181. package/lib/protect-restore.ts +0 -520
  182. package/lib/rate-limiter.ts +0 -127
  183. package/lib/response.ts +0 -197
  184. package/lib/restore-references.ts +0 -240
  185. package/lib/review.ts +0 -327
  186. package/lib/schema.ts +0 -494
  187. package/lib/scientific-words.ts +0 -73
  188. package/lib/sections.ts +0 -428
  189. package/lib/slides.ts +0 -756
  190. package/lib/spelling.ts +0 -334
  191. package/lib/templates.ts +0 -526
  192. package/lib/themes.ts +0 -742
  193. package/lib/trackchanges.ts +0 -166
  194. package/lib/tui.ts +0 -450
  195. package/lib/types.ts +0 -546
  196. package/lib/undo.ts +0 -250
  197. package/lib/utils.ts +0 -69
  198. package/lib/variables.ts +0 -179
  199. package/lib/word-extraction.ts +0 -525
  200. package/lib/word.ts +0 -526
  201. package/lib/wordcomments.ts +0 -789
  202. package/mkdocs.yml +0 -64
  203. package/scripts/postbuild.js +0 -47
  204. package/tsconfig.json +0 -26
  205. package/types/index.d.ts +0 -531
package/lib/word.ts DELETED
@@ -1,526 +0,0 @@
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
- }