docrev 0.11.3 → 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 (193) hide show
  1. package/CHANGELOG.md +22 -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/build.d.ts +43 -0
  6. package/dist/lib/build.d.ts.map +1 -1
  7. package/dist/lib/build.js +175 -5
  8. package/dist/lib/build.js.map +1 -1
  9. package/dist/lib/commands/build.d.ts.map +1 -1
  10. package/dist/lib/commands/build.js +39 -125
  11. package/dist/lib/commands/build.js.map +1 -1
  12. package/dist/lib/commands/comments.js +3 -3
  13. package/dist/lib/commands/comments.js.map +1 -1
  14. package/dist/lib/commands/context.d.ts +12 -1
  15. package/dist/lib/commands/context.d.ts.map +1 -1
  16. package/dist/lib/commands/context.js +34 -1
  17. package/dist/lib/commands/context.js.map +1 -1
  18. package/dist/lib/commands/merge-resolve.js +1 -1
  19. package/dist/lib/commands/merge-resolve.js.map +1 -1
  20. package/dist/lib/commands/quality.js +2 -2
  21. package/dist/lib/commands/quality.js.map +1 -1
  22. package/dist/lib/commands/sections.js +1 -1
  23. package/dist/lib/commands/sections.js.map +1 -1
  24. package/dist/lib/commands/text-ops.js +1 -1
  25. package/dist/lib/commands/text-ops.js.map +1 -1
  26. package/dist/lib/commands/utilities.d.ts.map +1 -1
  27. package/dist/lib/commands/utilities.js +5 -2
  28. package/dist/lib/commands/utilities.js.map +1 -1
  29. package/dist/lib/commands/word-tools.js +2 -2
  30. package/dist/lib/commands/word-tools.js.map +1 -1
  31. package/dist/lib/crossref.d.ts.map +1 -1
  32. package/dist/lib/crossref.js +12 -41
  33. package/dist/lib/crossref.js.map +1 -1
  34. package/dist/lib/csl.d.ts.map +1 -1
  35. package/dist/lib/csl.js +14 -22
  36. package/dist/lib/csl.js.map +1 -1
  37. package/dist/lib/diff-engine.js +1 -1
  38. package/dist/lib/diff-engine.js.map +1 -1
  39. package/dist/lib/equations.d.ts.map +1 -1
  40. package/dist/lib/equations.js +12 -3
  41. package/dist/lib/equations.js.map +1 -1
  42. package/dist/lib/errors.d.ts.map +1 -1
  43. package/dist/lib/errors.js +1 -30
  44. package/dist/lib/errors.js.map +1 -1
  45. package/dist/lib/image-registry.d.ts.map +1 -1
  46. package/dist/lib/image-registry.js +4 -0
  47. package/dist/lib/image-registry.js.map +1 -1
  48. package/dist/lib/import.d.ts +1 -1
  49. package/dist/lib/import.d.ts.map +1 -1
  50. package/dist/lib/import.js +1 -1
  51. package/dist/lib/import.js.map +1 -1
  52. package/dist/lib/input.d.ts +0 -11
  53. package/dist/lib/input.d.ts.map +1 -1
  54. package/dist/lib/input.js +0 -35
  55. package/dist/lib/input.js.map +1 -1
  56. package/dist/lib/merge.d.ts.map +1 -1
  57. package/dist/lib/merge.js +9 -1
  58. package/dist/lib/merge.js.map +1 -1
  59. package/dist/lib/ooxml.d.ts +22 -0
  60. package/dist/lib/ooxml.d.ts.map +1 -1
  61. package/dist/lib/ooxml.js +130 -0
  62. package/dist/lib/ooxml.js.map +1 -1
  63. package/dist/lib/orcid.d.ts.map +1 -1
  64. package/dist/lib/orcid.js +16 -7
  65. package/dist/lib/orcid.js.map +1 -1
  66. package/dist/lib/plugins.d.ts.map +1 -1
  67. package/dist/lib/plugins.js +8 -4
  68. package/dist/lib/plugins.js.map +1 -1
  69. package/dist/lib/protect-restore.d.ts.map +1 -1
  70. package/dist/lib/protect-restore.js +7 -5
  71. package/dist/lib/protect-restore.js.map +1 -1
  72. package/dist/lib/restore-references.d.ts.map +1 -1
  73. package/dist/lib/restore-references.js +25 -3
  74. package/dist/lib/restore-references.js.map +1 -1
  75. package/dist/lib/schema.d.ts.map +1 -1
  76. package/dist/lib/schema.js +1 -19
  77. package/dist/lib/schema.js.map +1 -1
  78. package/dist/lib/slides.js +1 -1
  79. package/dist/lib/slides.js.map +1 -1
  80. package/dist/lib/spelling.js +1 -1
  81. package/dist/lib/spelling.js.map +1 -1
  82. package/dist/lib/tui.js +0 -1
  83. package/dist/lib/tui.js.map +1 -1
  84. package/dist/lib/utils.d.ts +14 -0
  85. package/dist/lib/utils.d.ts.map +1 -1
  86. package/dist/lib/utils.js +41 -0
  87. package/dist/lib/utils.js.map +1 -1
  88. package/dist/lib/word-extraction.d.ts +5 -5
  89. package/dist/lib/word-extraction.d.ts.map +1 -1
  90. package/dist/lib/word-extraction.js +25 -106
  91. package/dist/lib/word-extraction.js.map +1 -1
  92. package/dist/lib/word.d.ts +14 -38
  93. package/dist/lib/word.d.ts.map +1 -1
  94. package/dist/lib/word.js +24 -179
  95. package/dist/lib/word.js.map +1 -1
  96. package/dist/lib/wordcomments.d.ts.map +1 -1
  97. package/dist/lib/wordcomments.js +9 -7
  98. package/dist/lib/wordcomments.js.map +1 -1
  99. package/package.json +17 -6
  100. package/.gitattributes +0 -1
  101. package/PLAN-tables-and-postprocess.md +0 -850
  102. package/bin/rev.ts +0 -145
  103. package/docs-src/build.py +0 -113
  104. package/docs-src/extra.css +0 -208
  105. package/docs-src/md-to-html.lua +0 -6
  106. package/docs-src/template.html +0 -116
  107. package/eslint.config.js +0 -27
  108. package/lib/anchor-match.ts +0 -307
  109. package/lib/annotations.ts +0 -695
  110. package/lib/build.ts +0 -2047
  111. package/lib/citations.ts +0 -160
  112. package/lib/commands/build.ts +0 -885
  113. package/lib/commands/citations.ts +0 -515
  114. package/lib/commands/comments.ts +0 -1077
  115. package/lib/commands/context.ts +0 -185
  116. package/lib/commands/core.ts +0 -328
  117. package/lib/commands/doi.ts +0 -451
  118. package/lib/commands/file-ops.ts +0 -372
  119. package/lib/commands/history.ts +0 -320
  120. package/lib/commands/index.ts +0 -87
  121. package/lib/commands/init.ts +0 -259
  122. package/lib/commands/merge-resolve.ts +0 -378
  123. package/lib/commands/preview.ts +0 -178
  124. package/lib/commands/project-info.ts +0 -244
  125. package/lib/commands/quality.ts +0 -517
  126. package/lib/commands/response.ts +0 -454
  127. package/lib/commands/section-boundaries.ts +0 -82
  128. package/lib/commands/sections.ts +0 -451
  129. package/lib/commands/sync.ts +0 -689
  130. package/lib/commands/text-ops.ts +0 -449
  131. package/lib/commands/utilities.ts +0 -448
  132. package/lib/commands/verify-anchors.ts +0 -272
  133. package/lib/commands/word-tools.ts +0 -340
  134. package/lib/comment-realign.ts +0 -99
  135. package/lib/config.ts +0 -84
  136. package/lib/crossref.ts +0 -781
  137. package/lib/csl.ts +0 -191
  138. package/lib/dependencies.ts +0 -132
  139. package/lib/diff-engine.ts +0 -465
  140. package/lib/doi-cache.ts +0 -115
  141. package/lib/doi.ts +0 -879
  142. package/lib/equations.ts +0 -506
  143. package/lib/errors.ts +0 -350
  144. package/lib/format.ts +0 -541
  145. package/lib/git.ts +0 -326
  146. package/lib/grammar.ts +0 -303
  147. package/lib/image-registry.ts +0 -180
  148. package/lib/import.ts +0 -932
  149. package/lib/input.ts +0 -174
  150. package/lib/journals.ts +0 -543
  151. package/lib/macro-filter.lua +0 -201
  152. package/lib/macros.ts +0 -273
  153. package/lib/merge.ts +0 -633
  154. package/lib/ooxml.ts +0 -768
  155. package/lib/orcid.ts +0 -144
  156. package/lib/pdf-comments.ts +0 -263
  157. package/lib/pdf-import.ts +0 -524
  158. package/lib/plugins.ts +0 -362
  159. package/lib/postprocess.ts +0 -188
  160. package/lib/pptx-color-filter.lua +0 -37
  161. package/lib/pptx-template.ts +0 -469
  162. package/lib/pptx-themes/academic.pptx +0 -0
  163. package/lib/pptx-themes/corporate.pptx +0 -0
  164. package/lib/pptx-themes/dark.pptx +0 -0
  165. package/lib/pptx-themes/default.pptx +0 -0
  166. package/lib/pptx-themes/minimal.pptx +0 -0
  167. package/lib/pptx-themes/plant.pptx +0 -0
  168. package/lib/pptx-themes.ts +0 -483
  169. package/lib/protect-restore.ts +0 -520
  170. package/lib/rate-limiter.ts +0 -127
  171. package/lib/response.ts +0 -200
  172. package/lib/restore-references.ts +0 -240
  173. package/lib/review.ts +0 -327
  174. package/lib/schema.ts +0 -494
  175. package/lib/scientific-words.ts +0 -73
  176. package/lib/sections.ts +0 -428
  177. package/lib/slides.ts +0 -756
  178. package/lib/spelling.ts +0 -334
  179. package/lib/templates.ts +0 -526
  180. package/lib/themes.ts +0 -742
  181. package/lib/trackchanges.ts +0 -166
  182. package/lib/tui.ts +0 -450
  183. package/lib/types.ts +0 -546
  184. package/lib/undo.ts +0 -250
  185. package/lib/utils.ts +0 -69
  186. package/lib/variables.ts +0 -179
  187. package/lib/word-extraction.ts +0 -525
  188. package/lib/word.ts +0 -526
  189. package/lib/wordcomments.ts +0 -789
  190. package/mkdocs.yml +0 -64
  191. package/scripts/postbuild.js +0 -47
  192. package/tsconfig.json +0 -26
  193. package/types/index.d.ts +0 -531
