@open-press/cli 0.8.0 → 1.1.0

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 (250) hide show
  1. package/README.md +33 -23
  2. package/dist/cli.js +320 -252
  3. package/package.json +9 -8
  4. package/template/core/AGENTS.md +0 -126
  5. package/template/core/CHANGELOG.md +0 -215
  6. package/template/core/README.md +0 -40
  7. package/template/core/engine/cli.mjs +0 -96
  8. package/template/core/engine/commands/_shared.mjs +0 -177
  9. package/template/core/engine/commands/deploy.mjs +0 -31
  10. package/template/core/engine/commands/dev.mjs +0 -49
  11. package/template/core/engine/commands/doctor.mjs +0 -229
  12. package/template/core/engine/commands/export.mjs +0 -8
  13. package/template/core/engine/commands/init.mjs +0 -24
  14. package/template/core/engine/commands/inspect.mjs +0 -35
  15. package/template/core/engine/commands/pdf.mjs +0 -26
  16. package/template/core/engine/commands/preview.mjs +0 -26
  17. package/template/core/engine/commands/render.mjs +0 -17
  18. package/template/core/engine/commands/replace.mjs +0 -41
  19. package/template/core/engine/commands/search.mjs +0 -33
  20. package/template/core/engine/commands/typecheck.mjs +0 -5
  21. package/template/core/engine/commands/upgrade.mjs +0 -159
  22. package/template/core/engine/commands/validate.mjs +0 -17
  23. package/template/core/engine/document-export.mjs +0 -15
  24. package/template/core/engine/init.mjs +0 -90
  25. package/template/core/engine/output/chrome-pdf.d.mts +0 -34
  26. package/template/core/engine/output/chrome-pdf.mjs +0 -358
  27. package/template/core/engine/output/deploy-sync.mjs +0 -15
  28. package/template/core/engine/output/fonts.mjs +0 -62
  29. package/template/core/engine/output/katex-assets.mjs +0 -45
  30. package/template/core/engine/output/page-block.mjs +0 -30
  31. package/template/core/engine/output/pdf-media.mjs +0 -45
  32. package/template/core/engine/output/public-assets.mjs +0 -19
  33. package/template/core/engine/output/static-server.mjs +0 -532
  34. package/template/core/engine/react/caption-numbering.mjs +0 -73
  35. package/template/core/engine/react/comment-endpoint.d.mts +0 -11
  36. package/template/core/engine/react/comment-endpoint.mjs +0 -102
  37. package/template/core/engine/react/comment-marker.mjs +0 -374
  38. package/template/core/engine/react/document-entry.mjs +0 -324
  39. package/template/core/engine/react/document-export.mjs +0 -373
  40. package/template/core/engine/react/http-json.mjs +0 -24
  41. package/template/core/engine/react/mdx-compile.mjs +0 -599
  42. package/template/core/engine/react/measurement-css.mjs +0 -136
  43. package/template/core/engine/react/object-entities.mjs +0 -119
  44. package/template/core/engine/react/pagination/allocator.mjs +0 -122
  45. package/template/core/engine/react/pagination/regions.mjs +0 -81
  46. package/template/core/engine/react/pagination-constants.mjs +0 -3
  47. package/template/core/engine/react/pagination.mjs +0 -9
  48. package/template/core/engine/react/pipeline/allocate.mjs +0 -251
  49. package/template/core/engine/react/pipeline/final-render.mjs +0 -94
  50. package/template/core/engine/react/pipeline/frame-measurement.mjs +0 -302
  51. package/template/core/engine/react/pipeline/press-tree.mjs +0 -135
  52. package/template/core/engine/react/project-asset-endpoint.d.mts +0 -10
  53. package/template/core/engine/react/project-asset-endpoint.mjs +0 -361
  54. package/template/core/engine/react/section-css.mjs +0 -56
  55. package/template/core/engine/react/source-edit-endpoint.d.mts +0 -10
  56. package/template/core/engine/react/source-edit-endpoint.mjs +0 -75
  57. package/template/core/engine/react/sources/heading-numbering.mjs +0 -132
  58. package/template/core/engine/react/sources/mdx-resolver.mjs +0 -439
  59. package/template/core/engine/react/style-discovery.mjs +0 -142
  60. package/template/core/engine/runtime/config.d.mts +0 -40
  61. package/template/core/engine/runtime/config.mjs +0 -175
  62. package/template/core/engine/runtime/file-utils.mjs +0 -106
  63. package/template/core/engine/runtime/file-walk.mjs +0 -22
  64. package/template/core/engine/runtime/inspection.mjs +0 -328
  65. package/template/core/engine/runtime/issue-report.mjs +0 -44
  66. package/template/core/engine/runtime/path-utils.mjs +0 -20
  67. package/template/core/engine/runtime/source-text-tools.d.mts +0 -102
  68. package/template/core/engine/runtime/source-text-tools.mjs +0 -832
  69. package/template/core/engine/runtime/source-workspace.mjs +0 -159
  70. package/template/core/engine/runtime/validation.mjs +0 -174
  71. package/template/core/index.html +0 -13
  72. package/template/core/openpress.config.mjs +0 -12
  73. package/template/core/package.json +0 -91
  74. package/template/core/src/main.tsx +0 -16
  75. package/template/core/src/openpress/app/OpenPressApp.tsx +0 -140
  76. package/template/core/src/openpress/app/OpenPressRuntime.tsx +0 -94
  77. package/template/core/src/openpress/app/index.ts +0 -2
  78. package/template/core/src/openpress/core/Frame.tsx +0 -78
  79. package/template/core/src/openpress/core/FrameContext.tsx +0 -24
  80. package/template/core/src/openpress/core/MdxArea.tsx +0 -34
  81. package/template/core/src/openpress/core/Press.tsx +0 -34
  82. package/template/core/src/openpress/core/cn.ts +0 -4
  83. package/template/core/src/openpress/core/index.tsx +0 -40
  84. package/template/core/src/openpress/core/primitives.tsx +0 -44
  85. package/template/core/src/openpress/core/types.ts +0 -191
  86. package/template/core/src/openpress/core/useSource.ts +0 -28
  87. package/template/core/src/openpress/document-model/anchorMapModel.ts +0 -27
  88. package/template/core/src/openpress/document-model/documentIndexes.ts +0 -329
  89. package/template/core/src/openpress/document-model/documentTypes.ts +0 -138
  90. package/template/core/src/openpress/document-model/index.ts +0 -6
  91. package/template/core/src/openpress/document-model/objectEntityModel.ts +0 -51
  92. package/template/core/src/openpress/document-model/projectIdentityModel.ts +0 -15
  93. package/template/core/src/openpress/document-model/reactDocumentMetadataModel.ts +0 -27
  94. package/template/core/src/openpress/manuscript/index.tsx +0 -238
  95. package/template/core/src/openpress/mdx/index.ts +0 -88
  96. package/template/core/src/openpress/numbering/index.ts +0 -294
  97. package/template/core/src/openpress/reader/PublicReaderPage.tsx +0 -267
  98. package/template/core/src/openpress/reader/ReaderNavigationPanel.tsx +0 -123
  99. package/template/core/src/openpress/reader/index.ts +0 -10
  100. package/template/core/src/openpress/reader/pageViewportScaleModel.ts +0 -73
  101. package/template/core/src/openpress/reader/readerPageRegistry.ts +0 -41
  102. package/template/core/src/openpress/reader/readerPageRoute.ts +0 -21
  103. package/template/core/src/openpress/reader/readerScroll.ts +0 -92
  104. package/template/core/src/openpress/reader/readerStateModel.ts +0 -15
  105. package/template/core/src/openpress/reader/readerTypes.ts +0 -4
  106. package/template/core/src/openpress/reader/usePageViewportScale.ts +0 -119
  107. package/template/core/src/openpress/reader/usePanelState.ts +0 -56
  108. package/template/core/src/openpress/reader/useReaderHashSync.ts +0 -61
  109. package/template/core/src/openpress/reader/useReaderKeyboardNav.ts +0 -48
  110. package/template/core/src/openpress/reader/useReaderRuntime.ts +0 -146
  111. package/template/core/src/openpress/reader/useReaderScrollAnchor.ts +0 -64
  112. package/template/core/src/openpress/shared/Panel.tsx +0 -77
  113. package/template/core/src/openpress/shared/frameScheduler.ts +0 -32
  114. package/template/core/src/openpress/shared/index.ts +0 -4
  115. package/template/core/src/openpress/shared/numberUtils.ts +0 -3
  116. package/template/core/src/openpress/shared/runtimeMode.ts +0 -11
  117. package/template/core/src/openpress/workbench/Workbench.tsx +0 -407
  118. package/template/core/src/openpress/workbench/actions/DeploymentControl.tsx +0 -157
  119. package/template/core/src/openpress/workbench/actions/PageZoomControl.tsx +0 -182
  120. package/template/core/src/openpress/workbench/actions/SearchControl.tsx +0 -345
  121. package/template/core/src/openpress/workbench/actions/deploymentStatusModel.ts +0 -112
  122. package/template/core/src/openpress/workbench/actions/index.ts +0 -5
  123. package/template/core/src/openpress/workbench/actions/useDeploymentWorkbench.ts +0 -136
  124. package/template/core/src/openpress/workbench/dialog/WorkbenchDialog.tsx +0 -72
  125. package/template/core/src/openpress/workbench/dialog/index.ts +0 -1
  126. package/template/core/src/openpress/workbench/document/components/DocumentPanel.tsx +0 -127
  127. package/template/core/src/openpress/workbench/document/components/InlineSourceEditorLayer.tsx +0 -207
  128. package/template/core/src/openpress/workbench/document/components/ReaderStage.tsx +0 -9
  129. package/template/core/src/openpress/workbench/document/hooks/useDocumentWorkbenchModel.ts +0 -34
  130. package/template/core/src/openpress/workbench/document/hooks/useInlineDocumentEditor.ts +0 -525
  131. package/template/core/src/openpress/workbench/document/index.ts +0 -10
  132. package/template/core/src/openpress/workbench/index.ts +0 -2
  133. package/template/core/src/openpress/workbench/inspector/InlineInspectorLayer.tsx +0 -459
  134. package/template/core/src/openpress/workbench/inspector/index.ts +0 -5
  135. package/template/core/src/openpress/workbench/inspector/inlineCommentModel.ts +0 -125
  136. package/template/core/src/openpress/workbench/inspector/inspectorGeometryModel.ts +0 -160
  137. package/template/core/src/openpress/workbench/inspector/inspectorModel.ts +0 -408
  138. package/template/core/src/openpress/workbench/inspector/useInspectorComments.ts +0 -248
  139. package/template/core/src/openpress/workbench/mentions/MentionSuggestionList.tsx +0 -41
  140. package/template/core/src/openpress/workbench/mentions/index.ts +0 -2
  141. package/template/core/src/openpress/workbench/mentions/useComposerMentions.ts +0 -185
  142. package/template/core/src/openpress/workbench/panels/Panel.tsx +0 -1
  143. package/template/core/src/openpress/workbench/panels/PendingCommentsPanel.tsx +0 -76
  144. package/template/core/src/openpress/workbench/panels/WorkbenchControlPanel.tsx +0 -29
  145. package/template/core/src/openpress/workbench/panels/index.ts +0 -3
  146. package/template/core/src/openpress/workbench/project/ProjectEntryPanel.tsx +0 -523
  147. package/template/core/src/openpress/workbench/project/ProjectPreviewDialog.tsx +0 -35
  148. package/template/core/src/openpress/workbench/project/index.ts +0 -2
  149. package/template/core/src/openpress/workbench/project/projectPreviewTypes.ts +0 -11
  150. package/template/core/src/openpress/workbench/project/projectSourceModel.ts +0 -24
  151. package/template/core/src/openpress/workbench/shell/WorkbenchShell.tsx +0 -167
  152. package/template/core/src/openpress/workbench/shell/index.ts +0 -1
  153. package/template/core/src/openpress/workbench/workbenchFormatters.ts +0 -120
  154. package/template/core/src/openpress/workbench/workbenchTypes.ts +0 -35
  155. package/template/core/src/styles/openpress/app-shell.css +0 -251
  156. package/template/core/src/styles/openpress/media-workspace.css +0 -230
  157. package/template/core/src/styles/openpress/print-route.css +0 -184
  158. package/template/core/src/styles/openpress/project-preview-panel.css +0 -924
  159. package/template/core/src/styles/openpress/public-viewer.css +0 -688
  160. package/template/core/src/styles/openpress/reader-runtime.css +0 -980
  161. package/template/core/src/styles/openpress/responsive.css +0 -245
  162. package/template/core/src/styles/openpress/workbench-panels.css +0 -594
  163. package/template/core/src/styles/openpress/workbench.css +0 -1255
  164. package/template/core/src/styles/openpress.css +0 -14
  165. package/template/core/src/vite-env.d.ts +0 -9
  166. package/template/core/tsconfig.json +0 -40
  167. package/template/core/vite.config.ts +0 -584
  168. package/template/packs/academic-paper/document/chapters/01-introduction/content/01-introduction.mdx +0 -35
  169. package/template/packs/academic-paper/document/chapters/02-methods/content/01-methods.mdx +0 -50
  170. package/template/packs/academic-paper/document/chapters/03-results-and-discussion/content/01-results.mdx +0 -47
  171. package/template/packs/academic-paper/document/chapters/04-acknowledgment/content/01-acknowledgment.mdx +0 -26
  172. package/template/packs/academic-paper/document/chapters/05-references/content/01-references.mdx +0 -32
  173. package/template/packs/academic-paper/document/components/ChapterOpenerVisual/index.tsx +0 -76
  174. package/template/packs/academic-paper/document/components/Page.tsx +0 -60
  175. package/template/packs/academic-paper/document/components/TokenSwatchGrid/index.tsx +0 -46
  176. package/template/packs/academic-paper/document/components/TokenSwatchGrid/style.css +0 -63
  177. package/template/packs/academic-paper/document/components/TypeSpecimen/index.tsx +0 -38
  178. package/template/packs/academic-paper/document/components/TypeSpecimen/style.css +0 -111
  179. package/template/packs/academic-paper/document/design.md +0 -279
  180. package/template/packs/academic-paper/document/index.tsx +0 -123
  181. package/template/packs/academic-paper/document/media/README.md +0 -13
  182. package/template/packs/academic-paper/document/media/figure-placeholder.svg +0 -9
  183. package/template/packs/academic-paper/document/openpress.config.mjs +0 -26
  184. package/template/packs/academic-paper/document/theme/README.md +0 -11
  185. package/template/packs/academic-paper/document/theme/base/page-contract.css +0 -522
  186. package/template/packs/academic-paper/document/theme/base/print.css +0 -93
  187. package/template/packs/academic-paper/document/theme/base/typography.css +0 -333
  188. package/template/packs/academic-paper/document/theme/fonts.css +0 -3
  189. package/template/packs/academic-paper/document/theme/page-surfaces/back-cover.css +0 -43
  190. package/template/packs/academic-paper/document/theme/page-surfaces/chapter-opener.css +0 -205
  191. package/template/packs/academic-paper/document/theme/page-surfaces/cover.css +0 -294
  192. package/template/packs/academic-paper/document/theme/page-surfaces/toc.css +0 -149
  193. package/template/packs/academic-paper/document/theme/patterns/_chart-frame.css +0 -49
  194. package/template/packs/academic-paper/document/theme/patterns/figure-grid.css +0 -68
  195. package/template/packs/academic-paper/document/theme/patterns/table-utilities.css +0 -66
  196. package/template/packs/academic-paper/document/theme/shell/reader-controls.css +0 -761
  197. package/template/packs/academic-paper/document/theme/tokens.css +0 -80
  198. package/template/packs/academic-paper/openpress.config.mjs +0 -5
  199. package/template/packs/claude-document/document/chapters/01-document-shape/content/01-document-shape.mdx +0 -51
  200. package/template/packs/claude-document/document/chapters/02-review-loop/content/01-review-loop.mdx +0 -31
  201. package/template/packs/claude-document/document/components/ChapterOpenerVisual.tsx +0 -96
  202. package/template/packs/claude-document/document/components/Page.tsx +0 -37
  203. package/template/packs/claude-document/document/design.md +0 -142
  204. package/template/packs/claude-document/document/index.tsx +0 -94
  205. package/template/packs/claude-document/document/media/README.md +0 -13
  206. package/template/packs/claude-document/document/openpress.config.mjs +0 -26
  207. package/template/packs/claude-document/document/theme/README.md +0 -15
  208. package/template/packs/claude-document/document/theme/base/page-contract.css +0 -525
  209. package/template/packs/claude-document/document/theme/base/print.css +0 -93
  210. package/template/packs/claude-document/document/theme/base/typography.css +0 -612
  211. package/template/packs/claude-document/document/theme/fonts.css +0 -4
  212. package/template/packs/claude-document/document/theme/page-surfaces/back-cover.css +0 -72
  213. package/template/packs/claude-document/document/theme/page-surfaces/chapter-opener.css +0 -236
  214. package/template/packs/claude-document/document/theme/page-surfaces/cover.css +0 -309
  215. package/template/packs/claude-document/document/theme/page-surfaces/toc.css +0 -225
  216. package/template/packs/claude-document/document/theme/patterns/_chart-frame.css +0 -53
  217. package/template/packs/claude-document/document/theme/patterns/figure-grid.css +0 -68
  218. package/template/packs/claude-document/document/theme/patterns/table-utilities.css +0 -66
  219. package/template/packs/claude-document/document/theme/shell/reader-controls.css +0 -789
  220. package/template/packs/claude-document/document/theme/tokens.css +0 -89
  221. package/template/packs/claude-document/openpress.config.mjs +0 -5
  222. package/template/packs/editorial-monograph/document/chapters/01-product-and-use-cases/content/01-product-and-use-cases.mdx +0 -31
  223. package/template/packs/editorial-monograph/document/chapters/02-workflow/content/01-workflow.mdx +0 -89
  224. package/template/packs/editorial-monograph/document/chapters/03-agent-skills-contributors/content/01-agent-skills-contributors.mdx +0 -51
  225. package/template/packs/editorial-monograph/document/chapters/04-validation-deploy/content/01-validation-deploy.mdx +0 -39
  226. package/template/packs/editorial-monograph/document/components/ChapterOpenerVisual/index.tsx +0 -76
  227. package/template/packs/editorial-monograph/document/components/Page.tsx +0 -37
  228. package/template/packs/editorial-monograph/document/components/TokenSwatchGrid/index.tsx +0 -46
  229. package/template/packs/editorial-monograph/document/components/TokenSwatchGrid/style.css +0 -63
  230. package/template/packs/editorial-monograph/document/components/TypeSpecimen/index.tsx +0 -38
  231. package/template/packs/editorial-monograph/document/components/TypeSpecimen/style.css +0 -111
  232. package/template/packs/editorial-monograph/document/design.md +0 -279
  233. package/template/packs/editorial-monograph/document/index.tsx +0 -97
  234. package/template/packs/editorial-monograph/document/media/README.md +0 -13
  235. package/template/packs/editorial-monograph/document/openpress.config.mjs +0 -26
  236. package/template/packs/editorial-monograph/document/theme/README.md +0 -11
  237. package/template/packs/editorial-monograph/document/theme/base/page-contract.css +0 -505
  238. package/template/packs/editorial-monograph/document/theme/base/print.css +0 -93
  239. package/template/packs/editorial-monograph/document/theme/base/typography.css +0 -336
  240. package/template/packs/editorial-monograph/document/theme/fonts.css +0 -3
  241. package/template/packs/editorial-monograph/document/theme/page-surfaces/back-cover.css +0 -43
  242. package/template/packs/editorial-monograph/document/theme/page-surfaces/chapter-opener.css +0 -205
  243. package/template/packs/editorial-monograph/document/theme/page-surfaces/cover.css +0 -147
  244. package/template/packs/editorial-monograph/document/theme/page-surfaces/toc.css +0 -149
  245. package/template/packs/editorial-monograph/document/theme/patterns/_chart-frame.css +0 -49
  246. package/template/packs/editorial-monograph/document/theme/patterns/figure-grid.css +0 -68
  247. package/template/packs/editorial-monograph/document/theme/patterns/table-utilities.css +0 -66
  248. package/template/packs/editorial-monograph/document/theme/shell/reader-controls.css +0 -761
  249. package/template/packs/editorial-monograph/document/theme/tokens.css +0 -80
  250. package/template/packs/editorial-monograph/openpress.config.mjs +0 -5
