@open-press/core 0.6.0 → 0.7.1

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 (71) hide show
  1. package/README.md +9 -5
  2. package/engine/cli.mjs +2 -5
  3. package/engine/commands/_shared.mjs +4 -4
  4. package/engine/commands/deploy.mjs +1 -1
  5. package/engine/commands/inspect.mjs +3 -3
  6. package/engine/commands/replace.mjs +1 -1
  7. package/engine/commands/search.mjs +1 -1
  8. package/engine/commands/upgrade.mjs +47 -5
  9. package/engine/commands/validate.mjs +2 -2
  10. package/engine/document-export.mjs +1 -1
  11. package/engine/{chrome-pdf.mjs → output/chrome-pdf.mjs} +1 -2
  12. package/engine/{deploy-sync.mjs → output/deploy-sync.mjs} +2 -2
  13. package/engine/{fonts.mjs → output/fonts.mjs} +1 -1
  14. package/engine/{public-assets.mjs → output/public-assets.mjs} +2 -2
  15. package/engine/{static-server.mjs → output/static-server.mjs} +2 -2
  16. package/engine/react/caption-numbering.mjs +73 -0
  17. package/engine/react/comment-marker.mjs +54 -10
  18. package/engine/react/document-entry.mjs +124 -64
  19. package/engine/react/document-export.mjs +266 -310
  20. package/engine/react/mdx-compile.mjs +214 -3
  21. package/engine/react/measurement-css.mjs +3 -3
  22. package/engine/react/pagination/allocator.mjs +122 -0
  23. package/engine/react/pagination/regions.mjs +81 -0
  24. package/engine/react/pagination.mjs +9 -121
  25. package/engine/react/pipeline/allocate.mjs +248 -0
  26. package/engine/react/pipeline/final-render.mjs +94 -0
  27. package/engine/react/pipeline/frame-measurement.mjs +300 -0
  28. package/engine/react/pipeline/press-tree.mjs +135 -0
  29. package/engine/react/project-asset-endpoint.mjs +2 -2
  30. package/engine/react/{chapter-css.mjs → section-css.mjs} +12 -9
  31. package/engine/react/sources/heading-numbering.mjs +132 -0
  32. package/engine/react/sources/mdx-resolver.mjs +441 -0
  33. package/engine/react/{workspace-discovery.mjs → style-discovery.mjs} +29 -40
  34. package/engine/{config.mjs → runtime/config.mjs} +15 -0
  35. package/engine/{file-utils.mjs → runtime/file-utils.mjs} +1 -1
  36. package/engine/{inspection.mjs → runtime/inspection.mjs} +3 -4
  37. package/engine/{source-text-tools.mjs → runtime/source-text-tools.mjs} +24 -7
  38. package/engine/runtime/source-workspace.mjs +186 -0
  39. package/engine/{validation.mjs → runtime/validation.mjs} +19 -17
  40. package/package.json +5 -2
  41. package/src/openpress/anchorMap.ts +27 -0
  42. package/src/openpress/core/Frame.tsx +80 -0
  43. package/src/openpress/core/FrameContext.tsx +19 -0
  44. package/src/openpress/core/MdxArea.tsx +35 -0
  45. package/src/openpress/core/Press.tsx +34 -0
  46. package/src/openpress/core/index.tsx +34 -15
  47. package/src/openpress/core/primitives.tsx +23 -0
  48. package/src/openpress/core/types.ts +131 -19
  49. package/src/openpress/core/useSource.ts +28 -0
  50. package/src/openpress/manuscript/index.tsx +196 -0
  51. package/src/openpress/mdx/index.ts +88 -0
  52. package/src/openpress/numbering/index.ts +294 -0
  53. package/src/openpress/publicPage.tsx +4 -186
  54. package/src/openpress/reactDocumentMetadata.ts +2 -16
  55. package/src/openpress/types.ts +0 -16
  56. package/src/openpress/workbench.tsx +2 -36
  57. package/src/styles/openpress/responsive.css +0 -14
  58. package/tsconfig.json +4 -1
  59. package/vite.config.ts +10 -3
  60. package/engine/commands/migrate-to-react.mjs +0 -27
  61. package/engine/page-renderer.mjs +0 -217
  62. package/engine/react/migrate-to-react.mjs +0 -355
  63. package/engine/source-workspace.mjs +0 -76
  64. package/src/openpress/core/basePages.tsx +0 -87
  65. package/src/openpress/pagination.ts +0 -845
  66. /package/engine/{chrome-pdf.d.mts → output/chrome-pdf.d.mts} +0 -0
  67. /package/engine/{katex-assets.mjs → output/katex-assets.mjs} +0 -0
  68. /package/engine/{page-block.mjs → output/page-block.mjs} +0 -0
  69. /package/engine/{pdf-media.mjs → output/pdf-media.mjs} +0 -0
  70. /package/engine/{config.d.mts → runtime/config.d.mts} +0 -0
  71. /package/engine/{issue-report.mjs → runtime/issue-report.mjs} +0 -0
