docrev 0.11.1 → 0.11.2

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 (116) hide show
  1. package/.gitattributes +1 -1
  2. package/CHANGELOG.md +202 -197
  3. package/PLAN-tables-and-postprocess.md +850 -850
  4. package/README.md +433 -433
  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/build.d.ts +28 -0
  11. package/dist/lib/build.d.ts.map +1 -1
  12. package/dist/lib/build.js +81 -4
  13. package/dist/lib/build.js.map +1 -1
  14. package/dist/lib/commands/utilities.js +164 -164
  15. package/dist/lib/commands/word-tools.js +8 -8
  16. package/dist/lib/grammar.js +3 -3
  17. package/dist/lib/macro-filter.lua +201 -201
  18. package/dist/lib/pdf-comments.js +44 -44
  19. package/dist/lib/plugins.js +57 -57
  20. package/dist/lib/pptx-color-filter.lua +37 -37
  21. package/dist/lib/pptx-themes.js +115 -115
  22. package/dist/lib/schema.d.ts.map +1 -1
  23. package/dist/lib/schema.js +8 -2
  24. package/dist/lib/schema.js.map +1 -1
  25. package/dist/lib/spelling.js +2 -2
  26. package/dist/lib/templates.js +387 -387
  27. package/dist/lib/themes.js +51 -51
  28. package/eslint.config.js +27 -27
  29. package/lib/anchor-match.ts +307 -307
  30. package/lib/annotations.ts +664 -664
  31. package/lib/build.ts +2047 -1956
  32. package/lib/citations.ts +160 -160
  33. package/lib/commands/build.ts +885 -885
  34. package/lib/commands/citations.ts +515 -515
  35. package/lib/commands/comments.ts +1050 -1050
  36. package/lib/commands/context.ts +176 -176
  37. package/lib/commands/core.ts +309 -309
  38. package/lib/commands/doi.ts +451 -451
  39. package/lib/commands/file-ops.ts +372 -372
  40. package/lib/commands/history.ts +320 -320
  41. package/lib/commands/index.ts +87 -87
  42. package/lib/commands/init.ts +259 -259
  43. package/lib/commands/merge-resolve.ts +378 -378
  44. package/lib/commands/preview.ts +178 -178
  45. package/lib/commands/project-info.ts +244 -244
  46. package/lib/commands/quality.ts +517 -517
  47. package/lib/commands/response.ts +454 -454
  48. package/lib/commands/section-boundaries.ts +82 -82
  49. package/lib/commands/sections.ts +451 -451
  50. package/lib/commands/sync.ts +689 -689
  51. package/lib/commands/text-ops.ts +449 -449
  52. package/lib/commands/utilities.ts +448 -448
  53. package/lib/commands/verify-anchors.ts +272 -272
  54. package/lib/commands/word-tools.ts +340 -340
  55. package/lib/comment-realign.ts +99 -99
  56. package/lib/config.ts +84 -84
  57. package/lib/crossref.ts +781 -781
  58. package/lib/csl.ts +191 -191
  59. package/lib/dependencies.ts +132 -132
  60. package/lib/diff-engine.ts +465 -465
  61. package/lib/doi-cache.ts +115 -115
  62. package/lib/doi.ts +879 -879
  63. package/lib/equations.ts +506 -506
  64. package/lib/errors.ts +350 -350
  65. package/lib/format.ts +541 -541
  66. package/lib/git.ts +326 -326
  67. package/lib/grammar.ts +303 -303
  68. package/lib/image-registry.ts +180 -180
  69. package/lib/import.ts +906 -906
  70. package/lib/journals.ts +543 -543
  71. package/lib/macro-filter.lua +201 -201
  72. package/lib/macros.ts +273 -273
  73. package/lib/merge.ts +633 -633
  74. package/lib/ooxml.ts +768 -768
  75. package/lib/orcid.ts +144 -144
  76. package/lib/pdf-comments.ts +263 -263
  77. package/lib/pdf-import.ts +524 -524
  78. package/lib/plugins.ts +362 -362
  79. package/lib/postprocess.ts +188 -188
  80. package/lib/pptx-color-filter.lua +37 -37
  81. package/lib/pptx-template.ts +469 -469
  82. package/lib/pptx-themes.ts +483 -483
  83. package/lib/protect-restore.ts +520 -520
  84. package/lib/rate-limiter.ts +127 -127
  85. package/lib/response.ts +197 -197
  86. package/lib/restore-references.ts +240 -240
  87. package/lib/review.ts +327 -327
  88. package/lib/schema.ts +494 -488
  89. package/lib/scientific-words.ts +73 -73
  90. package/lib/sections.ts +428 -428
  91. package/lib/slides.ts +756 -756
  92. package/lib/spelling.ts +334 -334
  93. package/lib/templates.ts +526 -526
  94. package/lib/themes.ts +742 -742
  95. package/lib/trackchanges.ts +166 -166
  96. package/lib/tui.ts +450 -450
  97. package/lib/types.ts +546 -546
  98. package/lib/undo.ts +250 -250
  99. package/lib/utils.ts +69 -69
  100. package/lib/variables.ts +179 -179
  101. package/lib/word-extraction.ts +525 -525
  102. package/lib/word.ts +526 -526
  103. package/lib/wordcomments.ts +789 -789
  104. package/package.json +1 -1
  105. package/scripts/postbuild.js +47 -47
  106. package/skill/REFERENCE.md +550 -550
  107. package/skill/SKILL.md +302 -302
  108. package/tsconfig.json +26 -26
  109. package/types/index.d.ts +531 -531
  110. package/issues.md +0 -180
  111. package/site/assets/extra.css +0 -208
  112. package/site/commands.html +0 -926
  113. package/site/configuration.html +0 -469
  114. package/site/index.html +0 -288
  115. package/site/troubleshooting.html +0 -461
  116. package/site/workflow.html +0 -518
