@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,251 +0,0 @@
1
- // Layer 4 — Allocation.
2
- //
3
- // Per chain, fills MdxAreas with block IDs using the Phase 1 region allocator.
4
- // Returns:
5
- // - `allocation`: { [frameKey]: { [chainId]: blockIds[][] } } — areas per chain, area index → blockIds
6
- // - `hints`: { totalPagesPerChain: { [chainId]: number } } — fed back to <Sections>
7
- // - `warnings`: chain-overflowed, etc.
8
-
9
- const SANITY_LIMIT = 200;
10
-
11
- /**
12
- * @param {object} opts
13
- * @param {Array<FrameInstance>} opts.frames Layer 2 frame structure.
14
- * @param {Array<MdxAreaMeasurement>} opts.mdxAreas Layer 3 MdxArea capacities.
15
- * @param {Array<BlockMeasurement>} opts.blockHeights Layer 3 block heights.
16
- * @param {Record<string, object>} opts.sources Resolved sources (for chain block lists).
17
- * @returns {AllocationResult}
18
- */
19
- export function allocateChains({ frames, mdxAreas, blockHeights, sources }) {
20
- // Group MdxAreas by chainId, sorted by (frame order, indexInFrame)
21
- const chainAreas = groupAreasByChain(frames, mdxAreas);
22
- // Group block heights by chainId
23
- const blockHeightsByChain = groupBlockHeights(blockHeights);
24
-
25
- const allocation = {};
26
- const warnings = [];
27
- const totalPagesPerChain = {};
28
- // Track per-chain how many extension iterations the caller (orchestrator)
29
- // should request. The actual *cloning* is done at the Press tree level
30
- // by feeding hints back to <Sections>; here we just report how many
31
- // frames each chain *should* have.
32
-
33
- for (const [chainId, chainSource] of iterateChains(sources)) {
34
- const areas = chainAreas.get(chainId) ?? [];
35
- const blocks = buildBlockStream(chainSource, blockHeightsByChain.get(chainId));
36
-
37
- const regions = areas.map((a) => ({
38
- id: `${a.frameKey}#${a.indexInFrame}`,
39
- capacity: a.capacity,
40
- pageIndex: 0, // not used here; we keep our own mapping
41
- columnIndex: a.indexInFrame,
42
- __frameKey: a.frameKey,
43
- __chainId: chainId,
44
- __overflow: a.overflow,
45
- }));
46
-
47
- if (regions.length === 0 || blocks.length === 0) {
48
- // No areas to fill, or no blocks to place.
49
- if (blocks.length > 0 && !chainId.startsWith("toc:")) {
50
- warnings.push({ code: "chain-has-no-area", chainId });
51
- }
52
- // Frames already correctly count to 0 for this chain.
53
- // (Or it's a static-source chain like the cover, which doesn't allocate.)
54
- // Skip.
55
- // If chain has blocks but no areas, that's an authoring error.
56
- const sourceFrameCount = frames.filter((f) => f.mdxAreas.some((a) => a.chainId === chainId)).length;
57
- totalPagesPerChain[chainId] = Math.max(1, sourceFrameCount);
58
- continue;
59
- }
60
-
61
- const { result, neededAreas } = greedyAllocate(blocks, regions);
62
- const lastOverflow = regions[regions.length - 1].__overflow;
63
- const sourceFramesForChain = uniqueFramesForChain(frames, chainId);
64
-
65
- if (neededAreas > regions.length) {
66
- // Overflow detected.
67
- if (lastOverflow === "error") {
68
- const remaining = blocks.slice(result.consumed).map((b) => b.id);
69
- throw new Error(
70
- `Chain "${chainId}" overflowed and the last MdxArea is overflow="error". ` +
71
- `Remaining block IDs: ${remaining.slice(0, 5).join(", ")}${remaining.length > 5 ? `, ... (${remaining.length} total)` : ""}.`,
72
- );
73
- }
74
- if (lastOverflow === "truncate") {
75
- warnings.push({
76
- code: "chain-overflowed",
77
- chainId,
78
- remainingBlocks: blocks.length - result.consumed,
79
- });
80
- // Don't grow totalPages; truncate at current capacity.
81
- totalPagesPerChain[chainId] = sourceFramesForChain;
82
- recordAllocation(allocation, result, regions);
83
- continue;
84
- }
85
- // overflow="extend" (default): scale frame count up.
86
- const areasPerFrame = Math.max(1, regions.length / Math.max(1, sourceFramesForChain));
87
- const extraAreasNeeded = neededAreas - regions.length;
88
- const extraFramesNeeded = Math.ceil(extraAreasNeeded / areasPerFrame);
89
- const newTotal = sourceFramesForChain + extraFramesNeeded;
90
- if (newTotal > SANITY_LIMIT) {
91
- throw new Error(
92
- `Chain "${chainId}" would require ${newTotal} frames after extension, exceeding the sanity limit of ${SANITY_LIMIT}. ` +
93
- `Check that block content fits within the MdxArea capacity.`,
94
- );
95
- }
96
- totalPagesPerChain[chainId] = newTotal;
97
- // Don't record allocation yet — orchestrator will re-render with more
98
- // frames and re-allocate.
99
- continue;
100
- }
101
-
102
- totalPagesPerChain[chainId] = sourceFramesForChain;
103
- recordAllocation(allocation, result, regions);
104
- }
105
-
106
- return {
107
- allocation,
108
- hints: { totalPagesPerChain },
109
- warnings,
110
- };
111
- }
112
-
113
- function greedyAllocate(blocks, regions) {
114
- const filled = [];
115
- let regionIndex = 0;
116
- let currentBlockIds = [];
117
- let currentHeight = 0;
118
- let consumed = 0;
119
- const flush = () => {
120
- if (currentBlockIds.length === 0) return;
121
- filled.push({
122
- region: regions[regionIndex],
123
- blockIds: currentBlockIds,
124
- });
125
- currentBlockIds = [];
126
- currentHeight = 0;
127
- };
128
- for (let blockIndex = 0; blockIndex < blocks.length; blockIndex += 1) {
129
- const block = blocks[blockIndex];
130
- while (regionIndex < regions.length) {
131
- const region = regions[regionIndex];
132
- const nextBlock = blocks[blockIndex + 1];
133
- const keepWithNextHeight = shouldKeepWithNext(block, nextBlock) ? block.height + nextBlock.height : 0;
134
- if (
135
- currentBlockIds.length > 0 &&
136
- keepWithNextHeight > 0 &&
137
- currentHeight + keepWithNextHeight > region.capacity
138
- ) {
139
- flush();
140
- regionIndex += 1;
141
- continue;
142
- }
143
- if (currentBlockIds.length === 0 || currentHeight + block.height <= region.capacity) {
144
- currentBlockIds.push(block.id);
145
- currentHeight += block.height;
146
- consumed += 1;
147
- break;
148
- }
149
- // Doesn't fit — flush current region and advance
150
- flush();
151
- regionIndex += 1;
152
- }
153
- if (regionIndex >= regions.length) break;
154
- }
155
- flush();
156
- // neededAreas = total regions consumed if we had unlimited supply
157
- // For overflow detection we estimate: if consumed < blocks.length, we need more areas.
158
- let neededAreas = filled.length;
159
- if (consumed < blocks.length) {
160
- // Estimate how many more areas needed
161
- const lastCap = regions[regions.length - 1].capacity;
162
- const remainingBlocks = blocks.slice(consumed);
163
- let h = 0;
164
- let extra = 0;
165
- let inExtra = false;
166
- for (const b of remainingBlocks) {
167
- if (!inExtra || h + b.height > lastCap) {
168
- extra += 1;
169
- h = b.height;
170
- inExtra = true;
171
- } else {
172
- h += b.height;
173
- }
174
- }
175
- neededAreas += extra;
176
- }
177
- return { result: { filled, consumed }, neededAreas };
178
- }
179
-
180
- function shouldKeepWithNext(block, nextBlock) {
181
- if (!nextBlock) return false;
182
- const name = String(block?.name ?? "");
183
- return /^h[1-6]$/.test(name) || name === "caption";
184
- }
185
-
186
- function recordAllocation(allocation, result, regions) {
187
- for (const fill of result.filled) {
188
- const frameKey = fill.region.__frameKey;
189
- const chainId = fill.region.__chainId;
190
- const areaIndex = fill.region.columnIndex;
191
- if (!allocation[frameKey]) allocation[frameKey] = {};
192
- if (!allocation[frameKey][chainId]) allocation[frameKey][chainId] = [];
193
- allocation[frameKey][chainId][areaIndex] = fill.blockIds;
194
- }
195
- }
196
-
197
- function groupAreasByChain(frames, mdxAreas) {
198
- // mdxAreas come from chromium in DOM order. We need to order by (frame
199
- // sequence position in Press tree, indexInFrame). Use frames[] order.
200
- const frameOrder = new Map();
201
- frames.forEach((f, idx) => frameOrder.set(f.frameKey, idx));
202
- const byChain = new Map();
203
- const sorted = [...mdxAreas].sort((a, b) => {
204
- const fa = frameOrder.get(a.frameKey) ?? Number.MAX_SAFE_INTEGER;
205
- const fb = frameOrder.get(b.frameKey) ?? Number.MAX_SAFE_INTEGER;
206
- if (fa !== fb) return fa - fb;
207
- return a.indexInFrame - b.indexInFrame;
208
- });
209
- for (const area of sorted) {
210
- if (!byChain.has(area.chainId)) byChain.set(area.chainId, []);
211
- byChain.get(area.chainId).push(area);
212
- }
213
- return byChain;
214
- }
215
-
216
- function groupBlockHeights(blockHeights) {
217
- const byChain = new Map();
218
- for (const block of blockHeights) {
219
- if (!byChain.has(block.chainId)) byChain.set(block.chainId, new Map());
220
- byChain.get(block.chainId).set(block.id, block.height);
221
- }
222
- return byChain;
223
- }
224
-
225
- function buildBlockStream(chainSource, heightMap) {
226
- if (!chainSource || !heightMap) return [];
227
- return chainSource
228
- .filter((block) => block.layout !== "attached")
229
- .map((block) => ({
230
- id: block.id,
231
- kind: block.kind,
232
- name: block.name,
233
- height: heightMap.get(block.id) ?? 0,
234
- }));
235
- }
236
-
237
- function* iterateChains(sources) {
238
- for (const source of Object.values(sources)) {
239
- for (const [chainId, blocks] of Object.entries(source.chains)) {
240
- yield [chainId, blocks];
241
- }
242
- }
243
- }
244
-
245
- function uniqueFramesForChain(frames, chainId) {
246
- const set = new Set();
247
- for (const f of frames) {
248
- if (f.mdxAreas.some((a) => a.chainId === chainId)) set.add(f.frameKey);
249
- }
250
- return Math.max(1, set.size);
251
- }
@@ -1,94 +0,0 @@
1
- // Layer 5 — Final Render.
2
- //
3
- // Given the allocation map (frameKey -> chainId -> areaIndex -> blockIds),
4
- // pre-compiles each MdxArea's block subset into React nodes, then renders
5
- // the Press tree one more time with `PressContext.allocation` populated.
6
- // The output HTML is parsed back into per-frame fragments for document.json.
7
-
8
- import React from "react";
9
- import { expandPressTree } from "./press-tree.mjs";
10
- import { compileChainBlocks } from "../sources/mdx-resolver.mjs";
11
-
12
- /**
13
- * @returns {{ frames: Array<{ frameKey, role, chrome, html, blockIds, mdxAreas }>, html: string }}
14
- */
15
- export async function renderFinalPress({
16
- Press,
17
- PressContext,
18
- sources,
19
- hints,
20
- toc,
21
- allocation: blockAllocation, // chainId blockId allocator output: { [frameKey]: { [chainId]: blockIds[][] } }
22
- renderRegistry,
23
- }) {
24
- // 1. Compile React nodes per (frameKey, chainId, areaIndex)
25
- const reactAllocation = await buildReactAllocation(blockAllocation, sources, renderRegistry, toc);
26
-
27
- // 2. Render Press tree with allocation in context
28
- const { html, frames } = expandPressTree({
29
- Press,
30
- PressContext,
31
- sources,
32
- hints,
33
- toc,
34
- allocation: reactAllocation,
35
- });
36
-
37
- // 3. Annotate frame instances with blockIds (for document.json)
38
- const framesWithBlocks = frames.map((frame) => {
39
- const frameBlocks = blockAllocation[frame.frameKey] ?? {};
40
- const blockIds = [];
41
- const mdxAreas = frame.mdxAreas.map((area) => {
42
- const ids = frameBlocks[area.chainId]?.[area.indexInFrame] ?? [];
43
- blockIds.push(...ids);
44
- return {
45
- chainId: area.chainId,
46
- indexInFrame: area.indexInFrame,
47
- blockIds: ids,
48
- };
49
- });
50
- return {
51
- frameKey: frame.frameKey,
52
- role: frame.role,
53
- chrome: frame.chrome,
54
- html: frame.html,
55
- blockIds,
56
- mdxAreas,
57
- };
58
- });
59
-
60
- return { frames: framesWithBlocks, html };
61
- }
62
-
63
- async function buildReactAllocation(blockAllocation, sources, renderRegistry, toc) {
64
- /** @type {Record<string, Record<string, React.ReactNode[]>>} */
65
- const out = {};
66
- const sourceOfChain = new Map();
67
- for (const source of Object.values(sources)) {
68
- for (const chainId of Object.keys(source.chains)) {
69
- sourceOfChain.set(chainId, source.id);
70
- }
71
- }
72
-
73
- for (const [frameKey, chainMap] of Object.entries(blockAllocation)) {
74
- out[frameKey] = {};
75
- for (const [chainId, areaArr] of Object.entries(chainMap)) {
76
- const nodesByArea = [];
77
- for (let areaIndex = 0; areaIndex < areaArr.length; areaIndex++) {
78
- const blockIds = areaArr[areaIndex] ?? [];
79
- if (blockIds.length === 0) {
80
- nodesByArea[areaIndex] = null;
81
- continue;
82
- }
83
- const sourceId = sourceOfChain.get(chainId);
84
- const renderData = renderRegistry.get(sourceId);
85
- const compiled = await compileChainBlocks({ renderData, chainId, blockIds, toc });
86
- nodesByArea[areaIndex] = compiled.map(({ Content }, i) =>
87
- React.createElement(Content, { key: `${areaIndex}-${i}` }),
88
- );
89
- }
90
- out[frameKey][chainId] = nodesByArea;
91
- }
92
- }
93
- return out;
94
- }
@@ -1,302 +0,0 @@
1
- // Layer 3 — Frame Measurement.
2
- //
3
- // Renders the Press tree (from Layer 2) plus a hidden "blocks zone" that
4
- // contains every chain's full content, sends the combined document to
5
- // Chromium, then queries the DOM for:
6
- // - MdxArea capacities (per frameKey + chainId + indexInFrame)
7
- // - Block heights (per chainId + blockId)
8
- //
9
- // The blocks zone uses the same `.page-body` CSS context as the live
10
- // frames so widths and font metrics match.
11
-
12
- import fs from "node:fs/promises";
13
- import path from "node:path";
14
- import React from "react";
15
- import { renderToStaticMarkup } from "react-dom/server";
16
- import { chromium } from "playwright";
17
- import { createCaptionNumberingState, numberCaptionsInHtml } from "../caption-numbering.mjs";
18
- import { compileChainBlocks } from "../sources/mdx-resolver.mjs";
19
-
20
- const DEFAULT_VIEWPORT = { width: 794, height: 1123 };
21
-
22
- // Safety inset applied to measured MdxArea capacities. A small reserve keeps
23
- // content that visually fits "exactly" from clipping due to anti-aliasing,
24
- // line breaks, and rounding.
25
- const CAPACITY_SAFETY_RATIO = 0.04;
26
- const CAPACITY_SAFETY_MAX_PX = 96;
27
-
28
- /**
29
- * @param {object} opts
30
- * @param {string} opts.pressHtml Rendered Press tree HTML (Layer 2).
31
- * @param {Record<string, object>} opts.sources Resolved sources keyed by sourceId.
32
- * @param {Map<string, object>} opts.renderRegistry Internal render data per sourceId.
33
- * @param {string} opts.css Combined CSS for measurement context.
34
- * @param {string=} opts.baseHref Base URL for relative media paths in MDX.
35
- * @param {string=} opts.mediaDir Local media dir for inlining /openpress/media/* assets.
36
- * @param {object=} opts.captionNumbering Caption label formatter options.
37
- * @param {{width:number,height:number}=} opts.viewport
38
- */
39
- export async function measureFrames({
40
- pressHtml,
41
- sources,
42
- renderRegistry,
43
- css = "",
44
- baseHref = "",
45
- mediaDir = "",
46
- captionNumbering = {},
47
- viewport = DEFAULT_VIEWPORT,
48
- }) {
49
- const chainContent = await buildChainContent(sources, renderRegistry, captionNumbering);
50
- const html = await buildMeasurementDocument({ pressHtml, chainContent, css, baseHref, mediaDir });
51
- return runChromiumMeasurement(html, viewport);
52
- }
53
-
54
- async function buildChainContent(sources, renderRegistry, captionNumbering) {
55
- const out = new Map();
56
- const captionState = createCaptionNumberingState();
57
- for (const source of Object.values(sources)) {
58
- for (const [chainId, blocks] of Object.entries(source.chains)) {
59
- const blockIds = blocks.map((b) => b.id);
60
- const renderData = renderRegistry.get(source.id);
61
- const compiled = await compileChainBlocks({ renderData, chainId, blockIds });
62
- const html = compiled
63
- .map(({ Content }, idx) => renderToStaticMarkup(React.createElement(Content, { key: idx })))
64
- .join("");
65
- out.set(
66
- chainId,
67
- chainId.startsWith("toc:") ? html : numberCaptionsInHtml(html, captionNumbering, captionState),
68
- );
69
- }
70
- }
71
- return out;
72
- }
73
-
74
- async function buildMeasurementDocument({ pressHtml, chainContent, css, baseHref, mediaDir }) {
75
- const normalizedPressHtml = await inlineMeasurementMediaUrls(pressHtml, mediaDir);
76
- const blocksZoneParts = [];
77
- for (const [chainId, contentHtml] of chainContent.entries()) {
78
- const normalizedContentHtml = await inlineMeasurementMediaUrls(contentHtml, mediaDir);
79
- const containerTag = chainId.startsWith("toc:") ? "ol" : "div";
80
- const containerClass = chainId.startsWith("toc:") ? ' class="toc-list"' : "";
81
- blocksZoneParts.push(`
82
- <section class="reader-page reader-page--content" data-openpress-measure-frame="${escapeAttr(chainId)}" data-page-kind="content">
83
- <div class="page-frame">
84
- <main class="page-body">
85
- <${containerTag}${containerClass} data-block-measurement-chain="${escapeAttr(chainId)}" style="overflow: visible;">
86
- ${normalizedContentHtml}
87
- </${containerTag}>
88
- </main>
89
- </div>
90
- </section>
91
- `);
92
- }
93
- const blocksZone = blocksZoneParts.join("\n");
94
-
95
- return `<!doctype html>
96
- <html>
97
- <head>
98
- <meta charset="utf-8">
99
- ${baseHref ? `<base href="${escapeAttr(baseHref)}">` : ""}
100
- <style>
101
- body { margin: 0; }
102
- ${css}
103
- /* Reader-page is hidden by default in the workspace theme (only the
104
- is-active page is shown). Override visibility in measurement zones
105
- but do not touch the page-frame display, because the theme uses
106
- CSS grid with grid-template-rows reserving page-header and
107
- page-footer space; we must preserve that layout so MdxArea
108
- inherits the real page-body cell height. */
109
- [data-openpress-frames-zone] .reader-page,
110
- [data-openpress-blocks-zone] .reader-page {
111
- display: block !important;
112
- }
113
- /* MdxArea fills its grid/flex parent so we measure the layout slot,
114
- not the inserted content. */
115
- [data-openpress-frames-zone] [data-openpress-mdx-area="true"] {
116
- display: block;
117
- box-sizing: border-box;
118
- min-height: 0;
119
- align-self: stretch;
120
- overflow: visible;
121
- }
122
- [data-openpress-frames-zone] { position: relative; }
123
- [data-openpress-blocks-zone] { position: fixed; left: -200000px; top: 0; visibility: hidden; pointer-events: none; }
124
- </style>
125
- </head>
126
- <body>
127
- <div data-openpress-frames-zone>${normalizedPressHtml}</div>
128
- <div data-openpress-blocks-zone>${blocksZone}</div>
129
- </body>
130
- </html>`;
131
- }
132
-
133
- async function runChromiumMeasurement(html, viewport) {
134
- const browser = await chromium.launch();
135
- try {
136
- const page = await browser.newPage({ viewport });
137
- await page.setContent(html, { waitUntil: "load" });
138
- // Match the print-ready settle: fonts first (font metrics affect image
139
- // alt-text fallback boxes), then await every image's `complete` AND
140
- // `decode()` so intrinsic sizes are committed before layout, then two
141
- // animation frames so the chromium layout pass observes the final box
142
- // model. Without this, `getBoundingClientRect()` on figures that hold
143
- // images can race the decode and return collapsed heights, causing the
144
- // allocator to pack too many blocks per page.
145
- await page.evaluate(async () => {
146
- if (document.fonts?.ready) await document.fonts.ready;
147
- await Promise.all(Array.from(document.images).map(async (img) => {
148
- if (!img.complete) {
149
- await new Promise((resolve) => {
150
- const settle = () => {
151
- img.removeEventListener("load", settle);
152
- img.removeEventListener("error", settle);
153
- resolve(undefined);
154
- };
155
- img.addEventListener("load", settle, { once: true });
156
- img.addEventListener("error", settle, { once: true });
157
- });
158
- }
159
- await img.decode?.().catch(() => undefined);
160
- }));
161
- await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)));
162
- });
163
-
164
- const mdxAreas = await page.evaluate((safety) => {
165
- const zone = document.querySelector("[data-openpress-frames-zone]");
166
- if (!zone) return [];
167
- const areas = Array.from(zone.querySelectorAll("[data-openpress-mdx-area]"));
168
- const out = [];
169
- for (const el of areas) {
170
- const frame = el.closest("[data-openpress-frame-key]");
171
- if (!frame) continue;
172
- const frameKey = frame.getAttribute("data-openpress-frame-key") || "";
173
- const chainId = el.getAttribute("data-openpress-mdx-area-chain") || "";
174
- const overflow = el.getAttribute("data-openpress-mdx-area-overflow") || "extend";
175
- // Index within this frame's same-chain areas
176
- const sameInFrame = Array.from(frame.querySelectorAll(`[data-openpress-mdx-area][data-openpress-mdx-area-chain="${chainId}"]`));
177
- const indexInFrame = sameInFrame.indexOf(el);
178
- const rect = measuredMdxAreaRect(el);
179
- const inset = Math.min(safety.maxPx, Math.max(0, rect.height * safety.ratio));
180
- const capacity = Math.max(1, rect.height - inset);
181
- out.push({
182
- frameKey,
183
- chainId,
184
- overflow,
185
- indexInFrame,
186
- capacity,
187
- rawHeight: rect.height,
188
- width: rect.width,
189
- });
190
- }
191
- return out;
192
-
193
- function measuredMdxAreaRect(el) {
194
- const rect = el.getBoundingClientRect();
195
- if (rect.height > 1) return rect;
196
- const candidates = [
197
- el.parentElement,
198
- el.closest(".page-body"),
199
- el.closest("[data-openpress-frame-key]")?.querySelector(".page-body"),
200
- ];
201
- for (const candidate of candidates) {
202
- if (!candidate) continue;
203
- const fallback = candidate.getBoundingClientRect();
204
- if (fallback.height > rect.height) {
205
- return {
206
- height: fallback.height,
207
- width: rect.width > 0 ? rect.width : fallback.width,
208
- };
209
- }
210
- }
211
- return rect;
212
- }
213
- }, { ratio: CAPACITY_SAFETY_RATIO, maxPx: CAPACITY_SAFETY_MAX_PX });
214
-
215
- const blockHeights = await page.evaluate(() => {
216
- const zone = document.querySelector("[data-openpress-blocks-zone]");
217
- if (!zone) return [];
218
- const out = [];
219
- for (const chain of zone.querySelectorAll("[data-block-measurement-chain]")) {
220
- const chainId = chain.getAttribute("data-block-measurement-chain") ?? "";
221
- const parentTop = chain.parentElement?.getBoundingClientRect().top ?? chain.getBoundingClientRect().top;
222
- let previousBottom = parentTop;
223
- for (const el of Array.from(chain.querySelectorAll("[data-openpress-block-id]"))) {
224
- if (el.tagName.toLowerCase() === "caption") continue;
225
- if (el.getAttribute("data-openpress-block-layout") === "attached") continue;
226
- const rect = el.getBoundingClientRect();
227
- out.push({
228
- id: el.getAttribute("data-openpress-block-id"),
229
- height: Math.max(rect.height, rect.bottom - previousBottom),
230
- chainId,
231
- });
232
- previousBottom = Math.max(previousBottom, rect.bottom);
233
- }
234
- }
235
- return out;
236
- });
237
-
238
- return { mdxAreas, blockHeights };
239
- } finally {
240
- await browser.close();
241
- }
242
- }
243
-
244
- function escapeAttr(value) {
245
- return String(value).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
246
- }
247
-
248
- async function inlineMeasurementMediaUrls(html, mediaDir) {
249
- if (!mediaDir || !html) return html;
250
- let out = String(html);
251
- const matches = new Set();
252
- for (const match of out.matchAll(/\bsrc=(['"])([^\1]*?)\1/g)) {
253
- const src = match[2];
254
- if (!src) continue;
255
- if (src.startsWith('/openpress/media/')) {
256
- matches.add(src.slice('/openpress/media/'.length));
257
- continue;
258
- }
259
- if (src.startsWith('media/')) {
260
- matches.add(src.slice('media/'.length));
261
- continue;
262
- }
263
- if (src.startsWith('./media/')) {
264
- matches.add(src.slice('./media/'.length));
265
- }
266
- }
267
- for (const rawName of matches) {
268
- const dataUrl = await mediaDataUrl(mediaDir, rawName);
269
- if (!dataUrl) continue;
270
- out = out.replaceAll(`/openpress/media/${rawName}`, dataUrl);
271
- out = out.replaceAll(`media/${rawName}`, dataUrl);
272
- out = out.replaceAll(`./media/${rawName}`, dataUrl);
273
- }
274
- return out;
275
- }
276
-
277
- async function mediaDataUrl(mediaDir, rawName) {
278
- let fileName;
279
- try {
280
- fileName = decodeURIComponent(String(rawName));
281
- } catch {
282
- fileName = String(rawName);
283
- }
284
- if (!fileName || fileName !== path.basename(fileName)) return null;
285
- const filePath = path.join(mediaDir, fileName);
286
- let bytes;
287
- try {
288
- bytes = await fs.readFile(filePath);
289
- } catch {
290
- return null;
291
- }
292
- return `data:${mediaMimeType(fileName)};base64,${bytes.toString("base64")}`;
293
- }
294
-
295
- function mediaMimeType(fileName) {
296
- const ext = path.extname(fileName).toLowerCase();
297
- if (ext === ".svg") return "image/svg+xml";
298
- if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
299
- if (ext === ".gif") return "image/gif";
300
- if (ext === ".webp") return "image/webp";
301
- return "image/png";
302
- }