@open-press/core 0.8.0 → 1.0.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 (64) hide show
  1. package/README.md +6 -3
  2. package/engine/cli.mjs +8 -8
  3. package/engine/commands/_shared.mjs +37 -15
  4. package/engine/commands/image.mjs +29 -0
  5. package/engine/commands/skills-sync.mjs +71 -0
  6. package/engine/commands/typecheck.mjs +63 -1
  7. package/engine/commands/upgrade.mjs +3 -3
  8. package/engine/document-export.mjs +1 -1
  9. package/engine/output/chrome-pdf.mjs +92 -0
  10. package/engine/output/static-server.mjs +48 -9
  11. package/engine/react/comment-marker.mjs +13 -13
  12. package/engine/react/document-entry.mjs +35 -28
  13. package/engine/react/document-export.mjs +309 -170
  14. package/engine/react/mdx-compile.mjs +30 -0
  15. package/engine/react/measurement-css.mjs +21 -0
  16. package/engine/react/object-entities.mjs +85 -0
  17. package/engine/react/pagination/allocator.mjs +48 -3
  18. package/engine/react/pagination.mjs +1 -1
  19. package/engine/react/pipeline/allocate.mjs +31 -65
  20. package/engine/react/pipeline/frame-measurement.mjs +4 -0
  21. package/engine/react/press-tree-inspection.mjs +172 -0
  22. package/engine/react/sources/mdx-resolver.mjs +1 -1
  23. package/engine/react/style-discovery.mjs +22 -4
  24. package/engine/runtime/config.d.mts +8 -0
  25. package/engine/runtime/config.mjs +57 -60
  26. package/engine/runtime/file-utils.mjs +9 -1
  27. package/engine/runtime/page-geometry.mjs +131 -0
  28. package/engine/runtime/source-text-tools.mjs +1 -1
  29. package/engine/runtime/source-workspace.mjs +12 -3
  30. package/engine/runtime/validation.mjs +19 -10
  31. package/package.json +3 -5
  32. package/src/openpress/app/OpenPressApp.tsx +173 -17
  33. package/src/openpress/app/OpenPressRuntime.tsx +10 -2
  34. package/src/openpress/app/WorkspaceGalleryPage.tsx +219 -0
  35. package/src/openpress/core/Frame.tsx +20 -7
  36. package/src/openpress/core/FrameContext.tsx +2 -0
  37. package/src/openpress/core/Press.tsx +25 -4
  38. package/src/openpress/core/Workspace.tsx +36 -0
  39. package/src/openpress/core/index.tsx +10 -3
  40. package/src/openpress/core/primitives.tsx +48 -1
  41. package/src/openpress/core/types.ts +86 -41
  42. package/src/openpress/core/useSource.ts +1 -1
  43. package/src/openpress/document-model/documentTypes.ts +9 -0
  44. package/src/openpress/document-model/index.ts +1 -0
  45. package/src/openpress/document-model/objectEntityModel.ts +4 -0
  46. package/src/openpress/document-model/workspaceManifestModel.ts +57 -0
  47. package/src/openpress/mdx/index.ts +15 -7
  48. package/src/openpress/reader/PageThumbnailsPanel.tsx +168 -0
  49. package/src/openpress/reader/index.ts +1 -0
  50. package/src/openpress/workbench/Workbench.tsx +120 -21
  51. package/src/openpress/workbench/actions/ExportImageControl.tsx +96 -0
  52. package/src/openpress/workbench/actions/SearchControl.tsx +3 -3
  53. package/src/openpress/workbench/actions/index.ts +1 -0
  54. package/src/openpress/workbench/inspector/useInspectorComments.ts +7 -1
  55. package/src/openpress/workbench/panels/PendingCommentsPanel.tsx +5 -1
  56. package/src/openpress/workbench/project/ProjectEntryPanel.tsx +4 -2
  57. package/src/openpress/workbench/workbenchFormatters.ts +2 -2
  58. package/src/styles/openpress/reader-runtime.css +9 -0
  59. package/src/styles/openpress/workbench-panels.css +113 -0
  60. package/src/styles/openpress/workspace-gallery.css +300 -0
  61. package/src/styles/openpress.css +1 -0
  62. package/tsconfig.json +1 -1
  63. package/engine/commands/init.mjs +0 -24
  64. package/engine/init.mjs +0 -90
@@ -1,6 +1,6 @@
1
1
  // Layer 1 — Document entry loader.
2
2
  //