package/lib/sections.ts CHANGED
@@ -1,428 +1,428 @@
1
- /**
2
- * Section handling - map between section .md files and combined documents
3
- */
4
-
5
- import * as fs from 'fs';
6
- import * as path from 'path';
7
- import YAML from 'yaml';
8
- import type { SectionConfig, SectionsConfig, ExtractedSection } from './types.js';
9
-
10
- /**
11
- * Default section order (common academic paper structure)
12
- */
13
- const DEFAULT_ORDER = [
14
- 'abstract',
15
- 'introduction',
16
- 'background',
17
- 'literature',
18
- 'theory',
19
- 'methods',
20
- 'materials',
21
- 'data',
22
- 'results',
23
- 'analysis',
24
- 'discussion',
25
- 'conclusion',
26
- 'references',
27
- 'appendix',
28
- 'supplementary',
29
- ];
30
-
31
- /**
32
- * Extract header from a markdown file
33
- */
34
- export function extractHeader(filePath: string): string | null {
35
- if (!fs.existsSync(filePath)) return null;
36
-
37
- const content = fs.readFileSync(filePath, 'utf-8');
38
- const lines = content.split('\n');
39
-
40
- for (const line of lines) {
41
- const match = line.match(/^#\s+(.+)$/);
42
- if (match && match[1]) {
43
- return match[1].trim();
44
- }
45
- }
46
-
47
- return null;
48
- }
49
-
50
- /**
51
- * Extract the first markdown heading of ANY level (`#`–`######`) from a file.
52
- *
53
- * Unlike {@link extractHeader} (which is H1-only by contract), this is used to
54
- * derive a section's header from files that lead with a subsection — e.g.
55
- * `02_objectives.md` starting with `## 1.2 Objectives`. Using the real first
56
- * heading lets the derived header match the corresponding docx heading.
57
- */
58
- export function extractFirstHeading(filePath: string): string | null {
59
- if (!fs.existsSync(filePath)) return null;
60
-
61
- const content = fs.readFileSync(filePath, 'utf-8');
62
- for (const line of content.split('\n')) {
63
- const match = line.match(/^#{1,6}\s+(.+)$/);
64
- if (match && match[1]) {
65
- return match[1].trim();
66
- }
67
- }
68
-
69
- return null;
70
- }
71
-
72
- /**
73
- * Generate sections.yaml from existing .md files
74
- */
75
- export function generateConfig(
76
- directory: string,
77
- excludePatterns: string[] = ['paper.md', 'README.md', 'CLAUDE.md']
78
- ): SectionsConfig {
79
- const files = fs.readdirSync(directory).filter((f) => {
80
- if (!f.endsWith('.md')) return false;
81
- if (excludePatterns.some((p) => f.toLowerCase().includes(p.toLowerCase()))) return false;
82
- return true;
83
- });
84
-
85
- const sections: Record<string, SectionConfig> = {};
86
-
87
- for (const file of files) {
88
- const filePath = path.join(directory, file);
89
- // Use the first heading at any level: a section file may be headed by a
90
- // subsection (## 1.2 Objectives). H1-only extraction left such files with a
91
- // filename-derived header that never matched the reviewed document.
92
- const header = extractFirstHeading(filePath);
93
- const baseName = path.basename(file, '.md').toLowerCase();
94
-
95
- // Determine order based on common patterns
96
- let order = DEFAULT_ORDER.findIndex((s) => baseName.includes(s));
97
- if (order === -1) order = 999;
98
-
99
- sections[file] = {
100
- header: header || titleCase(baseName),
101
- aliases: [],
102
- order: order,
103
- };
104
- }
105
-
106
- // Sort by order
107
- const sorted = Object.entries(sections)
108
- .sort((a, b) => (a[1].order ?? 999) - (b[1].order ?? 999))
109
- .reduce((acc, [k, v]) => {
110
- acc[k] = v;
111
- return acc;
112
- }, {} as Record<string, SectionConfig>);
113
-
114
- return {
115
- version: 1,
116
- description: 'Section configuration for rev import/split',
117
- sections: sorted,
118
- };
119
- }
120
-
121
- /**
122
- * Convert string to title case
123
- */
124
- function titleCase(str: string): string {
125
- return str
126
- .split(/[-_\s]+/)
127
- .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
128
- .join(' ');
129
- }
130
-
131
- /**
132
- * Load sections config from yaml file
133
- */
134
- export function loadConfig(configPath: string): SectionsConfig {
135
- const content = fs.readFileSync(configPath, 'utf-8');
136
- const config = YAML.parse(content);
137
-
138
- // Normalize: convert string values to full config objects
139
- const normalized: SectionsConfig = {
140
- version: config.version || 1,
141
- description: config.description,
142
- sections: {},
143
- };
144
-
145
- for (const [file, value] of Object.entries(config.sections || {})) {
146
- if (typeof value === 'string') {
147
- normalized.sections[file] = {
148
- header: value,
149
- aliases: [],
150
- };
151
- } else {
152
- const typedValue = value as SectionConfig;
153
- normalized.sections[file] = {
154
- header: typedValue.header,
155
- aliases: typedValue.aliases || [],
156
- order: typedValue.order,
157
- };
158
- }
159
- }
160
-
161
- return normalized;
162
- }
163
-
164
- /**
165
- * Save sections config to yaml file
166
- */
167
- export function saveConfig(configPath: string, config: SectionsConfig): void {
168
- const yamlStr = YAML.stringify(config, { indent: 2, lineWidth: 100 });
169
- fs.writeFileSync(configPath, yamlStr, 'utf-8');
170
- }
171
-
172
- /**
173
- * Derive a SectionsConfig from the `sections:` list in rev.yaml.
174
- *
175
- * Each listed file's header is its first markdown H1 (falling back to a
176
- * title-cased file name); order follows the list order in rev.yaml. This is
177
- * the same section list that `build` consumes, so a project that only has a
178
- * `rev.yaml` needs no separate `sections.yaml`.
179
- *
180
- * Returns null when rev.yaml is absent, unparseable, or has no `sections` list.
181
- */
182
- export function deriveSectionsFromRev(directory: string): SectionsConfig | null {
183
- const revPath = path.join(directory, 'rev.yaml');
184
- if (!fs.existsSync(revPath)) return null;
185
-
186
- let parsed: { sections?: unknown };
187
- try {
188
- parsed = YAML.parse(fs.readFileSync(revPath, 'utf-8')) || {};
189
- } catch {
190
- return null;
191
- }
192
-
193
- const list = parsed.sections;
194
- if (!Array.isArray(list) || list.length === 0) return null;
195
-
196
- const sections: Record<string, SectionConfig> = {};
197
- list.forEach((entry, index) => {
198
- if (typeof entry !== 'string') return;
199
- const header = extractFirstHeading(path.join(directory, entry)) || titleCase(path.basename(entry, '.md'));
200
- sections[entry] = { header, aliases: [], order: index };
201
- });
202
-
203
- if (Object.keys(sections).length === 0) return null;
204
-
205
- return {
206
- version: 1,
207
- description: 'Derived from rev.yaml sections list',
208
- sections,
209
- };
210
- }
211
-
212
- /**
213
- * Resolve the effective sections config for a project directory.
214
- *
215
- * Precedence (single source of truth, with optional override):
216
- * 1. An explicit sections config file (default `sections.yaml`) when it
217
- * exists — lets users override headers/aliases/order.
218
- * 2. Otherwise the `sections:` list in `rev.yaml`, via {@link deriveSectionsFromRev}.
219
- *
220
- * Returns null only when neither source yields any sections; callers turn that
221
- * into a user-facing error.
222
- */
223
- export function resolveSectionsConfig(
224
- directory: string,
225
- configFileName = 'sections.yaml'
226
- ): { config: SectionsConfig; source: string } | null {
227
- const explicitPath = path.resolve(directory, configFileName);
228
- if (fs.existsSync(explicitPath)) {
229
- return { config: loadConfig(explicitPath), source: explicitPath };
230
- }
231
-
232
- const derived = deriveSectionsFromRev(directory);
233
- if (derived) {
234
- return { config: derived, source: path.resolve(directory, 'rev.yaml') };
235
- }
236
-
237
- return null;
238
- }
239
-
240
- /**
241
- * Match a heading to a section file
242
- */
243
- export function matchHeading(
244
- heading: string,
245
- sections: Record<string, SectionConfig>
246
- ): { file: string; config: SectionConfig } | null {
247
- // Strip markdown header prefix (# or ##, etc.) before matching
248
- const normalizedHeading = heading.replace(/^#{1,6}\s+/, '').toLowerCase().trim();
249
-
250
- for (const [file, config] of Object.entries(sections)) {
251
- // Check primary header
252
- if (config.header.toLowerCase().trim() === normalizedHeading) {
253
- return { file, config };
254
- }
255
-
256
- // Check aliases
257
- if (config.aliases) {
258
- for (const alias of config.aliases) {
259
- if (alias.toLowerCase().trim() === normalizedHeading) {
260
- return { file, config };
261
- }
262
- }
263
- }
264
-
265
- // Fuzzy match: check if heading contains the key words
266
- const headerWords = config.header.toLowerCase().split(/\s+/);
267
- const headingWords = normalizedHeading.split(/\s+/);
268
- const matchCount = headerWords.filter((w) => headingWords.includes(w)).length;
269
- if (matchCount >= headerWords.length * 0.7) {
270
- return { file, config };
271
- }
272
- }
273
-
274
- return null;
275
- }
276
-
277
- /**
278
- * Extract sections from Word document text
279
- */
280
- export function extractSectionsFromText(
281
- text: string,
282
- sections: Record<string, SectionConfig>
283
- ): ExtractedSection[] {
284
- const result: ExtractedSection[] = [];
285
-
286
- // Process line by line to detect markdown headers
287
- const lines = text.split('\n');
288
- let currentSection: { file: string; header: string } | null = null;
289
- let currentContent: string[] = [];
290
-
291
- for (let i = 0; i < lines.length; i++) {
292
- const line = lines[i];
293
- if (!line) continue;
294
- const trimmed = line.trim();
295
-
296
- // Explicitly check for markdown headers (# Header)
297
- const headerMatch = trimmed.match(/^(#{1,6})\s+(.+)$/);
298
-
299
- let matchedSection: { file: string; config: SectionConfig } | null = null;
300
- if (headerMatch) {
301
- // This is a markdown header - try to match it to a section
302
- matchedSection = matchHeading(trimmed, sections);
303
- } else if (trimmed.length > 0 && trimmed.length < 100 && !trimmed.includes('.')) {
304
- // Fallback: check if short text without periods matches a section (for plain text headings)
305
- matchedSection = matchHeading(trimmed, sections);
306
- }
307
-
308
- if (matchedSection) {
309
- // Save previous section
310
- if (currentSection) {
311
- // Include header in content for proper diffing
312
- const fullContent = currentSection.header + '\n\n' + currentContent.join('\n').trim();
313
- result.push({
314
- file: currentSection.file,
315
- header: currentSection.header,
316
- content: fullContent.trim(),
317
- matched: true,
318
- });
319
- }
320
-
321
- currentSection = {
322
- file: matchedSection.file,
323
- header: trimmed,
324
- };
325
- currentContent = [];
326
- } else {
327
- currentContent.push(line);
328
- }
329
- }
330
-
331
- // Save last section
332
- if (currentSection) {
333
- // Include header in content for proper diffing
334
- const fullContent = currentSection.header + '\n\n' + currentContent.join('\n').trim();
335
- result.push({
336
- file: currentSection.file,
337
- header: currentSection.header,
338
- content: fullContent.trim(),
339
- matched: true,
340
- });
341
- }
342
-
343
- return result;
344
- }
345
-
346
- /**
347
- * Parse annotated paper.md and split back to section files
348
- */
349
- export function splitAnnotatedPaper(
350
- paperContent: string,
351
- sections: Record<string, SectionConfig>
352
- ): Map<string, string> {
353
- const result = new Map<string, string>();
354
-
355
- // Look for section markers: <!-- @section:filename.md -->
356
- const markerPattern = /<!--\s*@section:(\S+\.md)\s*-->/g;
357
- const markers = [...paperContent.matchAll(markerPattern)];
358
-
359
- if (markers.length > 0) {
360
- // Use markers
361
- for (let i = 0; i < markers.length; i++) {
362
- const marker = markers[i];
363
- if (!marker || !marker[1]) continue;
364
- const file = marker[1];
365
- const start = (marker.index || 0) + marker[0].length;
366
- const end = markers[i + 1]?.index || paperContent.length;
367
-
368
- let content = paperContent.slice(start, end).trim();
369
-
370
- // Remove trailing marker if present
371
- content = content.replace(/<!--\s*@section:\S+\.md\s*-->$/, '').trim();
372
-
373
- result.set(file, content);
374
- }
375
- } else {
376
- // Fall back to header detection
377
- const lines = paperContent.split('\n');
378
- let currentFile: string | null = null;
379
- let currentContent: string[] = [];
380
-
381
- for (const line of lines) {
382
- const headerMatch = line.match(/^#{1,6}\s+(.+)$/);
383
-
384
- if (headerMatch && headerMatch[1]) {
385
- // Save previous section
386
- if (currentFile) {
387
- result.set(currentFile, currentContent.join('\n').trim());
388
- }
389
-
390
- // Find matching section file
391
- const heading = headerMatch[1].trim();
392
- const match = matchHeading(heading, sections);
393
-
394
- if (match) {
395
- currentFile = match.file;
396
- currentContent = [line];
397
- } else {
398
- // Unknown section - keep accumulating to previous
399
- currentContent.push(line);
400
- }
401
- } else {
402
- currentContent.push(line);
403
- }
404
- }
405
-
406
- // Save last section
407
- if (currentFile) {
408
- result.set(currentFile, currentContent.join('\n').trim());
409
- }
410
- }
411
-
412
- return result;
413
- }
414
-
415
- /**
416
- * Get ordered list of section files from config
417
- */
418
- export function getOrderedSections(config: SectionsConfig): string[] {
419
- const entries = Object.entries(config.sections || {});
420
-
421
- return entries
422
- .sort((a, b) => {
423
- const orderA = a[1].order ?? 999;
424
- const orderB = b[1].order ?? 999;
425
- return orderA - orderB;
426
- })
427
- .map(([file]) => file);
428
- }
1
+ /**
2
+ * Section handling - map between section .md files and combined documents
3
+ */
4
+
5
+ import * as fs from 'fs';
6
+ import * as path from 'path';
7
+ import YAML from 'yaml';
8
+ import type { SectionConfig, SectionsConfig, ExtractedSection } from './types.js';
9
+
10
+ /**
11
+ * Default section order (common academic paper structure)
12
+ */
13
+ const DEFAULT_ORDER = [
14
+ 'abstract',
15
+ 'introduction',
16
+ 'background',
17
+ 'literature',
18
+ 'theory',
19
+ 'methods',
20
+ 'materials',
21
+ 'data',
22
+ 'results',
23
+ 'analysis',
24
+ 'discussion',
25
+ 'conclusion',
26
+ 'references',
27
+ 'appendix',
28
+ 'supplementary',
29
+ ];
30
+
31
+ /**
32
+ * Extract header from a markdown file
33
+ */
34
+ export function extractHeader(filePath: string): string | null {
35
+ if (!fs.existsSync(filePath)) return null;
36
+
37
+ const content = fs.readFileSync(filePath, 'utf-8');
38
+ const lines = content.split('\n');
39
+
40
+ for (const line of lines) {
41
+ const match = line.match(/^#\s+(.+)$/);
42
+ if (match && match[1]) {
43
+ return match[1].trim();
44
+ }
45
+ }
46
+
47
+ return null;
48
+ }
49
+
50
+ /**
51
+ * Extract the first markdown heading of ANY level (`#`–`######`) from a file.
52
+ *
53
+ * Unlike {@link extractHeader} (which is H1-only by contract), this is used to
54
+ * derive a section's header from files that lead with a subsection — e.g.
55
+ * `02_objectives.md` starting with `## 1.2 Objectives`. Using the real first
56
+ * heading lets the derived header match the corresponding docx heading.
57
+ */
58
+ export function extractFirstHeading(filePath: string): string | null {
59
+ if (!fs.existsSync(filePath)) return null;
60
+
61
+ const content = fs.readFileSync(filePath, 'utf-8');
62
+ for (const line of content.split('\n')) {
63
+ const match = line.match(/^#{1,6}\s+(.+)$/);
64
+ if (match && match[1]) {
65
+ return match[1].trim();
66
+ }
67
+ }
68
+
69
+ return null;
70
+ }
71
+
72
+ /**
73
+ * Generate sections.yaml from existing .md files
74
+ */
75
+ export function generateConfig(
76
+ directory: string,
77
+ excludePatterns: string[] = ['paper.md', 'README.md', 'CLAUDE.md']
78
+ ): SectionsConfig {
79
+ const files = fs.readdirSync(directory).filter((f) => {
80
+ if (!f.endsWith('.md')) return false;
81
+ if (excludePatterns.some((p) => f.toLowerCase().includes(p.toLowerCase()))) return false;
82
+ return true;
83
+ });
84
+
85
+ const sections: Record<string, SectionConfig> = {};
86
+
87
+ for (const file of files) {
88
+ const filePath = path.join(directory, file);
89
+ // Use the first heading at any level: a section file may be headed by a
90
+ // subsection (## 1.2 Objectives). H1-only extraction left such files with a
91
+ // filename-derived header that never matched the reviewed document.
92
+ const header = extractFirstHeading(filePath);
93
+ const baseName = path.basename(file, '.md').toLowerCase();
94
+
95
+ // Determine order based on common patterns
96
+ let order = DEFAULT_ORDER.findIndex((s) => baseName.includes(s));
97
+ if (order === -1) order = 999;
98
+
99
+ sections[file] = {
100
+ header: header || titleCase(baseName),
101
+ aliases: [],
102
+ order: order,
103
+ };
104
+ }
105
+
106
+ // Sort by order
107
+ const sorted = Object.entries(sections)
108
+ .sort((a, b) => (a[1].order ?? 999) - (b[1].order ?? 999))
109
+ .reduce((acc, [k, v]) => {
110
+ acc[k] = v;
111
+ return acc;
112
+ }, {} as Record<string, SectionConfig>);
113
+
114
+ return {
115
+ version: 1,
116
+ description: 'Section configuration for rev import/split',
117
+ sections: sorted,
118
+ };
119
+ }
120
+
121
+ /**
122
+ * Convert string to title case
123
+ */
124
+ function titleCase(str: string): string {
125
+ return str
126
+ .split(/[-_\s]+/)
127
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
128
+ .join(' ');
129
+ }
130
+
131
+ /**
132
+ * Load sections config from yaml file
133
+ */
134
+ export function loadConfig(configPath: string): SectionsConfig {
135
+ const content = fs.readFileSync(configPath, 'utf-8');
136
+ const config = YAML.parse(content);
137
+
138
+ // Normalize: convert string values to full config objects
139
+ const normalized: SectionsConfig = {
140
+ version: config.version || 1,
141
+ description: config.description,
142
+ sections: {},
143
+ };
144
+
145
+ for (const [file, value] of Object.entries(config.sections || {})) {
146
+ if (typeof value === 'string') {
147
+ normalized.sections[file] = {
148
+ header: value,
149
+ aliases: [],
150
+ };
151
+ } else {
152
+ const typedValue = value as SectionConfig;
153
+ normalized.sections[file] = {
154
+ header: typedValue.header,
155
+ aliases: typedValue.aliases || [],
156
+ order: typedValue.order,
157
+ };
158
+ }
159
+ }
160
+
161
+ return normalized;
162
+ }
163
+
164
+ /**
165
+ * Save sections config to yaml file
166
+ */
167
+ export function saveConfig(configPath: string, config: SectionsConfig): void {
168
+ const yamlStr = YAML.stringify(config, { indent: 2, lineWidth: 100 });
169
+ fs.writeFileSync(configPath, yamlStr, 'utf-8');
170
+ }
171
+
172
+ /**
173
+ * Derive a SectionsConfig from the `sections:` list in rev.yaml.
174
+ *
175
+ * Each listed file's header is its first markdown H1 (falling back to a
176
+ * title-cased file name); order follows the list order in rev.yaml. This is
177
+ * the same section list that `build` consumes, so a project that only has a
178
+ * `rev.yaml` needs no separate `sections.yaml`.
179
+ *
180
+ * Returns null when rev.yaml is absent, unparseable, or has no `sections` list.
181
+ */
182
+ export function deriveSectionsFromRev(directory: string): SectionsConfig | null {
183
+ const revPath = path.join(directory, 'rev.yaml');
184
+ if (!fs.existsSync(revPath)) return null;
185
+
186
+ let parsed: { sections?: unknown };
187
+ try {
188
+ parsed = YAML.parse(fs.readFileSync(revPath, 'utf-8')) || {};
189
+ } catch {
190
+ return null;
191
+ }
192
+
193
+ const list = parsed.sections;
194
+ if (!Array.isArray(list) || list.length === 0) return null;
195
+
196
+ const sections: Record<string, SectionConfig> = {};
197
+ list.forEach((entry, index) => {
198
+ if (typeof entry !== 'string') return;
199
+ const header = extractFirstHeading(path.join(directory, entry)) || titleCase(path.basename(entry, '.md'));
200
+ sections[entry] = { header, aliases: [], order: index };
201
+ });
202
+
203
+ if (Object.keys(sections).length === 0) return null;
204
+
205
+ return {
206
+ version: 1,
207
+ description: 'Derived from rev.yaml sections list',
208
+ sections,
209
+ };
210
+ }
211
+
212
+ /**
213
+ * Resolve the effective sections config for a project directory.
214
+ *
215
+ * Precedence (single source of truth, with optional override):
216
+ * 1. An explicit sections config file (default `sections.yaml`) when it
217
+ * exists — lets users override headers/aliases/order.
218
+ * 2. Otherwise the `sections:` list in `rev.yaml`, via {@link deriveSectionsFromRev}.
219
+ *
220
+ * Returns null only when neither source yields any sections; callers turn that
221
+ * into a user-facing error.
222
+ */
223
+ export function resolveSectionsConfig(
224
+ directory: string,
225
+ configFileName = 'sections.yaml'
226
+ ): { config: SectionsConfig; source: string } | null {
227
+ const explicitPath = path.resolve(directory, configFileName);
228
+ if (fs.existsSync(explicitPath)) {
229
+ return { config: loadConfig(explicitPath), source: explicitPath };
230
+ }
231
+
232
+ const derived = deriveSectionsFromRev(directory);
233
+ if (derived) {
234
+ return { config: derived, source: path.resolve(directory, 'rev.yaml') };
235
+ }
236
+
237
+ return null;
238
+ }
239
+
240
+ /**
241
+ * Match a heading to a section file
242
+ */
243
+ export function matchHeading(
244
+ heading: string,
245
+ sections: Record<string, SectionConfig>
246
+ ): { file: string; config: SectionConfig } | null {
247
+ // Strip markdown header prefix (# or ##, etc.) before matching
248
+ const normalizedHeading = heading.replace(/^#{1,6}\s+/, '').toLowerCase().trim();
249
+
250
+ for (const [file, config] of Object.entries(sections)) {
251
+ // Check primary header
252
+ if (config.header.toLowerCase().trim() === normalizedHeading) {
253
+ return { file, config };
254
+ }
255
+
256
+ // Check aliases
257
+ if (config.aliases) {
258
+ for (const alias of config.aliases) {
259
+ if (alias.toLowerCase().trim() === normalizedHeading) {
260
+ return { file, config };
261
+ }
262
+ }
263
+ }
264
+
265
+ // Fuzzy match: check if heading contains the key words
266
+ const headerWords = config.header.toLowerCase().split(/\s+/);
267
+ const headingWords = normalizedHeading.split(/\s+/);
268
+ const matchCount = headerWords.filter((w) => headingWords.includes(w)).length;
269
+ if (matchCount >= headerWords.length * 0.7) {
270
+ return { file, config };
271
+ }
272
+ }
273
+
274
+ return null;
275
+ }
276
+
277
+ /**
278
+ * Extract sections from Word document text
279
+ */
280
+ export function extractSectionsFromText(
281
+ text: string,
282
+ sections: Record<string, SectionConfig>
283
+ ): ExtractedSection[] {
284
+ const result: ExtractedSection[] = [];
285
+
286
+ // Process line by line to detect markdown headers
287
+ const lines = text.split('\n');
288
+ let currentSection: { file: string; header: string } | null = null;
289
+ let currentContent: string[] = [];
290
+
291
+ for (let i = 0; i < lines.length; i++) {
292
+ const line = lines[i];
293
+ if (!line) continue;
294
+ const trimmed = line.trim();
295
+
296
+ // Explicitly check for markdown headers (# Header)
297
+ const headerMatch = trimmed.match(/^(#{1,6})\s+(.+)$/);
298
+
299
+ let matchedSection: { file: string; config: SectionConfig } | null = null;
300
+ if (headerMatch) {
301
+ // This is a markdown header - try to match it to a section
302
+ matchedSection = matchHeading(trimmed, sections);
303
+ } else if (trimmed.length > 0 && trimmed.length < 100 && !trimmed.includes('.')) {
304
+ // Fallback: check if short text without periods matches a section (for plain text headings)
305
+ matchedSection = matchHeading(trimmed, sections);
306
+ }
307
+
308
+ if (matchedSection) {
309
+ // Save previous section
310
+ if (currentSection) {
311
+ // Include header in content for proper diffing
312
+ const fullContent = currentSection.header + '\n\n' + currentContent.join('\n').trim();
313
+ result.push({
314
+ file: currentSection.file,
315
+ header: currentSection.header,
316
+ content: fullContent.trim(),
317
+ matched: true,
318
+ });
319
+ }
320
+
321
+ currentSection = {
322
+ file: matchedSection.file,
323
+ header: trimmed,
324
+ };
325
+ currentContent = [];
326
+ } else {
327
+ currentContent.push(line);
328
+ }
329
+ }
330
+
331
+ // Save last section
332
+ if (currentSection) {
333
+ // Include header in content for proper diffing
334
+ const fullContent = currentSection.header + '\n\n' + currentContent.join('\n').trim();
335
+ result.push({
336
+ file: currentSection.file,
337
+ header: currentSection.header,
338
+ content: fullContent.trim(),
339
+ matched: true,
340
+ });
341
+ }
342
+
343
+ return result;
344
+ }
345
+
346
+ /**
347
+ * Parse annotated paper.md and split back to section files
348
+ */
349
+ export function splitAnnotatedPaper(
350
+ paperContent: string,
351
+ sections: Record<string, SectionConfig>
352
+ ): Map<string, string> {
353
+ const result = new Map<string, string>();
354
+
355
+ // Look for section markers: <!-- @section:filename.md -->
356
+ const markerPattern = /<!--\s*@section:(\S+\.md)\s*-->/g;
357
+ const markers = [...paperContent.matchAll(markerPattern)];
358
+
359
+ if (markers.length > 0) {
360
+ // Use markers
361
+ for (let i = 0; i < markers.length; i++) {
362
+ const marker = markers[i];
363
+ if (!marker || !marker[1]) continue;
364
+ const file = marker[1];
365
+ const start = (marker.index || 0) + marker[0].length;
366
+ const end = markers[i + 1]?.index || paperContent.length;
367
+
368
+ let content = paperContent.slice(start, end).trim();
369
+
370
+ // Remove trailing marker if present
371
+ content = content.replace(/<!--\s*@section:\S+\.md\s*-->$/, '').trim();
372
+
373
+ result.set(file, content);
374
+ }
375
+ } else {
376
+ // Fall back to header detection
377
+ const lines = paperContent.split('\n');
378
+ let currentFile: string | null = null;
379
+ let currentContent: string[] = [];
380
+
381
+ for (const line of lines) {
382
+ const headerMatch = line.match(/^#{1,6}\s+(.+)$/);
383
+
384
+ if (headerMatch && headerMatch[1]) {
385
+ // Save previous section
386
+ if (currentFile) {
387
+ result.set(currentFile, currentContent.join('\n').trim());
388
+ }
389
+
390
+ // Find matching section file
391
+ const heading = headerMatch[1].trim();
392
+ const match = matchHeading(heading, sections);
393
+
394
+ if (match) {
395
+ currentFile = match.file;
396
+ currentContent = [line];
397
+ } else {
398
+ // Unknown section - keep accumulating to previous
399
+ currentContent.push(line);
400
+ }
401
+ } else {
402
+ currentContent.push(line);
403
+ }
404
+ }
405
+
406
+ // Save last section
407
+ if (currentFile) {
408
+ result.set(currentFile, currentContent.join('\n').trim());
409
+ }
410
+ }
411
+
412
+ return result;
413
+ }
414
+
415
+ /**
416
+ * Get ordered list of section files from config
417
+ */
418
+ export function getOrderedSections(config: SectionsConfig): string[] {
419
+ const entries = Object.entries(config.sections || {});
420
+
421
+ return entries
422
+ .sort((a, b) => {
423
+ const orderA = a[1].order ?? 999;
424
+ const orderB = b[1].order ?? 999;
425
+ return orderA - orderB;
426
+ })
427
+ .map(([file]) => file);
428
+ }