@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,132 +0,0 @@
1
- // Heading numbering and outline emission for MDX sources.
2
- //
3
- // Pure functions called from `mdx-resolver.mjs` while walking each section's
4
- // compiled blocks. Produces:
5
- // - `data-chapter` / `data-section` / `data-topic` attributes on heading
6
- // blocks (h2/h3/h4)
7
- // - stable IDs on the same blocks
8
- // - outline entries (for TOC + reader navigation)
9
- //
10
- // Numbering state is per-source, advances forward through blocks in source
11
- // order. The caller threads `headingState` between calls.
12
-
13
- /**
14
- * Build a fresh per-source heading state. One state object per call to
15
- * `resolveSource` — counts subsection / topic indices and tracks whether
16
- * this section has already seen its chapter heading.
17
- */
18
- export function createHeadingState() {
19
- return {
20
- hasChapterHeading: false,
21
- subsectionCounter: 0,
22
- topicCounter: 0,
23
- };
24
- }
25
-
26
- /**
27
- * For one block emitted by the MDX compiler, decide whether it is a heading,
28
- * what id/data-* attributes it should carry, and whether it adds an entry to
29
- * the outline. Returns `null` for non-heading blocks.
30
- *
31
- * @param {object} opts
32
- * @param {object} opts.block The compiled block record.
33
- * @param {string} opts.sourceId Source registry key.
34
- * @param {object} opts.section Section descriptor (slug, title, …).
35
- * @param {Array} opts.outlineItems Mutable outline array — entries are pushed onto it.
36
- * @param {number} opts.chapterNumber 1-based chapter index for this section.
37
- * @param {string} opts.chapterLabel Display label, e.g. `"#3"` or `"03"`.
38
- * @param {object} opts.headingState Per-source counter state from `createHeadingState`.
39
- */
40
- export function headingAttributesForBlock({
41
- block,
42
- sourceId,
43
- section,
44
- outlineItems,
45
- chapterNumber,
46
- chapterLabel,
47
- headingState,
48
- }) {
49
- const title = String(block.text ?? "").trim();
50
-
51
- if (block.name === "h2") {
52
- const firstChapterHeading = !headingState.hasChapterHeading;
53
- const id = firstChapterHeading ? `section-${section.slug}` : block.id;
54
- const headingTitle = title || section.title || section.slug;
55
- outlineItems.push({
56
- id: `${sourceId}:${section.slug}:${block.id}`,
57
- tocId: firstChapterHeading
58
- ? `toc-${sourceId}-${section.slug}`
59
- : `toc-${sourceId}-${section.slug}-${block.id}`,
60
- depth: 0,
61
- title: headingTitle,
62
- label: chapterLabel,
63
- sectionSlug: section.slug,
64
- blockId: block.id,
65
- href: `#${id}`,
66
- });
67
- headingState.hasChapterHeading = true;
68
- headingState.subsectionCounter = 0;
69
- headingState.topicCounter = 0;
70
- return {
71
- attributes: {
72
- id,
73
- "data-chapter": chapterLabel,
74
- },
75
- sectionTitle: firstChapterHeading ? headingTitle : undefined,
76
- };
77
- }
78
-
79
- if (block.name === "h3") {
80
- headingState.subsectionCounter += 1;
81
- headingState.topicCounter = 0;
82
- const label = `${chapterNumber}.${headingState.subsectionCounter}`;
83
- outlineItems.push({
84
- id: `${sourceId}:${section.slug}:${block.id}`,
85
- tocId: `toc-${sourceId}-${section.slug}-${block.id}`,
86
- depth: 1,
87
- title: title || `Section ${label}`,
88
- label,
89
- sectionSlug: section.slug,
90
- blockId: block.id,
91
- href: `#${block.id}`,
92
- });
93
- return {
94
- attributes: {
95
- id: block.id,
96
- "data-section": label,
97
- },
98
- };
99
- }
100
-
101
- if (block.name === "h4") {
102
- headingState.topicCounter += 1;
103
- const label = `${chapterNumber}.${Math.max(1, headingState.subsectionCounter)}.${headingState.topicCounter}`;
104
- return {
105
- attributes: {
106
- id: block.id,
107
- "data-topic": label,
108
- },
109
- };
110
- }
111
-
112
- return null;
113
- }
114
-
115
- /**
116
- * Build a fallback single-entry outline when a section has no h2/h3 headings
117
- * the resolver could pick up. The outline still needs one row so the TOC
118
- * doesn't lose the section.
119
- */
120
- export function fallbackOutlineItems({ sourceId, section, chapterLabel, title, blocks }) {
121
- const targetBlock = blocks[0];
122
- return [{
123
- id: `${sourceId}:${section.slug}`,
124
- tocId: `toc-${sourceId}-${section.slug}`,
125
- depth: 0,
126
- title,
127
- label: chapterLabel,
128
- sectionSlug: section.slug,
129
- blockId: targetBlock?.id,
130
- href: `#section-${section.slug}`,
131
- }];
132
- }
@@ -1,439 +0,0 @@
1
- // MDX source resolver — Layer 1 of the Press pipeline.
2
- //
3
- // Takes a normalized `mdxSource()` descriptor and produces:
4
- // 1. A public `ResolvedSource` consumed by `useSource()` in user code.
5
- // 2. A private `RenderRegistry` consumed by Layer 5 to render specific
6
- // block-id subsets into React nodes.
7
- //
8
- // Both halves come from the same MDX compile so block IDs stay consistent.
9
-
10
- import fs from "node:fs/promises";
11
- import path from "node:path";
12
- import React from "react";
13
- import { documentRelativePath, resolveDocumentRelativePath } from "../../runtime/path-utils.mjs";
14
- import { compileMdx } from "../mdx-compile.mjs";
15
- import { createHeadingState, fallbackOutlineItems, headingAttributesForBlock } from "./heading-numbering.mjs";
16
-
17
- const MDX_EXT = ".mdx";
18
-
19
- /**
20
- * Resolve all sources registered in `document/index.tsx`.
21
- *
22
- * @param {object} opts
23
- * @param {Record<string, object>} opts.sources The raw `sources` export.
24
- * @param {string} opts.documentRoot Absolute path to document/.
25
- * @param {Record<string, Function>} opts.globalComponents Pre-resolved global components.
26
- * @returns {Promise<{ resolved: Record<string, object>, renderData: Map<string, object> }>}
27
- */
28
- export async function resolveAllSources({ sources, documentRoot, globalComponents }) {
29
- validateSourcesShape(sources);
30
-
31
- const resolved = {};
32
- const renderData = new Map();
33
-
34
- for (const [sourceId, descriptor] of Object.entries(sources)) {
35
- validateSourceKey(sourceId);
36
- const { resolved: source, renderData: rd } = await resolveSource({
37
- sourceId,
38
- descriptor,
39
- documentRoot,
40
- globalComponents,
41
- });
42
- resolved[sourceId] = source;
43
- renderData.set(sourceId, rd);
44
- }
45
-
46
- return { resolved, renderData };
47
- }
48
-
49
- async function resolveSource({ sourceId, descriptor, documentRoot, globalComponents }) {
50
- if (!descriptor || typeof descriptor !== "object") {
51
- throw new Error(`Source "${sourceId}" descriptor must be an object.`);
52
- }
53
- if (descriptor.type !== "mdx") {
54
- throw new Error(`Source "${sourceId}" type must be "mdx" in v0.6. Got "${descriptor.type}".`);
55
- }
56
-
57
- const sections = await collectSections({ descriptor, documentRoot, sourceId });
58
-
59
- const tree = [];
60
- const outline = [];
61
- const chains = {};
62
- const files = [];
63
- const sectionRenderData = new Map();
64
-
65
- for (let sectionIndex = 0; sectionIndex < sections.length; sectionIndex += 1) {
66
- const section = sections[sectionIndex];
67
- const chainId = `${sourceId}:${section.slug}`;
68
- const blocks = [];
69
- const fileRenderData = [];
70
- const outlineItems = [];
71
- const chapterNumber = sectionIndex + 1;
72
- const chapterLabel = String(chapterNumber).padStart(2, "0");
73
- let resolvedSectionTitle = section.title ?? section.slug;
74
- const headingState = createHeadingState();
75
-
76
- for (const file of section.files) {
77
- const source = await fs.readFile(file.absolutePath, "utf8");
78
- const compiled = await compileMdx({
79
- source,
80
- filePath: file.absolutePath,
81
- components: globalComponents,
82
- chapterSlug: section.slug,
83
- });
84
-
85
- const fileBlockIds = [];
86
- const fileBlockAttributes = {};
87
- for (const block of compiled.blocks) {
88
- const headingAttributes = headingAttributesForBlock({
89
- block,
90
- sourceId,
91
- section,
92
- outlineItems,
93
- chapterNumber,
94
- chapterLabel,
95
- headingState,
96
- });
97
- if (headingAttributes) {
98
- fileBlockAttributes[block.id] = headingAttributes.attributes;
99
- if (headingAttributes.sectionTitle) resolvedSectionTitle = headingAttributes.sectionTitle;
100
- }
101
-
102
- const record = {
103
- id: block.id,
104
- kind: block.kind,
105
- name: block.name,
106
- text: block.text,
107
- layout: block.layout,
108
- chainId,
109
- sectionSlug: section.slug,
110
- path: documentRelative(file.absolutePath, documentRoot),
111
- source: {
112
- file: path.basename(file.absolutePath),
113
- line: block.source?.line,
114
- column: block.source?.column,
115
- endLine: block.source?.endLine,
116
- endColumn: block.source?.endColumn,
117
- },
118
- };
119
- blocks.push(record);
120
- fileBlockIds.push(block.id);
121
- }
122
-
123
- files.push({
124
- path: documentRelative(file.absolutePath, documentRoot),
125
- absolutePath: file.absolutePath,
126
- sectionSlug: section.slug,
127
- });
128
-
129
- fileRenderData.push({
130
- filePath: file.absolutePath,
131
- source,
132
- blockIds: fileBlockIds,
133
- blockAttributes: fileBlockAttributes,
134
- });
135
- }
136
-
137
- chains[chainId] = blocks;
138
- tree.push({
139
- id: section.slug,
140
- slug: section.slug,
141
- title: resolvedSectionTitle,
142
- meta: section.meta ?? {},
143
- });
144
- outline.push(...(outlineItems.length > 0 ? outlineItems : fallbackOutlineItems({
145
- sourceId,
146
- section,
147
- chapterLabel,
148
- title: resolvedSectionTitle,
149
- blocks,
150
- })));
151
-
152
- sectionRenderData.set(section.slug, {
153
- slug: section.slug,
154
- chainId,
155
- contents: fileRenderData,
156
- });
157
- }
158
-
159
- const tocChainId = `toc:${sourceId}`;
160
- const tocBlocks = outline.map((item) => ({
161
- id: item.tocId,
162
- kind: "toc-entry",
163
- name: "toc-entry",
164
- chainId: tocChainId,
165
- sectionSlug: item.sectionSlug,
166
- targetBlockId: item.blockId,
167
- path: "index.tsx",
168
- source: {
169
- file: "index.tsx",
170
- },
171
- title: item.title,
172
- href: item.href,
173
- level: item.depth <= 0 ? 2 : 3,
174
- label: item.label,
175
- }));
176
- const h2TocChainId = `${tocChainId}:h2`;
177
- const h2TocBlocks = tocBlocks.filter((block) => block.level <= 2);
178
- chains[tocChainId] = tocBlocks;
179
- chains[h2TocChainId] = h2TocBlocks;
180
-
181
- return {
182
- resolved: {
183
- id: sourceId,
184
- type: "mdx",
185
- tree,
186
- outline,
187
- chains,
188
- files,
189
- },
190
- renderData: {
191
- sourceId,
192
- sections: sectionRenderData,
193
- tocChains: new Map([[tocChainId, tocBlocks], [h2TocChainId, h2TocBlocks]]),
194
- globalComponents,
195
- },
196
- };
197
- }
198
-
199
- async function collectSections({ descriptor, documentRoot, sourceId }) {
200
- if (descriptor.preset === "section-folders") {
201
- const root = resolveDocumentRelativePath(documentRoot, descriptor.root ?? "chapters", `Source "${sourceId}" section-folders root`);
202
- return collectSectionFolders(root);
203
- }
204
- if (descriptor.preset === "section-files") {
205
- const root = resolveDocumentRelativePath(documentRoot, descriptor.root ?? "content", `Source "${sourceId}" section-files root`);
206
- return collectSectionFiles(root);
207
- }
208
- if (descriptor.preset === "file-list") {
209
- return collectFileList(descriptor.files, documentRoot, sourceId);
210
- }
211
- throw new Error(`Source "${sourceId}" has unknown preset "${descriptor.preset}".`);
212
- }
213
-
214
- async function collectSectionFolders(root) {
215
- const entries = await readDir(root);
216
- const dirs = entries.filter((e) => e.isDirectory()).sort(compareOrderPrefix);
217
- const sections = [];
218
- for (const dir of dirs) {
219
- const dirPath = path.join(root, dir.name);
220
- const contentDir = path.join(dirPath, "content");
221
- const mdxFiles = await listMdxFiles(contentDir);
222
- if (mdxFiles.length === 0) continue;
223
- sections.push({
224
- slug: stripOrderPrefix(dir.name),
225
- title: deriveTitleFromDirName(dir.name),
226
- files: mdxFiles.map((name) => ({ absolutePath: path.join(contentDir, name) })),
227
- });
228
- }
229
- return sections;
230
- }
231
-
232
- async function collectSectionFiles(root) {
233
- const files = await listMdxFiles(root);
234
- return files.map((name) => ({
235
- slug: stripOrderPrefix(stripExtension(name)),
236
- title: deriveTitleFromDirName(stripExtension(name)),
237
- files: [{ absolutePath: path.join(root, name) }],
238
- }));
239
- }
240
-
241
- async function collectFileList(filePaths, documentRoot, sourceId) {
242
- const sections = [];
243
- const slugs = new Set();
244
- for (const rel of filePaths) {
245
- if (typeof rel !== "string" || !rel.trim()) {
246
- throw new Error(`Source "${sourceId}" file-list contains an empty or invalid entry.`);
247
- }
248
- const norm = rel.replace(/^[./]+/, "");
249
- if (rel.includes("..")) {
250
- throw new Error(`Source "${sourceId}" file-list path "${rel}" contains "..", rejected.`);
251
- }
252
- if (!rel.endsWith(MDX_EXT)) {
253
- throw new Error(`Source "${sourceId}" file-list path "${rel}" must end with .mdx.`);
254
- }
255
- const absolute = path.resolve(documentRoot, rel);
256
- const relCheck = path.relative(documentRoot, absolute);
257
- if (relCheck.startsWith("..") || path.isAbsolute(relCheck)) {
258
- throw new Error(`Source "${sourceId}" file-list path "${rel}" escapes the document root.`);
259
- }
260
- const slug = stripOrderPrefix(stripExtension(path.basename(rel)));
261
- if (slugs.has(slug)) {
262
- throw new Error(`Source "${sourceId}" file-list produces duplicate section slug "${slug}".`);
263
- }
264
- slugs.add(slug);
265
- sections.push({
266
- slug,
267
- title: deriveTitleFromDirName(stripExtension(path.basename(rel))),
268
- files: [{ absolutePath: absolute }],
269
- });
270
- }
271
- return sections;
272
- }
273
-
274
- // ---------------------------------------------------------------------------
275
- // Layer 5 helper — render specific blocks for a chain
276
- // ---------------------------------------------------------------------------
277
-
278
- /**
279
- * For a chain, given the block IDs to include, return a list of React nodes
280
- * to inject into MdxArea(s).
281
- *
282
- * @returns {Promise<Array<{ Content: React.FC, blockIds: string[] }>>}
283
- * One entry per source file participating in the chain. Caller can wrap
284
- * each Content in a fragment or distribute across MdxAreas.
285
- */
286
- export async function compileChainBlocks({ renderData, chainId, blockIds, toc = null }) {
287
- const ids = new Set(blockIds);
288
- if (ids.size === 0) return [];
289
- const tocBlocks = renderData?.tocChains?.get(chainId);
290
- if (tocBlocks) {
291
- return compileTocBlocks({ tocBlocks, chainId, blockIds, toc });
292
- }
293
- const section = locateSection(renderData, chainId);
294
- const out = [];
295
- for (const fileData of section.contents) {
296
- const fileIds = fileData.blockIds.filter((id) => ids.has(id));
297
- if (fileIds.length === 0) continue;
298
- const compiled = await compileMdx({
299
- source: fileData.source,
300
- filePath: fileData.filePath,
301
- components: renderData.globalComponents,
302
- chapterSlug: section.slug,
303
- includeBlockIds: fileIds,
304
- blockAttributes: fileData.blockAttributes,
305
- });
306
- out.push({ Content: compiled.Content, blockIds: fileIds });
307
- }
308
- return out;
309
- }
310
-
311
- function compileTocBlocks({ tocBlocks, chainId, blockIds, toc }) {
312
- const ids = new Set(blockIds);
313
- const pageNumberByBlockId = new Map();
314
- for (const entry of toc?.[chainId] ?? []) {
315
- pageNumberByBlockId.set(entry.blockId, entry.pageNumber);
316
- }
317
- const selected = tocBlocks.filter((block) => ids.has(block.id));
318
- return selected.map((block) => ({
319
- Content: function TocEntry() {
320
- const pageNumber = pageNumberByBlockId.get(block.id);
321
- const pageLabel = Number.isFinite(pageNumber) ? String(pageNumber).padStart(2, "0") : "00";
322
- const className = `toc-level-${block.level}`;
323
- return React.createElement(
324
- "li",
325
- {
326
- className,
327
- "data-openpress-block-id": block.id,
328
- "data-openpress-object-id": createBlockObjectEntityId(block.id),
329
- "data-openpress-toc-entry": block.sectionSlug,
330
- },
331
- React.createElement(
332
- "a",
333
- {
334
- href: block.href,
335
- "data-openpress-anchor": block.href.replace(/^#/, ""),
336
- "data-openpress-target-page-index": Number.isFinite(pageNumber) ? String(pageNumber - 1) : undefined,
337
- },
338
- React.createElement("span", { className: "toc-index", "data-toc-index": block.label }, block.label),
339
- React.createElement("span", { className: "toc-title" }, block.title),
340
- React.createElement("span", { className: "toc-page" }, pageLabel),
341
- ),
342
- );
343
- },
344
- blockIds: [block.id],
345
- }));
346
- }
347
-
348
- function locateSection(renderData, chainId) {
349
- if (!renderData) {
350
- throw new Error(`No render data for chainId "${chainId}".`);
351
- }
352
- for (const section of renderData.sections.values()) {
353
- if (section.chainId === chainId) return section;
354
- }
355
- throw new Error(`No section found for chainId "${chainId}" in source "${renderData.sourceId}".`);
356
- }
357
-
358
- function createObjectEntityId(kind, ...parts) {
359
- return [kind, ...parts.map((part) => encodeURIComponent(String(part)))].join(":");
360
- }
361
-
362
- function createBlockObjectEntityId(blockId) {
363
- return createObjectEntityId("mdx-block", blockId);
364
- }
365
-
366
- // ---------------------------------------------------------------------------
367
- // Validation
368
- // ---------------------------------------------------------------------------
369
-
370
- const SOURCE_KEY_RE = /^[a-z][a-z0-9-]*$/;
371
-
372
- function validateSourcesShape(sources) {
373
- if (sources == null) return;
374
- if (typeof sources !== "object" || Array.isArray(sources)) {
375
- throw new Error("`export const sources` must be an object literal of sourceId -> descriptor.");
376
- }
377
- }
378
-
379
- function validateSourceKey(sourceId) {
380
- if (!SOURCE_KEY_RE.test(sourceId)) {
381
- throw new Error(
382
- `Source key "${sourceId}" is invalid. Source keys must match /^[a-z][a-z0-9-]*$/ ` +
383
- `(lowercase letter, then lowercase letters, digits, or hyphens). ` +
384
- `Colons are reserved for chain ID separators.`,
385
- );
386
- }
387
- }
388
-
389
- // ---------------------------------------------------------------------------
390
- // IO helpers
391
- // ---------------------------------------------------------------------------
392
-
393
- async function readDir(dir) {
394
- try {
395
- return await fs.readdir(dir, { withFileTypes: true });
396
- } catch (error) {
397
- if (error?.code === "ENOENT") return [];
398
- throw error;
399
- }
400
- }
401
-
402
- async function listMdxFiles(dir) {
403
- const entries = await readDir(dir);
404
- return entries
405
- .filter((e) => e.isFile() && e.name.endsWith(MDX_EXT))
406
- .map((e) => e.name)
407
- .sort((a, b) => a.localeCompare(b));
408
- }
409
-
410
- function compareOrderPrefix(a, b) {
411
- const left = orderKey(a.name);
412
- const right = orderKey(b.name);
413
- if (left.order !== right.order) return left.order - right.order;
414
- return left.rest.localeCompare(right.rest);
415
- }
416
-
417
- function orderKey(name) {
418
- const match = name.match(/^(\d+)[-_]?(.*)$/);
419
- if (!match) return { order: Number.POSITIVE_INFINITY, rest: name };
420
- return { order: Number.parseInt(match[1], 10), rest: match[2] || name };
421
- }
422
-
423
- function stripOrderPrefix(name) {
424
- return name.replace(/^\d+[-_]?/, "");
425
- }
426
-
427
- function stripExtension(name) {
428
- return name.replace(/\.[^.]+$/, "");
429
- }
430
-
431
- function deriveTitleFromDirName(name) {
432
- return stripOrderPrefix(name)
433
- .split(/[-_]/)
434
- .filter(Boolean)
435
- .map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1))
436
- .join(" ");
437
- }
438
-
439
- const documentRelative = documentRelativePath;