docrev 0.10.2 → 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 (87) hide show
  1. package/dist/lib/anchor-match.d.ts +3 -1
  2. package/dist/lib/anchor-match.d.ts.map +1 -1
  3. package/dist/lib/anchor-match.js +5 -7
  4. package/dist/lib/anchor-match.js.map +1 -1
  5. package/dist/lib/annotations.d.ts +0 -6
  6. package/dist/lib/annotations.d.ts.map +1 -1
  7. package/dist/lib/annotations.js +26 -8
  8. package/dist/lib/annotations.js.map +1 -1
  9. package/dist/lib/build.d.ts.map +1 -1
  10. package/dist/lib/build.js +9 -6
  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 +5 -0
  14. package/dist/lib/commands/build.js.map +1 -1
  15. package/dist/lib/commands/doi.d.ts.map +1 -1
  16. package/dist/lib/commands/doi.js +14 -2
  17. package/dist/lib/commands/doi.js.map +1 -1
  18. package/dist/lib/commands/sync.d.ts.map +1 -1
  19. package/dist/lib/commands/sync.js +24 -11
  20. package/dist/lib/commands/sync.js.map +1 -1
  21. package/dist/lib/comment-realign.d.ts +12 -21
  22. package/dist/lib/comment-realign.d.ts.map +1 -1
  23. package/dist/lib/comment-realign.js +40 -348
  24. package/dist/lib/comment-realign.js.map +1 -1
  25. package/dist/lib/dependencies.d.ts +10 -0
  26. package/dist/lib/dependencies.d.ts.map +1 -1
  27. package/dist/lib/dependencies.js +41 -9
  28. package/dist/lib/dependencies.js.map +1 -1
  29. package/dist/lib/doi.d.ts.map +1 -1
  30. package/dist/lib/doi.js +68 -80
  31. package/dist/lib/doi.js.map +1 -1
  32. package/dist/lib/errors.d.ts.map +1 -1
  33. package/dist/lib/errors.js +3 -1
  34. package/dist/lib/errors.js.map +1 -1
  35. package/dist/lib/import.d.ts +7 -4
  36. package/dist/lib/import.d.ts.map +1 -1
  37. package/dist/lib/import.js +58 -62
  38. package/dist/lib/import.js.map +1 -1
  39. package/dist/lib/ooxml.d.ts +207 -0
  40. package/dist/lib/ooxml.d.ts.map +1 -0
  41. package/dist/lib/ooxml.js +634 -0
  42. package/dist/lib/ooxml.js.map +1 -0
  43. package/dist/lib/rate-limiter.d.ts +8 -0
  44. package/dist/lib/rate-limiter.d.ts.map +1 -1
  45. package/dist/lib/rate-limiter.js +41 -4
  46. package/dist/lib/rate-limiter.js.map +1 -1
  47. package/dist/lib/types.d.ts +9 -1
  48. package/dist/lib/types.d.ts.map +1 -1
  49. package/dist/lib/word-extraction.d.ts.map +1 -1
  50. package/dist/lib/word-extraction.js +48 -307
  51. package/dist/lib/word-extraction.js.map +1 -1
  52. package/dist/lib/word.d.ts.map +1 -1
  53. package/dist/lib/word.js +80 -175
  54. package/dist/lib/word.js.map +1 -1
  55. package/dist/lib/wordcomments.d.ts.map +1 -1
  56. package/dist/lib/wordcomments.js +53 -92
  57. package/dist/lib/wordcomments.js.map +1 -1
  58. package/docs-src/build.py +113 -113
  59. package/docs-src/extra.css +208 -208
  60. package/docs-src/md-to-html.lua +6 -6
  61. package/docs-src/template.html +116 -116
  62. package/lib/anchor-match.ts +6 -7
  63. package/lib/annotations.ts +28 -8
  64. package/lib/build.ts +10 -6
  65. package/lib/commands/build.ts +5 -0
  66. package/lib/commands/doi.ts +17 -1
  67. package/lib/commands/sync.ts +26 -13
  68. package/lib/comment-realign.ts +46 -464
  69. package/lib/dependencies.ts +43 -9
  70. package/lib/doi.ts +76 -94
  71. package/lib/errors.ts +5 -1
  72. package/lib/import.ts +65 -70
  73. package/lib/ooxml.ts +768 -0
  74. package/lib/rate-limiter.ts +37 -4
  75. package/lib/types.ts +9 -1
  76. package/lib/word-extraction.ts +49 -330
  77. package/lib/word.ts +86 -203
  78. package/lib/wordcomments.ts +53 -104
  79. package/mkdocs.yml +64 -64
  80. package/package.json +1 -1
  81. package/issues.md +0 -180
  82. package/site/assets/extra.css +0 -208
  83. package/site/commands.html +0 -926
  84. package/site/configuration.html +0 -469
  85. package/site/index.html +0 -288
  86. package/site/troubleshooting.html +0 -461
  87. package/site/workflow.html +0 -518
@@ -1,56 +1,19 @@
1
1
  /**
2
- * Realign comments from a reference DOCX to markdown
3
- * Uses paragraph-level matching with exact positions
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.
4
8
  */
5
9
 
6
10
  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
- }
11
+ import { extractWordComments, extractCommentAnchors } from './word-extraction.js';
12
+ import { insertCommentsIntoMarkdown } from './import.js';
51
13
 
52
14
  interface RealignOptions {
53
15
  dryRun?: boolean;
16
+ /** When set, realign only comments by these authors; otherwise all. */
54
17
  author?: string;
55
18
  replyAuthor?: string;
56
19
  }
@@ -59,8 +22,7 @@ interface RealignResult {
59
22
  success: boolean;
60
23
  dryRun?: boolean;
61
24
  insertions: number;
62
- matched?: number;
63
- unmatched?: number;
25
+ error?: string;
64
26
  }