@@ -0,0 +1,441 @@
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 { compileMdx } from "../mdx-compile.mjs";
14
+ import { createHeadingState, fallbackOutlineItems, headingAttributesForBlock } from "./heading-numbering.mjs";
15
+
16
+ const MDX_EXT = ".mdx";
17
+
18
+ /**
19
+ * Resolve all sources registered in `document/index.tsx`.
20
+ *
21
+ * @param {object} opts
22
+ * @param {Record<string, object>} opts.sources The raw `sources` export.
23
+ * @param {string} opts.documentRoot Absolute path to document/.
24
+ * @param {Record<string, Function>} opts.globalComponents Pre-resolved global components.
25
+ * @returns {Promise<{ resolved: Record<string, object>, renderData: Map<string, object> }>}
26
+ */
27
+ export async function resolveAllSources({ sources, documentRoot, globalComponents }) {
28
+ validateSourcesShape(sources);
29
+
30
+ const resolved = {};
31
+ const renderData = new Map();
32
+
33
+ for (const [sourceId, descriptor] of Object.entries(sources)) {
34
+ validateSourceKey(sourceId);
35
+ const { resolved: source, renderData: rd } = await resolveSource({
36
+ sourceId,
37
+ descriptor,
38
+ documentRoot,
39
+ globalComponents,
40
+ });
41
+ resolved[sourceId] = source;
42
+ renderData.set(sourceId, rd);
43
+ }
44
+
45
+ return { resolved, renderData };
46
+ }
47
+
48
+ async function resolveSource({ sourceId, descriptor, documentRoot, globalComponents }) {
49
+ if (!descriptor || typeof descriptor !== "object") {
50
+ throw new Error(`Source "${sourceId}" descriptor must be an object.`);
51
+ }
52
+ if (descriptor.type !== "mdx") {
53
+ throw new Error(`Source "${sourceId}" type must be "mdx" in v0.6. Got "${descriptor.type}".`);
54
+ }
55
+
56
+ const sections = await collectSections({ descriptor, documentRoot, sourceId });
57
+
58
+ const tree = [];
59
+ const outline = [];
60
+ const chains = {};
61
+ const files = [];
62
+ const sectionRenderData = new Map();
63
+
64
+ for (let sectionIndex = 0; sectionIndex < sections.length; sectionIndex += 1) {
65
+ const section = sections[sectionIndex];
66
+ const chainId = `${sourceId}:${section.slug}`;
67
+ const blocks = [];
68
+ const fileRenderData = [];
69
+ const outlineItems = [];
70
+ const chapterNumber = sectionIndex + 1;
71
+ const chapterLabel = String(chapterNumber).padStart(2, "0");
72
+ let resolvedSectionTitle = section.title ?? section.slug;
73
+ const headingState = createHeadingState();
74
+
75
+ for (const file of section.files) {
76
+ const source = await fs.readFile(file.absolutePath, "utf8");
77
+ const compiled = await compileMdx({
78
+ source,
79
+ filePath: file.absolutePath,
80
+ components: globalComponents,
81
+ chapterSlug: section.slug,
82
+ });
83
+
84
+ const fileBlockIds = [];
85
+ const fileBlockAttributes = {};
86
+ for (const block of compiled.blocks) {
87
+ const headingAttributes = headingAttributesForBlock({
88
+ block,
89
+ sourceId,
90
+ section,
91
+ outlineItems,
92
+ chapterNumber,
93
+ chapterLabel,
94
+ headingState,
95
+ });
96
+ if (headingAttributes) {
97
+ fileBlockAttributes[block.id] = headingAttributes.attributes;
98
+ if (headingAttributes.sectionTitle) resolvedSectionTitle = headingAttributes.sectionTitle;
99
+ }
100
+
101
+ const record = {
102
+ id: block.id,
103
+ kind: block.kind,
104
+ name: block.name,
105
+ text: block.text,
106
+ chainId,
107
+ sectionSlug: section.slug,
108
+ path: documentRelative(file.absolutePath, documentRoot),
109
+ source: {
110
+ file: path.basename(file.absolutePath),
111
+ line: block.source?.line,
112
+ column: block.source?.column,
113
+ endLine: block.source?.endLine,
114
+ endColumn: block.source?.endColumn,
115
+ },
116
+ };
117
+ blocks.push(record);
118
+ fileBlockIds.push(block.id);
119
+ }
120
+
121
+ files.push({
122
+ path: documentRelative(file.absolutePath, documentRoot),
123
+ absolutePath: file.absolutePath,
124
+ sectionSlug: section.slug,
125
+ });
126
+
127
+ fileRenderData.push({
128
+ filePath: file.absolutePath,
129
+ source,
130
+ blockIds: fileBlockIds,
131
+ blockAttributes: fileBlockAttributes,
132
+ });
133
+ }
134
+
135
+ chains[chainId] = blocks;
136
+ tree.push({
137
+ id: section.slug,
138
+ slug: section.slug,
139
+ title: resolvedSectionTitle,
140
+ meta: section.meta ?? {},
141
+ });
142
+ outline.push(...(outlineItems.length > 0 ? outlineItems : fallbackOutlineItems({
143
+ sourceId,
144
+ section,
145
+ chapterLabel,
146
+ title: resolvedSectionTitle,
147
+ blocks,
148
+ })));
149
+
150
+ sectionRenderData.set(section.slug, {
151
+ slug: section.slug,
152
+ chainId,
153
+ contents: fileRenderData,
154
+ });
155
+ }
156
+
157
+ const tocChainId = `toc:${sourceId}`;
158
+ const tocBlocks = outline.map((item) => ({
159
+ id: item.tocId,
160
+ kind: "toc-entry",
161
+ name: "toc-entry",
162
+ chainId: tocChainId,
163
+ sectionSlug: item.sectionSlug,
164
+ targetBlockId: item.blockId,
165
+ path: "index.tsx",
166
+ source: {
167
+ file: "index.tsx",
168
+ },
169
+ title: item.title,
170
+ href: item.href,
171
+ level: item.depth <= 0 ? 2 : 3,
172
+ label: item.label,
173
+ }));
174
+ const h2TocChainId = `${tocChainId}:h2`;
175
+ const h2TocBlocks = tocBlocks.filter((block) => block.level <= 2);
176
+ chains[tocChainId] = tocBlocks;
177
+ chains[h2TocChainId] = h2TocBlocks;
178
+
179
+ return {
180
+ resolved: {
181
+ id: sourceId,
182
+ type: "mdx",
183
+ tree,
184
+ outline,
185
+ chains,
186
+ files,
187
+ },
188
+ renderData: {
189
+ sourceId,
190
+ sections: sectionRenderData,
191
+ tocChains: new Map([[tocChainId, tocBlocks], [h2TocChainId, h2TocBlocks]]),
192
+ globalComponents,
193
+ },
194
+ };
195
+ }
196
+
197
+ async function collectSections({ descriptor, documentRoot, sourceId }) {
198
+ if (descriptor.preset === "section-folders") {
199
+ const root = resolveDocumentRelativePath(documentRoot, descriptor.root ?? "chapters", `Source "${sourceId}" section-folders root`);
200
+ return collectSectionFolders(root);
201
+ }
202
+ if (descriptor.preset === "section-files") {
203
+ const root = resolveDocumentRelativePath(documentRoot, descriptor.root ?? "content", `Source "${sourceId}" section-files root`);
204
+ return collectSectionFiles(root);
205
+ }
206
+ if (descriptor.preset === "file-list") {
207
+ return collectFileList(descriptor.files, documentRoot, sourceId);
208
+ }
209
+ throw new Error(`Source "${sourceId}" has unknown preset "${descriptor.preset}".`);
210
+ }
211
+
212
+ async function collectSectionFolders(root) {
213
+ const entries = await readDir(root);
214
+ const dirs = entries.filter((e) => e.isDirectory()).sort(compareOrderPrefix);
215
+ const sections = [];
216
+ for (const dir of dirs) {
217
+ const dirPath = path.join(root, dir.name);
218
+ const contentDir = path.join(dirPath, "content");
219
+ const mdxFiles = await listMdxFiles(contentDir);
220
+ if (mdxFiles.length === 0) continue;
221
+ sections.push({
222
+ slug: stripOrderPrefix(dir.name),
223
+ title: deriveTitleFromDirName(dir.name),
224
+ files: mdxFiles.map((name) => ({ absolutePath: path.join(contentDir, name) })),
225
+ });
226
+ }
227
+ return sections;
228
+ }
229
+
230
+ async function collectSectionFiles(root) {
231
+ const files = await listMdxFiles(root);
232
+ return files.map((name) => ({
233
+ slug: stripOrderPrefix(stripExtension(name)),
234
+ title: deriveTitleFromDirName(stripExtension(name)),
235
+ files: [{ absolutePath: path.join(root, name) }],
236
+ }));
237
+ }
238
+
239
+ async function collectFileList(filePaths, documentRoot, sourceId) {
240
+ const sections = [];
241
+ const slugs = new Set();
242
+ for (const rel of filePaths) {
243
+ if (typeof rel !== "string" || !rel.trim()) {
244
+ throw new Error(`Source "${sourceId}" file-list contains an empty or invalid entry.`);
245
+ }
246
+ const norm = rel.replace(/^[./]+/, "");
247
+ if (rel.includes("..")) {
248
+ throw new Error(`Source "${sourceId}" file-list path "${rel}" contains "..", rejected.`);
249
+ }
250
+ if (!rel.endsWith(MDX_EXT)) {
251
+ throw new Error(`Source "${sourceId}" file-list path "${rel}" must end with .mdx.`);
252
+ }
253
+ const absolute = path.resolve(documentRoot, rel);
254
+ const relCheck = path.relative(documentRoot, absolute);
255
+ if (relCheck.startsWith("..") || path.isAbsolute(relCheck)) {
256
+ throw new Error(`Source "${sourceId}" file-list path "${rel}" escapes the document root.`);
257
+ }
258
+ const slug = stripOrderPrefix(stripExtension(path.basename(rel)));
259
+ if (slugs.has(slug)) {
260
+ throw new Error(`Source "${sourceId}" file-list produces duplicate section slug "${slug}".`);
261
+ }
262
+ slugs.add(slug);
263
+ sections.push({
264
+ slug,
265
+ title: deriveTitleFromDirName(stripExtension(path.basename(rel))),
266
+ files: [{ absolutePath: absolute }],
267
+ });
268
+ }
269
+ return sections;
270
+ }
271
+
272
+ // ---------------------------------------------------------------------------
273
+ // Layer 5 helper — render specific blocks for a chain
274
+ // ---------------------------------------------------------------------------
275
+
276
+ /**
277
+ * For a chain, given the block IDs to include, return a list of React nodes
278
+ * to inject into MdxArea(s).
279
+ *
280
+ * @returns {Promise<Array<{ Content: React.FC, blockIds: string[] }>>}
281
+ * One entry per source file participating in the chain. Caller can wrap
282
+ * each Content in a fragment or distribute across MdxAreas.
283
+ */
284
+ export async function compileChainBlocks({ renderData, chainId, blockIds, toc = null }) {
285
+ const ids = new Set(blockIds);
286
+ if (ids.size === 0) return [];
287
+ const tocBlocks = renderData?.tocChains?.get(chainId);
288
+ if (tocBlocks) {
289
+ return compileTocBlocks({ tocBlocks, chainId, blockIds, toc });
290
+ }
291
+ const section = locateSection(renderData, chainId);
292
+ const out = [];
293
+ for (const fileData of section.contents) {
294
+ const fileIds = fileData.blockIds.filter((id) => ids.has(id));
295
+ if (fileIds.length === 0) continue;
296
+ const compiled = await compileMdx({
297
+ source: fileData.source,
298
+ filePath: fileData.filePath,
299
+ components: renderData.globalComponents,
300
+ chapterSlug: section.slug,
301
+ includeBlockIds: fileIds,
302
+ blockAttributes: fileData.blockAttributes,
303
+ });
304
+ out.push({ Content: compiled.Content, blockIds: fileIds });
305
+ }
306
+ return out;
307
+ }
308
+
309
+ function compileTocBlocks({ tocBlocks, chainId, blockIds, toc }) {
310
+ const ids = new Set(blockIds);
311
+ const pageNumberByBlockId = new Map();
312
+ for (const entry of toc?.[chainId] ?? []) {
313
+ pageNumberByBlockId.set(entry.blockId, entry.pageNumber);
314
+ }
315
+ const selected = tocBlocks.filter((block) => ids.has(block.id));
316
+ return selected.map((block) => ({
317
+ Content: function TocEntry() {
318
+ const pageNumber = pageNumberByBlockId.get(block.id);
319
+ const pageLabel = Number.isFinite(pageNumber) ? String(pageNumber).padStart(2, "0") : "00";
320
+ const className = `toc-level-${block.level}`;
321
+ return React.createElement(
322
+ "li",
323
+ {
324
+ className,
325
+ "data-openpress-block-id": block.id,
326
+ "data-openpress-toc-entry": block.sectionSlug,
327
+ },
328
+ React.createElement(
329
+ "a",
330
+ {
331
+ href: block.href,
332
+ "data-openpress-anchor": block.href.replace(/^#/, ""),
333
+ "data-openpress-target-page-index": Number.isFinite(pageNumber) ? String(pageNumber - 1) : undefined,
334
+ },
335
+ React.createElement("span", { className: "toc-index", "data-toc-index": block.label }, block.label),
336
+ React.createElement("span", { className: "toc-title" }, block.title),
337
+ React.createElement("span", { className: "toc-page" }, pageLabel),
338
+ ),
339
+ );
340
+ },
341
+ blockIds: [block.id],
342
+ }));
343
+ }
344
+
345
+ function locateSection(renderData, chainId) {
346
+ if (!renderData) {
347
+ throw new Error(`No render data for chainId "${chainId}".`);
348
+ }
349
+ for (const section of renderData.sections.values()) {
350
+ if (section.chainId === chainId) return section;
351
+ }
352
+ throw new Error(`No section found for chainId "${chainId}" in source "${renderData.sourceId}".`);
353
+ }
354
+
355
+ // ---------------------------------------------------------------------------
356
+ // Validation
357
+ // ---------------------------------------------------------------------------
358
+
359
+ const SOURCE_KEY_RE = /^[a-z][a-z0-9-]*$/;
360
+
361
+ function validateSourcesShape(sources) {
362
+ if (sources == null) return;
363
+ if (typeof sources !== "object" || Array.isArray(sources)) {
364
+ throw new Error("`export const sources` must be an object literal of sourceId -> descriptor.");
365
+ }
366
+ }
367
+
368
+ function validateSourceKey(sourceId) {
369
+ if (!SOURCE_KEY_RE.test(sourceId)) {
370
+ throw new Error(
371
+ `Source key "${sourceId}" is invalid. Source keys must match /^[a-z][a-z0-9-]*$/ ` +
372
+ `(lowercase letter, then lowercase letters, digits, or hyphens). ` +
373
+ `Colons are reserved for chain ID separators.`,
374
+ );
375
+ }
376
+ }
377
+
378
+ // ---------------------------------------------------------------------------
379
+ // IO helpers
380
+ // ---------------------------------------------------------------------------
381
+
382
+ async function readDir(dir) {
383
+ try {
384
+ return await fs.readdir(dir, { withFileTypes: true });
385
+ } catch (error) {
386
+ if (error?.code === "ENOENT") return [];
387
+ throw error;
388
+ }
389
+ }
390
+
391
+ async function listMdxFiles(dir) {
392
+ const entries = await readDir(dir);
393
+ return entries
394
+ .filter((e) => e.isFile() && e.name.endsWith(MDX_EXT))
395
+ .map((e) => e.name)
396
+ .sort((a, b) => a.localeCompare(b));
397
+ }
398
+
399
+ function compareOrderPrefix(a, b) {
400
+ const left = orderKey(a.name);
401
+ const right = orderKey(b.name);
402
+ if (left.order !== right.order) return left.order - right.order;
403
+ return left.rest.localeCompare(right.rest);
404
+ }
405
+
406
+ function orderKey(name) {
407
+ const match = name.match(/^(\d+)[-_]?(.*)$/);
408
+ if (!match) return { order: Number.POSITIVE_INFINITY, rest: name };
409
+ return { order: Number.parseInt(match[1], 10), rest: match[2] || name };
410
+ }
411
+
412
+ function stripOrderPrefix(name) {
413
+ return name.replace(/^\d+[-_]?/, "");
414
+ }
415
+
416
+ function stripExtension(name) {
417
+ return name.replace(/\.[^.]+$/, "");
418
+ }
419
+
420
+ function deriveTitleFromDirName(name) {
421
+ return stripOrderPrefix(name)
422
+ .split(/[-_]/)
423
+ .filter(Boolean)
424
+ .map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1))
425
+ .join(" ");
426
+ }
427
+
428
+ function documentRelative(absolutePath, documentRoot) {
429
+ return path.relative(documentRoot, absolutePath).split(path.sep).join("/");
430
+ }
431
+
432
+ function resolveDocumentRelativePath(documentRoot, rel, label) {
433
+ if (typeof rel !== "string" || !rel.trim()) throw new Error(`${label} must be a non-empty document-relative path.`);
434
+ if (rel.includes("..")) throw new Error(`${label} contains "..", rejected.`);
435
+ const absolutePath = path.resolve(documentRoot, rel);
436
+ const relCheck = path.relative(documentRoot, absolutePath);
437
+ if (relCheck.startsWith("..") || path.isAbsolute(relCheck)) {
438
+ throw new Error(`${label} escapes the document root.`);
439
+ }
440
+ return absolutePath;
441
+ }
@@ -1,52 +1,52 @@
1
1
  import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
