docrev 0.10.1 → 0.11.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 (167) hide show
  1. package/.gitattributes +1 -1
  2. package/CHANGELOG.md +184 -173
  3. package/PLAN-tables-and-postprocess.md +850 -850
  4. package/README.md +431 -431
  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/anchor-match.d.ts +4 -2
  11. package/dist/lib/anchor-match.d.ts.map +1 -1
  12. package/dist/lib/anchor-match.js +52 -24
  13. package/dist/lib/anchor-match.js.map +1 -1
  14. package/dist/lib/annotations.d.ts +0 -6
  15. package/dist/lib/annotations.d.ts.map +1 -1
  16. package/dist/lib/annotations.js +26 -8
  17. package/dist/lib/annotations.js.map +1 -1
  18. package/dist/lib/build.d.ts.map +1 -1
  19. package/dist/lib/build.js +13 -10
  20. package/dist/lib/build.js.map +1 -1
  21. package/dist/lib/commands/build.d.ts.map +1 -1
  22. package/dist/lib/commands/build.js +5 -0
  23. package/dist/lib/commands/build.js.map +1 -1
  24. package/dist/lib/commands/doi.d.ts.map +1 -1
  25. package/dist/lib/commands/doi.js +14 -2
  26. package/dist/lib/commands/doi.js.map +1 -1
  27. package/dist/lib/commands/sync.d.ts.map +1 -1
  28. package/dist/lib/commands/sync.js +98 -107
  29. package/dist/lib/commands/sync.js.map +1 -1
  30. package/dist/lib/commands/utilities.js +164 -164
  31. package/dist/lib/commands/word-tools.js +8 -8
  32. package/dist/lib/comment-realign.d.ts +12 -21
  33. package/dist/lib/comment-realign.d.ts.map +1 -1
  34. package/dist/lib/comment-realign.js +40 -348
  35. package/dist/lib/comment-realign.js.map +1 -1
  36. package/dist/lib/dependencies.d.ts +10 -0
  37. package/dist/lib/dependencies.d.ts.map +1 -1
  38. package/dist/lib/dependencies.js +41 -9
  39. package/dist/lib/dependencies.js.map +1 -1
  40. package/dist/lib/doi.d.ts.map +1 -1
  41. package/dist/lib/doi.js +68 -80
  42. package/dist/lib/doi.js.map +1 -1
  43. package/dist/lib/errors.d.ts.map +1 -1
  44. package/dist/lib/errors.js +3 -1
  45. package/dist/lib/errors.js.map +1 -1
  46. package/dist/lib/grammar.js +3 -3
  47. package/dist/lib/import.d.ts +7 -4
  48. package/dist/lib/import.d.ts.map +1 -1
  49. package/dist/lib/import.js +58 -62
  50. package/dist/lib/import.js.map +1 -1
  51. package/dist/lib/macro-filter.lua +201 -201
  52. package/dist/lib/ooxml.d.ts +207 -0
  53. package/dist/lib/ooxml.d.ts.map +1 -0
  54. package/dist/lib/ooxml.js +634 -0
  55. package/dist/lib/ooxml.js.map +1 -0
  56. package/dist/lib/pdf-comments.js +44 -44
  57. package/dist/lib/plugins.js +57 -57
  58. package/dist/lib/pptx-color-filter.lua +37 -37
  59. package/dist/lib/pptx-themes.js +115 -115
  60. package/dist/lib/rate-limiter.d.ts +8 -0
  61. package/dist/lib/rate-limiter.d.ts.map +1 -1
  62. package/dist/lib/rate-limiter.js +41 -4
  63. package/dist/lib/rate-limiter.js.map +1 -1
  64. package/dist/lib/sections.d.ts.map +1 -1
  65. package/dist/lib/sections.js +5 -2
  66. package/dist/lib/sections.js.map +1 -1
  67. package/dist/lib/spelling.js +2 -2
  68. package/dist/lib/templates.js +387 -387
  69. package/dist/lib/themes.js +51 -51
  70. package/dist/lib/types.d.ts +9 -1
  71. package/dist/lib/types.d.ts.map +1 -1
  72. package/dist/lib/word-extraction.d.ts.map +1 -1
  73. package/dist/lib/word-extraction.js +48 -307
  74. package/dist/lib/word-extraction.js.map +1 -1
  75. package/dist/lib/word.d.ts.map +1 -1
  76. package/dist/lib/word.js +80 -175
  77. package/dist/lib/word.js.map +1 -1
  78. package/dist/lib/wordcomments.d.ts.map +1 -1
  79. package/dist/lib/wordcomments.js +53 -92
  80. package/dist/lib/wordcomments.js.map +1 -1
  81. package/docs-src/build.py +113 -113
  82. package/docs-src/extra.css +208 -208
  83. package/docs-src/md-to-html.lua +6 -6
  84. package/docs-src/template.html +116 -116
  85. package/eslint.config.js +27 -27
  86. package/lib/anchor-match.ts +307 -276
  87. package/lib/annotations.ts +664 -644
  88. package/lib/build.ts +1770 -1766
  89. package/lib/citations.ts +160 -160
  90. package/lib/commands/build.ts +860 -855
  91. package/lib/commands/citations.ts +515 -515
  92. package/lib/commands/comments.ts +1050 -1050
  93. package/lib/commands/context.ts +176 -176
  94. package/lib/commands/core.ts +309 -309
  95. package/lib/commands/doi.ts +451 -435
  96. package/lib/commands/file-ops.ts +372 -372
  97. package/lib/commands/history.ts +320 -320
  98. package/lib/commands/index.ts +87 -87
  99. package/lib/commands/init.ts +259 -259
  100. package/lib/commands/merge-resolve.ts +378 -378
  101. package/lib/commands/preview.ts +178 -178
  102. package/lib/commands/project-info.ts +244 -244
  103. package/lib/commands/quality.ts +517 -517
  104. package/lib/commands/response.ts +454 -454
  105. package/lib/commands/section-boundaries.ts +82 -82
  106. package/lib/commands/sections.ts +451 -451
  107. package/lib/commands/sync.ts +689 -709
  108. package/lib/commands/text-ops.ts +449 -449
  109. package/lib/commands/utilities.ts +448 -448
  110. package/lib/commands/verify-anchors.ts +272 -272
  111. package/lib/commands/word-tools.ts +340 -340
  112. package/lib/comment-realign.ts +99 -517
  113. package/lib/config.ts +84 -84
  114. package/lib/crossref.ts +781 -781
  115. package/lib/csl.ts +191 -191
  116. package/lib/dependencies.ts +132 -98
  117. package/lib/diff-engine.ts +465 -465
  118. package/lib/doi-cache.ts +115 -115
  119. package/lib/doi.ts +879 -897
  120. package/lib/equations.ts +506 -506
  121. package/lib/errors.ts +350 -346
  122. package/lib/format.ts +541 -541
  123. package/lib/git.ts +326 -326
  124. package/lib/grammar.ts +303 -303
  125. package/lib/image-registry.ts +180 -180
  126. package/lib/import.ts +906 -911
  127. package/lib/journals.ts +543 -543
  128. package/lib/macro-filter.lua +201 -201
  129. package/lib/macros.ts +273 -273
  130. package/lib/merge.ts +633 -633
  131. package/lib/ooxml.ts +768 -0
  132. package/lib/orcid.ts +144 -144
  133. package/lib/pdf-comments.ts +263 -263
  134. package/lib/pdf-import.ts +524 -524
  135. package/lib/plugins.ts +362 -362
  136. package/lib/postprocess.ts +188 -188
  137. package/lib/pptx-color-filter.lua +37 -37
  138. package/lib/pptx-template.ts +469 -469
  139. package/lib/pptx-themes.ts +483 -483
  140. package/lib/protect-restore.ts +520 -520
  141. package/lib/rate-limiter.ts +127 -94
  142. package/lib/response.ts +197 -197
  143. package/lib/restore-references.ts +240 -240
  144. package/lib/review.ts +327 -327
  145. package/lib/schema.ts +488 -488
  146. package/lib/scientific-words.ts +73 -73
  147. package/lib/sections.ts +428 -425
  148. package/lib/slides.ts +756 -756
  149. package/lib/spelling.ts +334 -334
  150. package/lib/templates.ts +526 -526
  151. package/lib/themes.ts +742 -742
  152. package/lib/trackchanges.ts +247 -247
  153. package/lib/tui.ts +450 -450
  154. package/lib/types.ts +558 -550
  155. package/lib/undo.ts +250 -250
  156. package/lib/utils.ts +69 -69
  157. package/lib/variables.ts +179 -179
  158. package/lib/word-extraction.ts +525 -806
  159. package/lib/word.ts +526 -643
  160. package/lib/wordcomments.ts +789 -840
  161. package/mkdocs.yml +64 -64
  162. package/package.json +137 -137
  163. package/scripts/postbuild.js +47 -47
  164. package/skill/REFERENCE.md +539 -539
  165. package/skill/SKILL.md +295 -295
  166. package/tsconfig.json +26 -26
  167. package/types/index.d.ts +525 -525