65
27
 
66
28
  interface RealignMarkdownOptions {
@@ -75,443 +37,63 @@ interface RealignMarkdownResult {
75
37
  error?: string;
76
38
  }
77
39
 
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;
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, '');
188
43
  }
189
44
 
190
45
  /**
191
- * Extract paragraphs from markdown (split by blank lines)
46
+ * Realign the reference docx's comments onto an in-memory markdown string.
47
+ * Existing comments are cleared first so re-running is idempotent.
192
48
  */
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)];
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);
281
57
 
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);
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));
306
61
  }
307
- }
308
62
 
309
- // Fallback: proportional position
310
- const ratio = refPos / Math.max(refText.length, 1);
311
- return Math.round(ratio * mdText.length);
312
- }
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
+ });
313
70
 
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
- }
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 };
341
74
  }
342
-
343
- return replies;
344
75
  }
345
76
 
346
77
  /**
347
- * Realign comments from reference DOCX to markdown
78
+ * Realign the reference docx's comments onto a markdown file in place.
348
79
  */
349
80
  export async function realignComments(
350
81
  docxPath: string,
351
82
  markdownPath: string,
352
- options: RealignOptions = {}
83
+ options: RealignOptions = {},
353
84
  ): 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');
85
+ const { dryRun = false, author, replyAuthor } = options;
358
86
 
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
- }
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 };
424
91
  }
425
92
 
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
93
  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 };
94
+ return { success: true, dryRun: true, insertions: realigned.insertions };
436
95
  }
437
96
 
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
- }
97
+ fs.writeFileSync(markdownPath, realigned.markdown);
98
+ return { success: true, insertions: realigned.insertions };
517
99
  }
@@ -2,39 +2,68 @@
2
2
  * Dependency checking utilities for pandoc, LaTeX, and related tools
3
3
  */
4
4
 
5
- import { execSync } from 'child_process';
5
+ import { execFileSync } from 'child_process';
6
6
 
7
7
  /**
8
- * Check if a command is available by running it silently
8
+ * Run `<file> --version` without a shell and return its stdout, or null when
9
+ * the binary is absent (ENOENT) or exits non-zero (present but broken). Using
10
+ * execFileSync avoids shell quoting and treats both failure modes uniformly.
9
11
  */
10
- function commandExists(cmd: string): boolean {
12
+ function runVersion(file: string, args: string[] = ['--version']): string | null {
11
13
  try {
12
- execSync(cmd, { stdio: 'ignore' });
13
- return true;
14
+ return execFileSync(file, args, { stdio: ['ignore', 'pipe', 'ignore'], encoding: 'utf-8' });
14
15
  } catch {
15
- return false;
16
+ return null;
16
17
  }
17
18
  }
18
19
 
20
+ function commandExists(file: string, args: string[] = ['--version']): boolean {
21
+ return runVersion(file, args) !== null;
22
+ }
23
+
19
24
  /**
20
25
  * Check if pandoc-crossref is available
21
26
  */
22
27
  export function hasPandocCrossref(): boolean {
23
- return commandExists('pandoc-crossref --version');
28
+ return commandExists('pandoc-crossref');
24
29
  }
25
30
 
26
31
  /**
27
32
  * Check if pandoc is available
28
33
  */
29
34
  export function hasPandoc(): boolean {
30
- return commandExists('pandoc --version');
35
+ return commandExists('pandoc');
36
+ }
37
+
38
+ /**
39
+ * Parsed pandoc version (e.g. "3.9"), or null when pandoc is unavailable.
40
+ */
41
+ export function getPandocVersion(): string | null {
42
+ const out = runVersion('pandoc');
43
+ if (!out) return null;
44
+ const m = out.match(/pandoc(?:\.exe)?\s+(\d+\.\d+(?:\.\d+)?)/i);
45
+ return m ? m[1]! : null;
46
+ }
47
+
48
+ /**
49
+ * Whether pandoc bundles citeproc and accepts `--citeproc`. That flag and the
50
+ * built-in citeproc arrived in pandoc 2.11; earlier versions need the separate
51
+ * pandoc-citeproc filter.
52
+ */
53
+ export function pandocSupportsCiteproc(): boolean {
54
+ const v = getPandocVersion();
55
+ if (!v) return false;
56
+ const parts = v.split('.').map((n) => parseInt(n, 10));
57
+ const major = parts[0] ?? 0;
58
+ const minor = parts[1] ?? 0;
59
+ return major > 2 || (major === 2 && minor >= 11);
31
60
  }
32
61
 
33
62
  /**
34
63
  * Check if LaTeX is available (for PDF generation)
35
64
  */
36
65
  export function hasLatex(): boolean {
37
- return commandExists('pdflatex --version') || commandExists('xelatex --version');
66
+ return commandExists('pdflatex') || commandExists('xelatex');
38
67
  }
39
68
 
40
69
  /**
@@ -86,6 +115,11 @@ export function checkDependencies(): DependencyStatus {
86
115
 
87
116
  if (!status.pandoc) {
88
117
  status.messages.push(`Pandoc not found. Install with: ${getInstallInstructions('pandoc')}`);
118
+ } else if (!pandocSupportsCiteproc()) {
119
+ const version = getPandocVersion();
120
+ status.messages.push(
121
+ `Pandoc ${version ?? '(unknown version)'} is older than 2.11; citation processing (--citeproc) needs 2.11+. Upgrade with: ${getInstallInstructions('pandoc')}`,
122
+ );
89
123
  }
90
124
  if (!status.latex) {
91
125
  status.messages.push(`LaTeX not found (required for PDF). Install with: ${getInstallInstructions('latex')}`);