3
 
4
+ // Style discovery — only used to find per-section CSS files for the
5
+ // section-folders preset. MDX content discovery lives in `sources/mdx-resolver`.
6
+ // This module exists because section-scoped CSS (`[data-section-id]`) needs
7
+ // to know which section slugs exist before the source descriptor pass.
8
+
4
9
  const COMPONENT_EXT = ".tsx";
5
- const CHAPTER_ENTRY = "chapter.tsx";
6
10
 
7
- export async function discoverReactWorkspace(root = ".", config = {}) {
11
+ export async function discoverSectionStyles(root = ".", config = {}) {
8
12
  const workspaceRoot = path.resolve(root);
9
13
  const documentRoot = config.paths?.documentRoot ?? path.join(workspaceRoot, "document");
10
14
  const componentsRoot = config.paths?.componentsDir ?? path.join(documentRoot, "components");
11
- const chaptersRoot = config.paths?.chaptersDir ?? config.paths?.sourceDir ?? path.join(documentRoot, "chapters");
15
+ const sectionsRoot = config.paths?.chaptersDir ?? config.paths?.sourceDir ?? path.join(documentRoot, "chapters");
12
16
  const globalComponents = await discoverComponents(componentsRoot, documentRoot, "global");
13
- const chapters = await discoverChapters(documentRoot, chaptersRoot, globalComponents);
17
+ const sections = await discoverSections(documentRoot, sectionsRoot);
14
18
 
15
19
  return {
16
20
  root: workspaceRoot,
17
21
  documentRoot,
18
22
  globalComponents,
19
- chapters,
23
+ sections,
24
+ // Back-compat: `chapters` alias for callers that still expect the old shape.
25
+ chapters: sections,
20
26
  };
21
27
  }
22
28
 
23
- async function discoverChapters(documentRoot, chaptersDir, globalComponents) {
24
- const entries = await readDirectoryEntries(chaptersDir);
25
- const chapterDirs = entries.filter((entry) => entry.isDirectory()).sort(compareChapterDirectories);
29
+ async function discoverSections(documentRoot, sectionsDir) {
30
+ const entries = await readDirectoryEntries(sectionsDir);
31
+ const sectionDirs = entries.filter((entry) => entry.isDirectory()).sort(compareSectionDirectories);
26
32
 
27
- const chapters = [];
28
- for (const entry of chapterDirs) {
29
- const chapterPath = path.join(chaptersDir, entry.name);
30
- const chapterEntryPath = path.join(chapterPath, CHAPTER_ENTRY);
31
- const localComponents = await discoverComponents(path.join(chapterPath, "components"), documentRoot, "chapter");
32
- const contentFiles = await discoverContentFiles(path.join(chapterPath, "content"), documentRoot);
33
- const styleFiles = await discoverStyleFiles(path.join(chapterPath, "styles"), documentRoot);
34
- const chapterEntry = (await fileExists(chapterEntryPath)) ? pathRecord(chapterEntryPath, documentRoot) : null;
33
+ const sections = [];
34
+ for (const entry of sectionDirs) {
35
+ const sectionPath = path.join(sectionsDir, entry.name);
36
+ const contentFiles = await discoverContentFiles(path.join(sectionPath, "content"), documentRoot);
37
+ const styleFiles = await discoverStyleFiles(path.join(sectionPath, "styles"), documentRoot);
35
38
 
36
- chapters.push({
39
+ sections.push({
37
40
  directoryName: entry.name,
38
- slug: chapterSlugFromDirectory(entry.name),
39
- absolutePath: chapterPath,
40
- documentPath: documentRelativePath(chapterPath, documentRoot),
41
- chapterEntry,
41
+ slug: sectionSlugFromDirectory(entry.name),
42
+ absolutePath: sectionPath,
43
+ documentPath: documentRelativePath(sectionPath, documentRoot),
42
44
  contentFiles,
43
45
  styleFiles,
44
- localComponents,
45
- componentScope: createComponentScope(globalComponents, localComponents),
46
46
  });
47
47
  }
48
48
 
49
- return chapters;
49
+ return sections;
50
50
  }
51
51
 
52
52
  async function discoverComponents(componentsDir, documentRoot, scope) {
@@ -87,17 +87,6 @@ async function discoverFilesByExtension(directory, documentRoot, extension) {
87
87
  .map((entry) => pathRecord(path.join(directory, entry.name), documentRoot));
88
88
  }
89
89
 
90
- function createComponentScope(globalComponents, localComponents) {
91
- const scope = {};
92
- for (const component of globalComponents) {
93
- scope[component.name] = component;
94
- }
95
- for (const component of localComponents) {
96
- scope[component.name] = component;
97
- }
98
- return scope;
99
- }
100
-
101
90
  function componentRecord(name, absolutePath, documentRoot, scope) {
102
91
  return {
103
92
  name,
@@ -117,14 +106,14 @@ function documentRelativePath(absolutePath, documentRoot) {
117
106
  return path.relative(documentRoot, absolutePath).split(path.sep).join("/");
118
107
  }
119
108
 
120
- function compareChapterDirectories(a, b) {
121
- const left = chapterSortKey(a.name);
122
- const right = chapterSortKey(b.name);
109
+ function compareSectionDirectories(a, b) {
110
+ const left = sectionSortKey(a.name);
111
+ const right = sectionSortKey(b.name);
123
112
  if (left.order !== right.order) return left.order - right.order;
124
113
  return left.name.localeCompare(right.name);
125
114
  }
126
115
 
127
- function chapterSortKey(directoryName) {
116
+ function sectionSortKey(directoryName) {
128
117
  const match = directoryName.match(/^(\d+)[-_]?(.*)$/);
129
118
  if (!match) {
130
119
  return { order: Number.POSITIVE_INFINITY, name: directoryName };
@@ -132,7 +121,7 @@ function chapterSortKey(directoryName) {
132
121
  return { order: Number.parseInt(match[1], 10), name: match[2] || directoryName };
133
122
  }
134
123
 
135
- function chapterSlugFromDirectory(directoryName) {
124
+ function sectionSlugFromDirectory(directoryName) {
136
125
  return directoryName.replace(/^\d+[-_]?/, "");
137
126
  }
138
127
 
@@ -15,6 +15,11 @@ const DEFAULT_CONFIG = {
15
15
  componentsDir: "components",
16
16
  publicDir: "public/openpress",
17
17
  outputDir: "dist",
18
+ captionNumbering: {
19
+ figure: "Figure",
20
+ table: "Table",
21
+ separator: " ",
22
+ },
18
23
  pdf: {
19
24
  filename: "document.pdf",
20
25
  },
@@ -51,6 +56,7 @@ export function normalizeConfig(root, userConfig = {}, configPath = path.join(ro
51
56
  componentsDir: relativePathValue(userConfig.componentsDir, DEFAULT_CONFIG.componentsDir),
52
57
  publicDir: relativePathValue(userConfig.publicDir, DEFAULT_CONFIG.publicDir),
53
58
  outputDir: relativePathValue(userConfig.outputDir, DEFAULT_CONFIG.outputDir),
59
+ captionNumbering: captionNumberingValue(userConfig.captionNumbering, DEFAULT_CONFIG.captionNumbering),
54
60
  pdf: {
55
61
  filename: fileNameValue(userConfig.pdf?.filename, DEFAULT_CONFIG.pdf.filename),
56
62
  },
@@ -121,6 +127,15 @@ function optionalStringValue(value, fallback) {
121
127
  return fallback;
122
128
  }
123
129
 
130
+ function captionNumberingValue(value, fallback) {
131
+ const input = value && typeof value === "object" && !Array.isArray(value) ? value : {};
132
+ return {
133
+ figure: optionalStringValue(input.figure, fallback.figure) ?? fallback.figure,
134
+ table: optionalStringValue(input.table, fallback.table) ?? fallback.table,
135
+ separator: typeof input.separator === "string" ? input.separator : fallback.separator,
136
+ };
137
+ }
138
+
124
139
  function booleanValue(value, fallback) {
125
140
  return typeof value === "boolean" ? value : fallback;
126
141
  }
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
3
  import { loadConfig } from "./config.mjs";
4
- import { readKatexCss } from "./katex-assets.mjs";
4
+ import { readKatexCss } from "../output/katex-assets.mjs";
5
5
 
6
6
  const CONTENT_CSS_LAYERS = [
7
7
  "base/page-contract.css",
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
- import { evaluateUrlWithChrome, stopChildProcess } from "./chrome-pdf.mjs";
4
- import { buildReactStatic, startStaticServer } from "./commands/_shared.mjs";
3
+ import { evaluateUrlWithChrome, stopChildProcess } from "../output/chrome-pdf.mjs";
4
+ import { buildReactStatic, startStaticServer } from "../commands/_shared.mjs";
5
5
  import { createIssue, createIssueReport } from "./issue-report.mjs";
6
6
  import { collectActiveContentFiles, resolveActiveSourceWorkspace } from "./source-workspace.mjs";
7
7
 
@@ -260,8 +260,7 @@ function humanOverflowTarget(code) {
260
260
  function inspectionExpression() {
261
261
  return `Promise.resolve().then(async () => {
262
262
  const root = document.querySelector('[data-openpress-print-document="true"]');
263
- const ready = root?.getAttribute('data-openpress-pagination') === 'ready';
264
- if (!ready) return null;
263
+ if (!root || root.querySelectorAll('.openpress-html-page').length === 0) return null;
265
264
 
266
265
  await document.fonts?.ready;
267
266
  await Promise.all(Array.from(document.images).map(async (img) => {
@@ -177,24 +177,41 @@ export function replaceLiteralMatches(text, from, to, { caseSensitive = false, i
177
177
  async function sourceRoots(config, scope) {
178
178
  const sourceWorkspace = await resolveActiveSourceWorkspace(config);
179
179
  const sourceConfig = sourceWorkspace.config;
180
- const contentRoot = {
180
+ const contentRoots = (sourceWorkspace.contentRoots ?? [{ kind: "dir", absolutePath: sourceWorkspace.sourceDir }]).map((root) => ({
181
181
  scope: "content",
182
- kind: "dir",
183
- absolutePath: sourceWorkspace.sourceDir,
182
+ kind: root.kind,
183
+ absolutePath: root.absolutePath,
184
184
  extensions: sourceWorkspace.contentExtensions,
185
- };
185
+ }));
186
186
 
187
187
  if (scope === "all") {
188
188
  const roots = [
189
- contentRoot,
189
+ ...contentRoots,
190
190
  { scope: "design-doc", kind: "file", absolutePath: sourceConfig.paths.designDoc, extensions: MARKDOWN_EXTENSIONS },
191
191
  { scope: "components", kind: "dir", absolutePath: sourceConfig.paths.componentsDir, extensions: ALL_SOURCE_EXTENSIONS },
192
192
  { scope: "document-entry", kind: "file", absolutePath: sourceWorkspace.entryPath, extensions: REACT_IMPLEMENTATION_EXTENSIONS },
193
- { scope: "chapters", kind: "dir", absolutePath: sourceWorkspace.sourceDir, extensions: REACT_IMPLEMENTATION_EXTENSIONS },
193
+ ...implementationRoots(sourceWorkspace),
194
194
  ];
195
195
  return roots;
196
196
  }
197
- return [contentRoot];
197
+ return contentRoots;
198
+ }
199
+
200
+ function implementationRoots(sourceWorkspace) {
201
+ const roots = [];
202
+ const seen = new Set();
203
+ for (const root of sourceWorkspace.contentRoots ?? [{ kind: "dir", absolutePath: sourceWorkspace.sourceDir }]) {
204
+ const absolutePath = root.kind === "dir" ? root.absolutePath : path.dirname(root.absolutePath);
205
+ if (seen.has(absolutePath)) continue;
206
+ seen.add(absolutePath);
207
+ roots.push({
208
+ scope: "source-implementation",
209
+ kind: "dir",
210
+ absolutePath,
211
+ extensions: REACT_IMPLEMENTATION_EXTENSIONS,
212
+ });
213
+ }
214
+ return roots;
198
215
  }
199
216
 
200
217
  async function walkFiles(directory, visit) {