docrev 0.10.2 → 0.11.1
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.
- package/CHANGELOG.md +13 -0
- package/README.md +2 -0
- package/dist/lib/anchor-match.d.ts +3 -1
- package/dist/lib/anchor-match.d.ts.map +1 -1
- package/dist/lib/anchor-match.js +5 -7
- package/dist/lib/anchor-match.js.map +1 -1
- package/dist/lib/annotations.d.ts +0 -6
- package/dist/lib/annotations.d.ts.map +1 -1
- package/dist/lib/annotations.js +26 -8
- package/dist/lib/annotations.js.map +1 -1
- package/dist/lib/build.d.ts +52 -0
- package/dist/lib/build.d.ts.map +1 -1
- package/dist/lib/build.js +135 -6
- package/dist/lib/build.js.map +1 -1
- package/dist/lib/commands/build.d.ts.map +1 -1
- package/dist/lib/commands/build.js +39 -12
- package/dist/lib/commands/build.js.map +1 -1
- package/dist/lib/commands/doi.d.ts.map +1 -1
- package/dist/lib/commands/doi.js +14 -2
- package/dist/lib/commands/doi.js.map +1 -1
- package/dist/lib/commands/sync.d.ts.map +1 -1
- package/dist/lib/commands/sync.js +24 -11
- package/dist/lib/commands/sync.js.map +1 -1
- package/dist/lib/comment-realign.d.ts +12 -21
- package/dist/lib/comment-realign.d.ts.map +1 -1
- package/dist/lib/comment-realign.js +40 -348
- package/dist/lib/comment-realign.js.map +1 -1
- package/dist/lib/dependencies.d.ts +10 -0
- package/dist/lib/dependencies.d.ts.map +1 -1
- package/dist/lib/dependencies.js +41 -9
- package/dist/lib/dependencies.js.map +1 -1
- package/dist/lib/doi.d.ts.map +1 -1
- package/dist/lib/doi.js +68 -80
- package/dist/lib/doi.js.map +1 -1
- package/dist/lib/errors.d.ts.map +1 -1
- package/dist/lib/errors.js +3 -1
- package/dist/lib/errors.js.map +1 -1
- package/dist/lib/import.d.ts +7 -4
- package/dist/lib/import.d.ts.map +1 -1
- package/dist/lib/import.js +58 -62
- package/dist/lib/import.js.map +1 -1
- package/dist/lib/ooxml.d.ts +207 -0
- package/dist/lib/ooxml.d.ts.map +1 -0
- package/dist/lib/ooxml.js +634 -0
- package/dist/lib/ooxml.js.map +1 -0
- package/dist/lib/rate-limiter.d.ts +8 -0
- package/dist/lib/rate-limiter.d.ts.map +1 -1
- package/dist/lib/rate-limiter.js +41 -4
- package/dist/lib/rate-limiter.js.map +1 -1
- package/dist/lib/trackchanges.d.ts +41 -25
- package/dist/lib/trackchanges.d.ts.map +1 -1
- package/dist/lib/trackchanges.js +90 -151
- package/dist/lib/trackchanges.js.map +1 -1
- package/dist/lib/types.d.ts +9 -8
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/lib/word-extraction.d.ts.map +1 -1
- package/dist/lib/word-extraction.js +48 -307
- package/dist/lib/word-extraction.js.map +1 -1
- package/dist/lib/word.d.ts.map +1 -1
- package/dist/lib/word.js +80 -175
- package/dist/lib/word.js.map +1 -1
- package/dist/lib/wordcomments.d.ts.map +1 -1
- package/dist/lib/wordcomments.js +53 -92
- package/dist/lib/wordcomments.js.map +1 -1
- package/lib/anchor-match.ts +6 -7
- package/lib/annotations.ts +28 -8
- package/lib/build.ts +196 -6
- package/lib/commands/build.ts +42 -12
- package/lib/commands/doi.ts +17 -1
- package/lib/commands/sync.ts +26 -13
- package/lib/comment-realign.ts +46 -464
- package/lib/dependencies.ts +43 -9
- package/lib/doi.ts +76 -94
- package/lib/errors.ts +5 -1
- package/lib/import.ts +65 -70
- package/lib/ooxml.ts +768 -0
- package/lib/rate-limiter.ts +37 -4
- package/lib/trackchanges.ts +102 -183
- package/lib/types.ts +9 -13
- package/lib/word-extraction.ts +49 -330
- package/lib/word.ts +86 -203
- package/lib/wordcomments.ts +53 -104
- package/package.json +137 -137
- package/skill/REFERENCE.md +14 -3
- package/skill/SKILL.md +12 -5
- package/types/index.d.ts +16 -10
package/lib/commands/sync.ts
CHANGED
|
@@ -305,6 +305,7 @@ export function register(program: Command): void {
|
|
|
305
305
|
// reports placements rather than the raw extracted count.
|
|
306
306
|
const routedCommentIds = new Set<string>();
|
|
307
307
|
let totalCommentsPlaced = 0;
|
|
308
|
+
let totalCommentsLowConfidence = 0;
|
|
308
309
|
let totalCommentsDeduped = 0;
|
|
309
310
|
let totalCommentsUnmatched = 0;
|
|
310
311
|
|
|
@@ -375,19 +376,23 @@ export function register(program: Command): void {
|
|
|
375
376
|
|
|
376
377
|
if (sectionComments.length > 0) {
|
|
377
378
|
for (const c of sectionComments) routedCommentIds.add(c.id);
|
|
378
|
-
const cstats = { placed: 0, deduped: 0, unmatched: 0 };
|
|
379
|
+
const cstats = { placed: 0, lowConfidence: 0, deduped: 0, unmatched: 0 };
|
|
379
380
|
annotated = insertCommentsIntoMarkdown(annotated, sectionComments, anchors, {
|
|
380
381
|
quiet: !process.env.DEBUG,
|
|
381
382
|
sectionBoundary: boundary,
|
|
382
383
|
outStats: cstats,
|
|
383
384
|
});
|
|
384
|
-
|
|
385
|
-
|
|
385
|
+
// Both confident and approximate placements are written to the
|
|
386
|
+
// file; count both as placed, but track the approximate ones so
|
|
387
|
+
// the summary can flag them for review.
|
|
388
|
+
stats.comments = (stats.comments || 0) + cstats.placed + cstats.lowConfidence;
|
|
389
|
+
totalCommentsPlaced += cstats.placed + cstats.lowConfidence;
|
|
390
|
+
totalCommentsLowConfidence += cstats.lowConfidence;
|
|
386
391
|
totalCommentsDeduped += cstats.deduped;
|
|
387
392
|
totalCommentsUnmatched += cstats.unmatched;
|
|
388
393
|
|
|
389
394
|
if (process.env.DEBUG) {
|
|
390
|
-
console.log(`[DEBUG] ${section.file}: placed ${cstats.placed}, deduped ${cstats.deduped}, unmatched ${cstats.unmatched} of ${sectionComments.length}`);
|
|
395
|
+
console.log(`[DEBUG] ${section.file}: placed ${cstats.placed}, approx ${cstats.lowConfidence}, deduped ${cstats.deduped}, unmatched ${cstats.unmatched} of ${sectionComments.length}`);
|
|
391
396
|
}
|
|
392
397
|
}
|
|
393
398
|
}
|
|
@@ -485,6 +490,9 @@ export function register(program: Command): void {
|
|
|
485
490
|
if (totalCommentsPlaced > 0) {
|
|
486
491
|
summaryLines.push(`${chalk.bold(totalCommentsPlaced)} of ${comments.length} comments placed`);
|
|
487
492
|
}
|
|
493
|
+
if (totalCommentsLowConfidence > 0) {
|
|
494
|
+
summaryLines.push(`${chalk.yellow(totalCommentsLowConfidence)} placed approximately (review with verify-anchors)`);
|
|
495
|
+
}
|
|
488
496
|
if (totalCommentsDeduped > 0) {
|
|
489
497
|
summaryLines.push(`${chalk.cyan(totalCommentsDeduped)} already present (skipped)`);
|
|
490
498
|
}
|
|
@@ -587,12 +595,12 @@ async function syncCommentsOnly(
|
|
|
587
595
|
}
|
|
588
596
|
|
|
589
597
|
const firstBoundaryStart = boundaries[0].start;
|
|
590
|
-
const results: Array<{ file: string; placed: number; deduped: number; unmatched: number; skipped: boolean }> = [];
|
|
598
|
+
const results: Array<{ file: string; placed: number; lowConfidence: number; deduped: number; unmatched: number; skipped: boolean }> = [];
|
|
591
599
|
|
|
592
600
|
for (const boundary of activeBoundaries) {
|
|
593
601
|
const sectionPath = path.join(options.dir, boundary.file);
|
|
594
602
|
if (!fs.existsSync(sectionPath)) {
|
|
595
|
-
results.push({ file: boundary.file, placed: 0, deduped: 0, unmatched: 0, skipped: true });
|
|
603
|
+
results.push({ file: boundary.file, placed: 0, lowConfidence: 0, deduped: 0, unmatched: 0, skipped: true });
|
|
596
604
|
continue;
|
|
597
605
|
}
|
|
598
606
|
|
|
@@ -607,13 +615,13 @@ async function syncCommentsOnly(
|
|
|
607
615
|
});
|
|
608
616
|
|
|
609
617
|
if (sectionComments.length === 0) {
|
|
610
|
-
results.push({ file: boundary.file, placed: 0, deduped: 0, unmatched: 0, skipped: false });
|
|
618
|
+
results.push({ file: boundary.file, placed: 0, lowConfidence: 0, deduped: 0, unmatched: 0, skipped: false });
|
|
611
619
|
continue;
|
|
612
620
|
}
|
|
613
621
|
|
|
614
622
|
const original = fs.readFileSync(sectionPath, 'utf-8');
|
|
615
623
|
|
|
616
|
-
const stats = { placed: 0, deduped: 0, unmatched: 0 };
|
|
624
|
+
const stats = { placed: 0, lowConfidence: 0, deduped: 0, unmatched: 0 };
|
|
617
625
|
const annotated = insertCommentsIntoMarkdown(original, sectionComments, anchors, {
|
|
618
626
|
quiet: !process.env.DEBUG,
|
|
619
627
|
sectionBoundary: { start: boundary.start, end: boundary.end },
|
|
@@ -621,7 +629,7 @@ async function syncCommentsOnly(
|
|
|
621
629
|
outStats: stats,
|
|
622
630
|
});
|
|
623
631
|
|
|
624
|
-
if (!options.dryRun && stats.placed > 0) {
|
|
632
|
+
if (!options.dryRun && (stats.placed > 0 || stats.lowConfidence > 0)) {
|
|
625
633
|
fs.writeFileSync(sectionPath, annotated, 'utf-8');
|
|
626
634
|
}
|
|
627
635
|
results.push({ file: boundary.file, ...stats, skipped: false });
|
|
@@ -634,6 +642,7 @@ async function syncCommentsOnly(
|
|
|
634
642
|
return [
|
|
635
643
|
chalk.bold(r.file),
|
|
636
644
|
chalk.green(`${r.placed}`),
|
|
645
|
+
r.lowConfidence > 0 ? chalk.yellow(`${r.lowConfidence}`) : chalk.dim('-'),
|
|
637
646
|
r.deduped > 0 ? chalk.cyan(`${r.deduped}`) : chalk.dim('-'),
|
|
638
647
|
r.unmatched > 0 ? chalk.yellow(`${r.unmatched}`) : chalk.dim('-'),
|
|
639
648
|
chalk.dim('comments only'),
|
|
@@ -641,13 +650,14 @@ async function syncCommentsOnly(
|
|
|
641
650
|
});
|
|
642
651
|
|
|
643
652
|
console.log(fmt.table(
|
|
644
|
-
['File', 'Placed', 'Already', 'Unmatched', 'Mode'],
|
|
653
|
+
['File', 'Placed', 'Approx', 'Already', 'Unmatched', 'Mode'],
|
|
645
654
|
tableRows,
|
|
646
|
-
{ align: ['left', 'right', 'right', 'right', 'left'] },
|
|
655
|
+
{ align: ['left', 'right', 'right', 'right', 'right', 'left'] },
|
|
647
656
|
));
|
|
648
657
|
console.log();
|
|
649
658
|
|
|
650
659
|
const totalPlaced = results.reduce((s, r) => s + r.placed, 0);
|
|
660
|
+
const totalLowConfidence = results.reduce((s, r) => s + r.lowConfidence, 0);
|
|
651
661
|
const totalDeduped = results.reduce((s, r) => s + r.deduped, 0);
|
|
652
662
|
const totalUnmatched = results.reduce((s, r) => s + r.unmatched, 0);
|
|
653
663
|
|
|
@@ -656,6 +666,9 @@ async function syncCommentsOnly(
|
|
|
656
666
|
if (totalPlaced > 0) {
|
|
657
667
|
lines.push(`${chalk.bold(totalPlaced)} placed at anchors`);
|
|
658
668
|
}
|
|
669
|
+
if (totalLowConfidence > 0) {
|
|
670
|
+
lines.push(`${chalk.yellow(totalLowConfidence)} placed approximately (review with verify-anchors)`);
|
|
671
|
+
}
|
|
659
672
|
if (totalDeduped > 0) {
|
|
660
673
|
lines.push(`${chalk.cyan(totalDeduped)} already present (skipped to avoid duplication)`);
|
|
661
674
|
}
|
|
@@ -664,12 +677,12 @@ async function syncCommentsOnly(
|
|
|
664
677
|
}
|
|
665
678
|
if (options.dryRun) {
|
|
666
679
|
lines.push(chalk.yellow('Dry run — no files written'));
|
|
667
|
-
} else if (totalPlaced > 0) {
|
|
680
|
+
} else if (totalPlaced > 0 || totalLowConfidence > 0) {
|
|
668
681
|
lines.push(chalk.dim('Existing prose unchanged.'));
|
|
669
682
|
}
|
|
670
683
|
console.log(fmt.box(lines.join('\n'), { title: 'Summary', padding: 0 }));
|
|
671
684
|
|
|
672
|
-
if (totalUnmatched > 0) {
|
|
685
|
+
if (totalUnmatched > 0 || totalLowConfidence > 0) {
|
|
673
686
|
console.log();
|
|
674
687
|
console.log(chalk.dim('Tip: run "rev verify-anchors" to see which comments drifted.'));
|
|
675
688
|
}
|
package/lib/comment-realign.ts
CHANGED
|
@@ -1,56 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Realign comments from a reference DOCX
|
|
3
|
-
*
|
|
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
|
|
8
|
-
import {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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 (
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
316
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
360
|
-
const
|
|
361
|
-
|
|
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
|
-
|
|
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
|
-
|
|
439
|
-
|
|
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
|
}
|