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