@@ -1,102 +0,0 @@
1
- import {
2
- clearCommentMarkers,
3
- insertCommentMarker,
4
- listCommentMarkers,
5
- updateCommentMarker,
6
- } from "./comment-marker.mjs";
7
- import { readJsonBody, writeJson } from "./http-json.mjs";
8
-
9
- export async function handleCommentRequest(req, res, {
10
- root = ".",
11
- id = undefined,
12
- timestamp = undefined,
13
- } = {}) {
14
- if (req.method === "GET") {
15
- try {
16
- writeJson(res, 200, { ok: true, comments: await listCommentMarkers({ root }) });
17
- } catch (error) {
18
- writeErrorJson(res, error);
19
- }
20
- return;
21
- }
22
-
23
- if (req.method === "DELETE") {
24
- try {
25
- const body = await readJsonBody(req, { bodyLabel: "OpenPress comment request" });
26
- const result = await clearCommentMarkers({
27
- root,
28
- id: body?.id,
29
- all: body?.all === true,
30
- });
31
- writeJson(res, 200, { ok: true, ...result });
32
- } catch (error) {
33
- writeErrorJson(res, error);
34
- }
35
- return;
36
- }
37
-
38
- if (req.method === "PATCH") {
39
- try {
40
- const body = await readJsonBody(req, { bodyLabel: "OpenPress comment request" });
41
- const result = await updateCommentMarker({
42
- root,
43
- id: body?.id,
44
- note: body?.note,
45
- hint: body?.hint,
46
- timestamp,
47
- });
48
- writeJson(res, 200, {
49
- ok: true,
50
- comment: {
51
- id: result.id,
52
- timestamp: result.timestamp,
53
- path: result.path,
54
- line: result.line,
55
- note: result.note,
56
- hint: result.hint,
57
- },
58
- });
59
- } catch (error) {
60
- writeErrorJson(res, error);
61
- }
62
- return;
63
- }
64
-
65
- if (req.method !== "POST") {
66
- writeJson(res, 405, { ok: false, message: "OpenPress comment endpoint requires GET, POST, PATCH, or DELETE." });
67
- return;
68
- }
69
-
70
- try {
71
- const body = await readJsonBody(req, { bodyLabel: "OpenPress comment request" });
72
- const target = body?.target ?? {};
73
- const result = await insertCommentMarker({
74
- root,
75
- path: target.path ?? body?.path,
76
- source: target.source ?? body?.source,
77
- note: body?.note,
78
- hint: body?.hint,
79
- id,
80
- timestamp,
81
- });
82
-
83
- writeJson(res, 200, {
84
- ok: true,
85
- comment: {
86
- id: result.id,
87
- timestamp: result.timestamp,
88
- path: result.path,
89
- line: result.line,
90
- },
91
- });
92
- } catch (error) {
93
- writeErrorJson(res, error);
94
- }
95
- }
96
-
97
- function writeErrorJson(res, error) {
98
- writeJson(res, 400, {
99
- ok: false,
100
- message: error instanceof Error ? error.message : String(error),
101
- });
102
- }
@@ -1,374 +0,0 @@
1
- import crypto from "node:crypto";
2
- import fs from "node:fs/promises";
3
- import path from "node:path";
4
- import { loadConfig } from "../runtime/config.mjs";
5
- import { collectSourceTextFiles } from "../runtime/source-text-tools.mjs";
6
-
7
- // Any `.mdx` or `.tsx` file under `document/` is a legal comment target.
8
- // The Press Tree allows arbitrary source layouts — `section-folders`,
9
- // `section-files`, `file-list`, custom `root` paths, etc. — so we no
10
- // longer hardcode `document/chapters/<slug>/content/*.mdx`. The boundary
11
- // is "inside the workspace's authored `document/` directory" and "looks
12
- // like an editable React/MDX source" by extension.
13
- const EDITABLE_COMMENT_SOURCE_PATTERNS = [
14
- /^document\/.+\.mdx$/,
15
- /^document\/.+\.tsx$/,
16
- ];
17
- const COMMENT_MARKER_RE = /(?:\{\/\*|\/\*)\s*@openpress-comment\b(?<attrs>[^*]*)\*\/\}?/g;
18
- const COMMENT_LINE_RE = /^\s*(?:\{\/\*|\/\*)\s*@openpress-comment\b[^*]*\*\/\}?\s*$/;
19
-
20
- export async function insertCommentMarker({
21
- root = ".",
22
- path: sourcePath,
23
- source,
24
- note,
25
- hint,
26
- id = createCommentId(),
27
- timestamp = new Date().toISOString(),
28
- } = {}) {
29
- const workspaceRoot = path.resolve(root);
30
- const relativePath = normalizeEditableSourcePath(sourcePath);
31
- assertEditableCommentPath(relativePath);
32
- const absolutePath = path.resolve(workspaceRoot, relativePath);
33
- if (!absolutePath.startsWith(`${workspaceRoot}${path.sep}`)) {
34
- throw new Error(`OpenPress comment target path escapes workspace: ${sourcePath}`);
35
- }
36
-
37
- const noteText = normalizedNote(note);
38
- const line = normalizeLineNumber(source?.line);
39
- const text = await fs.readFile(absolutePath, "utf8");
40
- const marker = createCommentMarker({
41
- id,
42
- timestamp,
43
- note: noteText,
44
- hint,
45
- syntax: commentMarkerSyntaxForInsert(text, line, relativePath),
46
- });
47
- const nextText = insertLineBefore(text, line, marker);
48
- await fs.writeFile(absolutePath, nextText, "utf8");
49
-
50
- return {
51
- id,
52
- timestamp,
53
- marker,
54
- path: relativePath,
55
- absolutePath,
56
- line,
57
- };
58
- }
59
-
60
- export function createCommentMarker({ id = createCommentId(), timestamp = new Date().toISOString(), note, hint, syntax = "jsx" } = {}) {
61
- const payload = { note: normalizedNote(note), ...(typeof hint === "string" && hint.trim() ? { hint: hint.trim() } : {}) };
62
- const body = `@openpress-comment id="${escapeMarkerAttribute(id)}" ts="${escapeMarkerAttribute(timestamp)}" text="${encodeCommentPayload(payload)}"`;
63
- return syntax === "block" ? `/* ${body} */` : `{/* ${body} */}`;
64
- }
65
-
66
- export function decodeCommentMarkerText(marker) {
67
- const match = String(marker ?? "").match(/\btext="([^"]+)"/);
68
- if (!match) return null;
69
- return JSON.parse(Buffer.from(match[1], "base64url").toString("utf8"));
70
- }
71
-
72
- export async function listCommentMarkers({ root = "." } = {}) {
73
- const config = await loadConfig(root);
74
- const files = await collectSourceTextFiles(config, { scope: "all" });
75
- const comments = [];
76
-
77
- for (const file of files) {
78
- if (!isEditableCommentPath(file.path ?? file.relativePath)) continue;
79
- comments.push(...extractCommentMarkers(file));
80
- }
81
-
82
- comments.sort((a, b) => a.path.localeCompare(b.path) || a.line - b.line || a.id.localeCompare(b.id));
83
- return comments;
84
- }
85
-
86
- export async function clearCommentMarkers({ root = ".", id = null, all = false } = {}) {
87
- if (!all && !(typeof id === "string" && id.trim())) {
88
- throw new Error("OpenPress comment clear requires an `id` or `all: true`.");
89
- }
90
-
91
- const config = await loadConfig(root);
92
- const files = await collectSourceTextFiles(config, { scope: "all" });
93
- const removed = [];
94
-
95
- for (const file of files) {
96
- if (!isEditableCommentPath(file.path ?? file.relativePath)) continue;
97
- const next = removeCommentMarkerLines(file.text, { id, all });
98
- if (next.removed.length === 0) continue;
99
- await fs.writeFile(file.absolutePath, next.text, "utf8");
100
- for (const marker of next.removed) {
101
- removed.push({
102
- ...marker,
103
- path: file.path ?? file.relativePath,
104
- absolutePath: file.absolutePath,
105
- });
106
- }
107
- }
108
-
109
- return {
110
- removedCount: removed.length,
111
- comments: removed,
112
- };
113
- }
114
-
115
- export async function updateCommentMarker({
116
- root = ".",
117
- id,
118
- note,
119
- hint,
120
- timestamp = new Date().toISOString(),
121
- } = {}) {
122
- if (!(typeof id === "string" && id.trim())) {
123
- throw new Error("OpenPress comment update requires an `id`.");
124
- }
125
-
126
- const config = await loadConfig(root);
127
- const files = await collectSourceTextFiles(config, { scope: "all" });
128
- const noteText = normalizedNote(note);
129
-
130
- for (const file of files) {
131
- if (!isEditableCommentPath(file.path ?? file.relativePath)) continue;
132
- const next = replaceCommentMarkerLine(file.text, { id, note: noteText, hint, timestamp });
133
- if (!next.comment) continue;
134
- await fs.writeFile(file.absolutePath, next.text, "utf8");
135
- return {
136
- ...next.comment,
137
- path: file.path ?? file.relativePath,
138
- absolutePath: file.absolutePath,
139
- };
140
- }
141
-
142
- throw new Error(`OpenPress comment marker not found: ${id}`);
143
- }
144
-
145
- export function assertEditableCommentPath(relativePath) {
146
- if (!isEditableCommentPath(relativePath)) {
147
- throw new Error(`OpenPress comment target is not an editable OpenPress document source: ${relativePath}`);
148
- }
149
- }
150
-
151
- // Strict check against workspace-relative paths. Callers walking the
152
- // workspace (`applyCommentMarker` via `collectSourceTextFiles`) already
153
- // receive paths with the `document/` prefix and must not have system
154
- // paths silently mapped into the editable set.
155
- export function isEditableCommentPath(relativePath) {
156
- if (typeof relativePath !== "string" || !relativePath) return false;
157
- const trimmed = relativePath.trim().replaceAll("\\", "/").replace(/^\.\//, "");
158
- return EDITABLE_COMMENT_SOURCE_PATTERNS.some((pattern) => pattern.test(trimmed));
159
- }
160
-
161
- function normalizeEditableSourcePath(value) {
162
- if (typeof value !== "string" || !value.trim()) {
163
- throw new Error("OpenPress comment target requires a source path.");
164
- }
165
- const normalized = value.trim().replaceAll("\\", "/").replace(/^\.\//, "");
166
- if (path.posix.isAbsolute(normalized) || normalized.includes("\0") || normalized === "." || normalized.startsWith("../")) {
167
- throw new Error(`OpenPress comment target path is invalid: ${value}`);
168
- }
169
- const posix = path.posix.normalize(normalized);
170
- // The Press Tree source resolver emits paths relative to `document/`
171
- // (e.g. "chapters/01-start/content/01-start.mdx"). The comment marker
172
- // works in workspace-relative paths (with the `document/` prefix). If
173
- // the incoming path is documentRoot-relative, prepend `document/`.
174
- if (!posix.startsWith("document/") && looksDocumentRelative(posix)) {
175
- return `document/${posix}`;
176
- }
177
- return posix;
178
- }
179
-
180
- // Identify paths the Press Tree source resolver emits — those are relative
181
- // to `document/`. Match `.mdx` / `.tsx` files that don't already have the
182
- // `document/` prefix and don't look like system / engine paths. The check
183
- // is intentionally tight so we never silently rewrite engine internals
184
- // (e.g. `src/openpress/...`) into "editable" workspace paths.
185
- const SYSTEM_PATH_PREFIXES = [
186
- "document/",
187
- "src/",
188
- "engine/",
189
- "dist/",
190
- "dist-react/",
191
- "node_modules/",
192
- "tests/",
193
- "public/",
194
- "packages/",
195
- ".openpress/",
196
- ".deploy/",
197
- ".changeset/",
198
- ".github/",
199
- ];
200
-
201
- function looksDocumentRelative(posixPath) {
202
- if (!/\.(mdx|tsx)$/.test(posixPath)) return false;
203
- if (SYSTEM_PATH_PREFIXES.some((prefix) => posixPath.startsWith(prefix))) return false;
204
- return true;
205
- }
206
-
207
- function normalizeLineNumber(value) {
208
- const line = Number(value);
209
- if (!Number.isInteger(line) || line < 1) {
210
- throw new Error("OpenPress comment target requires a 1-based source line.");
211
- }
212
- return line;
213
- }
214
-
215
- function normalizedNote(value) {
216
- const note = typeof value === "string" ? value.trim() : "";
217
- if (!note) throw new Error("OpenPress comment note must not be empty.");
218
- return note;
219
- }
220
-
221
- function insertLineBefore(text, line, marker) {
222
- const newline = text.includes("\r\n") ? "\r\n" : "\n";
223
- const hasTrailingNewline = /\r?\n$/.test(text);
224
- const lines = text.split(/\r?\n/);
225
- if (hasTrailingNewline) lines.pop();
226
- const index = Math.min(Math.max(line - 1, 0), lines.length);
227
- lines.splice(index, 0, marker);
228
- return `${lines.join(newline)}${hasTrailingNewline ? newline : ""}`;
229
- }
230
-
231
- function encodeCommentPayload(payload) {
232
- return Buffer.from(JSON.stringify(payload), "utf8").toString("base64url");
233
- }
234
-
235
- function extractCommentMarkers(file) {
236
- const comments = [];
237
- const text = String(file.text ?? "");
238
- const lineStarts = lineStartOffsets(text);
239
- for (const match of text.matchAll(COMMENT_MARKER_RE)) {
240
- const marker = match[0];
241
- const attrs = parseMarkerAttributes(match.groups?.attrs ?? "");
242
- const payload = decodeCommentMarkerText(marker) ?? {};
243
- const line = lineNumberForOffset(lineStarts, match.index ?? 0);
244
- comments.push({
245
- id: attrs.id ?? "",
246
- timestamp: attrs.ts,
247
- path: file.path ?? file.relativePath,
248
- absolutePath: file.absolutePath,
249
- line,
250
- marker,
251
- note: typeof payload.note === "string" ? payload.note : "",
252
- hint: typeof payload.hint === "string" ? payload.hint : undefined,
253
- });
254
- }
255
- return comments;
256
- }
257
-
258
- function removeCommentMarkerLines(text, { id, all }) {
259
- const newline = text.includes("\r\n") ? "\r\n" : "\n";
260
- const hasTrailingNewline = /\r?\n$/.test(text);
261
- const lines = text.split(/\r?\n/);
262
- if (hasTrailingNewline) lines.pop();
263
- const kept = [];
264
- const removed = [];
265
-
266
- for (const [index, line] of lines.entries()) {
267
- if (!COMMENT_LINE_RE.test(line)) {
268
- kept.push(line);
269
- continue;
270
- }
271
- const attrs = parseMarkerAttributes(line);
272
- if (all || attrs.id === id) {
273
- removed.push({ id: attrs.id ?? "", timestamp: attrs.ts, line: index + 1, marker: line.trim() });
274
- continue;
275
- }
276
- kept.push(line);
277
- }
278
-
279
- return {
280
- text: `${kept.join(newline)}${hasTrailingNewline ? newline : ""}`,
281
- removed,
282
- };
283
- }
284
-
285
- function replaceCommentMarkerLine(text, { id, note, hint, timestamp }) {
286
- const newline = text.includes("\r\n") ? "\r\n" : "\n";
287
- const hasTrailingNewline = /\r?\n$/.test(text);
288
- const lines = text.split(/\r?\n/);
289
- if (hasTrailingNewline) lines.pop();
290
-
291
- for (const [index, line] of lines.entries()) {
292
- if (!COMMENT_LINE_RE.test(line)) continue;
293
- const attrs = parseMarkerAttributes(line);
294
- if (attrs.id !== id) continue;
295
- const marker = createCommentMarker({ id, timestamp, note, hint, syntax: commentMarkerSyntaxForLine(line) });
296
- lines[index] = `${line.match(/^\s*/)?.[0] ?? ""}${marker}`;
297
- return {
298
- text: `${lines.join(newline)}${hasTrailingNewline ? newline : ""}`,
299
- comment: {
300
- id,
301
- timestamp,
302
- line: index + 1,
303
- marker,
304
- note,
305
- hint: typeof hint === "string" && hint.trim() ? hint.trim() : undefined,
306
- },
307
- };
308
- }
309
-
310
- return { text, comment: null };
311
- }
312
-
313
- function parseMarkerAttributes(value) {
314
- const attrs = {};
315
- for (const match of String(value ?? "").matchAll(/\b([A-Za-z_:][-A-Za-z0-9_:.]*)="([^"]*)"/g)) {
316
- attrs[match[1]] = unescapeMarkerAttribute(match[2]);
317
- }
318
- return attrs;
319
- }
320
-
321
- function commentMarkerSyntaxForInsert(text, line, relativePath) {
322
- if (!String(relativePath).endsWith(".tsx")) return "jsx";
323
- const lines = String(text ?? "").split(/\r?\n/);
324
- const index = Math.min(Math.max(line - 1, 0), lines.length);
325
- const priorContent = lines.slice(0, index).some((entry) => entry.trim().length > 0);
326
- if (!priorContent) return "block";
327
- const targetLine = lines[index] ?? "";
328
- if (/^\s*(import\b|export\b|function\b|const\b|let\b|var\b|type\b|interface\b|return\b)/.test(targetLine)) {
329
- return "block";
330
- }
331
- return "jsx";
332
- }
333
-
334
- function commentMarkerSyntaxForLine(line) {
335
- return /^\s*\/\*/.test(line) ? "block" : "jsx";
336
- }
337
-
338
- function lineStartOffsets(text) {
339
- const starts = [0];
340
- for (let index = 0; index < text.length; index += 1) {
341
- if (text[index] === "\n") starts.push(index + 1);
342
- }
343
- return starts;
344
- }
345
-
346
- function lineNumberForOffset(starts, offset) {
347
- let line = 1;
348
- for (const [index, start] of starts.entries()) {
349
- if (start > offset) break;
350
- line = index + 1;
351
- }
352
- return line;
353
- }
354
-
355
- function createCommentId() {
356
- return `c-${crypto.randomBytes(4).toString("hex")}`;
357
- }
358
-
359
- function escapeMarkerAttribute(value) {
360
- return String(value ?? "").replace(/["&<>]/g, (char) => ({
361
- "\"": "&quot;",
362
- "&": "&amp;",
363
- "<": "&lt;",
364
- ">": "&gt;",
365
- }[char]));
366
- }
367
-
368
- function unescapeMarkerAttribute(value) {
369
- return String(value ?? "")
370
- .replace(/&quot;/g, "\"")
371
- .replace(/&amp;/g, "&")
372
- .replace(/&lt;/g, "<")
373
- .replace(/&gt;/g, ">");
374
- }