docrev 0.9.6 → 0.9.7
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 +20 -0
- package/dev_notes/bug_repro_comment_parser.md +71 -0
- package/dist/lib/anchor-match.d.ts +41 -0
- package/dist/lib/anchor-match.d.ts.map +1 -0
- package/dist/lib/anchor-match.js +192 -0
- package/dist/lib/anchor-match.js.map +1 -0
- package/dist/lib/annotations.d.ts.map +1 -1
- package/dist/lib/annotations.js +8 -5
- package/dist/lib/annotations.js.map +1 -1
- package/dist/lib/commands/index.d.ts +2 -1
- package/dist/lib/commands/index.d.ts.map +1 -1
- package/dist/lib/commands/index.js +3 -1
- package/dist/lib/commands/index.js.map +1 -1
- package/dist/lib/commands/section-boundaries.d.ts +22 -0
- package/dist/lib/commands/section-boundaries.d.ts.map +1 -0
- package/dist/lib/commands/section-boundaries.js +53 -0
- package/dist/lib/commands/section-boundaries.js.map +1 -0
- package/dist/lib/commands/sync.d.ts.map +1 -1
- package/dist/lib/commands/sync.js +135 -0
- package/dist/lib/commands/sync.js.map +1 -1
- package/dist/lib/commands/verify-anchors.d.ts +17 -0
- package/dist/lib/commands/verify-anchors.d.ts.map +1 -0
- package/dist/lib/commands/verify-anchors.js +215 -0
- package/dist/lib/commands/verify-anchors.js.map +1 -0
- package/dist/lib/import.d.ts +14 -8
- package/dist/lib/import.d.ts.map +1 -1
- package/dist/lib/import.js +16 -144
- package/dist/lib/import.js.map +1 -1
- package/dist/lib/word-extraction.d.ts +23 -0
- package/dist/lib/word-extraction.d.ts.map +1 -1
- package/dist/lib/word-extraction.js +79 -0
- package/dist/lib/word-extraction.js.map +1 -1
- package/lib/anchor-match.ts +238 -0
- package/lib/annotations.ts +9 -5
- package/lib/commands/index.ts +3 -0
- package/lib/commands/section-boundaries.ts +72 -0
- package/lib/commands/sync.ts +165 -0
- package/lib/commands/verify-anchors.ts +261 -0
- package/lib/import.ts +29 -165
- package/lib/word-extraction.ts +93 -0
- package/package.json +1 -1
- package/skill/REFERENCE.md +29 -2
- package/skill/SKILL.md +12 -2
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anchor matching primitives shared between sync (insertion) and
|
|
3
|
+
* verify-anchors (drift reporting). The functions are pure: given an
|
|
4
|
+
* anchor string and surrounding context, locate candidate positions in
|
|
5
|
+
* a target text using progressively looser strategies.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type AnchorStrategy =
|
|
9
|
+
| 'direct'
|
|
10
|
+
| 'normalized'
|
|
11
|
+
| 'stripped'
|
|
12
|
+
| 'partial-start'
|
|
13
|
+
| 'partial-start-stripped'
|
|
14
|
+
| 'context-both'
|
|
15
|
+
| 'context-before'
|
|
16
|
+
| 'context-after'
|
|
17
|
+
| 'split-match'
|
|
18
|
+
| 'empty-anchor'
|
|
19
|
+
| 'failed';
|
|
20
|
+
|
|
21
|
+
export interface AnchorSearchResult {
|
|
22
|
+
occurrences: number[];
|
|
23
|
+
matchedAnchor: string | null;
|
|
24
|
+
strategy: AnchorStrategy;
|
|
25
|
+
stripped?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Strip CriticMarkup so the matcher sees plain prose instead of
|
|
30
|
+
* `{++inserted++}`/`{--deleted--}`/etc. Used when an anchor lives
|
|
31
|
+
* underneath previously imported track changes.
|
|
32
|
+
*/
|
|
33
|
+
export function stripCriticMarkup(text: string): string {
|
|
34
|
+
return text
|
|
35
|
+
.replace(/\{\+\+([^+]*)\+\+\}/g, '$1') // insertions: keep new text
|
|
36
|
+
.replace(/\{--([^-]*)--\}/g, '') // deletions: remove old text
|
|
37
|
+
.replace(/\{~~([^~]*)~>([^~]*)~~\}/g, '$2') // substitutions: keep new text
|
|
38
|
+
.replace(/\{>>[\s\S]*?<<\}/g, '') // comments: remove (non-greedy; comment text may contain '<')
|
|
39
|
+
.replace(/\[([^\]]*)\]\{\.mark\}/g, '$1'); // marked text: keep text
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Return every starting index where `needle` occurs in `haystack`.
|
|
44
|
+
* Empty needles return no occurrences (empty matches are not useful
|
|
45
|
+
* for anchor placement).
|
|
46
|
+
*/
|
|
47
|
+
export function findAllOccurrences(haystack: string, needle: string): number[] {
|
|
48
|
+
if (!needle || needle.length === 0) return [];
|
|
49
|
+
const occurrences: number[] = [];
|
|
50
|
+
let idx = 0;
|
|
51
|
+
while ((idx = haystack.indexOf(needle, idx)) !== -1) {
|
|
52
|
+
occurrences.push(idx);
|
|
53
|
+
idx += 1;
|
|
54
|
+
}
|
|
55
|
+
return occurrences;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Find candidate positions for `anchor` in `text`, falling back through
|
|
60
|
+
* progressively looser strategies (whitespace normalization, stripped
|
|
61
|
+
* CriticMarkup, partial-prefix, surrounding context, word splitting).
|
|
62
|
+
*
|
|
63
|
+
* The returned `strategy` lets callers distinguish a clean direct hit
|
|
64
|
+
* from a fuzzy approximation — useful for drift reporting.
|
|
65
|
+
*/
|
|
66
|
+
export function findAnchorInText(
|
|
67
|
+
anchor: string,
|
|
68
|
+
text: string,
|
|
69
|
+
before: string = '',
|
|
70
|
+
after: string = ''
|
|
71
|
+
): AnchorSearchResult {
|
|
72
|
+
// Empty anchor: skip directly to context-based matching
|
|
73
|
+
if (!anchor || anchor.trim().length === 0) {
|
|
74
|
+
if (before || after) {
|
|
75
|
+
const beforeLower = (before || '').toLowerCase();
|
|
76
|
+
const afterLower = (after || '').toLowerCase();
|
|
77
|
+
const textLower = text.toLowerCase();
|
|
78
|
+
|
|
79
|
+
if (before && after) {
|
|
80
|
+
const beforeIdx = textLower.indexOf(beforeLower.slice(-50));
|
|
81
|
+
if (beforeIdx !== -1) {
|
|
82
|
+
const searchStart = beforeIdx + beforeLower.slice(-50).length;
|
|
83
|
+
const afterIdx = textLower.indexOf(afterLower.slice(0, 50), searchStart);
|
|
84
|
+
if (afterIdx !== -1 && afterIdx - searchStart < 500) {
|
|
85
|
+
return { occurrences: [searchStart], matchedAnchor: null, strategy: 'context-both' };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (before) {
|
|
91
|
+
const beforeIdx = textLower.lastIndexOf(beforeLower.slice(-30));
|
|
92
|
+
if (beforeIdx !== -1) {
|
|
93
|
+
return {
|
|
94
|
+
occurrences: [beforeIdx + beforeLower.slice(-30).length],
|
|
95
|
+
matchedAnchor: null,
|
|
96
|
+
strategy: 'context-before',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (after) {
|
|
102
|
+
const afterIdx = textLower.indexOf(afterLower.slice(0, 30));
|
|
103
|
+
if (afterIdx !== -1) {
|
|
104
|
+
return { occurrences: [afterIdx], matchedAnchor: null, strategy: 'context-after' };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return { occurrences: [], matchedAnchor: null, strategy: 'empty-anchor' };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const anchorLower = anchor.toLowerCase();
|
|
112
|
+
const textLower = text.toLowerCase();
|
|
113
|
+
|
|
114
|
+
// Strategy 1: direct match
|
|
115
|
+
let occurrences = findAllOccurrences(textLower, anchorLower);
|
|
116
|
+
if (occurrences.length > 0) {
|
|
117
|
+
return { occurrences, matchedAnchor: anchor, strategy: 'direct' };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Strategy 2: normalized whitespace
|
|
121
|
+
const normalizedAnchor = anchor.replace(/\s+/g, ' ').toLowerCase();
|
|
122
|
+
const normalizedText = text.replace(/\s+/g, ' ').toLowerCase();
|
|
123
|
+
const idx = normalizedText.indexOf(normalizedAnchor);
|
|
124
|
+
if (idx !== -1) {
|
|
125
|
+
return { occurrences: [idx], matchedAnchor: anchor, strategy: 'normalized' };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Strategy 3: match in stripped CriticMarkup version
|
|
129
|
+
const strippedText = stripCriticMarkup(text);
|
|
130
|
+
const strippedLower = strippedText.toLowerCase();
|
|
131
|
+
occurrences = findAllOccurrences(strippedLower, anchorLower);
|
|
132
|
+
if (occurrences.length > 0) {
|
|
133
|
+
return { occurrences, matchedAnchor: anchor, strategy: 'stripped', stripped: true };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Strategy 4: first N words of anchor (long anchors)
|
|
137
|
+
const words = anchor.split(/\s+/);
|
|
138
|
+
if (words.length > 3) {
|
|
139
|
+
for (let n = Math.min(6, words.length); n >= 3; n--) {
|
|
140
|
+
const partialAnchor = words.slice(0, n).join(' ').toLowerCase();
|
|
141
|
+
if (partialAnchor.length >= 15) {
|
|
142
|
+
occurrences = findAllOccurrences(textLower, partialAnchor);
|
|
143
|
+
if (occurrences.length > 0) {
|
|
144
|
+
return { occurrences, matchedAnchor: words.slice(0, n).join(' '), strategy: 'partial-start' };
|
|
145
|
+
}
|
|
146
|
+
occurrences = findAllOccurrences(strippedLower, partialAnchor);
|
|
147
|
+
if (occurrences.length > 0) {
|
|
148
|
+
return {
|
|
149
|
+
occurrences,
|
|
150
|
+
matchedAnchor: words.slice(0, n).join(' '),
|
|
151
|
+
strategy: 'partial-start-stripped',
|
|
152
|
+
stripped: true,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Strategy 5: context (before/after) only
|
|
160
|
+
if (before || after) {
|
|
161
|
+
const beforeLower = before.toLowerCase();
|
|
162
|
+
const afterLower = after.toLowerCase();
|
|
163
|
+
|
|
164
|
+
if (before && after) {
|
|
165
|
+
const beforeIdx = textLower.indexOf(beforeLower.slice(-50));
|
|
166
|
+
if (beforeIdx !== -1) {
|
|
167
|
+
const searchStart = beforeIdx + beforeLower.slice(-50).length;
|
|
168
|
+
const afterIdx = textLower.indexOf(afterLower.slice(0, 50), searchStart);
|
|
169
|
+
if (afterIdx !== -1 && afterIdx - searchStart < 500) {
|
|
170
|
+
return { occurrences: [searchStart], matchedAnchor: null, strategy: 'context-both' };
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (before) {
|
|
176
|
+
const beforeIdx = textLower.lastIndexOf(beforeLower.slice(-30));
|
|
177
|
+
if (beforeIdx !== -1) {
|
|
178
|
+
return {
|
|
179
|
+
occurrences: [beforeIdx + beforeLower.slice(-30).length],
|
|
180
|
+
matchedAnchor: null,
|
|
181
|
+
strategy: 'context-before',
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (after) {
|
|
187
|
+
const afterIdx = textLower.indexOf(afterLower.slice(0, 30));
|
|
188
|
+
if (afterIdx !== -1) {
|
|
189
|
+
return { occurrences: [afterIdx], matchedAnchor: null, strategy: 'context-after' };
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Strategy 6: split anchor on transition characters
|
|
195
|
+
const splitPatterns = [' ', ', ', '. ', ' - ', ' – '];
|
|
196
|
+
for (const sep of splitPatterns) {
|
|
197
|
+
if (anchor.includes(sep)) {
|
|
198
|
+
const parts = anchor.split(sep).filter(p => p.length >= 4);
|
|
199
|
+
for (const part of parts) {
|
|
200
|
+
const partLower = part.toLowerCase();
|
|
201
|
+
occurrences = findAllOccurrences(textLower, partLower);
|
|
202
|
+
if (occurrences.length > 0 && occurrences.length < 5) {
|
|
203
|
+
return { occurrences, matchedAnchor: part, strategy: 'split-match' };
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return { occurrences: [], matchedAnchor: null, strategy: 'failed' };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Classify a strategy as a clean hit, a fuzzy/drifted hit, or no hit.
|
|
214
|
+
* Used by `verify-anchors` to summarize per-comment match quality.
|
|
215
|
+
*/
|
|
216
|
+
export type AnchorMatchQuality = 'clean' | 'drift' | 'context-only' | 'unmatched';
|
|
217
|
+
|
|
218
|
+
export function classifyStrategy(strategy: AnchorStrategy, occurrences: number): AnchorMatchQuality {
|
|
219
|
+
if (occurrences === 0) return 'unmatched';
|
|
220
|
+
switch (strategy) {
|
|
221
|
+
case 'direct':
|
|
222
|
+
case 'normalized':
|
|
223
|
+
return 'clean';
|
|
224
|
+
case 'stripped':
|
|
225
|
+
case 'partial-start':
|
|
226
|
+
case 'partial-start-stripped':
|
|
227
|
+
case 'split-match':
|
|
228
|
+
return 'drift';
|
|
229
|
+
case 'context-both':
|
|
230
|
+
case 'context-before':
|
|
231
|
+
case 'context-after':
|
|
232
|
+
return 'context-only';
|
|
233
|
+
case 'empty-anchor':
|
|
234
|
+
case 'failed':
|
|
235
|
+
default:
|
|
236
|
+
return 'unmatched';
|
|
237
|
+
}
|
|
238
|
+
}
|
package/lib/annotations.ts
CHANGED
|
@@ -91,16 +91,20 @@ function isCommentFalsePositive(commentContent: string, fullText: string, positi
|
|
|
91
91
|
// Contains markdown figure reference syntax
|
|
92
92
|
if (/\{#fig:|!\[/.test(commentContent)) return true;
|
|
93
93
|
|
|
94
|
-
//
|
|
95
|
-
|
|
94
|
+
// Real comments typically have "Author:" at start. Accept hyphens, apostrophes,
|
|
95
|
+
// periods, and Unicode letters so names like "Jens-Christian Svenning" or
|
|
96
|
+
// "Camilla T Colding-Jørgensen" don't get rejected. See gcol33/docrev#1.
|
|
97
|
+
const hasAuthorPrefix = /^[\p{L}][\p{L}\s\-'.]{0,30}:\s/u.test(commentContent.trim());
|
|
98
|
+
const hasResolvedMark = /^[✓✔]\s/.test(commentContent.trim());
|
|
99
|
+
|
|
100
|
+
// Contains URL patterns (likely a link, not a comment) — only filter when
|
|
101
|
+
// there is no real author prefix, since reviewers legitimately cite URLs/DOIs.
|
|
102
|
+
if (!hasAuthorPrefix && /https?:\/\/|www\./i.test(commentContent) && commentContent.length < 150) return true;
|
|
96
103
|
|
|
97
104
|
// Looks like code (contains programming patterns)
|
|
98
105
|
if (/function\s*\(|=>|import\s+|export\s+|const\s+|let\s+|var\s+/.test(commentContent)) return true;
|
|
99
106
|
|
|
100
107
|
// Very long without clear author pattern (likely caption, not comment)
|
|
101
|
-
// Real comments typically have "Author:" at start and are shorter
|
|
102
|
-
const hasAuthorPrefix = /^[A-Za-z][A-Za-z\s]{0,20}:\s/.test(commentContent.trim());
|
|
103
|
-
const hasResolvedMark = /^[✓✔]\s/.test(commentContent.trim());
|
|
104
108
|
if (!hasAuthorPrefix && !hasResolvedMark && commentContent.length > MAX_COMMENT_CONTENT_LENGTH) return true;
|
|
105
109
|
|
|
106
110
|
// Looks like a figure caption (starts with "Fig" or contains typical caption words)
|
package/lib/commands/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { register as registerCommentCommands } from './comments.js';
|
|
|
11
11
|
import { register as registerInitCommands } from './init.js';
|
|
12
12
|
import { register as registerSectionCommands } from './sections.js';
|
|
13
13
|
import { register as registerSyncCommands } from './sync.js';
|
|
14
|
+
import { register as registerVerifyAnchorsCommands } from './verify-anchors.js';
|
|
14
15
|
import { register as registerMergeResolveCommands } from './merge-resolve.js';
|
|
15
16
|
import { register as registerBuildCommands } from './build.js';
|
|
16
17
|
import { register as registerResponseCommands } from './response.js';
|
|
@@ -31,6 +32,7 @@ export {
|
|
|
31
32
|
registerInitCommands,
|
|
32
33
|
registerSectionCommands,
|
|
33
34
|
registerSyncCommands,
|
|
35
|
+
registerVerifyAnchorsCommands,
|
|
34
36
|
registerMergeResolveCommands,
|
|
35
37
|
registerBuildCommands,
|
|
36
38
|
registerResponseCommands,
|
|
@@ -68,6 +70,7 @@ export function registerAllCommands(program: Command, pkg?: PackageJson): void {
|
|
|
68
70
|
registerInitCommands(program);
|
|
69
71
|
registerSectionCommands(program);
|
|
70
72
|
registerSyncCommands(program);
|
|
73
|
+
registerVerifyAnchorsCommands(program);
|
|
71
74
|
registerMergeResolveCommands(program);
|
|
72
75
|
registerBuildCommands(program, pkg || {});
|
|
73
76
|
registerResponseCommands(program);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute section boundaries in a DOCX from its real heading paragraphs.
|
|
3
|
+
*
|
|
4
|
+
* Given the configured `sections.yaml` and the headings extracted via
|
|
5
|
+
* `extractHeadings()`, return one boundary per section file with text
|
|
6
|
+
* positions in the same coordinate system as `CommentAnchorData.docPosition`.
|
|
7
|
+
*
|
|
8
|
+
* Matching is by heading text (primary header + aliases, case-insensitive).
|
|
9
|
+
* This replaces the older keyword-search-in-body-text approach which would
|
|
10
|
+
* pick up section names that happen to appear inside prose ("results across
|
|
11
|
+
* countries") or in structured-abstract labels where paragraph boundaries
|
|
12
|
+
* are lost in concatenation.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { DocxHeading } from '../word-extraction.js';
|
|
16
|
+
import type { SectionConfig } from '../types.js';
|
|
17
|
+
|
|
18
|
+
export interface SectionBoundary {
|
|
19
|
+
file: string;
|
|
20
|
+
start: number;
|
|
21
|
+
end: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function computeSectionBoundaries(
|
|
25
|
+
sections: Record<string, SectionConfig>,
|
|
26
|
+
headings: DocxHeading[],
|
|
27
|
+
): SectionBoundary[] {
|
|
28
|
+
const matched: SectionBoundary[] = [];
|
|
29
|
+
|
|
30
|
+
// Only consider top-level (Heading1-style) when level info is available;
|
|
31
|
+
// when level==0 (unparseable style), fall back to all headings.
|
|
32
|
+
const haveLevels = headings.some(h => h.level > 0);
|
|
33
|
+
const candidates = haveLevels ? headings.filter(h => h.level === 1) : headings;
|
|
34
|
+
|
|
35
|
+
for (const [file, cfg] of Object.entries(sections)) {
|
|
36
|
+
const targets = [cfg.header, ...(cfg.aliases || [])]
|
|
37
|
+
.filter(Boolean)
|
|
38
|
+
.map(s => s.toLowerCase().trim());
|
|
39
|
+
|
|
40
|
+
let firstMatch = -1;
|
|
41
|
+
for (const h of candidates) {
|
|
42
|
+
const text = h.text.toLowerCase().trim();
|
|
43
|
+
if (targets.includes(text)) {
|
|
44
|
+
firstMatch = h.docPosition;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Fallback: if no level-1 hit, allow any-level match (handles single-level docs)
|
|
50
|
+
if (firstMatch < 0 && haveLevels) {
|
|
51
|
+
for (const h of headings) {
|
|
52
|
+
const text = h.text.toLowerCase().trim();
|
|
53
|
+
if (targets.includes(text)) {
|
|
54
|
+
firstMatch = h.docPosition;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (firstMatch >= 0) {
|
|
61
|
+
matched.push({ file, start: firstMatch, end: Number.MAX_SAFE_INTEGER });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Sort by start position and tighten each end to the next start
|
|
66
|
+
matched.sort((a, b) => a.start - b.start);
|
|
67
|
+
for (let i = 0; i < matched.length - 1; i++) {
|
|
68
|
+
matched[i].end = matched[i + 1].start;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return matched;
|
|
72
|
+
}
|
package/lib/commands/sync.ts
CHANGED
|
@@ -35,6 +35,10 @@ interface SyncOptions {
|
|
|
35
35
|
diff?: boolean;
|
|
36
36
|
force?: boolean;
|
|
37
37
|
dryRun?: boolean;
|
|
38
|
+
/** Commander maps `--comments-only` (a positive flag) cleanly. `--no-overwrite`
|
|
39
|
+
* conflicts with the existing `overwrite` semantics in `--force`-style flags
|
|
40
|
+
* and Commander's `--no-X` convention assigns `options.x === false`. */
|
|
41
|
+
commentsOnly?: boolean;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
/**
|
|
@@ -57,6 +61,7 @@ export function register(program: Command): void {
|
|
|
57
61
|
.option('--no-diff', 'Skip showing diff preview')
|
|
58
62
|
.option('--force', 'Overwrite files without conflict warning')
|
|
59
63
|
.option('--dry-run', 'Preview without writing files')
|
|
64
|
+
.option('--comments-only', 'Insert comments at fuzzy-matched anchors only; never modify existing prose or apply track changes (use when markdown was revised after the docx was sent for review)')
|
|
60
65
|
.action(async (docx: string | undefined, sections: string[], options: SyncOptions) => {
|
|
61
66
|
// Auto-detect most recent docx or pdf if not provided
|
|
62
67
|
if (!docx) {
|
|
@@ -137,6 +142,14 @@ export function register(program: Command): void {
|
|
|
137
142
|
process.exit(1);
|
|
138
143
|
}
|
|
139
144
|
|
|
145
|
+
// --comments-only: import comments only, never modify existing prose.
|
|
146
|
+
// Use this when the markdown has been revised since the docx was sent
|
|
147
|
+
// out — track changes from a stale draft would clobber newer edits.
|
|
148
|
+
if (options.commentsOnly) {
|
|
149
|
+
await syncCommentsOnly(docx, sections, options, configPath);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
140
153
|
// Check pandoc availability upfront and warn
|
|
141
154
|
const { hasPandoc, getInstallInstructions } = await import('../dependencies.js');
|
|
142
155
|
if (!hasPandoc()) {
|
|
@@ -534,3 +547,155 @@ export function register(program: Command): void {
|
|
|
534
547
|
}
|
|
535
548
|
});
|
|
536
549
|
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* `sync --comments-only`: import only Word comments at fuzzy-matched anchors.
|
|
553
|
+
*
|
|
554
|
+
* Skips the Word→Markdown diff entirely (no track changes, no pandoc, no
|
|
555
|
+
* prose modifications). Useful when the markdown has been edited after the
|
|
556
|
+
* docx was sent for review — applying track changes from a stale draft
|
|
557
|
+
* would overwrite newer edits.
|
|
558
|
+
*/
|
|
559
|
+
async function syncCommentsOnly(
|
|
560
|
+
docx: string,
|
|
561
|
+
sectionFilter: string[] | undefined,
|
|
562
|
+
options: SyncOptions,
|
|
563
|
+
configPath: string,
|
|
564
|
+
): Promise<void> {
|
|
565
|
+
const config = loadConfig(configPath);
|
|
566
|
+
const { extractWordComments, extractCommentAnchors, extractHeadings, insertCommentsIntoMarkdown } = await import('../import.js');
|
|
567
|
+
const { computeSectionBoundaries } = await import('./section-boundaries.js');
|
|
568
|
+
|
|
569
|
+
const spin = fmt.spinner(`Reading comments from ${path.basename(docx)}...`).start();
|
|
570
|
+
|
|
571
|
+
let comments;
|
|
572
|
+
let anchors;
|
|
573
|
+
let headings;
|
|
574
|
+
try {
|
|
575
|
+
comments = await extractWordComments(docx);
|
|
576
|
+
const result = await extractCommentAnchors(docx);
|
|
577
|
+
anchors = result.anchors;
|
|
578
|
+
headings = await extractHeadings(docx);
|
|
579
|
+
spin.stop();
|
|
580
|
+
} catch (err) {
|
|
581
|
+
spin.stop();
|
|
582
|
+
const error = err as Error;
|
|
583
|
+
console.error(fmt.status('error', error.message));
|
|
584
|
+
process.exit(1);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
console.log(fmt.header(`Comments from ${path.basename(docx)} (comments-only)`));
|
|
588
|
+
console.log();
|
|
589
|
+
|
|
590
|
+
if (comments.length === 0) {
|
|
591
|
+
console.log(fmt.status('info', 'No comments found in document.'));
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const boundaries = computeSectionBoundaries(config.sections, headings);
|
|
596
|
+
|
|
597
|
+
if (boundaries.length === 0) {
|
|
598
|
+
console.error(fmt.status('warning', 'No section headings detected in Word document.'));
|
|
599
|
+
console.error(chalk.dim(' Check that headers in sections.yaml match heading paragraphs in the docx.'));
|
|
600
|
+
process.exit(1);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// Apply optional section filter from CLI
|
|
604
|
+
let activeBoundaries = boundaries;
|
|
605
|
+
if (sectionFilter && sectionFilter.length > 0) {
|
|
606
|
+
const wanted = sectionFilter.map(s => s.trim().toLowerCase());
|
|
607
|
+
activeBoundaries = boundaries.filter(b => {
|
|
608
|
+
const base = b.file.replace(/\.md$/i, '').toLowerCase();
|
|
609
|
+
return wanted.some(name => base === name || base.includes(name));
|
|
610
|
+
});
|
|
611
|
+
if (activeBoundaries.length === 0) {
|
|
612
|
+
console.error(fmt.status('error', `No sections matched: ${sectionFilter.join(', ')}`));
|
|
613
|
+
process.exit(1);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const firstBoundaryStart = boundaries[0].start;
|
|
618
|
+
const results: Array<{ file: string; placed: number; unmatched: number; skipped: boolean }> = [];
|
|
619
|
+
|
|
620
|
+
for (const boundary of activeBoundaries) {
|
|
621
|
+
const sectionPath = path.join(options.dir, boundary.file);
|
|
622
|
+
if (!fs.existsSync(sectionPath)) {
|
|
623
|
+
results.push({ file: boundary.file, placed: 0, unmatched: 0, skipped: true });
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const isFirstSection = boundary === activeBoundaries[0];
|
|
628
|
+
const sectionComments = comments.filter((c: { id: string }) => {
|
|
629
|
+
const anchor = anchors.get(c.id);
|
|
630
|
+
if (!anchor || anchor.docPosition === undefined) return false;
|
|
631
|
+
if (anchor.docPosition >= boundary.start && anchor.docPosition < boundary.end) return true;
|
|
632
|
+
// Comments before the first heading land in the first matched section
|
|
633
|
+
if (isFirstSection && anchor.docPosition < firstBoundaryStart) return true;
|
|
634
|
+
return false;
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
if (sectionComments.length === 0) {
|
|
638
|
+
results.push({ file: boundary.file, placed: 0, unmatched: 0, skipped: false });
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const original = fs.readFileSync(sectionPath, 'utf-8');
|
|
643
|
+
const commentPattern = /\{>>.*?<<\}/gs;
|
|
644
|
+
const beforeCount = (original.match(commentPattern) || []).length;
|
|
645
|
+
|
|
646
|
+
const annotated = insertCommentsIntoMarkdown(original, sectionComments, anchors, {
|
|
647
|
+
quiet: !process.env.DEBUG,
|
|
648
|
+
sectionBoundary: { start: boundary.start, end: boundary.end },
|
|
649
|
+
wrapAnchor: false,
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
const afterCount = (annotated.match(commentPattern) || []).length;
|
|
653
|
+
const placed = afterCount - beforeCount;
|
|
654
|
+
const unmatched = sectionComments.length - placed;
|
|
655
|
+
|
|
656
|
+
if (!options.dryRun && placed > 0) {
|
|
657
|
+
fs.writeFileSync(sectionPath, annotated, 'utf-8');
|
|
658
|
+
}
|
|
659
|
+
results.push({ file: boundary.file, placed, unmatched, skipped: false });
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const tableRows = results.map(r => {
|
|
663
|
+
if (r.skipped) {
|
|
664
|
+
return [chalk.dim(r.file), chalk.yellow('missing'), '', ''];
|
|
665
|
+
}
|
|
666
|
+
return [
|
|
667
|
+
chalk.bold(r.file),
|
|
668
|
+
chalk.green(`${r.placed}`),
|
|
669
|
+
r.unmatched > 0 ? chalk.yellow(`${r.unmatched}`) : chalk.dim('-'),
|
|
670
|
+
chalk.dim('comments only'),
|
|
671
|
+
];
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
console.log(fmt.table(
|
|
675
|
+
['File', 'Placed', 'Unmatched', 'Mode'],
|
|
676
|
+
tableRows,
|
|
677
|
+
{ align: ['left', 'right', 'right', 'left'] },
|
|
678
|
+
));
|
|
679
|
+
console.log();
|
|
680
|
+
|
|
681
|
+
const totalPlaced = results.reduce((s, r) => s + r.placed, 0);
|
|
682
|
+
const totalUnmatched = results.reduce((s, r) => s + r.unmatched, 0);
|
|
683
|
+
|
|
684
|
+
const lines: string[] = [];
|
|
685
|
+
lines.push(`${chalk.bold(comments.length)} comments in document`);
|
|
686
|
+
lines.push(`${chalk.bold(totalPlaced)} placed at fuzzy-matched anchors`);
|
|
687
|
+
if (totalUnmatched > 0) {
|
|
688
|
+
lines.push(`${chalk.yellow(totalUnmatched)} unmatched (no anchor in current prose)`);
|
|
689
|
+
}
|
|
690
|
+
if (options.dryRun) {
|
|
691
|
+
lines.push(chalk.yellow('Dry run — no files written'));
|
|
692
|
+
} else if (totalPlaced > 0) {
|
|
693
|
+
lines.push(chalk.dim('Existing prose unchanged.'));
|
|
694
|
+
}
|
|
695
|
+
console.log(fmt.box(lines.join('\n'), { title: 'Summary', padding: 0 }));
|
|
696
|
+
|
|
697
|
+
if (totalUnmatched > 0) {
|
|
698
|
+
console.log();
|
|
699
|
+
console.log(chalk.dim('Tip: run "rev verify-anchors" to see which comments drifted.'));
|
|
700
|
+
}
|
|
701
|
+
}
|