3
- // Loads `document/index.tsx`, validates it exports a Press component as
3
+ // Loads `press/index.tsx`, validates it exports a Press component as
4
4
  // default, reads optional `config` and `sources` named exports, and sets
5
5
  // up the vite SSR server with `@open-press/core` aliases (including the
6
6
  // subpaths `/mdx` and `/manuscript`).
@@ -12,7 +12,8 @@ import { fileURLToPath } from "node:url";
12
12
  import react from "@vitejs/plugin-react";
13
13
  import ts from "typescript";
14
14
  import { createServer as createViteServer } from "vite";
15
- import { normalizeConfig } from "../runtime/config.mjs";
15
+ import { loadConfig } from "../runtime/config.mjs";
16
+ import { inspectPressTree } from "./press-tree-inspection.mjs";
16
17
 
17
18
  const ENGINE_REACT_DIR = path.dirname(fileURLToPath(import.meta.url));
18
19
  const FRAMEWORK_ROOT = path.resolve(ENGINE_REACT_DIR, "..", "..");
@@ -24,10 +25,17 @@ const REACT_PACKAGE_ROOT = path.join(FRAMEWORK_ROOT, "node_modules", "react");
24
25
  const require = createRequire(import.meta.url);
25
26
  const REACT_EXPORT_NAMES = Object.keys(require("react")).filter((name) => /^[A-Za-z_$][\w$]*$/.test(name));
26
27
 
