docrev 0.8.1 → 0.8.5

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 (306) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/PLAN-tables-and-postprocess.md +850 -0
  3. package/README.md +33 -0
  4. package/bin/rev.js +12 -131
  5. package/bin/rev.ts +145 -0
  6. package/dist/bin/rev.d.ts +9 -0
  7. package/dist/bin/rev.d.ts.map +1 -0
  8. package/dist/bin/rev.js +118 -0
  9. package/dist/bin/rev.js.map +1 -0
  10. package/dist/lib/annotations.d.ts +91 -0
  11. package/dist/lib/annotations.d.ts.map +1 -0
  12. package/dist/lib/annotations.js +554 -0
  13. package/dist/lib/annotations.js.map +1 -0
  14. package/dist/lib/build.d.ts +171 -0
  15. package/dist/lib/build.d.ts.map +1 -0
  16. package/dist/lib/build.js +755 -0
  17. package/dist/lib/build.js.map +1 -0
  18. package/dist/lib/citations.d.ts +34 -0
  19. package/dist/lib/citations.d.ts.map +1 -0
  20. package/dist/lib/citations.js +140 -0
  21. package/dist/lib/citations.js.map +1 -0
  22. package/dist/lib/commands/build.d.ts +13 -0
  23. package/dist/lib/commands/build.d.ts.map +1 -0
  24. package/dist/lib/commands/build.js +678 -0
  25. package/dist/lib/commands/build.js.map +1 -0
  26. package/dist/lib/commands/citations.d.ts +11 -0
  27. package/dist/lib/commands/citations.d.ts.map +1 -0
  28. package/dist/lib/commands/citations.js +428 -0
  29. package/dist/lib/commands/citations.js.map +1 -0
  30. package/dist/lib/commands/comments.d.ts +11 -0
  31. package/dist/lib/commands/comments.d.ts.map +1 -0
  32. package/dist/lib/commands/comments.js +883 -0
  33. package/dist/lib/commands/comments.js.map +1 -0
  34. package/dist/lib/commands/context.d.ts +35 -0
  35. package/dist/lib/commands/context.d.ts.map +1 -0
  36. package/dist/lib/commands/context.js +59 -0
  37. package/dist/lib/commands/context.js.map +1 -0
  38. package/dist/lib/commands/core.d.ts +11 -0
  39. package/dist/lib/commands/core.d.ts.map +1 -0
  40. package/dist/lib/commands/core.js +246 -0
  41. package/dist/lib/commands/core.js.map +1 -0
  42. package/dist/lib/commands/doi.d.ts +11 -0
  43. package/dist/lib/commands/doi.d.ts.map +1 -0
  44. package/dist/lib/commands/doi.js +373 -0
  45. package/dist/lib/commands/doi.js.map +1 -0
  46. package/dist/lib/commands/history.d.ts +11 -0
  47. package/dist/lib/commands/history.d.ts.map +1 -0
  48. package/dist/lib/commands/history.js +245 -0
  49. package/dist/lib/commands/history.js.map +1 -0
  50. package/dist/lib/commands/index.d.ts +28 -0
  51. package/dist/lib/commands/index.d.ts.map +1 -0
  52. package/dist/lib/commands/index.js +35 -0
  53. package/dist/lib/commands/index.js.map +1 -0
  54. package/dist/lib/commands/init.d.ts +11 -0
  55. package/dist/lib/commands/init.d.ts.map +1 -0
  56. package/dist/lib/commands/init.js +209 -0
  57. package/dist/lib/commands/init.js.map +1 -0
  58. package/dist/lib/commands/response.d.ts +11 -0
  59. package/dist/lib/commands/response.d.ts.map +1 -0
  60. package/dist/lib/commands/response.js +317 -0
  61. package/dist/lib/commands/response.js.map +1 -0
  62. package/dist/lib/commands/sections.d.ts +11 -0
  63. package/dist/lib/commands/sections.d.ts.map +1 -0
  64. package/dist/lib/commands/sections.js +1071 -0
  65. package/dist/lib/commands/sections.js.map +1 -0
  66. package/dist/lib/commands/utilities.d.ts +19 -0
  67. package/dist/lib/commands/utilities.d.ts.map +1 -0
  68. package/dist/lib/commands/utilities.js +2009 -0
  69. package/dist/lib/commands/utilities.js.map +1 -0
  70. package/dist/lib/comment-realign.d.ts +50 -0
  71. package/dist/lib/comment-realign.d.ts.map +1 -0
  72. package/dist/lib/comment-realign.js +372 -0
  73. package/dist/lib/comment-realign.js.map +1 -0
  74. package/dist/lib/config.d.ts +41 -0
  75. package/dist/lib/config.d.ts.map +1 -0
  76. package/dist/lib/config.js +76 -0
  77. package/dist/lib/config.js.map +1 -0
  78. package/dist/lib/crossref.d.ts +108 -0
  79. package/dist/lib/crossref.d.ts.map +1 -0
  80. package/dist/lib/crossref.js +597 -0
  81. package/dist/lib/crossref.js.map +1 -0
  82. package/dist/lib/dependencies.d.ts +30 -0
  83. package/dist/lib/dependencies.d.ts.map +1 -0
  84. package/dist/lib/dependencies.js +95 -0
  85. package/dist/lib/dependencies.js.map +1 -0
  86. package/dist/lib/doi-cache.d.ts +29 -0
  87. package/dist/lib/doi-cache.d.ts.map +1 -0
  88. package/dist/lib/doi-cache.js +104 -0
  89. package/dist/lib/doi-cache.js.map +1 -0
  90. package/dist/lib/doi.d.ts +65 -0
  91. package/dist/lib/doi.d.ts.map +1 -0
  92. package/dist/lib/doi.js +710 -0
  93. package/dist/lib/doi.js.map +1 -0
  94. package/dist/lib/equations.d.ts +61 -0
  95. package/dist/lib/equations.d.ts.map +1 -0
  96. package/dist/lib/equations.js +445 -0
  97. package/dist/lib/equations.js.map +1 -0
  98. package/dist/lib/errors.d.ts +60 -0
  99. package/dist/lib/errors.d.ts.map +1 -0
  100. package/dist/lib/errors.js +303 -0
  101. package/dist/lib/errors.js.map +1 -0
  102. package/dist/lib/format.d.ts +104 -0
  103. package/dist/lib/format.d.ts.map +1 -0
  104. package/dist/lib/format.js +416 -0
  105. package/dist/lib/format.js.map +1 -0
  106. package/dist/lib/git.d.ts +88 -0
  107. package/dist/lib/git.d.ts.map +1 -0
  108. package/dist/lib/git.js +304 -0
  109. package/dist/lib/git.js.map +1 -0
  110. package/dist/lib/grammar.d.ts +62 -0
  111. package/dist/lib/grammar.d.ts.map +1 -0
  112. package/dist/lib/grammar.js +244 -0
  113. package/dist/lib/grammar.js.map +1 -0
  114. package/dist/lib/image-registry.d.ts +68 -0
  115. package/dist/lib/image-registry.d.ts.map +1 -0
  116. package/dist/lib/image-registry.js +112 -0
  117. package/dist/lib/image-registry.js.map +1 -0
  118. package/dist/lib/import.d.ts +184 -0
  119. package/dist/lib/import.d.ts.map +1 -0
  120. package/dist/lib/import.js +1581 -0
  121. package/dist/lib/import.js.map +1 -0
  122. package/dist/lib/journals.d.ts +55 -0
  123. package/dist/lib/journals.d.ts.map +1 -0
  124. package/dist/lib/journals.js +417 -0
  125. package/dist/lib/journals.js.map +1 -0
  126. package/dist/lib/merge.d.ts +138 -0
  127. package/dist/lib/merge.d.ts.map +1 -0
  128. package/dist/lib/merge.js +603 -0
  129. package/dist/lib/merge.js.map +1 -0
  130. package/dist/lib/orcid.d.ts +36 -0
  131. package/dist/lib/orcid.d.ts.map +1 -0
  132. package/dist/lib/orcid.js +117 -0
  133. package/dist/lib/orcid.js.map +1 -0
  134. package/dist/lib/pdf-comments.d.ts +95 -0
  135. package/dist/lib/pdf-comments.d.ts.map +1 -0
  136. package/dist/lib/pdf-comments.js +192 -0
  137. package/dist/lib/pdf-comments.js.map +1 -0
  138. package/dist/lib/pdf-import.d.ts +118 -0
  139. package/dist/lib/pdf-import.d.ts.map +1 -0
  140. package/dist/lib/pdf-import.js +397 -0
  141. package/dist/lib/pdf-import.js.map +1 -0
  142. package/dist/lib/plugins.d.ts +76 -0
  143. package/dist/lib/plugins.d.ts.map +1 -0
  144. package/dist/lib/plugins.js +235 -0
  145. package/dist/lib/plugins.js.map +1 -0
  146. package/dist/lib/postprocess.d.ts +42 -0
  147. package/dist/lib/postprocess.d.ts.map +1 -0
  148. package/dist/lib/postprocess.js +138 -0
  149. package/dist/lib/postprocess.js.map +1 -0
  150. package/dist/lib/pptx-template.d.ts +59 -0
  151. package/dist/lib/pptx-template.d.ts.map +1 -0
  152. package/dist/lib/pptx-template.js +613 -0
  153. package/dist/lib/pptx-template.js.map +1 -0
  154. package/dist/lib/pptx-themes.d.ts +80 -0
  155. package/dist/lib/pptx-themes.d.ts.map +1 -0
  156. package/dist/lib/pptx-themes.js +818 -0
  157. package/dist/lib/pptx-themes.js.map +1 -0
  158. package/dist/lib/protect-restore.d.ts +137 -0
  159. package/dist/lib/protect-restore.d.ts.map +1 -0
  160. package/dist/lib/protect-restore.js +394 -0
  161. package/dist/lib/protect-restore.js.map +1 -0
  162. package/dist/lib/rate-limiter.d.ts +27 -0
  163. package/dist/lib/rate-limiter.d.ts.map +1 -0
  164. package/dist/lib/rate-limiter.js +79 -0
  165. package/dist/lib/rate-limiter.js.map +1 -0
  166. package/dist/lib/response.d.ts +41 -0
  167. package/dist/lib/response.d.ts.map +1 -0
  168. package/dist/lib/response.js +150 -0
  169. package/dist/lib/response.js.map +1 -0
  170. package/dist/lib/review.d.ts +35 -0
  171. package/dist/lib/review.d.ts.map +1 -0
  172. package/dist/lib/review.js +263 -0
  173. package/dist/lib/review.js.map +1 -0
  174. package/dist/lib/schema.d.ts +66 -0
  175. package/dist/lib/schema.d.ts.map +1 -0
  176. package/dist/lib/schema.js +339 -0
  177. package/dist/lib/schema.js.map +1 -0
  178. package/dist/lib/scientific-words.d.ts +6 -0
  179. package/dist/lib/scientific-words.d.ts.map +1 -0
  180. package/dist/lib/scientific-words.js +66 -0
  181. package/dist/lib/scientific-words.js.map +1 -0
  182. package/dist/lib/sections.d.ts +40 -0
  183. package/dist/lib/sections.d.ts.map +1 -0
  184. package/dist/lib/sections.js +288 -0
  185. package/dist/lib/sections.js.map +1 -0
  186. package/dist/lib/slides.d.ts +86 -0
  187. package/dist/lib/slides.d.ts.map +1 -0
  188. package/dist/lib/slides.js +676 -0
  189. package/dist/lib/slides.js.map +1 -0
  190. package/dist/lib/spelling.d.ts +76 -0
  191. package/dist/lib/spelling.d.ts.map +1 -0
  192. package/dist/lib/spelling.js +272 -0
  193. package/dist/lib/spelling.js.map +1 -0
  194. package/dist/lib/templates.d.ts +30 -0
  195. package/dist/lib/templates.d.ts.map +1 -0
  196. package/dist/lib/templates.js +504 -0
  197. package/dist/lib/templates.js.map +1 -0
  198. package/dist/lib/themes.d.ts +85 -0
  199. package/dist/lib/themes.d.ts.map +1 -0
  200. package/dist/lib/themes.js +652 -0
  201. package/dist/lib/themes.js.map +1 -0
  202. package/dist/lib/trackchanges.d.ts +51 -0
  203. package/dist/lib/trackchanges.d.ts.map +1 -0
  204. package/dist/lib/trackchanges.js +202 -0
  205. package/dist/lib/trackchanges.js.map +1 -0
  206. package/dist/lib/tui.d.ts +76 -0
  207. package/dist/lib/tui.d.ts.map +1 -0
  208. package/dist/lib/tui.js +377 -0
  209. package/dist/lib/tui.js.map +1 -0
  210. package/dist/lib/types.d.ts +447 -0
  211. package/dist/lib/types.d.ts.map +1 -0
  212. package/dist/lib/types.js +6 -0
  213. package/dist/lib/types.js.map +1 -0
  214. package/dist/lib/undo.d.ts +57 -0
  215. package/dist/lib/undo.d.ts.map +1 -0
  216. package/dist/lib/undo.js +185 -0
  217. package/dist/lib/undo.js.map +1 -0
  218. package/dist/lib/utils.d.ts +16 -0
  219. package/dist/lib/utils.d.ts.map +1 -0
  220. package/dist/lib/utils.js +40 -0
  221. package/dist/lib/utils.js.map +1 -0
  222. package/dist/lib/variables.d.ts +42 -0
  223. package/dist/lib/variables.d.ts.map +1 -0
  224. package/dist/lib/variables.js +141 -0
  225. package/dist/lib/variables.js.map +1 -0
  226. package/dist/lib/word.d.ts +80 -0
  227. package/dist/lib/word.d.ts.map +1 -0
  228. package/dist/lib/word.js +360 -0
  229. package/dist/lib/word.js.map +1 -0
  230. package/dist/lib/wordcomments.d.ts +51 -0
  231. package/dist/lib/wordcomments.d.ts.map +1 -0
  232. package/dist/lib/wordcomments.js +587 -0
  233. package/dist/lib/wordcomments.js.map +1 -0
  234. package/eslint.config.js +27 -0
  235. package/lib/annotations.ts +622 -0
  236. package/lib/apply-buildup-colors.py +88 -0
  237. package/lib/build.ts +1013 -0
  238. package/lib/{citations.js → citations.ts} +38 -27
  239. package/lib/commands/{build.js → build.ts} +80 -27
  240. package/lib/commands/{citations.js → citations.ts} +36 -18
  241. package/lib/commands/{comments.js → comments.ts} +187 -54
  242. package/lib/commands/{context.js → context.ts} +18 -8
  243. package/lib/commands/{core.js → core.ts} +34 -20
  244. package/lib/commands/{doi.js → doi.ts} +32 -16
  245. package/lib/commands/{history.js → history.ts} +25 -12
  246. package/lib/commands/{index.js → index.ts} +9 -5
  247. package/lib/commands/{init.js → init.ts} +20 -8
  248. package/lib/commands/{response.js → response.ts} +47 -20
  249. package/lib/commands/{sections.js → sections.ts} +273 -68
  250. package/lib/commands/{utilities.js → utilities.ts} +338 -158
  251. package/lib/{comment-realign.js → comment-realign.ts} +117 -45
  252. package/lib/config.ts +84 -0
  253. package/lib/{crossref.js → crossref.ts} +213 -138
  254. package/lib/dependencies.ts +106 -0
  255. package/lib/doi-cache.ts +115 -0
  256. package/lib/{doi.js → doi.ts} +115 -281
  257. package/lib/{equations.js → equations.ts} +60 -64
  258. package/lib/{errors.js → errors.ts} +56 -48
  259. package/lib/{format.js → format.ts} +137 -63
  260. package/lib/{git.js → git.ts} +66 -63
  261. package/lib/{grammar.js → grammar.ts} +45 -32
  262. package/lib/image-registry.ts +180 -0
  263. package/lib/import.ts +2060 -0
  264. package/lib/journals.ts +505 -0
  265. package/lib/{merge.js → merge.ts} +185 -135
  266. package/lib/{orcid.js → orcid.ts} +17 -22
  267. package/lib/{pdf-comments.js → pdf-comments.ts} +76 -18
  268. package/lib/{pdf-import.js → pdf-import.ts} +148 -70
  269. package/lib/{plugins.js → plugins.ts} +82 -39
  270. package/lib/postprocess.ts +188 -0
  271. package/lib/pptx-color-filter.lua +37 -0
  272. package/lib/pptx-template.ts +625 -0
  273. package/lib/pptx-themes/academic.pptx +0 -0
  274. package/lib/pptx-themes/corporate.pptx +0 -0
  275. package/lib/pptx-themes/dark.pptx +0 -0
  276. package/lib/pptx-themes/default.pptx +0 -0
  277. package/lib/pptx-themes/minimal.pptx +0 -0
  278. package/lib/pptx-themes/plant.pptx +0 -0
  279. package/lib/pptx-themes.ts +896 -0
  280. package/lib/protect-restore.ts +516 -0
  281. package/lib/rate-limiter.ts +94 -0
  282. package/lib/{response.js → response.ts} +36 -21
  283. package/lib/{review.js → review.ts} +53 -43
  284. package/lib/{schema.js → schema.ts} +70 -25
  285. package/lib/{sections.js → sections.ts} +71 -76
  286. package/lib/slides.ts +793 -0
  287. package/lib/{spelling.js → spelling.ts} +43 -59
  288. package/lib/{templates.js → templates.ts} +20 -17
  289. package/lib/themes.ts +742 -0
  290. package/lib/{trackchanges.js → trackchanges.ts} +52 -23
  291. package/lib/types.ts +509 -0
  292. package/lib/{undo.js → undo.ts} +75 -52
  293. package/lib/utils.ts +41 -0
  294. package/lib/{variables.js → variables.ts} +60 -54
  295. package/lib/word.ts +428 -0
  296. package/lib/{wordcomments.js → wordcomments.ts} +94 -40
  297. package/package.json +15 -5
  298. package/skill/REFERENCE.md +67 -0
  299. package/tsconfig.json +26 -0
  300. package/lib/annotations.js +0 -414
  301. package/lib/build.js +0 -639
  302. package/lib/config.js +0 -79
  303. package/lib/import.js +0 -1145
  304. package/lib/journals.js +0 -629
  305. package/lib/word.js +0 -225
  306. /package/lib/{scientific-words.js → scientific-words.ts} +0 -0