@@ -1,517 +1,99 @@
1
- /**
2
- * Realign comments from a reference DOCX to markdown
3
- * Uses paragraph-level matching with exact positions
4
- */
5
-
6
- import * as fs from 'fs';
7
- import AdmZip from 'adm-zip';
8
- import { parseStringPromise } from 'xml2js';
9
-
10
- interface CommentData {
11
- author: string;
12
- text: string;
13
- }
14
-
15
- interface CommentWithPosition {
16
- id: string;
17
- position: number;
18
- author: string;
19
- text: string;
20
- }
21
-
22
- interface Paragraph {
23
- text: string;
24
- comments: CommentWithPosition[];
25
- }
26
-
27
- interface MdParagraph {
28
- text: string;
29
- start: number;
30
- end: number;
31
- }
32
-
33
- interface ParagraphMatch {
34
- index: number;
35
- score: number;
36
- paragraph: MdParagraph;
37
- }
38
-
39
- interface WordContext {
40
- before: string[];
41
- after: string[];
42
- }
43
-
44
- interface CommentInsertion {
45
- position: number;
46
- text: string;
47
- commentText: string;
48
- hasReplies: boolean;
49
- debug: string;
50
- }
51
-
52
- interface RealignOptions {
53
- dryRun?: boolean;
54
- author?: string;
55
- replyAuthor?: string;
56
- }
57
-
58
- interface RealignResult {
59
- success: boolean;
60
- dryRun?: boolean;
61
- insertions: number;
62
- matched?: number;
63
- unmatched?: number;
64
- }
65
-
66
- interface RealignMarkdownOptions {
67
- author?: string;
68
- replyAuthor?: string;
69
- }
70
-
71
- interface RealignMarkdownResult {
72
- success: boolean;
73
- markdown: string;
74
- insertions: number;
75
- error?: string;
76
- }
77
-
78
- /**
79
- * Extract paragraphs with their full text and comment positions from DOCX
80
- */
81
- export async function extractParagraphsWithComments(docxPath: string): Promise<Paragraph[]> {
82
- const zip = new AdmZip(docxPath);
83
- const doc = zip.readAsText('word/document.xml');
84
- const commentsXml = zip.readAsText('word/comments.xml');
85
-
86
- // Parse comments to get authors and texts
87
- const parsed = await parseStringPromise(commentsXml, { explicitArray: false });
88
- const commentNodes = parsed['w:comments']?.['w:comment'];
89
- if (!commentNodes) return [];
90
-
91
- const nodes = Array.isArray(commentNodes) ? commentNodes : [commentNodes];
92
- const commentData: Record<string, CommentData> = {};
93
-
94
- for (const c of nodes) {
95
- const id = c.$?.['w:id'] ?? '';
96
- const author = c.$?.['w:author'] ?? 'Unknown';
97
- let text = '';
98
- const extractT = (n: any): void => {
99
- if (!n) return;
100
- if (n['w:t']) {
101
- const t = n['w:t'];
102
- text += typeof t === 'string' ? t : (t._ || t);
103
- }
104
- if (n['w:r']) {
105
- (Array.isArray(n['w:r']) ? n['w:r'] : [n['w:r']]).forEach(extractT);
106
- }
107
- if (n['w:p']) {
108
- (Array.isArray(n['w:p']) ? n['w:p'] : [n['w:p']]).forEach(extractT);
109
- }
110
- };
111
- extractT(c);
112
- commentData[id] = { author, text: text.trim() };
113
- }
114
-
115
- // Extract paragraphs with comments
116
- const paragraphs: Paragraph[] = [];
117
- const paraPattern = /<w:p\b[^>]*>([\s\S]*?)<\/w:p>/g;
118
- let match;
119
-
120
- while ((match = paraPattern.exec(doc)) !== null) {
121
- const paraContent = match[1];
122
- const hasComments = /commentRangeStart/.test(paraContent);
123
-
124
- // Build paragraph text and track comment positions
125
- let text = '';
126
- const comments: CommentWithPosition[] = [];
127
-
128
- const tokenPattern = /<w:t[^>]*>([^<]*)<\/w:t>|<w:commentRangeStart[^>]*w:id="(\d+)"[^>]*\/?>/g;
129
- let tokenMatch;
130
-
131
- while ((tokenMatch = tokenPattern.exec(paraContent)) !== null) {
132
- if (tokenMatch[1] !== undefined) {
133
- text += tokenMatch[1];
134
- } else if (tokenMatch[2] !== undefined) {
135
- const cid = tokenMatch[2];
136
- const data = commentData[cid];
137
- if (data) {
138
- comments.push({
139
- id: cid,
140
- position: text.length,
141
- author: data.author,
142
- text: data.text,
143
- });
144
- }
145
- }
146
- }
147
-
148
- if (text.trim() || hasComments) {
149
- paragraphs.push({ text: text.trim(), comments });
150
- }
151
- }
152
-
153
- return paragraphs;
154
- }
155
-
156
- /**
157
- * Find best matching paragraph in markdown for a reference paragraph
158
- */
159
- function findMatchingParagraph(refText: string, mdParagraphs: MdParagraph[]): ParagraphMatch | null {
160
- // Normalize for comparison
161
- const normalize = (s: string): string => s.toLowerCase().replace(/\s+/g, ' ').trim();
162
- const refNorm = normalize(refText);
163
-
164
- if (refNorm.length < 20) return null;
165
-
166
- let bestMatch: ParagraphMatch | null = null;
167
- let bestScore = 0;
168
-
169
- for (let i = 0; i < mdParagraphs.length; i++) {
170
- const mdNorm = normalize(mdParagraphs[i].text);
171
-
172
- // Calculate word overlap
173
- const refWords = new Set(refNorm.split(' ').filter((w) => w.length > 3));
174
- const mdWords = mdNorm.split(' ').filter((w) => w.length > 3);
175
- const overlap = mdWords.filter((w) => refWords.has(w)).length;
176
- const score = overlap / Math.max(refWords.size, 1);
177
-
178
- // Also check for substring containment (for section headers)
179
- const containsStart = mdNorm.includes(refNorm.slice(0, 50));
180
-
181
- if (score > bestScore || (containsStart && score > 0.3)) {
182
- bestScore = Math.max(score, containsStart ? 0.8 : score);
183
- bestMatch = { index: i, score: bestScore, paragraph: mdParagraphs[i] };
184
- }
185
- }
186
-
187
- return bestScore > 0.4 ? bestMatch : null;
188
- }
189
-
190
- /**
191
- * Extract paragraphs from markdown (split by blank lines)
192
- */
193
- function parseMdParagraphs(markdown: string): MdParagraph[] {
194
- const paragraphs: MdParagraph[] = [];
195
- const parts = markdown.split(/\n\n+/);
196
-
197
- let pos = 0;
198
- for (const part of parts) {
199
- const trimmed = part.trim();
200
- if (trimmed) {
201
- const partStart = markdown.indexOf(part, pos);
202
- if (partStart !== -1) {
203
- paragraphs.push({
204
- text: trimmed,
205
- start: partStart,
206
- end: partStart + part.length,
207
- });
208
- pos = partStart + part.length;
209
- }
210
- }
211
- }
212
-
213
- return paragraphs;
214
- }
215
-
216
- /**
217
- * Normalize text for matching (remove citations, extra whitespace)
218
- */
219
- function normalizeForMatching(text: string): string {
220
- return text
221
- // Remove Word citation placeholders
222
- .replace(/\(\s*\$+\s*\)/g, '')
223
- .replace(/\$+/g, '')
224
- // Remove markdown citations
225
- .replace(/\[@[^\]]+\]/g, '')
226
- .replace(/@[A-Z][a-z]+\d{4}/g, '')
227
- // Remove rendered citations like "(Author et al. 2021)"
228
- .replace(/\([A-Z][a-z]+(?:\s+et\s+al\.?)?\s+\d{4}[a-z]?(?:[;,]\s*[A-Z][a-z]+(?:\s+et\s+al\.?)?\s+\d{4}[a-z]?)*\)/g, '')
229
- // Remove figure references like "Fig. 1" or "(Fig. 1)"
230
- .replace(/\(?Fig\.?\s*\d+[a-z]?\)?/gi, '')
231
- // Normalize whitespace
232
- .replace(/\s+/g, ' ')
233
- .trim()
234
- .toLowerCase();
235
- }
236
-
237
- /**
238
- * Find the word at or near a position in text
239
- */
240
- function getWordAtPosition(text: string, pos: number): WordContext {
241
- const before = text.slice(Math.max(0, pos - 30), pos);
242
- const after = text.slice(pos, pos + 30);
243
-
244
- // Get the last complete word before position
245
- const beforeWords = before.split(/\s+/).filter(w => w.length > 2);
246
- const afterWords = after.split(/\s+/).filter(w => w.length > 2);
247
-
248
- return {
249
- before: beforeWords.slice(-3),
250
- after: afterWords.slice(0, 3)
251
- };
252
- }
253
-
254
- /**
255
- * Find position in markdown paragraph matching reference position
256
- * Uses the anchor word (word immediately before the comment) for precise matching
257
- */
258
- function findMdPosition(refText: string, refPos: number, mdText: string): number {
259
- // Get the word(s) immediately before the comment position in reference
260
- const refWords = getWordAtPosition(refText, refPos);
261
- const normalizedMd = normalizeForMatching(mdText);
262
-
263
- // The "anchor word" is the last word before the comment
264
- const anchorWords = refWords.before;
265
-
266
- if (anchorWords.length === 0) {
267
- const ratio = refPos / Math.max(refText.length, 1);
268
- return Math.round(ratio * mdText.length);
269
- }
270
-
271
- // Try to find the anchor word(s) in markdown
272
- // Start with the most specific (all words), fall back to fewer
273
- for (let numWords = anchorWords.length; numWords >= 1; numWords--) {
274
- const searchWords = anchorWords.slice(-numWords);
275
- const pattern = searchWords.map(w =>
276
- w.toLowerCase().replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
277
- ).join('\\s+');
278
-
279
- const regex = new RegExp(pattern, 'g');
280
- const matches = [...normalizedMd.matchAll(regex)];
281
-
282
- if (matches.length === 1) {
283
- // Unique match - use this position
284
- const matchEnd = matches[0].index! + matches[0][0].length;
285
- // Map back to original markdown position
286
- const ratio = matchEnd / Math.max(normalizedMd.length, 1);
287
- return Math.round(ratio * mdText.length);
288
- } else if (matches.length > 1) {
289
- // Multiple matches - use context after to disambiguate
290
- const afterWords = refWords.after;
291
- if (afterWords.length > 0) {
292
- const afterPattern = afterWords[0].toLowerCase().replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
293
- for (const match of matches) {
294
- const matchEnd = match.index! + match[0].length;
295
- const afterContext = normalizedMd.slice(matchEnd, matchEnd + 50);
296
- if (afterContext.includes(afterPattern)) {
297
- const ratio = matchEnd / Math.max(normalizedMd.length, 1);
298
- return Math.round(ratio * mdText.length);
299
- }
300
- }
301
- }
302
- // Fall back to first match
303
- const matchEnd = matches[0].index! + matches[0][0].length;
304
- const ratio = matchEnd / Math.max(normalizedMd.length, 1);
305
- return Math.round(ratio * mdText.length);
306
- }
307
- }
308
-
309
- // Fallback: proportional position
310
- const ratio = refPos / Math.max(refText.length, 1);
311
- return Math.round(ratio * mdText.length);
312
- }
313
-
314
- /**
315
- * Extract reply comments that follow a parent comment
316
- * Returns map of parent comment text -> array of reply texts
317
- */
318
- function extractReplies(markdown: string, parentAuthor: string, replyAuthor: string): Map<string, string[]> {
319
- const replies = new Map<string, string[]>();
320
- const pattern = new RegExp(
321
- `\\{>>${parentAuthor}:\\s*([^<]+)<<\\}((?:\\s*\\{>>${replyAuthor}:[^<]+<<\\})*)`,
322
- 'g'
323
- );
324
-
325
- let match;
326
- while ((match = pattern.exec(markdown)) !== null) {
327
- const parentText = match[1].trim();
328
- const replyBlock = match[2];
329
-
330
- if (replyBlock) {
331
- const replyPattern = new RegExp(`\\{>>${replyAuthor}:\\s*([^<]+)<<\\}`, 'g');
332
- const replyTexts: string[] = [];
333
- let replyMatch;
334
- while ((replyMatch = replyPattern.exec(replyBlock)) !== null) {
335
- replyTexts.push(replyMatch[1].trim());
336
- }
337
- if (replyTexts.length > 0) {
338
- replies.set(parentText.slice(0, 50), replyTexts); // Use first 50 chars as key
339
- }
340
- }
341
- }
342
-
343
- return replies;
344
- }
345
-
346
- /**
347
- * Realign comments from reference DOCX to markdown
348
- */
349
- export async function realignComments(
350
- docxPath: string,
351
- markdownPath: string,
352
- options: RealignOptions = {}
353
- ): Promise<RealignResult> {
354
- const { dryRun = false, author = 'Guy Colling', replyAuthor = 'Gilles Colling' } = options;
355
-
356
- // Read original markdown to extract replies before stripping
357
- const originalMarkdown = fs.readFileSync(markdownPath, 'utf-8');
358
-
359
- // Extract reply relationships
360
- const replies = extractReplies(originalMarkdown, author, replyAuthor);
361
- console.log(`Found ${replies.size} ${author} comments with ${replyAuthor} replies`);
362
-
363
- // Extract reference paragraphs with comments
364
- const refParagraphs = await extractParagraphsWithComments(docxPath);
365
- const refWithComments = refParagraphs.filter(
366
- (p) => p.comments.length > 0 && p.comments.some((c) => c.author === author)
367
- );
368
-
369
- console.log(`Found ${refWithComments.length} paragraphs with ${author} comments in reference`);
370
-
371
- // Strip ALL comments (both authors) from markdown to start fresh
372
- let markdown = originalMarkdown;
373
- markdown = markdown.replace(/\s*\{>>[\s\S]+?<<\}/g, '');
374
- console.log(`Stripped all comments from markdown`);
375
-
376
- // Parse markdown paragraphs
377
- const mdParagraphs = parseMdParagraphs(markdown);
378
-
379
- // Track insertions (position, text) - will insert from end to start
380
- const insertions: CommentInsertion[] = [];
381
- let matched = 0;
382
- let unmatched = 0;
383
-
384
- for (const refPara of refWithComments) {
385
- const match = findMatchingParagraph(refPara.text, mdParagraphs);
386
-
387
- if (!match) {
388
- console.log(` No match for: "${refPara.text.slice(0, 60)}..."`);
389
- unmatched++;
390
- continue;
391
- }
392
-
393
- matched++;
394
- const mdPara = match.paragraph;
395
-
396
- // Get author's comments in this paragraph
397
- const authorComments = refPara.comments.filter((c) => c.author === author);
398
-
399
- for (const comment of authorComments) {
400
- // Find corresponding position in markdown paragraph
401
- const mdPos = findMdPosition(refPara.text, comment.position, mdPara?.text ?? '');
402
- const absolutePos = (mdPara?.start ?? 0) + mdPos;
403
-
404
- // Build comment mark with any replies
405
- let commentMark = ` {>>${comment.author}: ${comment.text}<<}`;
406
-
407
- // Check for replies
408
- const replyKey = comment.text.trim().slice(0, 50);
409
- const replyTexts = replies.get(replyKey);
410
- if (replyTexts) {
411
- for (const replyText of replyTexts) {
412
- commentMark += ` {>>${replyAuthor}: ${replyText}<<}`;
413
- }
414
- }
415
-
416
- insertions.push({
417
- position: absolutePos,
418
- text: commentMark,
419
- commentText: comment.text.slice(0, 30),
420
- hasReplies: !!replyTexts,
421
- debug: `"${(mdPara?.text ?? '').slice(Math.max(0, mdPos - 20), mdPos)}|HERE|${(mdPara?.text ?? '').slice(mdPos, mdPos + 20)}"`,
422
- });
423
- }
424
- }
425
-
426
- console.log(`Matched ${matched} paragraphs, ${unmatched} unmatched`);
427
- console.log(`Inserting ${insertions.length} comments (${insertions.filter((i) => i.hasReplies).length} with replies)`);
428
-
429
- if (dryRun) {
430
- console.log('\nDry run - would insert:');
431
- for (const ins of insertions.slice(0, 10)) {
432
- console.log(` At ${ins.position}: ${ins.debug}`);
433
- console.log(` Comment: "${ins.commentText}..."${ins.hasReplies ? ' (+ replies)' : ''}`);
434
- }
435
- return { success: true, dryRun: true, insertions: insertions.length };
436
- }
437
-
438
- // Sort by position descending and insert
439
- insertions.sort((a, b) => b.position - a.position);
440
-
441
- for (const ins of insertions) {
442
- markdown = markdown.slice(0, ins.position) + ins.text + markdown.slice(ins.position);
443
- }
444
-
445
- // Write result
446
- fs.writeFileSync(markdownPath, markdown);
447
-
448
- return { success: true, insertions: insertions.length, matched, unmatched };
449
- }
450
-
451
- /**
452
- * Realign comments in markdown string (in-memory, doesn't write to file)
453
- */
454
- export async function realignMarkdown(
455
- docxPath: string,
456
- markdown: string,
457
- options: RealignMarkdownOptions = {}
458
- ): Promise<RealignMarkdownResult> {
459
- const { author = 'Guy Colling', replyAuthor = 'Gilles Colling' } = options;
460
-
461
- try {
462
- // Extract reply relationships from original markdown
463
- const replies = extractReplies(markdown, author, replyAuthor);
464
-
465
- // Extract reference paragraphs with comments
466
- const refParagraphs = await extractParagraphsWithComments(docxPath);
467
- const refWithComments = refParagraphs.filter(
468
- (p) => p.comments.length > 0 && p.comments.some((c) => c.author === author)
469
- );
470
-
471
- // Strip ALL comments from markdown
472
- let result = markdown.replace(/\s*\{>>[\s\S]+?<<\}/g, '');
473
-
474
- // Parse markdown paragraphs
475
- const mdParagraphs = parseMdParagraphs(result);
476
-
477
- // Track insertions
478
- const insertions: Array<{ position: number; text: string }> = [];
479
-
480
- for (const refPara of refWithComments) {
481
- const match = findMatchingParagraph(refPara.text, mdParagraphs);
482
- if (!match) continue;
483
-
484
- const mdPara = match.paragraph;
485
- const authorComments = refPara.comments.filter((c) => c.author === author);
486
-
487
- for (const comment of authorComments) {
488
- const mdPos = findMdPosition(refPara.text, comment.position, mdPara?.text ?? '');
489
- const absolutePos = (mdPara?.start ?? 0) + mdPos;
490
-
491
- let commentMark = ` {>>${comment.author}: ${comment.text}<<}`;
492
-
493
- // Check for replies
494
- const replyKey = comment.text.trim().slice(0, 50);
495
- const replyTexts = replies.get(replyKey);
496
- if (replyTexts) {
497
- for (const replyText of replyTexts) {
498
- commentMark += ` {>>${replyAuthor}: ${replyText}<<}`;
499
- }
500
- }
501
-
502
- insertions.push({ position: absolutePos, text: commentMark });
503
- }
504
- }
505
-
506
- // Sort by position descending and insert
507
- insertions.sort((a, b) => b.position - a.position);
508
-
509
- for (const ins of insertions) {
510
- result = result.slice(0, ins.position) + ins.text + result.slice(ins.position);
511
- }
512
-
513
- return { success: true, markdown: result, insertions: insertions.length };
514
- } catch (err: any) {
515
- return { success: false, markdown, insertions: 0, error: err.message };
516
- }
517
- }
1
+ /**
2
+ * Realign comments from a reference DOCX onto markdown.
3
+ *
4
+ * This is the same job as `rev sync`: read the comments and their anchors from
5
+ * a docx, then place them into the current markdown. It therefore reuses the
6
+ * one extraction + placement engine rather than carrying a second docx parser
7
+ * and a proportional position-mapper.
8
+ */
9
+
10
+ import * as fs from 'fs';
11
+ import { extractWordComments, extractCommentAnchors } from './word-extraction.js';
12
+ import { insertCommentsIntoMarkdown } from './import.js';
13
+
14
+ interface RealignOptions {
15
+ dryRun?: boolean;
16
+ /** When set, realign only comments by these authors; otherwise all. */
17
+ author?: string;
18
+ replyAuthor?: string;
19
+ }
20
+
21
+ interface RealignResult {
22
+ success: boolean;
23
+ dryRun?: boolean;
24
+ insertions: number;
25
+ error?: string;
26
+ }
27
+
28
+ interface RealignMarkdownOptions {
29
+ author?: string;
30
+ replyAuthor?: string;
31
+ }
32
+
33
+ interface RealignMarkdownResult {
34
+ success: boolean;
35
+ markdown: string;
36
+ insertions: number;
37
+ error?: string;
38
+ }
39
+
40
+ /** Existing CriticMarkup comments, removed before the reference set is placed. */
41
+ function stripExistingComments(markdown: string): string {
42
+ return markdown.replace(/\s*\{>>[\s\S]+?<<\}/g, '');
43
+ }
44
+
45
+ /**
46
+ * Realign the reference docx's comments onto an in-memory markdown string.
47
+ * Existing comments are cleared first so re-running is idempotent.
48
+ */
49
+ export async function realignMarkdown(
50
+ docxPath: string,
51
+ markdown: string,
52
+ options: RealignMarkdownOptions = {},
53
+ ): Promise<RealignMarkdownResult> {
54
+ try {
55
+ let comments = await extractWordComments(docxPath);
56
+ const { anchors } = await extractCommentAnchors(docxPath);
57
+
58
+ if (options.author) {
59
+ const keep = new Set([options.author, options.replyAuthor].filter(Boolean) as string[]);
60
+ comments = comments.filter((c) => keep.has(c.author));
61
+ }
62
+
63
+ const stripped = stripExistingComments(markdown);
64
+ const stats = { placed: 0, lowConfidence: 0, deduped: 0, unmatched: 0 };
65
+ const result = insertCommentsIntoMarkdown(stripped, comments, anchors, {
66
+ quiet: true,
67
+ wrapAnchor: false,
68
+ outStats: stats,
69
+ });
70
+
71
+ return { success: true, markdown: result, insertions: stats.placed + stats.lowConfidence };
72
+ } catch (err: any) {
73
+ return { success: false, markdown, insertions: 0, error: err.message };
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Realign the reference docx's comments onto a markdown file in place.
79
+ */
80
+ export async function realignComments(
81
+ docxPath: string,
82
+ markdownPath: string,
83
+ options: RealignOptions = {},
84
+ ): Promise<RealignResult> {
85
+ const { dryRun = false, author, replyAuthor } = options;
86
+
87
+ const original = fs.readFileSync(markdownPath, 'utf-8');
88
+ const realigned = await realignMarkdown(docxPath, original, { author, replyAuthor });
89
+ if (!realigned.success) {
90
+ return { success: false, insertions: 0, error: realigned.error };
91
+ }
92
+
93
+ if (dryRun) {
94
+ return { success: true, dryRun: true, insertions: realigned.insertions };
95
+ }
96
+
97
+ fs.writeFileSync(markdownPath, realigned.markdown);
98
+ return { success: true, insertions: realigned.insertions };
99
+ }