28
+ // 1.0 contract: the document entry lives at press/index.tsx.
29
+ async function resolveEntryPath(workspaceRoot) {
30
+ const candidate = path.join(workspaceRoot, "press", "index.tsx");
31
+ if (await fileExists(candidate)) return candidate;
32
+ return null;
33
+ }
34
+
27
35
  export async function loadReactDocumentEntry(root = ".", { server: externalServer } = {}) {
28
36
  const workspaceRoot = path.resolve(root);
29
- const entryPath = path.join(workspaceRoot, "document", "index.tsx");
30
- if (!(await fileExists(entryPath))) return null;
37
+ const entryPath = await resolveEntryPath(workspaceRoot);
38
+ if (!entryPath) return null;
31
39
 
32
40
  const source = await fs.readFile(entryPath, "utf8");
33
41
  assertNoObviousTopLevelSideEffects(source, entryPath);
@@ -44,19 +52,35 @@ export async function loadReactDocumentEntry(root = ".", { server: externalServe
44
52
  // export pipeline throws separately if it's missing when actually needed.
45
53
  const Press = typeof mod.default === "function" ? mod.default : null;
46
54
 
47
- const config = normalizeReactDocumentConfig(workspaceRoot, entryPath, mod.config);
48
- const sources = mod.sources ?? {};
49
- if (sources && (typeof sources !== "object" || Array.isArray(sources))) {
50
- throw new Error(
51
- `OpenPress document entry ${entryPath} \`sources\` export must be an object literal (or omitted).`,
55
+ // Inspect the JSX tree returned by the user's default export to
56
+ // pull <Workspace> / <Press> props declared inline. The 1.0 contract
57
+ // treats workspaces uniformly as "array of Press children" the
58
+ // single-doc case is just length 1.
59
+ let inspection = { workspaceProps: {}, presses: [], wrappedInWorkspace: false };
60
+ if (Press) {
61
+ const coreModule = await ownServer.ssrLoadModule(
62
+ path.join(FRAMEWORK_ROOT, "src", "openpress", "core", "index.tsx"),
52
63
  );
64
+ inspection = inspectPressTree({
65
+ UserComponent: Press,
66
+ PRESS_MARKER: coreModule.PRESS_MARKER,
67
+ WORKSPACE_MARKER: coreModule.WORKSPACE_MARKER,
68
+ });
53
69
  }
54
70
 
71
+ // Workspace-level config (deploy, pdf, captionNumbering defaults)
72
+ // comes from package.json "openpress" via loadConfig. Each Press
73
+ // overlays its own metadata via JSX props at export time.
74
+ const config = await loadConfig(workspaceRoot);
75
+
55
76
  return {
56
77
  entryPath,
57
78
  config,
58
79
  Press,
59
- sources,
80
+ presses: inspection.presses,
81
+ workspaceProps: inspection.workspaceProps,
82
+ pressCount: inspection.presses.length,
83
+ wrappedInWorkspace: inspection.wrappedInWorkspace,
60
84
  };
61
85
  } finally {
62
86
  if (!externalServer) await ownServer.close();
@@ -80,7 +104,7 @@ export async function createReactSsrServer(workspaceRoot = ".") {
80
104
  { find: "@open-press/core/manuscript", replacement: MANUSCRIPT_ENTRY },
81
105
  { find: "@open-press/core/numbering", replacement: NUMBERING_ENTRY },
82
106
  { find: "@open-press/core", replacement: CORE_ENTRY },
83
- { find: "@/components", replacement: path.join(resolvedWorkspaceRoot, "document", "components") },
107
+ { find: "@/components", replacement: path.join(resolvedWorkspaceRoot, "press", "components") },
84
108
  ],
85
109
  },
86
110
  optimizeDeps: {
@@ -250,23 +274,6 @@ function isFileSystemModule(moduleName) {
250
274
  return moduleName === "fs" || moduleName === "node:fs" || moduleName === "fs/promises" || moduleName === "node:fs/promises";
251
275
  }
252
276
 
253
- function normalizeReactDocumentConfig(workspaceRoot, entryPath, config) {
254
- if (config != null && (typeof config !== "object" || Array.isArray(config))) {
255
- throw new Error("OpenPress React document entry `config` export must be an object when provided.");
256
- }
257
- const rawConfig = config ?? {};
258
- const paths = rawConfig.paths ?? {};
259
- return normalizeConfig(workspaceRoot, {
260
- ...rawConfig,
261
- documentDir: rawConfig.documentDir ?? paths.documentDir ?? "document",
262
- sourceDir: rawConfig.sourceDir ?? paths.chaptersDir ?? paths.sourceDir ?? "chapters",
263
- componentsDir: rawConfig.componentsDir ?? paths.componentsDir ?? "components",
264
- mediaDir: rawConfig.mediaDir ?? paths.mediaDir ?? "media",
265
- themeDir: rawConfig.themeDir ?? paths.themeDir ?? "theme",
266
- designDoc: rawConfig.designDoc ?? paths.designDoc ?? "design.md",
267
- }, entryPath);
268
- }
269
-
270
277
  async function fileExists(filePath) {
271
278
  try {
272
279
  const stat = await fs.stat(filePath);
@@ -6,8 +6,11 @@
6
6
  import fs from "node:fs/promises";
7
7
  import path from "node:path";
8
8
  import { pathToFileURL } from "node:url";
9
+ import React from "react";
9
10
  import { documentRelativePath, pageToBlock } from "../output/page-block.mjs";
10
11
  import { syncPublicAssets } from "../output/public-assets.mjs";
12
+ import { pageGeometryToTheme } from "../runtime/page-geometry.mjs";
13
+ import { normalizePageGeometry } from "../runtime/page-geometry.mjs";
11
14
  import { createCaptionNumberingState, numberCaptionsInHtml } from "./caption-numbering.mjs";
12
15
  import { buildSectionScopedCss } from "./section-css.mjs";
13
16
  import { CORE_ENTRY, createReactSsrServer, loadReactDocumentEntry } from "./document-entry.mjs";
@@ -50,7 +53,11 @@ export async function exportReactDocument(root = ".", { syncAssets = true } = {}
50
53
  }
51
54
 
52
55
  // Discover workspace for component scope and chapter-scoped style files.
53
- const workspace = await discoverSectionStyles(workspaceRoot, entry.config);
56
+ // Pass every Press's resolved section-folders root so per-Press chapter
57
+ // folders (e.g. press/userstory/chapters/) are all picked up — the
58
+ // workspace can host more than one chapter root.
59
+ const sectionRoots = collectSectionRoots(entry.presses, entry.config.paths.documentRoot);
60
+ const workspace = await discoverSectionStyles(workspaceRoot, entry.config, { sectionRoots });
54
61
  const coreAuthorComponents = {};
55
62
  for (const name of ["MediaFigure", "ImageFigure"]) {
56
63
  if (typeof coreModule[name] === "function") coreAuthorComponents[name] = coreModule[name];
@@ -60,199 +67,309 @@ export async function exportReactDocument(root = ".", { syncAssets = true } = {}
60
67
  ...(await loadComponentModules(server, workspace.globalComponents ?? [])),
61
68
  };
62
69
 
63
- // Resolve sources.
64
- const documentRoot = entry.config.paths.documentRoot;
65
- const { resolved: sources, renderData: renderRegistry } = await resolveAllSources({
66
- sources: entry.sources,
67
- documentRoot,
68
- globalComponents,
69
- });
70
+ // Build measurement CSS once at the workspace level — shared by every
71
+ // Press inside the Workspace.
72
+ const measurementCss = await buildReactMeasurementCss(workspaceRoot, entry.config, workspace);
70
73
 
71
- // Build measurement CSS.
72
- const css = await buildReactMeasurementCss(workspaceRoot, entry.config, workspace);
73
-
74
- // Iterative allocation loop.
75
- let hints = null;
76
- let allocation = null;
77
- let lastFrames = null;
78
- let warnings = [];
79
- for (let iteration = 0; iteration < MAX_ITERATIONS; iteration++) {
80
- const { html, frames } = expandPressTree({
81
- Press: entry.Press,
82
- PressContext,
83
- sources,
84
- hints,
85
- });
86
- lastFrames = frames;
87
- validateAllChainsKnown(frames, sources);
88
- const measurement = await measureFrames({
89
- pressHtml: html,
90
- sources,
91
- renderRegistry,
92
- css,
93
- baseHref: pathToFileURL(`${documentRoot}${path.sep}`).href,
94
- mediaDir: path.join(documentRoot, "media"),
95
- captionNumbering: entry.config.captionNumbering,
74
+ // Write chapter-scoped CSS once (workspace shared). Every per-press
75
+ // readerDocument references the same file via "/openpress/chapter-scoped.css".
76
+ const chapterCss = await buildSectionScopedCss(workspace);
77
+ const sharedStyles = [];
78
+ await fs.mkdir(entry.config.paths.publicDir, { recursive: true });
79
+ if (chapterCss.trim()) {
80
+ await fs.writeFile(path.join(entry.config.paths.publicDir, "chapter-scoped.css"), chapterCss, "utf8");
81
+ sharedStyles.push({
82
+ kind: "chapter-scoped-css",
83
+ href: "/openpress/chapter-scoped.css",
84
+ path: "chapter-scoped.css",
96
85
  });
97
- const alloc = allocateChains({
98
- frames,
99
- mdxAreas: measurement.mdxAreas,
100
- blockHeights: measurement.blockHeights,
101
- sources,
86
+ }
87
+
88
+ // Iterate every Press declared inside <Workspace>. Single-doc
89
+ // workspaces just have length-1 here; the code path is uniform.
90
+ const pressResults = [];
91
+ for (const press of entry.presses) {
92
+ const result = await exportSinglePress({
93
+ press,
94
+ entry,
95
+ workspaceRoot,
96
+ server,
97
+ coreModule,
98
+ PressContext,
99
+ workspace,
100
+ globalComponents,
101
+ measurementCss,
102
+ sharedStyles,
102
103
  });
103
- if (process.env.OPENPRESS_DEBUG_ALLOC) {
104
- const sample = measurement.mdxAreas
105
- .slice(0, 5)
106
- .map((a) => `${a.frameKey}#${a.indexInFrame} cap=${a.capacity.toFixed(0)} raw=${(a.rawHeight ?? 0).toFixed(0)}`);
107
- const blocks = measurement.blockHeights
108
- .slice(0, 8)
109
- .map((b) => `${b.id} h=${b.height.toFixed(0)}`);
110
- process.stderr.write(`[allocator iter ${iteration}]\n`);
111
- process.stderr.write(` mdxAreas[0..4]: ${sample.join(" | ")}\n`);
112
- process.stderr.write(` blocks[0..7]: ${blocks.join(" | ")}\n`);
113
- process.stderr.write(` hints: ${JSON.stringify(alloc.hints.totalPagesPerChain)}\n`);
114
- if (alloc.warnings.length > 0) {
115
- process.stderr.write(` warnings: ${JSON.stringify(alloc.warnings)}\n`);
116
- }
117
- }
118
- if (hintsEqual(hints, alloc.hints)) {
119
- allocation = alloc.allocation;
120
- warnings = alloc.warnings;
121
- break;
122
- }
123
- hints = alloc.hints;
104
+ pressResults.push(result);
124
105
  }
125
- if (allocation == null) {
126
- throw new Error(
127
- `Allocation did not converge after ${MAX_ITERATIONS} iterations. ` +
128
- `This usually means a chain keeps growing without fitting; check MdxArea capacities and block heights.`,
129
- );
106
+
107
+ // Build workspace.json — one entry per Press. The reader fetches
108
+ // this first to decide between gallery (length > 1) and direct
109
+ // load (length 1).
110
+ const workspaceManifest = {
111
+ version: 1,
112
+ name: typeof entry.workspaceProps?.name === "string" && entry.workspaceProps.name.trim()
113
+ ? entry.workspaceProps.name.trim()
114
+ : null,
115
+ presses: pressResults.map((r) => ({
116
+ slug: r.slug,
117
+ title: r.readerDocument.meta.title,
118
+ page: r.readerDocument.theme ?? null,
119
+ pageCount: r.pageCount,
120
+ documentUrl: r.documentUrl,
121
+ })),
122
+ };
123
+ const workspacePath = path.join(entry.config.paths.publicDir, "workspace.json");
124
+ await fs.writeFile(workspacePath, JSON.stringify(workspaceManifest, null, 2), "utf8");
125
+
126
+ if (syncAssets) {
127
+ await syncPublicAssets(workspaceRoot, entry.config.paths.publicDir, entry.config);
130
128
  }
131
129
 
132
- const toc = buildTocContext({ sources, frames: lastFrames ?? [], allocation });
130
+ const primary = pressResults[0];
131
+ return {
132
+ documentPath: primary?.documentPath,
133
+ pageCount: primary?.pageCount ?? 0,
134
+ document: primary?.readerDocument,
135
+ presses: pressResults,
136
+ };
137
+ } finally {
138
+ await server.close();
139
+ }
140
+ }
141
+
142
+ // Render one Press from the Workspace into its own document.json.
143
+ // Called once per <Press> child; single-doc workspaces just call this
144
+ // once with the only Press. Returns the per-press summary the
145
+ // workspace manifest is built from.
146
+ async function exportSinglePress({
147
+ press,
148
+ entry,
149
+ workspaceRoot,
150
+ server,
151
+ coreModule,
152
+ PressContext,
153
+ workspace,
154
+ globalComponents,
155
+ measurementCss,
156
+ sharedStyles,
157
+ }) {
158
+ const slug = typeof press.metadata?.slug === "string" && press.metadata.slug.trim()
159
+ ? press.metadata.slug.trim()
160
+ : "";
161
+
162
+ // Effective config for this press: workspace config with per-press
163
+ // metadata overlaid. Press JSX page prop wins over the workspace page.
164
+ const effectiveConfig = applyPressOverridesToConfig(entry.config, press.metadata);
165
+ const documentRoot = effectiveConfig.paths.documentRoot;
166
+
167
+ // Resolve sources for this press. The 1.0 contract reads them from
168
+ // <Press sources={[...]}>; the v0.x legacy path uses the synthesized
169
+ // record from `export const sources`.
170
+ const sourcesRecord = press.sources ?? {};
171
+ const { resolved: sources, renderData: renderRegistry } = await resolveAllSources({
172
+ sources: sourcesRecord,
173
+ documentRoot,
174
+ globalComponents,
175
+ });
176
+
177
+ // Component the render pipeline drives. For Press elements captured
178
+ // by inspection (1.0 contract), wrap the captured element in a thin
179
+ // function component. For legacy projects without inspection data,
180
+ // fall back to the user's whole default export.
181
+ const PressComponent = press.element
182
+ ? () => press.element
183
+ : entry.Press;
133
184
 
134
- // Final render.
135
- const final = await renderFinalPress({
136
- Press: entry.Press,
185
+ // Iterative allocation loop (identical to v0.x — paginates until the
186
+ // hints stabilise).
187
+ let hints = null;
188
+ let allocation = null;
189
+ let lastFrames = null;
190
+ let warnings = [];
191
+ for (let iteration = 0; iteration < MAX_ITERATIONS; iteration++) {
192
+ const { html, frames } = expandPressTree({
193
+ Press: PressComponent,
137
194
  PressContext,
138
195
  sources,
139
196
  hints,
140
- toc,
141
- allocation,
197
+ });
198
+ lastFrames = frames;
199
+ validateAllChainsKnown(frames, sources);
200
+ const measurement = await measureFrames({
201
+ pressHtml: html,
202
+ sources,
142
203
  renderRegistry,
204
+ css: measurementCss,
205
+ baseHref: pathToFileURL(`${documentRoot}${path.sep}`).href,
206
+ mediaDir: path.join(documentRoot, "media"),
207
+ captionNumbering: effectiveConfig.captionNumbering,
143
208
  });
144
-
145
- // Write chapter-scoped CSS (under section-scoped rules but filename
146
- // unchanged for now).
147
- const chapterCss = await buildSectionScopedCss(workspace);
148
- const styles = [];
149
- await fs.mkdir(entry.config.paths.publicDir, { recursive: true });
150
- if (chapterCss.trim()) {
151
- await fs.writeFile(path.join(entry.config.paths.publicDir, "chapter-scoped.css"), chapterCss, "utf8");
152
- styles.push({
153
- kind: "chapter-scoped-css",
154
- href: "/openpress/chapter-scoped.css",
155
- path: "chapter-scoped.css",
156
- });
209
+ const alloc = allocateChains({
210
+ frames,
211
+ mdxAreas: measurement.mdxAreas,
212
+ blockHeights: measurement.blockHeights,
213
+ sources,
214
+ });
215
+ if (process.env.OPENPRESS_DEBUG_ALLOC) {
216
+ const sample = measurement.mdxAreas
217
+ .slice(0, 5)
218
+ .map((a) => `${a.frameKey}#${a.indexInFrame} cap=${a.capacity.toFixed(0)} raw=${(a.rawHeight ?? 0).toFixed(0)}`);
219
+ const blocks = measurement.blockHeights
220
+ .slice(0, 8)
221
+ .map((b) => `${b.id} h=${b.height.toFixed(0)}`);
222
+ process.stderr.write(`[allocator press=${slug || "(root)"} iter ${iteration}]\n`);
223
+ process.stderr.write(` mdxAreas[0..4]: ${sample.join(" | ")}\n`);
224
+ process.stderr.write(` blocks[0..7]: ${blocks.join(" | ")}\n`);
225
+ process.stderr.write(` hints: ${JSON.stringify(alloc.hints.totalPagesPerChain)}\n`);
226
+ if (alloc.warnings.length > 0) {
227
+ process.stderr.write(` warnings: ${JSON.stringify(alloc.warnings)}\n`);
228
+ }
229
+ }
230
+ if (hintsEqual(hints, alloc.hints)) {
231
+ allocation = alloc.allocation;
232
+ warnings = alloc.warnings;
233
+ break;
157
234
  }
235
+ hints = alloc.hints;
236
+ }
237
+ if (allocation == null) {
238
+ throw new Error(
239
+ `Allocation did not converge after ${MAX_ITERATIONS} iterations (press="${slug || "(root)"}"). ` +
240
+ `This usually means a chain keeps growing without fitting; check MdxArea capacities and block heights.`,
241
+ );
242
+ }
158
243
 
159
- // Build document.json. The reader filters blocks by kind === "htmlPage",
160
- // so wrap each frame through `pageToBlock` to inherit that contract.
161
- const blockMap = {};
162
- const captionState = createCaptionNumberingState();
163
- const blocks = final.frames.map((frame, index) => {
164
- const source = {
165
- file: "index.tsx",
166
- path: "document/index.tsx",
167
- kind: frame.role ?? "manuscript.content",
168
- slug: frame.frameKey,
169
- sectionIndex: index + 1,
170
- };
171
- const html = numberCaptionsInHtml(frame.html, entry.config.captionNumbering, captionState);
172
- for (const id of collectFrameBlockIds(frame.blockIds, html)) {
173
- blockMap[id] = { id, pageIndex: index, pageNumber: index + 1, frameKey: frame.frameKey };
174
- }
175
- const block = pageToBlock(index, html, source, entry.config, {
176
- idPrefix: "openpress-page",
177
- anchorPrefix: "page",
178
- titleFallback: "Page",
179
- });
180
- return {
181
- ...block,
182
- frameKey: frame.frameKey,
183
- role: frame.role ?? null,
184
- chrome: frame.chrome ?? true,
185
- blockIds: frame.blockIds,
186
- };
244
+ const toc = buildTocContext({ sources, frames: lastFrames ?? [], allocation });
245
+
246
+ const final = await renderFinalPress({
247
+ Press: PressComponent,
248
+ PressContext,
249
+ sources,
250
+ hints,
251
+ toc,
252
+ allocation,
253
+ renderRegistry,
254
+ });
255
+
256
+ // Build the reader's document.json. Same shape as v0.x; the only
257
+ // change is metadata.title comes from the per-press Press JSX prop.
258
+ const blockMap = {};
259
+ const captionState = createCaptionNumberingState();
260
+ const blocks = final.frames.map((frame, index) => {
261
+ const source = {
262
+ file: "index.tsx",
263
+ path: slug ? `press/${slug}/index.tsx` : "press/index.tsx",
264
+ kind: frame.role ?? "manuscript.content",
265
+ slug: frame.frameKey,
266
+ sectionIndex: index + 1,
267
+ };
268
+ const html = numberCaptionsInHtml(frame.html, effectiveConfig.captionNumbering, captionState);
269
+ for (const id of collectFrameBlockIds(frame.blockIds, html)) {
270
+ blockMap[id] = { id, pageIndex: index, pageNumber: index + 1, frameKey: frame.frameKey };
271
+ }
272
+ const block = pageToBlock(index, html, source, effectiveConfig, {
273
+ idPrefix: "openpress-page",
274
+ anchorPrefix: "page",
275
+ titleFallback: "Page",
187
276
  });
277
+ return {
278
+ ...block,
279
+ frameKey: frame.frameKey,
280
+ role: frame.role ?? null,
281
+ chrome: frame.chrome ?? true,
282
+ blockIds: frame.blockIds,
283
+ };
284
+ });
188
285
 
189
- // Enrich blockMap with source records from resolved chains so comment
190
- // tooling can resolve block IDs back to MDX positions.
191
- const sourceBlockIndex = buildSourceBlockIndex(sources);
192
- for (const id of Object.keys(blockMap)) {
193
- const sourceRecord = sourceBlockIndex.get(id);
194
- if (sourceRecord) {
195
- blockMap[id] = {
196
- ...blockMap[id],
197
- kind: sourceRecord.kind,
198
- name: sourceRecord.name,
199
- path: sourceRecord.path,
200
- source: sourceRecord.source,
201
- chainId: sourceRecord.chainId,
202
- sectionSlug: sourceRecord.sectionSlug,
203
- };
204
- }
286
+ const sourceBlockIndex = buildSourceBlockIndex(sources);
287
+ for (const id of Object.keys(blockMap)) {
288
+ const sourceRecord = sourceBlockIndex.get(id);
289
+ if (sourceRecord) {
290
+ blockMap[id] = {
291
+ ...blockMap[id],
292
+ kind: sourceRecord.kind,
293
+ name: sourceRecord.name,
294
+ path: sourceRecord.path,
295
+ source: sourceRecord.source,
296
+ chainId: sourceRecord.chainId,
297
+ sectionSlug: sourceRecord.sectionSlug,
298
+ };
205
299
  }
300
+ }
206
301
 
207
- const objectEntities = buildObjectEntities({
208
- frames: final.frames.map((frame, index) => ({ ...frame, pageIndex: index })),
209
- blocks,
210
- blockMap,
211
- });
302
+ const objectEntities = buildObjectEntities({
303
+ frames: final.frames.map((frame, index) => ({ ...frame, pageIndex: index })),
304
+ blocks,
305
+ blockMap,
306
+ });
212
307
 
213
- const readerDocument = {
214
- meta: {
215
- title: trimmedString(entry.config.title) ?? "Untitled Document",
216
- subtitle: trimmedString(entry.config.subtitle) ?? "",
217
- organization: trimmedString(entry.config.organization) ?? "",
218
- workspaceLabel: trimmedString(entry.config.workspaceLabel) ?? "",
219
- version: "openpress-press-tree-v1",
220
- },
221
- source: {
222
- type: "openpress-press-tree-mdx",
223
- contentDir: documentRelativePath(entry.config, entry.config.sourceDir),
224
- editable: true,
225
- editMode: "source-mdx",
226
- styles,
227
- blockMap,
228
- objectEntities,
229
- frames: final.frames.map((frame, index) => ({
230
- frameKey: frame.frameKey,
231
- role: frame.role ?? null,
232
- pageIndex: index,
233
- mdxAreas: frame.mdxAreas.map((area) => ({
234
- chainId: area.chainId,
235
- indexInFrame: area.indexInFrame,
236
- blockIds: area.blockIds,
237
- })),
308
+ const readerDocument = {
309
+ meta: {
310
+ title: trimmedString(effectiveConfig.title) ?? "Untitled Document",
311
+ subtitle: trimmedString(effectiveConfig.subtitle) ?? "",
312
+ organization: trimmedString(effectiveConfig.organization) ?? "",
313
+ workspaceLabel: trimmedString(effectiveConfig.workspaceLabel) ?? "",
314
+ version: "openpress-press-tree-v1",
315
+ },
316
+ theme: pageGeometryToTheme(effectiveConfig.page),
317
+ source: {
318
+ type: "openpress-press-tree-mdx",
319
+ contentDir: documentRelativePath(effectiveConfig, effectiveConfig.sourceDir),
320
+ editable: true,
321
+ editMode: "source-mdx",
322
+ styles: sharedStyles,
323
+ blockMap,
324
+ objectEntities,
325
+ frames: final.frames.map((frame, index) => ({
326
+ frameKey: frame.frameKey,
327
+ role: frame.role ?? null,
328
+ pageIndex: index,
329
+ mdxAreas: frame.mdxAreas.map((area) => ({
330
+ chainId: area.chainId,
331
+ indexInFrame: area.indexInFrame,
332
+ blockIds: area.blockIds,
238
333
  })),
239
- chains: Object.keys(sources).flatMap((id) => Object.keys(sources[id].chains)),
240
- warnings,
241
- },
242
- blocks,
243
- };
334
+ })),
335
+ chains: Object.keys(sources).flatMap((id) => Object.keys(sources[id].chains)),
336
+ warnings,
337
+ },
338
+ blocks,
339
+ };
244
340
 
245
- const documentPath = path.join(entry.config.paths.publicDir, "document.json");
246
- await fs.writeFile(documentPath, JSON.stringify(readerDocument, null, 2), "utf8");
341
+ // Output path: empty slug → root /openpress/document.json (legacy
342
+ // single-Press shape). Non-empty slug → /openpress/<slug>/document.json.
343
+ const pressOutputDir = slug
344
+ ? path.join(effectiveConfig.paths.publicDir, slug)
345
+ : effectiveConfig.paths.publicDir;
346
+ await fs.mkdir(pressOutputDir, { recursive: true });
347
+ const documentPath = path.join(pressOutputDir, "document.json");
348
+ await fs.writeFile(documentPath, JSON.stringify(readerDocument, null, 2), "utf8");
247
349
 
248
- if (syncAssets) {
249
- await syncPublicAssets(workspaceRoot, entry.config.paths.publicDir, entry.config);
250
- }
350
+ return {
351
+ slug,
352
+ documentPath,
353
+ documentUrl: slug ? `/openpress/${slug}/document.json` : "/openpress/document.json",
354
+ readerDocument,
355
+ pageCount: blocks.length,
356
+ };
357
+ }
251
358
 
252
- return { documentPath, pageCount: blocks.length, document: readerDocument };
253
- } finally {
254
- await server.close();
359
+ // Apply per-Press JSX prop overrides onto the workspace-level config.
360
+ // Returns a new config object — the original is untouched so other
361
+ // presses in the same workspace get a clean base.
362
+ function applyPressOverridesToConfig(workspaceConfig, pressMetadata) {
363
+ if (!pressMetadata) return workspaceConfig;
364
+ const out = { ...workspaceConfig };
365
+ if (pressMetadata.title) out.title = pressMetadata.title;
366
+ if (pressMetadata.page !== undefined) {
367
+ out.page = normalizePageGeometry(pressMetadata.page);
255
368
  }
369
+ if (pressMetadata.captionNumbering !== undefined) {
370
+ out.captionNumbering = { ...workspaceConfig.captionNumbering, ...pressMetadata.captionNumbering };
371
+ }
372
+ return out;
256
373
  }
257
374
 
258
375
  async function loadComponentModules(server, components) {
@@ -371,3 +488,25 @@ function trimmedString(value) {
371
488
  const trimmed = value.trim();
372
489
  return trimmed ? trimmed : null;
373
490
  }
491
+
492
+ // Walk every Press's mdxSource descriptors and collect the absolute
493
+ // path each section-folders root resolves to. discoverSectionStyles
494
+ // iterates these to find section-scoped CSS across a multi-Press
495
+ // workspace where chapters live under per-Press subfolders.
496
+ function collectSectionRoots(presses, documentRoot) {
497
+ const roots = new Set();
498
+ for (const press of presses ?? []) {
499
+ const sources = press?.sources;
500
+ if (!sources || typeof sources !== "object") continue;
501
+ for (const descriptor of Object.values(sources)) {
502
+ if (descriptor?.type !== "mdx") continue;
503
+ if (descriptor?.preset !== "section-folders") continue;
504
+ const rel = typeof descriptor.root === "string" && descriptor.root.trim()
505
+ ? descriptor.root.trim()
506
+ : "chapters";
507
+ roots.add(path.resolve(documentRoot, rel));
508
+ }
509
+ }
510
+ return [...roots];
511
+ }
512
+