@@ -0,0 +1,27 @@
1
+ export default [
2
+ {
3
+ languageOptions: {
4
+ ecmaVersion: 2022,
5
+ sourceType: 'module',
6
+ globals: {
7
+ console: 'readonly',
8
+ process: 'readonly',
9
+ Buffer: 'readonly',
10
+ URL: 'readonly',
11
+ setTimeout: 'readonly',
12
+ clearTimeout: 'readonly',
13
+ setInterval: 'readonly',
14
+ clearInterval: 'readonly',
15
+ },
16
+ },
17
+ rules: {
18
+ 'no-unused-vars': ['warn', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
19
+ 'no-constant-condition': 'warn',
20
+ 'no-empty': ['warn', { allowEmptyCatch: true }],
21
+ 'prefer-const': 'warn',
22
+ 'no-var': 'error',
23
+ 'eqeqeq': ['warn', 'smart'],
24
+ },
25
+ ignores: ['node_modules/**', 'test/**'],
26
+ },
27
+ ];
@@ -0,0 +1,622 @@
1
+ /**
2
+ * CriticMarkup annotation parsing and manipulation
3
+ *
4
+ * Syntax:
5
+ * {++inserted text++} - Insertions
6
+ * {--deleted text--} - Deletions
7
+ * {~~old~>new~~} - Substitutions
8
+ * {>>Author: comment<<} - Comments
9
+ * {==text==} - Highlights
10
+ */
11
+
12
+ import type { Annotation, AnnotationCounts, StripOptions, CommentFilterOptions } from './types.js';
13
+
14
+ // =============================================================================
15
+ // Constants
16
+ // =============================================================================
17
+
18
+ /** Window size for context lookup (characters before/after position) */
19
+ const CONTEXT_WINDOW_SIZE = 2000;
20
+
21
+ /** Characters of context to include in annotation results */
22
+ const CONTEXT_SNIPPET_SIZE = 50;
23
+
24
+ /** Maximum iterations for nested annotation stripping (safety limit) */
25
+ const MAX_STRIP_ITERATIONS = 20;
26
+
27
+ /** Maximum author name length in comments */
28
+ const MAX_AUTHOR_LENGTH = 30;
29
+
30
+ /** Maximum content length before heuristic assumes it's not a comment */
31
+ const MAX_COMMENT_CONTENT_LENGTH = 200;
32
+
33
+ // =============================================================================
34
+ // Patterns
35
+ // =============================================================================
36
+
37
+ // Patterns for each annotation type
38
+ const PATTERNS = {
39
+ insert: /\{\+\+(.+?)\+\+\}/gs,
40
+ delete: /\{--(.+?)--\}/gs,
41
+ substitute: /\{~~(.+?)~>(.+?)~~\}/gs,
42
+ comment: /\{>>(.+?)<<\}/gs,
43
+ highlight: /\{==(.+?)==\}/gs,
44
+ };
45
+
46
+ /**
47
+ * Check if a potential comment is actually a false positive
48
+ * (e.g., figure caption, nested inside other annotation, code block, etc.)
49
+ * @param commentContent - The content inside {>>...<<}
50
+ * @param fullText - The full document text
51
+ * @param position - Position of the comment in the text
52
+ * @returns true if this is a false positive (not a real comment)
53
+ */
54
+ function isCommentFalsePositive(commentContent: string, fullText: string, position: number): boolean {
55
+ // Check if inside a code block (fenced or indented)
56
+ const textBefore = fullText.slice(Math.max(0, position - CONTEXT_WINDOW_SIZE), position);
57
+ const textAfter = fullText.slice(position, Math.min(fullText.length, position + CONTEXT_WINDOW_SIZE));
58
+
59
+ // Count unclosed fenced code blocks (``` or ~~~)
60
+ const fenceOpens = (textBefore.match(/^```|^~~~/gm) || []).length;
61
+ const fenceCloses = (textBefore.match(/```$|~~~$/gm) || []).length;
62
+ if (fenceOpens > fenceCloses) return true; // Inside code block
63
+
64
+ // Check if on an indented line (4+ spaces or tab at line start = code)
65
+ const lineStart = textBefore.lastIndexOf('\n') + 1;
66
+ const linePrefix = fullText.slice(lineStart, position);
67
+ if (/^(\t| )/.test(linePrefix)) return true; // Indented code
68
+
69
+ // Check if inside inline code backticks
70
+ const backticksBefore = (linePrefix.match(/`/g) || []).length;
71
+ if (backticksBefore % 2 === 1) return true; // Inside inline code
72
+
73
+ // Check if nested inside a deletion or insertion block
74
+ const nearTextBefore = fullText.slice(Math.max(0, position - 500), position);
75
+
76
+ // Count unclosed deletion markers
77
+ const delOpens = (nearTextBefore.match(/\{--/g) || []).length;
78
+ const delCloses = (nearTextBefore.match(/--\}/g) || []).length;
79
+ if (delOpens > delCloses) return true; // Nested inside deletion
80
+
81
+ // Count unclosed insertion markers
82
+ const insOpens = (nearTextBefore.match(/\{\+\+/g) || []).length;
83
+ const insCloses = (nearTextBefore.match(/\+\+\}/g) || []).length;
84
+ if (insOpens > insCloses) return true; // Nested inside insertion
85
+
86
+ // Heuristics for figure captions and other false positives:
87
+
88
+ // Contains image/figure path patterns
89
+ if (/\(figures?\/|\(images?\/|\.png|\.jpg|\.jpeg|\.gif|\.svg|\.pdf/i.test(commentContent)) return true;
90
+
91
+ // Contains markdown figure reference syntax
92
+ if (/\{#fig:|!\[/.test(commentContent)) return true;
93
+
94
+ // Contains URL patterns (likely a link, not a comment)
95
+ if (/https?:\/\/|www\./i.test(commentContent) && commentContent.length < 150) return true;
96
+
97
+ // Looks like code (contains programming patterns)
98
+ if (/function\s*\(|=>|import\s+|export\s+|const\s+|let\s+|var\s+/.test(commentContent)) return true;
99
+
100
+ // 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
+ if (!hasAuthorPrefix && !hasResolvedMark && commentContent.length > MAX_COMMENT_CONTENT_LENGTH) return true;
105
+
106
+ // Looks like a figure caption (starts with "Fig" or contains typical caption words)
107
+ if (/^(Fig\.?|Figure|Table|Sankey|Diagram|Proportion|Distribution|Map|Chart|Graph|Plot|Panel)/i.test(commentContent.trim())) {
108
+ return true;
109
+ }
110
+
111
+ // Contains LaTeX-like patterns (likely equation, not comment)
112
+ if (/\\[a-z]+\{|\\frac|\\sum|\\int|\\begin\{/.test(commentContent)) return true;
113
+
114
+ // Looks like BibTeX entry (not a comment)
115
+ if (/@article\{|@book\{|@inproceedings\{/i.test(commentContent)) return true;
116
+
117
+ return false;
118
+ }
119
+
120
+ // Combined pattern for any track change (not comments)
121
+ const TRACK_CHANGE_PATTERN = /(\{\+\+.+?\+\+\}|\{--.+?--\}|\{~~.+?~>.+?~~\})/gs;
122
+
123
+ // =============================================================================
124
+ // Public API
125
+ // =============================================================================
126
+
127
+ /**
128
+ * Parse all annotations from text
129
+ * @param text - Markdown text containing CriticMarkup annotations
130
+ * @returns Array of parsed annotations sorted by position
131
+ * @throws TypeError If text is not a string
132
+ */
133
+ export function parseAnnotations(text: string): Annotation[] {
134
+ if (typeof text !== 'string') {
135
+ throw new TypeError(`text must be a string, got ${typeof text}`);
136
+ }
137
+
138
+ const annotations: Annotation[] = [];
139
+
140
+ // Build line number lookup
141
+ const lines = text.split('\n');
142
+ let pos = 0;
143
+ const lineStarts = lines.map((line) => {
144
+ const start = pos;
145
+ pos += line.length + 1;
146
+ return start;
147
+ });
148
+
149
+ function getLine(position: number): number {
150
+ for (let i = 0; i < lineStarts.length; i++) {
151
+ const start = lineStarts[i];
152
+ if (start !== undefined && start > position) return i;
153
+ }
154
+ return lineStarts.length;
155
+ }
156
+
157
+ function getContext(position: number, length: number): { before: string; after: string } {
158
+ const start = Math.max(0, position - CONTEXT_SNIPPET_SIZE);
159
+ const end = Math.min(text.length, position + length + CONTEXT_SNIPPET_SIZE);
160
+ const before = text.slice(start, position).split('\n').pop() || '';
161
+ const after = text.slice(position + length, end).split('\n')[0] || '';
162
+ return { before, after };
163
+ }
164
+
165
+ // Parse insertions
166
+ for (const match of text.matchAll(PATTERNS.insert)) {
167
+ if (match.index === undefined) continue;
168
+ const ctx = getContext(match.index, match[0].length);
169
+ annotations.push({
170
+ type: 'insert',
171
+ match: match[0],
172
+ content: match[1] || '',
173
+ position: match.index,
174
+ line: getLine(match.index),
175
+ ...ctx,
176
+ });
177
+ }
178
+
179
+ // Parse deletions
180
+ for (const match of text.matchAll(PATTERNS.delete)) {
181
+ if (match.index === undefined) continue;
182
+ const ctx = getContext(match.index, match[0].length);
183
+ annotations.push({
184
+ type: 'delete',
185
+ match: match[0],
186
+ content: match[1] || '',
187
+ position: match.index,
188
+ line: getLine(match.index),
189
+ ...ctx,
190
+ });
191
+ }
192
+
193
+ // Parse substitutions
194
+ for (const match of text.matchAll(PATTERNS.substitute)) {
195
+ if (match.index === undefined) continue;
196
+ const ctx = getContext(match.index, match[0].length);
197
+ annotations.push({
198
+ type: 'substitute',
199
+ match: match[0],
200
+ content: match[1] || '',
201
+ replacement: match[2] || '',
202
+ position: match.index,
203
+ line: getLine(match.index),
204
+ ...ctx,
205
+ });
206
+ }
207
+
208
+ // Parse comments (with false positive filtering)
209
+ for (const match of text.matchAll(PATTERNS.comment)) {
210
+ if (match.index === undefined) continue;
211
+ // Skip false positives (figure captions, nested annotations, etc.)
212
+ const commentContent = match[1] || '';
213
+ if (isCommentFalsePositive(commentContent, text, match.index)) {
214
+ continue;
215
+ }
216
+
217
+ const ctx = getContext(match.index, match[0].length);
218
+ let commentText = commentContent;
219
+ let author = '';
220
+
221
+ // Extract author if present (format: "Author: comment")
222
+ const colonIdx = commentText.indexOf(':');
223
+ if (colonIdx > 0 && colonIdx < MAX_AUTHOR_LENGTH) {
224
+ author = commentText.slice(0, colonIdx).trim();
225
+ commentText = commentText.slice(colonIdx + 1).trim();
226
+ }
227
+
228
+ annotations.push({
229
+ type: 'comment',
230
+ match: match[0],
231
+ content: commentText,
232
+ author,
233
+ position: match.index,
234
+ line: getLine(match.index),
235
+ ...ctx,
236
+ });
237
+ }
238
+
239
+ // Sort by position
240
+ annotations.sort((a, b) => a.position - b.position);
241
+ return annotations;
242
+ }
243
+
244
+ /**
245
+ * Strip annotations from text, applying changes
246
+ * Handles nested annotations by iterating until stable
247
+ * @param text - Markdown text with CriticMarkup annotations
248
+ * @param options - Strip options
249
+ * @returns Clean text with annotations applied/removed
250
+ * @throws TypeError If text is not a string
251
+ */
252
+ export function stripAnnotations(text: string, options: StripOptions = {}): string {
253
+ if (typeof text !== 'string') {
254
+ throw new TypeError(`text must be a string, got ${typeof text}`);
255
+ }
256
+
257
+ const { keepComments = false } = options;
258
+
259
+ // Iterate until no more changes (handles nested annotations)
260
+ let prev: string;
261
+ let iterations = 0;
262
+
263
+ do {
264
+ prev = text;
265
+
266
+ // Apply substitutions: {~~old~>new~~} → new
267
+ text = text.replace(PATTERNS.substitute, '$2');
268
+
269
+ // Apply insertions: {++text++} → text
270
+ text = text.replace(PATTERNS.insert, '$1');
271
+
272
+ // Apply deletions: {--text--} → nothing
273
+ // Don't touch surrounding whitespace - just remove the annotation
274
+ text = text.replace(PATTERNS.delete, '');
275
+
276
+ // Remove highlights: {==text==} → text
277
+ text = text.replace(PATTERNS.highlight, '$1');
278
+
279
+ // Remove comments unless keeping
280
+ if (!keepComments) {
281
+ text = text.replace(PATTERNS.comment, '');
282
+ }
283
+
284
+ // Clean up partial/orphaned markers within the loop
285
+ // This handles cases where nested annotations leave behind fragments
286
+
287
+ // Empty annotations (from nested stripping)
288
+ text = text.replace(/\{----\}/g, '');
289
+ text = text.replace(/\{\+\+\+\+\}/g, '');
290
+ text = text.replace(/\{--\s*--\}/g, '');
291
+ text = text.replace(/\{\+\+\s*\+\+\}/g, '');
292
+
293
+ // Orphaned substitution fragments: ~>text~~} or {~~text (no proper pairs)
294
+ text = text.replace(/~>[^{]*?~~\}/g, '');
295
+ text = text.replace(/\{~~[^~}]*$/gm, '');
296
+
297
+ // Handle malformed substitution from nested: {~~{~~old → just strip the {~~
298
+ text = text.replace(/\{~~\{~~/g, '{~~');
299
+ text = text.replace(/~~\}~~\}/g, '~~}');
300
+
301
+ iterations++;
302
+ } while (text !== prev && iterations < MAX_STRIP_ITERATIONS);
303
+
304
+ // Final cleanup of any remaining orphaned markers
305
+ // Orphaned closing markers
306
+ text = text.replace(/--\}(?:--\})+/g, '');
307
+ text = text.replace(/\+\+\}(?:\+\+\})+/g, '');
308
+ text = text.replace(/~~\}(?:~~\})+/g, '');
309
+ text = text.replace(/--\}/g, '');
310
+ text = text.replace(/\+\+\}/g, '');
311
+ text = text.replace(/~~\}/g, '');
312
+
313
+ // Orphaned opening markers
314
+ text = text.replace(/\{--(?:\{--)+/g, '');
315
+ text = text.replace(/\{\+\+(?:\{\+\+)+/g, '');
316
+ text = text.replace(/\{~~(?:\{~~)+/g, '');
317
+ text = text.replace(/\{--/g, '');
318
+ text = text.replace(/\{\+\+/g, '');
319
+ text = text.replace(/\{~~/g, '');
320
+ text = text.replace(/~>/g, '');
321
+
322
+ return text;
323
+ }
324
+
325
+ /**
326
+ * Collapse multiple spaces to single space, preserving table formatting
327
+ * Useful for cleaning up messy Word imports
328
+ * @param text - Text to normalize
329
+ * @returns Text with multiple spaces collapsed to single spaces
330
+ * @throws TypeError If text is not a string
331
+ */
332
+ export function stripToSingleSpace(text: string): string {
333
+ if (typeof text !== 'string') {
334
+ throw new TypeError(`text must be a string, got ${typeof text}`);
335
+ }
336
+
337
+ const lines = text.split('\n');
338
+ let inTable = false;
339
+
340
+ // Helper to check if a line looks like table content
341
+ const looksLikeTableRow = (ln: string): boolean => {
342
+ const trimmed = ln.trim();
343
+ if (!trimmed) return false;
344
+ // Has multiple consecutive spaces (column spacing)
345
+ // OR italicized category header with trailing spaces
346
+ return /\S\s{2,}\S/.test(trimmed) || (/^\*[^*]+\*\s*$/.test(trimmed) && /\s{2,}$/.test(ln));
347
+ };
348
+
349
+ for (let i = 0; i < lines.length; i++) {
350
+ const line = lines[i];
351
+ if (!line) continue;
352
+
353
+ // Detect table separator line
354
+ const isTableSeparator = /^\|?[\s-]*[-]{3,}[\s|:-]+[-]{3,}/.test(line) ||
355
+ /^[-]{3,}\s{2,}[-]{3,}/.test(line);
356
+
357
+ if (isTableSeparator) {
358
+ inTable = true;
359
+ continue;
360
+ }
361
+
362
+ // Check if we're exiting the table
363
+ if (inTable && line.trim() === '') {
364
+ let nextContentLine = '';
365
+ for (let j = i + 1; j < lines.length && j < i + 5; j++) {
366
+ const nextLine = lines[j];
367
+ if (nextLine && nextLine.trim() !== '') {
368
+ nextContentLine = nextLine;
369
+ break;
370
+ }
371
+ }
372
+ if (!looksLikeTableRow(nextContentLine) && !/^[-]{3,}/.test(nextContentLine.trim())) {
373
+ inTable = false;
374
+ }
375
+ continue;
376
+ }
377
+
378
+ // Only collapse spaces outside tables
379
+ if (!inTable) {
380
+ lines[i] = line.replace(/ +/g, ' ');
381
+ }
382
+ }
383
+
384
+ return lines.join('\n');
385
+ }
386
+
387
+ /**
388
+ * Check if text contains any CriticMarkup annotations
389
+ * @param text - Text to check
390
+ * @returns True if text contains any annotations
391
+ * @throws TypeError If text is not a string
392
+ */
393
+ export function hasAnnotations(text: string): boolean {
394
+ if (typeof text !== 'string') {
395
+ throw new TypeError(`text must be a string, got ${typeof text}`);
396
+ }
397
+
398
+ return PATTERNS.insert.test(text) ||
399
+ PATTERNS.delete.test(text) ||
400
+ PATTERNS.substitute.test(text) ||
401
+ PATTERNS.comment.test(text) ||
402
+ PATTERNS.highlight.test(text);
403
+ }
404
+
405
+ /**
406
+ * Apply a decision to a single annotation (accept or reject)
407
+ * @param text - Document text containing the annotation
408
+ * @param annotation - Annotation object from parseAnnotations()
409
+ * @param accept - True to accept the change, false to reject
410
+ * @returns Updated text with the decision applied
411
+ * @throws TypeError If text is not a string or annotation is invalid
412
+ */
413
+ export function applyDecision(text: string, annotation: Annotation, accept: boolean): string {
414
+ if (typeof text !== 'string') {
415
+ throw new TypeError(`text must be a string, got ${typeof text}`);
416
+ }
417
+ if (!annotation || typeof annotation.type !== 'string' || typeof annotation.match !== 'string') {
418
+ throw new TypeError('annotation must have type and match properties');
419
+ }
420
+ let replacement: string;
421
+
422
+ // Extract any comments embedded in the annotation content
423
+ // These should be preserved when accepting deletions or rejecting insertions
424
+ const commentPattern = /\{>>[^<]*<<\}/g;
425
+ const embeddedComments = (annotation.match || '').match(commentPattern) || [];
426
+
427
+ switch (annotation.type) {
428
+ case 'insert':
429
+ if (accept) {
430
+ replacement = annotation.content;
431
+ } else {
432
+ // Rejecting insertion - preserve any comments that were inside
433
+ replacement = embeddedComments.length > 0 ? embeddedComments.join('') : '';
434
+ }
435
+ break;
436
+ case 'delete':
437
+ if (accept) {
438
+ // Accepting deletion - preserve any comments by placing them before
439
+ replacement = embeddedComments.length > 0 ? embeddedComments.join('') : '';
440
+ } else {
441
+ replacement = annotation.content;
442
+ }
443
+ break;
444
+ case 'substitute':
445
+ if (accept) {
446
+ // For substitutions, check if comments are in the old text being replaced
447
+ const oldTextComments = (annotation.content || '').match(commentPattern) || [];
448
+ replacement = annotation.replacement || '';
449
+ if (oldTextComments.length > 0) {
450
+ // Prepend comments that were in the old text
451
+ replacement = oldTextComments.join('') + replacement;
452
+ }
453
+ } else {
454
+ replacement = annotation.content;
455
+ }
456
+ break;
457
+ default:
458
+ return text;
459
+ }
460
+
461
+ return text.replace(annotation.match, replacement);
462
+ }
463
+
464
+ /**
465
+ * Get track changes only (no comments)
466
+ * @param text - Markdown text with CriticMarkup annotations
467
+ * @returns Array of insert/delete/substitute annotations
468
+ * @throws TypeError If text is not a string
469
+ */
470
+ export function getTrackChanges(text: string): Annotation[] {
471
+ // Input validation delegated to parseAnnotations
472
+ return parseAnnotations(text).filter((a) => a.type !== 'comment');
473
+ }
474
+
475
+ /**
476
+ * Get comments only
477
+ * @param text - Markdown text with CriticMarkup annotations
478
+ * @param options - Filter options
479
+ * @returns Array of comment annotations
480
+ * @throws TypeError If text is not a string
481
+ */
482
+ export function getComments(text: string, options: CommentFilterOptions = {}): Annotation[] {
483
+ // Input validation delegated to parseAnnotations
484
+ const { pendingOnly = false, resolvedOnly = false } = options;
485
+ let comments = parseAnnotations(text).filter((a) => a.type === 'comment');
486
+
487
+ // Check for resolved status marker at end of comment
488
+ comments = comments.map((c) => {
489
+ const resolved = c.content.endsWith('[RESOLVED]') || c.content.endsWith('[✓]');
490
+ return {
491
+ ...c,
492
+ resolved,
493
+ content: resolved
494
+ ? c.content.replace(/\s*\[(RESOLVED|✓)\]$/, '').trim()
495
+ : c.content,
496
+ };
497
+ });
498
+
499
+ if (pendingOnly) {
500
+ comments = comments.filter((c) => !c.resolved);
501
+ }
502
+ if (resolvedOnly) {
503
+ comments = comments.filter((c) => c.resolved);
504
+ }
505
+
506
+ return comments;
507
+ }
508
+
509
+ /**
510
+ * Mark a comment as resolved or pending
511
+ * @param text - Document text containing the comment
512
+ * @param comment - Comment annotation object from getComments()
513
+ * @param resolved - True to mark resolved, false to mark pending
514
+ * @returns Updated text with status marker applied
515
+ * @throws TypeError If text is not a string or comment is invalid
516
+ */
517
+ export function setCommentStatus(text: string, comment: Annotation, resolved: boolean): string {
518
+ if (typeof text !== 'string') {
519
+ throw new TypeError(`text must be a string, got ${typeof text}`);
520
+ }
521
+ if (!comment || typeof comment.match !== 'string') {
522
+ throw new TypeError('comment must have a match property');
523
+ }
524
+ // Find the comment in the text
525
+ const originalMatch = comment.match;
526
+
527
+ if (resolved) {
528
+ // Add [RESOLVED] marker before the closing <<
529
+ const newMatch = originalMatch.replace(/<<\}$/, ' [RESOLVED]<<}');
530
+ return text.replace(originalMatch, newMatch);
531
+ } else {
532
+ // Remove resolved markers
533
+ const newMatch = originalMatch.replace(/\s*\[(RESOLVED|✓)\]<<\}$/, '<<}');
534
+ return text.replace(originalMatch, newMatch);
535
+ }
536
+ }
537
+
538
+ /**
539
+ * Count annotations by type
540
+ * @param text - Markdown text with CriticMarkup annotations
541
+ * @returns Counts by annotation type
542
+ * @throws TypeError If text is not a string
543
+ */
544
+ export function countAnnotations(text: string): AnnotationCounts {
545
+ // Input validation delegated to parseAnnotations
546
+ const annotations = parseAnnotations(text);
547
+ const counts: AnnotationCounts = { inserts: 0, deletes: 0, substitutes: 0, comments: 0, total: 0 };
548
+
549
+ for (const a of annotations) {
550
+ counts.total++;
551
+ switch (a.type) {
552
+ case 'insert':
553
+ counts.inserts++;
554
+ break;
555
+ case 'delete':
556
+ counts.deletes++;
557
+ break;
558
+ case 'substitute':
559
+ counts.substitutes++;
560
+ break;
561
+ case 'comment':
562
+ counts.comments++;
563
+ break;
564
+ }
565
+ }
566
+
567
+ return counts;
568
+ }
569
+
570
+ /**
571
+ * Clean up orphaned/malformed CriticMarkup markers
572
+ * This can happen when track changes span across comment boundaries
573
+ * @param text - Document text with potentially malformed markers
574
+ * @returns Cleaned text with orphaned markers removed
575
+ * @throws TypeError If text is not a string
576
+ */
577
+ export function cleanupOrphanedMarkers(text: string): string {
578
+ if (typeof text !== 'string') {
579
+ throw new TypeError(`text must be a string, got ${typeof text}`);
580
+ }
581
+ let result = text;
582
+
583
+ // Remove orphaned insertion end markers (++} not preceded by {++)
584
+ // These occur when an insertion's start was inside something that got deleted/replaced
585
+ result = result.replace(/(?<!\{\+\+[^}]*)\+\+\}/g, '');
586
+
587
+ // Remove orphaned deletion end markers (--} not preceded by {--)
588
+ result = result.replace(/(?<!\{--[^}]*)--\}/g, '');
589
+
590
+ // Remove orphaned substitution end markers (~~} not preceded by {~~)
591
+ result = result.replace(/(?<!\{~~[^}]*)~~\}/g, '');
592
+
593
+ // Fix unclosed insertions: {++ without matching ++}
594
+ // Find {++ and check if there's a matching ++} before the next { marker
595
+ result = result.replace(/\{\+\+([^+]*?)(?=\{[+\-~>]|\{>>|$)/g, (match, content) => {
596
+ // If content has no ++}, it's unclosed - just keep the content
597
+ if (!content.includes('++}')) {
598
+ return content;
599
+ }
600
+ return match;
601
+ });
602
+
603
+ // Fix unclosed deletions: {-- without matching --}
604
+ result = result.replace(/\{--([^-]*?)(?=\{[+\-~>]|\{>>|$)/g, (match, content) => {
605
+ if (!content.includes('--}')) {
606
+ return content;
607
+ }
608
+ return match;
609
+ });
610
+
611
+ // Fix unclosed substitutions: {~~ without matching ~~}
612
+ // This is trickier because we need both ~> and ~~}
613
+ result = result.replace(/\{~~([^~]*?)~>([^~]*?)(?=\{[+\-~>]|\{>>|$)/g, (match, old, newText) => {
614
+ if (!match.includes('~~}')) {
615
+ // Unclosed substitution - keep the new text
616
+ return newText;
617
+ }
618
+ return match;
619
+ });
620
+
621
+ return result;
622
+ }