package/lib/import.ts DELETED
@@ -1,932 +0,0 @@
1
- /**
2
- * Import functionality - convert Word docs to annotated Markdown
3
- *
4
- * Orchestration workflows + re-exports from extraction/diff/restore modules
5
- */
6
-
7
- import * as fs from 'fs';
8
- import * as path from 'path';
9
- import { stripAnnotations } from './annotations.js';
10
- import { readImageRegistry } from './image-registry.js';
11
- import { exec } from 'child_process';
12
- import { promisify } from 'util';
13
-
14
- // Import from split modules
15
- import {
16
- extractFromWord,
17
- extractWordComments,
18
- extractCommentAnchors,
19
- extractWordTables,
20
- } from './word-extraction.js';
21
- import type {
22
- WordComment,
23
- CommentAnchorData,
24
- WordTable,
25
- ExtractFromWordResult,
26
- } from './word-extraction.js';
27
- import {
28
- generateSmartDiff,
29
- generateAnnotatedDiff,
30
- cleanupAnnotations,
31
- fixCitationAnnotations,
32
- } from './diff-engine.js';
33
- import {
34
- restoreCrossrefFromWord,
35
- restoreImagesFromRegistry,
36
- parseVisibleComments,
37
- convertVisibleComments,
38
- } from './restore-references.js';
39
- import { findAnchorInText, findAllOccurrences } from './anchor-match.js';
40
-
41
- /**
42
- * Pick the best position from candidate `occurrences` given the
43
- * surrounding `before` / `after` context from the docx, while
44
- * respecting `usedPositions` to avoid stacking distinct comments at
45
- * the same anchor instance.
46
- *
47
- * Returns the chosen position, or -1 if every candidate is already used.
48
- */
49
- function pickBestOccurrence(
50
- occurrences: number[],
51
- result: string,
52
- before: string,
53
- after: string,
54
- anchorLen: number,
55
- usedPositions: Set<number>,
56
- nearPos?: number,
57
- ): number {
58
- if (occurrences.length === 0) return -1;
59
- if (occurrences.length === 1) {
60
- return usedPositions.has(occurrences[0]) ? -1 : occurrences[0];
61
- }
62
-
63
- let bestIdx = occurrences.find(p => !usedPositions.has(p)) ?? -1;
64
- if (bestIdx < 0) return -1;
65
- let bestScore = -1;
66
-
67
- // Tie-break by context score first; when scores tie, prefer the occurrence
68
- // nearest `nearPos` (a section-position estimate) if given, else the
69
- // leftmost. The estimate only orders equally-scored candidates — it is never
70
- // the returned position.
71
- const closer = (pos: number, incumbent: number) =>
72
- nearPos === undefined ? pos < incumbent : Math.abs(pos - nearPos) < Math.abs(incumbent - nearPos);
73
-
74
- for (const pos of occurrences) {
75
- if (usedPositions.has(pos)) continue;
76
- let score = 0;
77
-
78
- if (before) {
79
- const contextBefore = result.slice(Math.max(0, pos - before.length - 20), pos).toLowerCase();
80
- const beforeLower = before.toLowerCase();
81
- const beforeWords = beforeLower.split(/\s+/).filter(w => w.length > 3);
82
- for (const word of beforeWords) {
83
- if (contextBefore.includes(word)) score += 2;
84
- }
85
- if (contextBefore.includes(beforeLower.slice(-30))) score += 5;
86
- }
87
-
88
- if (after) {
89
- const contextAfter = result.slice(pos + anchorLen, pos + anchorLen + after.length + 20).toLowerCase();
90
- const afterLower = after.toLowerCase();
91
- const afterWords = afterLower.split(/\s+/).filter(w => w.length > 3);
92
- for (const word of afterWords) {
93
- if (contextAfter.includes(word)) score += 2;
94
- }
95
- if (contextAfter.includes(afterLower.slice(0, 30))) score += 5;
96
- }
97
-
98
- if (score > bestScore || (score === bestScore && closer(pos, bestIdx))) {
99
- bestScore = score;
100
- bestIdx = pos;
101
- }
102
- }
103
-
104
- return bestIdx;
105
- }
106
-
107
- // Re-export everything so existing imports from './import.js' still work
108
- export {
109
- extractFromWord,
110
- extractWordComments,
111
- extractCommentAnchors,
112
- extractHeadings,
113
- extractWordTables,
114
- } from './word-extraction.js';
115
- export type {
116
- WordComment,
117
- TextNode,
118
- CommentAnchorData,
119
- CommentAnchorsResult,
120
- DocxHeading,
121
- WordTable,
122
- ParsedRow,
123
- ExtractFromWordOptions,
124
- ExtractMessage,
125
- ExtractFromWordResult,
126
- } from './word-extraction.js';
127
-
128
- export {
129
- generateSmartDiff,
130
- generateAnnotatedDiff,
131
- cleanupAnnotations,
132
- fixCitationAnnotations,
133
- } from './diff-engine.js';
134
- export type {
135
- GenerateSmartDiffOptions,
136
- } from './diff-engine.js';
137
-
138
- export {
139
- restoreCrossrefFromWord,
140
- restoreImagesFromRegistry,
141
- parseVisibleComments,
142
- convertVisibleComments,
143
- } from './restore-references.js';
144
- export type {
145
- RestoreCrossrefResult,
146
- RestoreImagesResult,
147
- } from './restore-references.js';
148
-
149
- const execAsync = promisify(exec);
150
-
151
- // ============================================
152
- // Type Definitions (orchestration-specific)
153
- // ============================================
154
-
155
- export interface InsertCommentsOptions {
156
- quiet?: boolean;
157
- sectionBoundary?: { start: number; end: number } | null;
158
- /**
159
- * When true (default), comments wrap their anchor text in `[anchor]{.mark}`
160
- * so the rebuilt docx restores the original Word comment range. When false,
161
- * comments are inserted as standalone `{>>...<<}` blocks adjacent to the
162
- * anchor — the prose stays byte-identical except for the inserted blocks.
163
- *
164
- * Set to false from `sync --comments-only` so a draft revised after the
165
- * docx was sent for review keeps its prose intact, and so multiple
166
- * comments sharing one anchor don't produce nested broken markup.
167
- */
168
- wrapAnchor?: boolean;
169
- /**
170
- * Mutable output: when provided, the function fills in counters so callers
171
- * can distinguish placement outcomes in their summary. `placed` counts
172
- * insertions made at a matched anchor or context, `lowConfidence` counts
173
- * insertions made at an approximate (proportional / context-only) position
174
- * that should be reviewed with `rev verify-anchors`, `deduped` counts
175
- * comments already present at their anchor (skipped on re-sync), and
176
- * `unmatched` counts comments that could not be placed at all.
177
- */
178
- outStats?: { placed: number; deduped: number; unmatched: number; lowConfidence?: number };
179
- }
180
-
181
- /**
182
- * Whether a resolved placement landed on matched anchor/context text (high) or
183
- * on an approximate position the user should review (low). Low-confidence
184
- * strategies place the comment without matching the anchor word itself.
185
- */
186
- function placementConfidence(strategy: string | undefined): 'high' | 'low' {
187
- if (!strategy) return 'high';
188
- return /proportional|position-only|context/.test(strategy) ? 'low' : 'high';
189
- }
190
-
191
- export interface CommentWithPos {
192
- id: string;
193
- author: string;
194
- text: string;
195
- date: string;
196
- pos: number;
197
- anchorText: string | null;
198
- anchorEnd?: number;
199
- isEmpty?: boolean;
200
- strategy?: string;
201
- }
202
-
203
- export type { AnchorSearchResult } from './anchor-match.js';
204
-
205
- export interface MarkdownPrefixResult {
206
- prefix: string;
207
- content: string;
208
- }
209
-
210
- export interface ImportWordWithTrackChangesOptions {
211
- mediaDir?: string;
212
- projectDir?: string;
213
- }
214
-
215
- export interface ImportWordWithTrackChangesResult {
216
- text: string;
217
- stats: {
218
- insertions: number;
219
- deletions: number;
220
- substitutions: number;
221
- comments: number;
222
- total: number;
223
- hasTrackChanges: boolean;
224
- trackChangeStats: { insertions: number; deletions: number };
225
- };
226
- extractedMedia: string[];
227
- comments: WordComment[];
228
- }
229
-
230
- export interface ImportFromWordOptions {
231
- author?: string;
232
- sectionContent?: string;
233
- figuresDir?: string;
234
- wordTables?: WordTable[];
235
- }
236
-
237
- export interface ImportFromWordResult {
238
- annotated: string;
239
- stats: {
240
- insertions: number;
241
- deletions: number;
242
- substitutions: number;
243
- comments: number;
244
- total: number;
245
- };
246
- extractedMedia: string[];
247
- }
248
-
249
- export interface MovedFile {
250
- from: string;
251
- to: string;
252
- name: string;
253
- }
254
-
255
- export interface MoveExtractedMediaResult {
256
- moved: MovedFile[];
257
- errors: string[];
258
- }
259
-
260
- // ============================================
261
- // Functions
262
- // ============================================
263
-
264
- /**
265
- * If `pos` lands inside a section file's leading `# Heading` line (or the
266
- * blank line right after it), advance past the first paragraph break so
267
- * the comment stays inside the section. A comment authored at the very
268
- * start of a Word section maps to `pos === 0`, but inserting at column 0
269
- * of a markdown file that begins with `# Heading` puts the `{>>...<<}`
270
- * before the heading marker — Pandoc then treats the line as ordinary
271
- * paragraph text and the comment renders in the previous section.
272
- */
273
- function pushPastSectionHeading(text: string, pos: number): number {
274
- if (pos > 0) {
275
- const headingMatch = text.match(/^#{1,6}\s.+$/m);
276
- if (!headingMatch || headingMatch.index === undefined) return pos;
277
- const headingEnd = headingMatch.index + headingMatch[0].length;
278
- if (pos >= headingEnd) return pos;
279
- }
280
- // pos is at-or-before the first heading line. Advance to the first
281
- // non-blank position after the heading paragraph.
282
- const headingLine = text.match(/^#{1,6}\s.+(?:\n|$)/m);
283
- if (!headingLine || headingLine.index === undefined) return pos;
284
- let after = headingLine.index + headingLine[0].length;
285
- // Skip blank lines so we land at the start of the first body paragraph.
286
- while (after < text.length && (text[after] === '\n' || text[after] === '\r')) {
287
- after++;
288
- }
289
- return after;
290
- }
291
-
292
- /**
293
- * Snap a position to the nearest whitespace boundary within ±50 chars so a
294
- * proportional fallback insertion never lands mid-word.
295
- */
296
- function snapToWordBoundary(text: string, pos: number): number {
297
- if (pos <= 0) return 0;
298
- if (pos >= text.length) return text.length;
299
- if (/\s/.test(text[pos] ?? '')) return pos;
300
- for (let d = 1; d <= 50; d++) {
301
- if (pos + d < text.length && /\s/.test(text[pos + d] ?? '')) return pos + d;
302
- if (pos - d >= 0 && /\s/.test(text[pos - d] ?? '')) return pos - d;
303
- }
304
- return pos;
305
- }
306
-
307
- /**
308
- * Final-resort placement when every text-matching strategy failed. The docx
309
- * carries a real `<w:commentRangeStart w:id="N">` marker at a known offset
310
- * inside its body text — that's a structural anchor, even if the anchored
311
- * span itself is empty and the surrounding context drifted in the target.
312
- *
313
- * Map docPosition into the target markdown proportionally and snap to a word
314
- * boundary. This is approximate when the document was heavily restructured,
315
- * but it's strictly better than silently dropping a reviewer's comment: the
316
- * comment lands in roughly the right neighborhood and the reviewer can
317
- * relocate it during their next pass.
318
- */
319
- function proportionalFallback(
320
- anchorData: CommentAnchorData,
321
- target: string,
322
- ): number | null {
323
- if (anchorData.docLength <= 0) return null;
324
- const proportion = Math.min(anchorData.docPosition / anchorData.docLength, 1.0);
325
- const rawPos = Math.floor(proportion * target.length);
326
- return pushPastSectionHeading(target, snapToWordBoundary(target, rawPos));
327
- }
328
-
329
- /**
330
- * Insert comments into markdown text based on anchor texts with context
331
- */
332
- export function insertCommentsIntoMarkdown(
333
- markdown: string,
334
- comments: WordComment[],
335
- anchors: Map<string, CommentAnchorData | string>,
336
- options: InsertCommentsOptions = {}
337
- ): string {
338
- const { quiet = false, sectionBoundary = null, wrapAnchor = true, outStats } = options;
339
- let result = markdown;
340
- let unmatchedCount = 0;
341
- let placedCount = 0;
342
- let lowConfidenceCount = 0;
343
- const duplicateWarnings: string[] = [];
344
- const usedPositions = new Set<number>(); // For tie-breaking: track used positions
345
-
346
- // Resolve threading: replies share their parent's anchor in Word, so they
347
- // must inherit the parent's position and ride alongside it as one cluster.
348
- // Letting each reply run through anchor scoring scatters the cluster (the
349
- // same docPosition forces `usedPositions` to push later replies onto a
350
- // different occurrence), which on re-build looks like independent comments
351
- // and loses the paraIdParent threading. See gcol33/docrev issue #2.
352
- const inputById = new Map<string, WordComment>();
353
- for (const c of comments) inputById.set(c.id, c);
354
- function rootIdOf(c: WordComment): string {
355
- let cur: WordComment = c;
356
- const seen = new Set<string>();
357
- while (cur.parentId && !seen.has(cur.id)) {
358
- seen.add(cur.id);
359
- const parent = inputById.get(cur.parentId);
360
- if (!parent || parent === cur) break;
361
- cur = parent;
362
- }
363
- return cur.id;
364
- }
365
- const replyRootId = new Map<string, string>();
366
- for (const c of comments) {
367
- const root = rootIdOf(c);
368
- if (root !== c.id) replyRootId.set(c.id, root);
369
- }
370
-
371
- // Anchor matching primitives live in lib/anchor-match.ts so that
372
- // `rev verify-anchors` can use the same strategies for drift reporting.
373
-
374
- // Get all positions in order (for sequential tie-breaking).
375
- // Replies skip scoring entirely — they piggyback on their root's position
376
- // in the emit pass below.
377
- const commentsWithPositions = comments.map((c): CommentWithPos => {
378
- if (replyRootId.has(c.id)) {
379
- return { ...c, pos: -1, anchorText: null, strategy: 'reply' };
380
- }
381
- const anchorData = anchors.get(c.id);
382
- if (!anchorData) {
383
- unmatchedCount++;
384
- return { ...c, pos: -1, anchorText: null };
385
- }
386
-
387
- // Support both old format (string) and new format ({anchor, before, after})
388
- const anchor = typeof anchorData === 'string' ? anchorData : anchorData.anchor;
389
- const before = typeof anchorData === 'object' ? anchorData.before : '';
390
- const after = typeof anchorData === 'object' ? anchorData.after : '';
391
- const isEmpty = typeof anchorData === 'object' && anchorData.isEmpty;
392
- const docPosition = typeof anchorData === 'object' ? anchorData.docPosition : undefined;
393
-
394
- // Section-scoped insertion. The comment is known to belong to this
395
- // section (the caller already filtered by docPosition); place it by
396
- // matching its anchor text within the section, never by mapping a length
397
- // ratio across the docx and markdown coordinate systems. The section
398
- // estimate only seeds duplicate tie-breaking and the final fallback.
399
- if (sectionBoundary && docPosition !== undefined) {
400
- const sectionLength = sectionBoundary.end - sectionBoundary.start;
401
- if (sectionLength > 0) {
402
- const relativePos = docPosition < sectionBoundary.start ? 0 : docPosition - sectionBoundary.start;
403
- const proportion = Math.min(relativePos / sectionLength, 1.0);
404
- const estimatedPos = Math.floor(proportion * result.length);
405
-
406
- // Empty anchor: before/after context is the only text signal.
407
- if ((!anchor || isEmpty) && (before || after)) {
408
- const ctx = findAnchorInText('', result, before, after);
409
- if (ctx.occurrences.length > 0) {
410
- const pos = pushPastSectionHeading(result, ctx.occurrences[0]);
411
- return { ...c, pos, anchorText: null, isEmpty: true, strategy: `ctx:${ctx.strategy}` };
412
- }
413
- }
414
-
415
- // Non-empty anchor: locate it anywhere in the section text. Duplicates
416
- // are disambiguated by before/after context and, only as a tie-break,
417
- // by proximity to the section estimate.
418
- if (anchor && !isEmpty) {
419
- const { occurrences, matchedAnchor, strategy } = findAnchorInText(anchor, result, before, after);
420
- if (occurrences.length > 0) {
421
- const anchorLen = matchedAnchor ? matchedAnchor.length : 0;
422
- const chosen = pickBestOccurrence(occurrences, result, before, after, anchorLen, usedPositions, estimatedPos);
423
- const finalIdx = chosen >= 0 ? chosen : occurrences[0];
424
- if (occurrences.length > 1 && matchedAnchor) {
425
- duplicateWarnings.push(`"${matchedAnchor.slice(0, 40)}${matchedAnchor.length > 40 ? '...' : ''}" appears ${occurrences.length} times in section`);
426
- }
427
- usedPositions.add(finalIdx);
428
- if (matchedAnchor) {
429
- return { ...c, pos: finalIdx, anchorText: matchedAnchor, anchorEnd: finalIdx + anchorLen, strategy: `section:${strategy}` };
430
- }
431
- return { ...c, pos: finalIdx, anchorText: null, strategy: `section:${strategy}` };
432
- }
433
- }
434
-
435
- // Anchor text and context are both gone from the section. The docx
436
- // marker's offset is the only remaining signal: map it proportionally
437
- // and snap to a word boundary. This is an approximate placement, not a
438
- // match — flagged low-confidence so the summary surfaces it.
439
- const insertPos = pushPastSectionHeading(result, snapToWordBoundary(result, estimatedPos));
440
- return { ...c, pos: insertPos, anchorText: null, strategy: 'proportional-fallback' };
441
- }
442
- }
443
-
444
- // Handle empty anchors
445
- if (!anchor || isEmpty) {
446
- if (before || after) {
447
- const { occurrences } = findAnchorInText('', result, before, after);
448
- if (occurrences.length > 0) {
449
- return { ...c, pos: occurrences[0], anchorText: null, isEmpty: true };
450
- }
451
- }
452
- // Last resort: docx carried a structural marker at docPosition; map
453
- // it proportionally into the target so the comment isn't dropped.
454
- if (typeof anchorData === 'object') {
455
- const fallback = proportionalFallback(anchorData, result);
456
- if (fallback !== null) {
457
- return { ...c, pos: fallback, anchorText: null, isEmpty: true, strategy: 'proportional-fallback' };
458
- }
459
- }
460
- unmatchedCount++;
461
- return { ...c, pos: -1, anchorText: null, isEmpty: true };
462
- }
463
-
464
- // Text-based matching strategies
465
- const { occurrences, matchedAnchor, strategy, stripped } = findAnchorInText(anchor, result, before, after);
466
-
467
- if (occurrences.length === 0) {
468
- // Same last-resort as the empty-anchor path: anchor text is gone from
469
- // the target, but the marker's text-offset survived extraction.
470
- if (typeof anchorData === 'object') {
471
- const fallback = proportionalFallback(anchorData, result);
472
- if (fallback !== null) {
473
- return { ...c, pos: fallback, anchorText: null, strategy: 'proportional-fallback' };
474
- }
475
- }
476
- unmatchedCount++;
477
- return { ...c, pos: -1, anchorText: null };
478
- }
479
-
480
- const anchorLen = matchedAnchor ? matchedAnchor.length : 0;
481
-
482
- if (occurrences.length === 1) {
483
- if (matchedAnchor) {
484
- return { ...c, pos: occurrences[0], anchorText: matchedAnchor, anchorEnd: occurrences[0] + anchorLen };
485
- } else {
486
- return { ...c, pos: occurrences[0], anchorText: null };
487
- }
488
- }
489
-
490
- // Multiple occurrences - use context for disambiguation
491
- if (matchedAnchor) {
492
- duplicateWarnings.push(`"${matchedAnchor.slice(0, 40)}${matchedAnchor.length > 40 ? '...' : ''}" appears ${occurrences.length} times`);
493
- }
494
-
495
- const bestIdx = pickBestOccurrence(occurrences, result, before, after, anchorLen, usedPositions);
496
- const finalIdx = bestIdx >= 0 ? bestIdx : occurrences[0];
497
- usedPositions.add(finalIdx);
498
-
499
- if (matchedAnchor) {
500
- return { ...c, pos: finalIdx, anchorText: matchedAnchor, anchorEnd: finalIdx + anchorLen };
501
- } else {
502
- return { ...c, pos: finalIdx, anchorText: null };
503
- }
504
- });
505
-
506
- // Group comments into clusters (root + ordered replies). The root carries
507
- // the resolved position; replies inherit it and ride along in input order
508
- // so the rebuilt CriticMarkup looks like `{>>p<<}{>>r1<<}{>>r2<<}[anchor]`
509
- // and adjacency-based reply detection picks the cluster up again.
510
- const byId = new Map<string, CommentWithPos>();
511
- for (const cwp of commentsWithPositions) byId.set(cwp.id, cwp);
512
- const repliesByRoot = new Map<string, CommentWithPos[]>();
513
- for (const c of comments) {
514
- const rootId = replyRootId.get(c.id);
515
- if (!rootId) continue;
516
- const cwp = byId.get(c.id);
517
- if (!cwp) continue;
518
- const list = repliesByRoot.get(rootId);
519
- if (list) list.push(cwp);
520
- else repliesByRoot.set(rootId, [cwp]);
521
- }
522
-
523
- // Replies whose root never resolved (parent missing from the input slice or
524
- // parent unmatched) count as unmatched too — there's no position to attach
525
- // them to.
526
- for (const [rootId, replies] of repliesByRoot) {
527
- const root = byId.get(rootId);
528
- if (!root || root.pos < 0) {
529
- unmatchedCount += replies.length;
530
- }
531
- }
532
-
533
- // Roots only — replies attach during emission.
534
- const rootsWithPos = commentsWithPositions.filter(
535
- c => !replyRootId.has(c.id)
536
- );
537
-
538
- // Log any unmatched roots for debugging
539
- const unmatched = rootsWithPos.filter((c) => c.pos < 0);
540
- if (process.env.DEBUG) {
541
- console.log(`[DEBUG] insertComments: ${comments.length} input, ${rootsWithPos.length} roots, ${unmatched.length} unmatched roots, ${replyRootId.size} replies`);
542
- if (unmatched.length > 0) {
543
- unmatched.forEach(c => console.log(`[DEBUG] Unmatched ID=${c.id}: anchor="${(c.anchorText || 'none').slice(0,30)}"`));
544
- }
545
- }
546
-
547
- const matchedRoots = rootsWithPos.filter((c) => c.pos >= 0);
548
-
549
- // Sort by position descending (insert from end to avoid offset issues)
550
- matchedRoots.sort((a, b) => b.pos - a.pos);
551
-
552
- // Insert each cluster. With `wrapAnchor` (the default), the anchor text
553
- // gets wrapped in `[anchor]{.mark}` so the rebuilt docx restores the
554
- // original Word comment range. Without it, the comment block is inserted
555
- // adjacent to the anchor and prose stays untouched — required for
556
- // comments-only sync where multiple comments may share one anchor.
557
- // Skip insertion when the parent's CriticMarkup already lives near the
558
- // target — re-running sync against the same docx would otherwise stack
559
- // duplicates. A 200-char window catches both wrapped
560
- // (`{>>...<<}[anchor]{.mark}`) and bare (`{>>...<<}anchor`) forms while
561
- // ignoring incidental matches farther away.
562
- let dedupedCount = 0;
563
- for (const c of matchedRoots) {
564
- const parentBlock = `{>>${c.author}: ${c.text}<<}`;
565
- const replies = repliesByRoot.get(c.id) ?? [];
566
- const windowStart = Math.max(0, c.pos - 200);
567
- const windowEnd = Math.min(result.length, c.pos + 200);
568
- if (result.slice(windowStart, windowEnd).includes(parentBlock)) {
569
- // Cluster already synced; treat all members as deduped.
570
- dedupedCount += 1 + replies.length;
571
- continue;
572
- }
573
- // Replies carry an explicit `↪ ` author prefix so the round-trip does not
574
- // depend on positional adjacency in the markdown. On dense reviewer docs
575
- // distinct clusters frequently land at the same anchor position; without
576
- // the prefix the re-parse would misthread them. The injection side strips
577
- // `↪ ` back off the author so Word renders the original name.
578
- const replyBlocks = replies.map(r => `{>>↪ ${r.author}: ${r.text}<<}`);
579
- const combined = parentBlock + replyBlocks.join('');
580
- if (wrapAnchor && c.anchorText && c.anchorEnd) {
581
- const before = result.slice(0, c.pos);
582
- const anchor = result.slice(c.pos, c.anchorEnd);
583
- const after = result.slice(c.anchorEnd);
584
- result = before + combined + `[${anchor}]{.mark}` + after;
585
- } else {
586
- result = result.slice(0, c.pos) + combined + result.slice(c.pos);
587
- }
588
- // Replies inherit the root's confidence — they ride the same position.
589
- if (placementConfidence(c.strategy) === 'low') {
590
- lowConfidenceCount += 1 + replies.length;
591
- } else {
592
- placedCount += 1 + replies.length;
593
- }
594
- }
595
-
596
- if (outStats) {
597
- outStats.placed = placedCount;
598
- outStats.lowConfidence = lowConfidenceCount;
599
- outStats.deduped = dedupedCount;
600
- outStats.unmatched = unmatchedCount;
601
- }
602
-
603
- // Log warnings unless quiet mode
604
- if (!quiet) {
605
- if (unmatchedCount > 0) {
606
- console.warn(`Warning: ${unmatchedCount} comment(s) could not be matched to anchor text`);
607
- }
608
- if (lowConfidenceCount > 0) {
609
- console.warn(`Note: ${lowConfidenceCount} comment(s) placed approximately — run \`rev verify-anchors\` to review`);
610
- }
611
- if (dedupedCount > 0) {
612
- console.warn(`Note: ${dedupedCount} comment(s) already present at anchor — skipped to avoid duplication`);
613
- }
614
- if (duplicateWarnings.length > 0) {
615
- console.warn(`Warning: Duplicate anchor text found (using context & tie-breaks for placement):`);
616
- for (const w of duplicateWarnings) {
617
- console.warn(` - ${w}`);
618
- }
619
- }
620
- }
621
-
622
- return result;
623
- }
624
-
625
- /**
626
- * Import Word document with track changes directly as CriticMarkup
627
- */
628
- export async function importWordWithTrackChanges(
629
- docxPath: string,
630
- options: ImportWordWithTrackChangesOptions = {}
631
- ): Promise<ImportWordWithTrackChangesResult> {
632
- const { mediaDir, projectDir } = options;
633
- const docxDir = path.dirname(docxPath);
634
- const targetMediaDir = mediaDir || path.join(docxDir, 'media');
635
- const targetProjectDir = projectDir || docxDir;
636
-
637
- const registry = readImageRegistry(targetProjectDir);
638
- const hasRegistry = registry && registry.figures && registry.figures.length > 0;
639
-
640
- // First pass: count images
641
- const { stdout: rawText } = await execAsync(
642
- `pandoc "${docxPath}" -t markdown --wrap=none --track-changes=all`,
643
- { maxBuffer: 50 * 1024 * 1024 }
644
- );
645
-
646
- const wordImageCount = (rawText.match(/!\[[^\]]*\]\(media\/[^)]+\)/g) || []).length;
647
- const registryCount = hasRegistry ? registry.figures.length : 0;
648
-
649
- const needsMediaExtraction = wordImageCount > registryCount;
650
-
651
- if (hasRegistry) {
652
- console.log(`Registry has ${registryCount} figures, Word doc has ${wordImageCount} images`);
653
- if (needsMediaExtraction) {
654
- console.log(`Extracting media (${wordImageCount - registryCount} new image(s) detected)`);
655
- } else {
656
- console.log(`Using existing figures from registry`);
657
- }
658
- }
659
-
660
- // Extract from Word
661
- const extracted = await extractFromWord(docxPath, {
662
- mediaDir: targetMediaDir,
663
- skipMediaExtraction: !needsMediaExtraction,
664
- });
665
-
666
- let text = extracted.text;
667
- const extractedMedia = extracted.extractedMedia || [];
668
- const comments = extracted.comments || [];
669
- const anchors = extracted.anchors || new Map();
670
-
671
- // Log messages
672
- for (const msg of extracted.messages || []) {
673
- if (msg.type === 'info') {
674
- console.log(msg.message);
675
- } else if (msg.type === 'warning') {
676
- console.warn(`Warning: ${msg.message}`);
677
- }
678
- }
679
-
680
- // Restore crossref
681
- const crossrefResult = restoreCrossrefFromWord(text, targetProjectDir);
682
- text = crossrefResult.text;
683
- if (crossrefResult.restored > 0) {
684
- console.log(`Restored ${crossrefResult.restored} crossref reference(s)`);
685
- }
686
-
687
- // Restore images
688
- const imageRestoreResult = restoreImagesFromRegistry(text, targetProjectDir, crossrefResult.restoredLabels);
689
- text = imageRestoreResult.text;
690
- if (imageRestoreResult.restored > 0) {
691
- console.log(`Restored ${imageRestoreResult.restored} image(s) from registry`);
692
- }
693
-
694
- // Insert comments
695
- if (comments.length > 0) {
696
- text = insertCommentsIntoMarkdown(text, comments, anchors);
697
- console.log(`Inserted ${comments.length} comment(s)`);
698
- }
699
-
700
- // Clean up
701
- text = cleanupAnnotations(text);
702
-
703
- // Count final changes
704
- const insertions = (text.match(/\{\+\+/g) || []).length;
705
- const deletions = (text.match(/\{--/g) || []).length;
706
- const substitutions = (text.match(/\{~~/g) || []).length;
707
- const commentCount = (text.match(/\{>>/g) || []).length;
708
-
709
- return {
710
- text,
711
- stats: {
712
- insertions,
713
- deletions,
714
- substitutions,
715
- comments: commentCount,
716
- total: insertions + deletions + substitutions + commentCount,
717
- hasTrackChanges: extracted.hasTrackChanges,
718
- trackChangeStats: extracted.trackChangeStats,
719
- },
720
- extractedMedia,
721
- comments,
722
- };
723
- }
724
-
725
- /**
726
- * Read a `.docx` into the same CriticMarkup-annotated Markdown that
727
- * `rev import` produces, for read-only inspection (`rev status`, `rev
728
- * comments`, ...). Track changes become `{++..++}` / `{--..--}` / `{~~..~~}`
729
- * and comments are placed at their anchors, so `countAnnotations` /
730
- * `getComments` and the rest operate on it exactly as they do on imported
731
- * Markdown — the numbers match a subsequent `rev import`.
732
- *
733
- * Side-effect free: no media is extracted, nothing is written to disk, and
734
- * extraction/placement messages are discarded (the caller only wants the
735
- * annotated text). Works with or without pandoc — the XML fallback emits the
736
- * same CriticMarkup — so it never reads the binary ZIP as text.
737
- */
738
- export async function readDocxAsAnnotatedMarkdown(docxPath: string): Promise<string> {
739
- const extracted = await extractFromWord(docxPath, { skipMediaExtraction: true });
740
- let text = extracted.text;
741
- const comments = extracted.comments || [];
742
- const anchors = extracted.anchors || new Map();
743
-
744
- if (comments.length > 0) {
745
- text = insertCommentsIntoMarkdown(text, comments, anchors, { quiet: true });
746
- }
747
-
748
- return cleanupAnnotations(text);
749
- }
750
-
751
- /**
752
- * Legacy import function: Word doc → annotated MD via diff
753
- */
754
- export async function importFromWord(
755
- docxPath: string,
756
- originalMdPath: string,
757
- options: ImportFromWordOptions = {}
758
- ): Promise<ImportFromWordResult> {
759
- const { author = 'Reviewer', sectionContent, figuresDir } = options;
760
- const projectDir = path.dirname(originalMdPath);
761
-
762
- let wordText: string;
763
- let extractedMedia: string[] = [];
764
- let wordTables: WordTable[] = options.wordTables || [];
765
- let hasTrackChanges = false;
766
-
767
- if (sectionContent !== undefined) {
768
- let annotated = cleanupAnnotations(sectionContent);
769
-
770
- const insertions = (annotated.match(/\{\+\+/g) || []).length;
771
- const deletions = (annotated.match(/\{--/g) || []).length;
772
- const substitutions = (annotated.match(/\{~~/g) || []).length;
773
- const commentCount = (annotated.match(/\{>>/g) || []).length;
774
-
775
- return {
776
- annotated,
777
- stats: {
778
- insertions,
779
- deletions,
780
- substitutions,
781
- comments: commentCount,
782
- total: insertions + deletions + substitutions + commentCount,
783
- },
784
- extractedMedia: [],
785
- };
786
- } else {
787
- const docxDir = path.dirname(docxPath);
788
- const mediaDir = figuresDir || docxDir;
789
-
790
- const extracted = await extractFromWord(docxPath, { mediaDir });
791
- wordText = extracted.text;
792
- extractedMedia = extracted.extractedMedia || [];
793
- wordTables = extracted.tables || [];
794
- hasTrackChanges = extracted.hasTrackChanges || false;
795
-
796
- for (const msg of extracted.messages || []) {
797
- if (msg.type === 'info') {
798
- console.log(msg.message);
799
- } else if (msg.type === 'warning') {
800
- console.warn(`Warning: ${msg.message}`);
801
- }
802
- }
803
-
804
- if (hasTrackChanges) {
805
- const crossrefResult = restoreCrossrefFromWord(wordText, projectDir);
806
- wordText = crossrefResult.text;
807
- if (crossrefResult.restored > 0) {
808
- console.log(`Restored ${crossrefResult.restored} crossref reference(s)`);
809
- }
810
-
811
- const imageRestoreResult = restoreImagesFromRegistry(wordText, projectDir, crossrefResult.restoredLabels);
812
- wordText = imageRestoreResult.text;
813
- if (imageRestoreResult.restored > 0) {
814
- console.log(`Restored ${imageRestoreResult.restored} image(s) from registry`);
815
- }
816
-
817
- const comments = extracted.comments || [];
818
- const anchors = extracted.anchors || new Map();
819
- if (comments.length > 0) {
820
- wordText = insertCommentsIntoMarkdown(wordText, comments, anchors);
821
- console.log(`Inserted ${comments.length} comment(s)`);
822
- }
823
-
824
- wordText = cleanupAnnotations(wordText);
825
-
826
- const insertions = (wordText.match(/\{\+\+/g) || []).length;
827
- const deletions = (wordText.match(/\{--/g) || []).length;
828
- const substitutions = (wordText.match(/\{~~/g) || []).length;
829
- const commentCount = (wordText.match(/\{>>/g) || []).length;
830
-
831
- return {
832
- annotated: wordText,
833
- stats: {
834
- insertions,
835
- deletions,
836
- substitutions,
837
- comments: commentCount,
838
- total: insertions + deletions + substitutions + commentCount,
839
- },
840
- extractedMedia,
841
- };
842
- }
843
-
844
- console.warn('Warning: No track changes detected in Word document.');
845
- console.warn(' For best results, reviewers should use Track Changes in Word.');
846
- console.warn(' Falling back to diff-based import (comparing against original MD).');
847
- console.warn(' This approach may produce less accurate change annotations.');
848
-
849
- const crossrefResult = restoreCrossrefFromWord(wordText, projectDir);
850
- wordText = crossrefResult.text;
851
- if (crossrefResult.restored > 0) {
852
- console.log(`Restored ${crossrefResult.restored} crossref reference(s)`);
853
- }
854
-
855
- const imageRestoreResult = restoreImagesFromRegistry(wordText, projectDir, crossrefResult.restoredLabels);
856
- wordText = imageRestoreResult.text;
857
- if (imageRestoreResult.restored > 0) {
858
- console.log(`Restored ${imageRestoreResult.restored} image(s) from registry`);
859
- }
860
- }
861
-
862
- // Read original markdown
863
- let originalMd = fs.readFileSync(originalMdPath, 'utf-8');
864
-
865
- // Strip existing annotations
866
- originalMd = stripAnnotations(originalMd, { keepComments: false });
867
-
868
- // Load image registry
869
- const imageRegistry = readImageRegistry(projectDir);
870
-
871
- // Generate diff
872
- let annotated = generateSmartDiff(originalMd, wordText, author, { wordTables, imageRegistry });
873
-
874
- // Clean up
875
- annotated = cleanupAnnotations(annotated);
876
-
877
- // Fix citation annotations
878
- annotated = fixCitationAnnotations(annotated, originalMd);
879
-
880
- // Convert visible comments
881
- annotated = convertVisibleComments(annotated);
882
-
883
- // Count changes
884
- const insertions = (annotated.match(/\{\+\+/g) || []).length;
885
- const deletions = (annotated.match(/\{--/g) || []).length;
886
- const substitutions = (annotated.match(/\{~~/g) || []).length;
887
- const comments = (annotated.match(/\{>>/g) || []).length;
888
-
889
- return {
890
- annotated,
891
- stats: {
892
- insertions,
893
- deletions,
894
- substitutions,
895
- comments,
896
- total: insertions + deletions + substitutions + comments,
897
- },
898
- extractedMedia,
899
- };
900
- }
901
-
902
- /**
903
- * Move extracted media files to a figures directory with better names
904
- */
905
- export function moveExtractedMedia(
906
- mediaFiles: string[],
907
- figuresDir: string,
908
- prefix: string = 'figure'
909
- ): MoveExtractedMediaResult {
910
- const moved: MovedFile[] = [];
911
- const errors: string[] = [];
912
-
913
- if (!fs.existsSync(figuresDir)) {
914
- fs.mkdirSync(figuresDir, { recursive: true });
915
- }
916
-
917
- for (let i = 0; i < mediaFiles.length; i++) {
918
- const src = mediaFiles[i];
919
- const ext = path.extname(src).toLowerCase();
920
- const newName = `${prefix}${i + 1}${ext}`;
921
- const dest = path.join(figuresDir, newName);
922
-
923
- try {
924
- fs.copyFileSync(src, dest);
925
- moved.push({ from: src, to: dest, name: newName });
926
- } catch (err: any) {
927
- errors.push(`Failed to copy ${src}: ${err.message}`);
928
- }
929
- }
930
-
931
- return { moved, errors };
932
- }