@open-press/cli 1.0.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 (175) hide show
  1. package/README.md +11 -12
  2. package/dist/cli.js +298 -79
  3. package/package.json +9 -7
  4. package/template/core/AGENTS.md +0 -130
  5. package/template/core/CHANGELOG.md +0 -218
  6. package/template/core/README.md +0 -43
  7. package/template/core/engine/cli.mjs +0 -96
  8. package/template/core/engine/commands/_shared.mjs +0 -199
  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/image.mjs +0 -29
  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/skills-sync.mjs +0 -71
  21. package/template/core/engine/commands/typecheck.mjs +0 -67
  22. package/template/core/engine/commands/upgrade.mjs +0 -159
  23. package/template/core/engine/commands/validate.mjs +0 -17
  24. package/template/core/engine/document-export.mjs +0 -15
  25. package/template/core/engine/output/chrome-pdf.d.mts +0 -34
  26. package/template/core/engine/output/chrome-pdf.mjs +0 -450
  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 -571
  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 -331
  39. package/template/core/engine/react/document-export.mjs +0 -512
  40. package/template/core/engine/react/http-json.mjs +0 -24
  41. package/template/core/engine/react/mdx-compile.mjs +0 -629
  42. package/template/core/engine/react/measurement-css.mjs +0 -157
  43. package/template/core/engine/react/object-entities.mjs +0 -204
  44. package/template/core/engine/react/pagination/allocator.mjs +0 -167
  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 -217
  49. package/template/core/engine/react/pipeline/final-render.mjs +0 -94
  50. package/template/core/engine/react/pipeline/frame-measurement.mjs +0 -306
  51. package/template/core/engine/react/pipeline/press-tree.mjs +0 -135
  52. package/template/core/engine/react/press-tree-inspection.mjs +0 -172
  53. package/template/core/engine/react/project-asset-endpoint.d.mts +0 -10
  54. package/template/core/engine/react/project-asset-endpoint.mjs +0 -361
  55. package/template/core/engine/react/section-css.mjs +0 -56
  56. package/template/core/engine/react/source-edit-endpoint.d.mts +0 -10
  57. package/template/core/engine/react/source-edit-endpoint.mjs +0 -75
  58. package/template/core/engine/react/sources/heading-numbering.mjs +0 -132
  59. package/template/core/engine/react/sources/mdx-resolver.mjs +0 -439
  60. package/template/core/engine/react/style-discovery.mjs +0 -160
  61. package/template/core/engine/runtime/config.d.mts +0 -48
  62. package/template/core/engine/runtime/config.mjs +0 -172
  63. package/template/core/engine/runtime/file-utils.mjs +0 -114
  64. package/template/core/engine/runtime/file-walk.mjs +0 -22
  65. package/template/core/engine/runtime/inspection.mjs +0 -328
  66. package/template/core/engine/runtime/issue-report.mjs +0 -44
  67. package/template/core/engine/runtime/page-geometry.mjs +0 -131
  68. package/template/core/engine/runtime/path-utils.mjs +0 -20
  69. package/template/core/engine/runtime/source-text-tools.d.mts +0 -102
  70. package/template/core/engine/runtime/source-text-tools.mjs +0 -832
  71. package/template/core/engine/runtime/source-workspace.mjs +0 -168
  72. package/template/core/engine/runtime/validation.mjs +0 -183
  73. package/template/core/index.html +0 -13
  74. package/template/core/openpress.config.mjs +0 -8
  75. package/template/core/package.json +0 -89
  76. package/template/core/src/main.tsx +0 -16
  77. package/template/core/src/openpress/app/OpenPressApp.tsx +0 -296
  78. package/template/core/src/openpress/app/OpenPressRuntime.tsx +0 -102
  79. package/template/core/src/openpress/app/WorkspaceGalleryPage.tsx +0 -219
  80. package/template/core/src/openpress/app/index.ts +0 -2
  81. package/template/core/src/openpress/core/Frame.tsx +0 -91
  82. package/template/core/src/openpress/core/FrameContext.tsx +0 -26
  83. package/template/core/src/openpress/core/MdxArea.tsx +0 -34
  84. package/template/core/src/openpress/core/Press.tsx +0 -55
  85. package/template/core/src/openpress/core/Workspace.tsx +0 -36
  86. package/template/core/src/openpress/core/cn.ts +0 -4
  87. package/template/core/src/openpress/core/index.tsx +0 -47
  88. package/template/core/src/openpress/core/primitives.tsx +0 -91
  89. package/template/core/src/openpress/core/types.ts +0 -236
  90. package/template/core/src/openpress/core/useSource.ts +0 -28
  91. package/template/core/src/openpress/document-model/anchorMapModel.ts +0 -27
  92. package/template/core/src/openpress/document-model/documentIndexes.ts +0 -329
  93. package/template/core/src/openpress/document-model/documentTypes.ts +0 -147
  94. package/template/core/src/openpress/document-model/index.ts +0 -7
  95. package/template/core/src/openpress/document-model/objectEntityModel.ts +0 -55
  96. package/template/core/src/openpress/document-model/projectIdentityModel.ts +0 -15
  97. package/template/core/src/openpress/document-model/reactDocumentMetadataModel.ts +0 -27
  98. package/template/core/src/openpress/document-model/workspaceManifestModel.ts +0 -57
  99. package/template/core/src/openpress/manuscript/index.tsx +0 -238
  100. package/template/core/src/openpress/mdx/index.ts +0 -96
  101. package/template/core/src/openpress/numbering/index.ts +0 -294
  102. package/template/core/src/openpress/reader/PageThumbnailsPanel.tsx +0 -168
  103. package/template/core/src/openpress/reader/PublicReaderPage.tsx +0 -267
  104. package/template/core/src/openpress/reader/ReaderNavigationPanel.tsx +0 -123
  105. package/template/core/src/openpress/reader/index.ts +0 -11
  106. package/template/core/src/openpress/reader/pageViewportScaleModel.ts +0 -73
  107. package/template/core/src/openpress/reader/readerPageRegistry.ts +0 -41
  108. package/template/core/src/openpress/reader/readerPageRoute.ts +0 -21
  109. package/template/core/src/openpress/reader/readerScroll.ts +0 -92
  110. package/template/core/src/openpress/reader/readerStateModel.ts +0 -15
  111. package/template/core/src/openpress/reader/readerTypes.ts +0 -4
  112. package/template/core/src/openpress/reader/usePageViewportScale.ts +0 -119
  113. package/template/core/src/openpress/reader/usePanelState.ts +0 -56
  114. package/template/core/src/openpress/reader/useReaderHashSync.ts +0 -61
  115. package/template/core/src/openpress/reader/useReaderKeyboardNav.ts +0 -48
  116. package/template/core/src/openpress/reader/useReaderRuntime.ts +0 -146
  117. package/template/core/src/openpress/reader/useReaderScrollAnchor.ts +0 -64
  118. package/template/core/src/openpress/shared/Panel.tsx +0 -77
  119. package/template/core/src/openpress/shared/frameScheduler.ts +0 -32
  120. package/template/core/src/openpress/shared/index.ts +0 -4
  121. package/template/core/src/openpress/shared/numberUtils.ts +0 -3
  122. package/template/core/src/openpress/shared/runtimeMode.ts +0 -11
  123. package/template/core/src/openpress/workbench/Workbench.tsx +0 -506
  124. package/template/core/src/openpress/workbench/actions/DeploymentControl.tsx +0 -157
  125. package/template/core/src/openpress/workbench/actions/ExportImageControl.tsx +0 -96
  126. package/template/core/src/openpress/workbench/actions/PageZoomControl.tsx +0 -182
  127. package/template/core/src/openpress/workbench/actions/SearchControl.tsx +0 -345
  128. package/template/core/src/openpress/workbench/actions/deploymentStatusModel.ts +0 -112
  129. package/template/core/src/openpress/workbench/actions/index.ts +0 -6
  130. package/template/core/src/openpress/workbench/actions/useDeploymentWorkbench.ts +0 -136
  131. package/template/core/src/openpress/workbench/dialog/WorkbenchDialog.tsx +0 -72
  132. package/template/core/src/openpress/workbench/dialog/index.ts +0 -1
  133. package/template/core/src/openpress/workbench/document/components/DocumentPanel.tsx +0 -127
  134. package/template/core/src/openpress/workbench/document/components/InlineSourceEditorLayer.tsx +0 -207
  135. package/template/core/src/openpress/workbench/document/components/ReaderStage.tsx +0 -9
  136. package/template/core/src/openpress/workbench/document/hooks/useDocumentWorkbenchModel.ts +0 -34
  137. package/template/core/src/openpress/workbench/document/hooks/useInlineDocumentEditor.ts +0 -525
  138. package/template/core/src/openpress/workbench/document/index.ts +0 -10
  139. package/template/core/src/openpress/workbench/index.ts +0 -2
  140. package/template/core/src/openpress/workbench/inspector/InlineInspectorLayer.tsx +0 -459
  141. package/template/core/src/openpress/workbench/inspector/index.ts +0 -5
  142. package/template/core/src/openpress/workbench/inspector/inlineCommentModel.ts +0 -125
  143. package/template/core/src/openpress/workbench/inspector/inspectorGeometryModel.ts +0 -160
  144. package/template/core/src/openpress/workbench/inspector/inspectorModel.ts +0 -408
  145. package/template/core/src/openpress/workbench/inspector/useInspectorComments.ts +0 -254
  146. package/template/core/src/openpress/workbench/mentions/MentionSuggestionList.tsx +0 -41
  147. package/template/core/src/openpress/workbench/mentions/index.ts +0 -2
  148. package/template/core/src/openpress/workbench/mentions/useComposerMentions.ts +0 -185
  149. package/template/core/src/openpress/workbench/panels/Panel.tsx +0 -1
  150. package/template/core/src/openpress/workbench/panels/PendingCommentsPanel.tsx +0 -80
  151. package/template/core/src/openpress/workbench/panels/WorkbenchControlPanel.tsx +0 -29
  152. package/template/core/src/openpress/workbench/panels/index.ts +0 -3
  153. package/template/core/src/openpress/workbench/project/ProjectEntryPanel.tsx +0 -525
  154. package/template/core/src/openpress/workbench/project/ProjectPreviewDialog.tsx +0 -35
  155. package/template/core/src/openpress/workbench/project/index.ts +0 -2
  156. package/template/core/src/openpress/workbench/project/projectPreviewTypes.ts +0 -11
  157. package/template/core/src/openpress/workbench/project/projectSourceModel.ts +0 -24
  158. package/template/core/src/openpress/workbench/shell/WorkbenchShell.tsx +0 -167
  159. package/template/core/src/openpress/workbench/shell/index.ts +0 -1
  160. package/template/core/src/openpress/workbench/workbenchFormatters.ts +0 -120
  161. package/template/core/src/openpress/workbench/workbenchTypes.ts +0 -35
  162. package/template/core/src/styles/openpress/app-shell.css +0 -251
  163. package/template/core/src/styles/openpress/media-workspace.css +0 -230
  164. package/template/core/src/styles/openpress/print-route.css +0 -184
  165. package/template/core/src/styles/openpress/project-preview-panel.css +0 -924
  166. package/template/core/src/styles/openpress/public-viewer.css +0 -688
  167. package/template/core/src/styles/openpress/reader-runtime.css +0 -989
  168. package/template/core/src/styles/openpress/responsive.css +0 -245
  169. package/template/core/src/styles/openpress/workbench-panels.css +0 -707
  170. package/template/core/src/styles/openpress/workbench.css +0 -1255
  171. package/template/core/src/styles/openpress/workspace-gallery.css +0 -300
  172. package/template/core/src/styles/openpress.css +0 -15
  173. package/template/core/src/vite-env.d.ts +0 -9
  174. package/template/core/tsconfig.json +0 -40
  175. package/template/core/vite.config.ts +0 -584
@@ -1,450 +0,0 @@
1
- import { spawn, spawnSync } from "node:child_process";
2
- import { existsSync } from "node:fs";
3
- import fs from "node:fs/promises";
4
- import path from "node:path";
5
-
6
- const CHROME_CANDIDATE_PATHS = {
7
- darwin: [
8
- "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
9
- "/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta",
10
- "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
11
- "/Applications/Chromium.app/Contents/MacOS/Chromium",
12
- "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
13
- ],
14
- linux: [
15
- "/usr/bin/google-chrome",
16
- "/usr/bin/google-chrome-stable",
17
- "/usr/bin/chromium",
18
- "/usr/bin/chromium-browser",
19
- "/snap/bin/chromium",
20
- "/opt/google/chrome/chrome",
21
- ],
22
- win32: [
23
- "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
24
- "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
25
- ],
26
- };
27
-
28
- const CHROME_BIN_NAMES = {
29
- darwin: [],
30
- linux: ["google-chrome", "google-chrome-stable", "chromium", "chromium-browser"],
31
- win32: ["chrome.exe"],
32
- };
33
-
34
- let cachedChromePath = null;
35
-
36
- function resolveChromePath() {
37
- if (cachedChromePath) return cachedChromePath;
38
- if (process.env.CHROME) {
39
- cachedChromePath = process.env.CHROME;
40
- return cachedChromePath;
41
- }
42
-
43
- const platform = process.platform;
44
- const candidates = [...(CHROME_CANDIDATE_PATHS[platform] ?? [])];
45
-
46
- if (platform === "win32") {
47
- const roots = [process.env.PROGRAMFILES, process.env["PROGRAMFILES(X86)"], process.env.LOCALAPPDATA].filter(Boolean);
48
- for (const root of roots) {
49
- candidates.push(`${root}\\Google\\Chrome\\Application\\chrome.exe`);
50
- }
51
- }
52
-
53
- for (const candidate of candidates) {
54
- if (existsSync(candidate)) {
55
- cachedChromePath = candidate;
56
- return cachedChromePath;
57
- }
58
- }
59
-
60
- const whichCommand = platform === "win32" ? "where" : "which";
61
- for (const name of CHROME_BIN_NAMES[platform] ?? []) {
62
- try {
63
- const result = spawnSync(whichCommand, [name], { encoding: "utf8" });
64
- const output = result.stdout?.trim().split(/\r?\n/)[0];
65
- if (result.status === 0 && output && existsSync(output)) {
66
- cachedChromePath = output;
67
- return cachedChromePath;
68
- }
69
- } catch {
70
- // continue
71
- }
72
- }
73
-
74
- const lines = [
75
- `Cannot locate a Chrome / Chromium executable on ${platform}.`,
76
- `Set the CHROME environment variable to your Chrome binary path, or install Chrome at one of:`,
77
- ...candidates.map((p) => ` - ${p}`),
78
- ];
79
- throw new Error(lines.join("\n"));
80
- }
81
-
82
- const DEFAULT_PRINT_OPTIONS = {
83
- printBackground: true,
84
- displayHeaderFooter: false,
85
- preferCSSPageSize: true,
86
- paperWidth: 8.2677,
87
- paperHeight: 11.6929,
88
- marginTop: 0,
89
- marginRight: 0,
90
- marginBottom: 0,
91
- marginLeft: 0,
92
- };
93
-
94
- export const DEFAULT_PRINT_VIEWPORT = Object.freeze({
95
- width: 1200,
96
- height: 1698,
97
- deviceScaleFactor: 1,
98
- mobile: false,
99
- });
100
-
101
- export async function printUrlToPdf({
102
- root,
103
- url,
104
- outPath,
105
- chrome,
106
- waitForReady = waitForPrintReady,
107
- printOptions = {},
108
- viewport = DEFAULT_PRINT_VIEWPORT,
109
- debuggingPortBase = 9600,
110
- debuggingPortRange = 300,
111
- profilePrefix = "chrome-pdf",
112
- }) {
113
- chrome ??= resolveChromePath();
114
- await fs.mkdir(path.dirname(outPath), { recursive: true });
115
-
116
- const debuggingPort = String(debuggingPortBase + Math.floor(Math.random() * debuggingPortRange));
117
- const profileDir = path.join(root, ".openpress", "tmp", `${profilePrefix}-${process.pid}-${Date.now()}`);
118
- await fs.mkdir(profileDir, { recursive: true });
119
-
120
- const child = spawn(
121
- chrome,
122
- [
123
- "--headless=new",
124
- "--disable-gpu",
125
- "--no-sandbox",
126
- `--remote-debugging-port=${debuggingPort}`,
127
- `--user-data-dir=${profileDir}`,
128
- "about:blank",
129
- ],
130
- { cwd: root, stdio: ["ignore", "pipe", "pipe"] },
131
- );
132
-
133
- try {
134
- const tab = await waitForChromeTab(debuggingPort);
135
- const client = await connectChromeDevTools(tab.webSocketDebuggerUrl);
136
- try {
137
- await preparePdfPage(client, { viewport });
138
- await client.send("Page.navigate", { url });
139
- const readyResult = await waitForReady(client);
140
- const result = await client.send("Page.printToPDF", {
141
- ...DEFAULT_PRINT_OPTIONS,
142
- ...printOptions,
143
- });
144
- await fs.writeFile(outPath, Buffer.from(String(result.data ?? ""), "base64"));
145
- return readyResult;
146
- } finally {
147
- client.close();
148
- }
149
- } finally {
150
- await stopChildProcess(child);
151
- await cleanupChromeProfile(profileDir);
152
- }
153
- }
154
-
155
- export async function captureUrlPagesToPng({
156
- root,
157
- url,
158
- outDir,
159
- chrome,
160
- waitForReady = waitForPrintReady,
161
- viewport = DEFAULT_PRINT_VIEWPORT,
162
- debuggingPortBase = 9700,
163
- debuggingPortRange = 300,
164
- profilePrefix = "chrome-image",
165
- }) {
166
- chrome ??= resolveChromePath();
167
- await fs.mkdir(outDir, { recursive: true });
168
-
169
- const debuggingPort = String(debuggingPortBase + Math.floor(Math.random() * debuggingPortRange));
170
- const profileDir = path.join(root, ".openpress", "tmp", `${profilePrefix}-${process.pid}-${Date.now()}`);
171
- await fs.mkdir(profileDir, { recursive: true });
172
-
173
- const child = spawn(
174
- chrome,
175
- [
176
- "--headless=new",
177
- "--disable-gpu",
178
- "--no-sandbox",
179
- `--remote-debugging-port=${debuggingPort}`,
180
- `--user-data-dir=${profileDir}`,
181
- "about:blank",
182
- ],
183
- { cwd: root, stdio: ["ignore", "pipe", "pipe"] },
184
- );
185
-
186
- try {
187
- const tab = await waitForChromeTab(debuggingPort);
188
- const client = await connectChromeDevTools(tab.webSocketDebuggerUrl);
189
- try {
190
- await preparePdfPage(client, { viewport });
191
- await client.send("Page.navigate", { url });
192
- const pageCount = await waitForReady(client);
193
- const rects = await getPrintPageRects(client);
194
- if (rects.length === 0) throw new Error("No OpenPress pages found for image export.");
195
-
196
- const padWidth = Math.max(3, String(rects.length).length);
197
- const files = [];
198
- for (const [index, rect] of rects.entries()) {
199
- const filename = `page-${String(index + 1).padStart(padWidth, "0")}.png`;
200
- const filePath = path.join(outDir, filename);
201
- const result = await client.send("Page.captureScreenshot", {
202
- format: "png",
203
- fromSurface: true,
204
- captureBeyondViewport: true,
205
- clip: {
206
- x: Math.max(0, rect.x),
207
- y: Math.max(0, rect.y),
208
- width: rect.width,
209
- height: rect.height,
210
- scale: 1,
211
- },
212
- });
213
- await fs.writeFile(filePath, Buffer.from(String(result.data ?? ""), "base64"));
214
- files.push(filePath);
215
- }
216
-
217
- return { pageCount, files };
218
- } finally {
219
- client.close();
220
- }
221
- } finally {
222
- await stopChildProcess(child);
223
- await cleanupChromeProfile(profileDir);
224
- }
225
- }
226
-
227
- export async function preparePdfPage(client, { viewport = DEFAULT_PRINT_VIEWPORT } = {}) {
228
- await client.send("Page.enable");
229
- await client.send("Runtime.enable");
230
- if (viewport) {
231
- await client.send("Emulation.setDeviceMetricsOverride", viewport);
232
- }
233
- await client.send("Emulation.setEmulatedMedia", { media: "print" });
234
- }
235
-
236
- export async function evaluateUrlWithChrome({
237
- root,
238
- url,
239
- chrome,
240
- evaluate,
241
- emulatedMedia,
242
- debuggingPortBase = 9900,
243
- debuggingPortRange = 300,
244
- profilePrefix = "chrome-eval",
245
- }) {
246
- chrome ??= resolveChromePath();
247
-
248
- const debuggingPort = String(debuggingPortBase + Math.floor(Math.random() * debuggingPortRange));
249
- const profileDir = path.join(root, ".openpress", "tmp", `${profilePrefix}-${process.pid}-${Date.now()}`);
250
- await fs.mkdir(profileDir, { recursive: true });
251
-
252
- const child = spawn(
253
- chrome,
254
- [
255
- "--headless=new",
256
- "--disable-gpu",
257
- "--no-sandbox",
258
- `--remote-debugging-port=${debuggingPort}`,
259
- `--user-data-dir=${profileDir}`,
260
- "about:blank",
261
- ],
262
- { cwd: root, stdio: ["ignore", "pipe", "pipe"] },
263
- );
264
-
265
- try {
266
- const tab = await waitForChromeTab(debuggingPort);
267
- const client = await connectChromeDevTools(tab.webSocketDebuggerUrl);
268
- try {
269
- await client.send("Page.enable");
270
- await client.send("Runtime.enable");
271
- if (emulatedMedia) {
272
- await client.send("Emulation.setEmulatedMedia", { media: emulatedMedia });
273
- }
274
- await client.send("Page.navigate", { url });
275
- return await evaluate(client);
276
- } finally {
277
- client.close();
278
- }
279
- } finally {
280
- await stopChildProcess(child);
281
- await cleanupChromeProfile(profileDir);
282
- }
283
- }
284
-
285
- export async function waitForPrintReady(client) {
286
- const deadline = Date.now() + 30000;
287
- while (Date.now() < deadline) {
288
- const result = await client.send("Runtime.evaluate", {
289
- returnByValue: true,
290
- awaitPromise: true,
291
- expression: `Promise.resolve().then(async () => {
292
- const root = document.querySelector('[data-openpress-print-document="true"]');
293
- if (!root || root.querySelectorAll('.openpress-html-page').length === 0) return 0;
294
-
295
- await document.fonts?.ready;
296
- await Promise.all(Array.from(document.images).map(async (img) => {
297
- if (!img.complete) {
298
- await new Promise((resolve) => {
299
- const settle = () => {
300
- img.removeEventListener('load', settle);
301
- img.removeEventListener('error', settle);
302
- resolve();
303
- };
304
-
305
- img.addEventListener('load', settle, { once: true });
306
- img.addEventListener('error', settle, { once: true });
307
- });
308
- }
309
-
310
- await img.decode?.().catch(() => undefined);
311
- }));
312
-
313
- await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)));
314
-
315
- const pages = Array.from(document.querySelectorAll('.openpress-public-page > .openpress-html-page'));
316
- const contentFitsPageBody = (body) => {
317
- const bodyBottom = body.getBoundingClientRect().bottom;
318
- const contentBottom = Array.from(body.children).reduce((bottom, child) => {
319
- if (getComputedStyle(child).display === 'none') return bottom;
320
- const marginBottom = Number.parseFloat(getComputedStyle(child).marginBottom) || 0;
321
- return Math.max(bottom, child.getBoundingClientRect().bottom + marginBottom);
322
- }, body.getBoundingClientRect().top);
323
- return contentBottom <= bodyBottom + 1;
324
- };
325
- const bodyOverflowSafe = pages.every((page) => {
326
- const body = page.querySelector('.page-body');
327
- return !body || contentFitsPageBody(body);
328
- });
329
-
330
- return pages.length > 0 && bodyOverflowSafe ? pages.length : 0;
331
- })`,
332
- });
333
- const count = Number(result.result?.value ?? 0);
334
- if (count > 0) return count;
335
- await delay(100);
336
- }
337
- throw new Error("Timed out waiting for OpenPress pagination before PDF export.");
338
- }
339
-
340
- async function getPrintPageRects(client) {
341
- const result = await client.send("Runtime.evaluate", {
342
- returnByValue: true,
343
- expression: `(() => {
344
- return Array.from(document.querySelectorAll('.openpress-public-page > .openpress-html-page')).map((page, index) => {
345
- const target = page.querySelector('.openpress-html-page__html') || page;
346
- const rect = target.getBoundingClientRect();
347
- return {
348
- index,
349
- x: rect.left + window.scrollX,
350
- y: rect.top + window.scrollY,
351
- width: rect.width,
352
- height: rect.height,
353
- };
354
- }).filter((rect) => rect.width > 0 && rect.height > 0);
355
- })()`,
356
- });
357
- return Array.isArray(result.result?.value) ? result.result.value : [];
358
- }
359
-
360
- export async function stopChildProcess(child) {
361
- if (child.exitCode !== null || child.signalCode !== null) return;
362
- child.kill();
363
- const closed = await waitForChildClose(child, 2500);
364
- if (closed || child.exitCode !== null || child.signalCode !== null) return;
365
- child.kill("SIGKILL");
366
- await waitForChildClose(child, 2500);
367
- }
368
-
369
- async function waitForChromeTab(debuggingPort) {
370
- const deadline = Date.now() + 10000;
371
- let lastError;
372
- while (Date.now() < deadline) {
373
- try {
374
- const response = await fetch(`http://127.0.0.1:${debuggingPort}/json/list`);
375
- const tabs = await response.json();
376
- const tab = tabs.find((item) => item.type === "page" && item.webSocketDebuggerUrl);
377
- if (tab) return tab;
378
- } catch (error) {
379
- lastError = error;
380
- }
381
- await delay(100);
382
- }
383
- throw new Error(`Timed out waiting for Chrome DevTools${lastError ? `: ${lastError.message}` : ""}`);
384
- }
385
-
386
- async function connectChromeDevTools(webSocketDebuggerUrl) {
387
- const socket = new WebSocket(webSocketDebuggerUrl);
388
- const pending = new Map();
389
- let nextId = 0;
390
-
391
- await new Promise((resolve, reject) => {
392
- socket.addEventListener("open", resolve, { once: true });
393
- socket.addEventListener("error", reject, { once: true });
394
- });
395
-
396
- socket.addEventListener("message", (event) => {
397
- const message = JSON.parse(String(event.data));
398
- if (!message.id || !pending.has(message.id)) return;
399
- const callbacks = pending.get(message.id);
400
- pending.delete(message.id);
401
- if (message.error) callbacks?.reject(new Error(message.error.message ?? "Chrome DevTools command failed."));
402
- else callbacks?.resolve(message.result ?? {});
403
- });
404
-
405
- return {
406
- send(method, params = {}) {
407
- return new Promise((resolve, reject) => {
408
- const id = ++nextId;
409
- pending.set(id, { resolve, reject });
410
- socket.send(JSON.stringify({ id, method, params }));
411
- });
412
- },
413
- close() {
414
- socket.close();
415
- },
416
- };
417
- }
418
-
419
- function waitForChildClose(child, timeoutMs) {
420
- if (child.exitCode !== null || child.signalCode !== null) return Promise.resolve(true);
421
- return new Promise((resolve) => {
422
- let settled = false;
423
- const finish = (closed) => {
424
- if (settled) return;
425
- settled = true;
426
- clearTimeout(timer);
427
- child.off("close", onClose);
428
- child.off("error", onError);
429
- resolve(closed);
430
- };
431
- const onClose = () => finish(true);
432
- const onError = () => finish(true);
433
- const timer = setTimeout(() => finish(false), timeoutMs);
434
- child.once("close", onClose);
435
- child.once("error", onError);
436
- });
437
- }
438
-
439
- async function cleanupChromeProfile(profileDir) {
440
- try {
441
- await fs.rm(profileDir, { recursive: true, force: true, maxRetries: 8, retryDelay: 150 });
442
- } catch (error) {
443
- const message = error instanceof Error ? error.message : String(error);
444
- console.warn(`Could not remove temporary Chrome profile ${profileDir}: ${message}`);
445
- }
446
- }
447
-
448
- function delay(ms) {
449
- return new Promise((resolve) => setTimeout(resolve, ms));
450
- }
@@ -1,15 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- import { loadConfig } from "../runtime/config.mjs";
4
- import { copyDirectory } from "../runtime/file-utils.mjs";
5
-
6
- export async function deploySync(root, sourceDir, deployDir) {
7
- const config = await loadConfig(root);
8
- sourceDir ??= config.outputDir;
9
- deployDir ??= config.deploy.source;
10
- const dist = path.join(root, sourceDir);
11
- const deploy = path.join(root, deployDir);
12
- await fs.rm(deploy, { recursive: true, force: true });
13
- await fs.mkdir(deploy, { recursive: true });
14
- await copyDirectory(dist, deploy);
15
- }
@@ -1,62 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- import { copyDirectory } from "../runtime/file-utils.mjs";
4
-
5
- export async function copyThemeFonts(root, publicOutputDir, config) {
6
- const themeDir = config?.paths?.themeDir ?? path.join(path.resolve(root), "theme");
7
- const target = path.join(publicOutputDir, "fonts.css");
8
- const themeFonts = path.join(themeDir, "fonts.css");
9
- if (await isFile(themeFonts)) {
10
- await fs.copyFile(themeFonts, target);
11
- } else {
12
- await fs.writeFile(target, defaultFontsCss(), "utf8");
13
- }
14
-
15
- const themeFontFiles = path.join(themeDir, "fonts");
16
- if (await isDirectory(themeFontFiles)) {
17
- await copyDirectory(themeFontFiles, path.join(publicOutputDir, "fonts"));
18
- } else {
19
- await fs.rm(path.join(publicOutputDir, "fonts"), { recursive: true, force: true });
20
- }
21
- }
22
-
23
- async function isFile(filePath) {
24
- try {
25
- return (await fs.stat(filePath)).isFile();
26
- } catch {
27
- return false;
28
- }
29
- }
30
-
31
- async function isDirectory(filePath) {
32
- try {
33
- return (await fs.stat(filePath)).isDirectory();
34
- } catch {
35
- return false;
36
- }
37
- }
38
-
39
- function defaultFontsCss() {
40
- return `@font-face {
41
- font-family: "OpenPress Body";
42
- src: local("PingFang TC"), local("Noto Sans TC"), local("Hiragino Sans"), local("Microsoft JhengHei");
43
- font-weight: 300 700;
44
- font-style: normal;
45
- font-display: swap;
46
- }
47
-
48
- @font-face {
49
- font-family: "OpenPress Serif";
50
- src: local("Noto Serif TC"), local("Songti TC"), local("Source Han Serif TC"), local("PMingLiU");
51
- font-weight: 300 700;
52
- font-style: normal;
53
- font-display: swap;
54
- }
55
-
56
- :root {
57
- --openpress-font-body: "OpenPress Body", "PingFang TC", "Noto Sans TC", "Hiragino Sans", "Microsoft JhengHei", sans-serif;
58
- --openpress-font-serif: "OpenPress Serif", "Noto Serif TC", "Songti TC", "Source Han Serif TC", "PMingLiU", serif;
59
- --openpress-font-mono: "SFMono-Regular", "Menlo", "Consolas", monospace;
60
- }
61
- `;
62
- }
@@ -1,45 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- import { createRequire } from "node:module";
4
-
5
- const require = createRequire(import.meta.url);
6
-
7
- const KATEX_OVERRIDES = `
8
-
9
- /* OpenPress math sizing keeps inline equations aligned with document typography. */
10
- .reader-page--content .katex {
11
- font-size: 1em;
12
- }
13
-
14
- .reader-page--content .katex-display {
15
- display: block;
16
- width: fit-content;
17
- max-width: 100%;
18
- margin: var(--openpress-space-2) auto var(--openpress-space-3);
19
- border-block: 1px solid color-mix(in srgb, var(--openpress-color-line) 76%, transparent);
20
- padding: 0.55em 1.15em;
21
- background: color-mix(in srgb, var(--openpress-color-panel) 78%, var(--openpress-color-document));
22
- color: var(--openpress-color-ink);
23
- overflow-x: auto;
24
- overflow-y: hidden;
25
- text-align: center;
26
- }
27
-
28
- .reader-page--content .katex-display > .katex {
29
- font-size: 1.05em;
30
- }
31
- `;
32
-
33
- export async function readKatexCss() {
34
- const cssPath = require.resolve("katex/dist/katex.min.css");
35
- const css = await fs.readFile(cssPath, "utf8");
36
- return `${css.replace(/url\(fonts\/([^)]+)\)/g, 'url("/openpress/katex-fonts/$1")')}${KATEX_OVERRIDES}`;
37
- }
38
-
39
- export async function copyKatexFonts(publicOutputDir) {
40
- const sampleFont = require.resolve("katex/dist/fonts/KaTeX_Main-Regular.woff2");
41
- const sourceDir = path.dirname(sampleFont);
42
- const targetDir = path.join(publicOutputDir, "katex-fonts");
43
- await fs.rm(targetDir, { recursive: true, force: true });
44
- await fs.cp(sourceDir, targetDir, { recursive: true });
45
- }
@@ -1,30 +0,0 @@
1
- import path from "node:path";
2
-
3
- export function documentRelativePath(config, ...parts) {
4
- return path.posix.join(
5
- ...(config.documentDir === "." ? [] : [config.documentDir]),
6
- ...parts,
7
- );
8
- }
9
-
10
- function rewriteAssetPaths(pageHtml, config) {
11
- const mediaDir = config.mediaDir.replace(/^\/+|\/+$/g, "");
12
- return pageHtml
13
- .replaceAll(`src="${mediaDir}/`, 'src="/openpress/media/')
14
- .replaceAll(`src='${mediaDir}/`, "src='/openpress/media/");
15
- }
16
-
17
- export function pageToBlock(index, pageHtml, source, config, { idPrefix = "openpress-page", anchorPrefix = "page", titleFallback = "Page" } = {}) {
18
- const paddedIndex = String(index + 1).padStart(2, "0");
19
- const title = pageHtml.match(/data-page-title="([^"]*)"/)?.[1] ?? `${titleFallback} ${index + 1}`;
20
- const anchor = pageHtml.match(/\bid="([^"]+)"/)?.[1] ?? `${anchorPrefix}-${paddedIndex}`;
21
- return {
22
- id: `${idPrefix}-${paddedIndex}`,
23
- kind: "htmlPage",
24
- title,
25
- pageNumber: index + 1,
26
- source,
27
- html: rewriteAssetPaths(pageHtml, config),
28
- anchors: [anchor],
29
- };
30
- }
@@ -1,45 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- import { spawnSync } from "node:child_process";
4
-
5
- export async function optimizePdfMediaForStaticRoot(staticRoot) {
6
- await optimizePdfMedia(path.join(staticRoot, "openpress", "media"));
7
- }
8
-
9
- async function optimizePdfMedia(mediaDir) {
10
- const files = await listImageFiles(mediaDir);
11
- for (const file of files) {
12
- const stat = await fs.stat(file);
13
- if (stat.size < 320 * 1024) continue;
14
-
15
- const ext = path.extname(file).toLowerCase();
16
- const args =
17
- ext === ".jpg" || ext === ".jpeg"
18
- ? ["-Z", "1600", "--setProperty", "formatOptions", "78", file, "--out", file]
19
- : ["-Z", "1600", file, "--out", file];
20
- const result = spawnSync("sips", args, { stdio: "ignore" });
21
- if (result.status !== 0) {
22
- console.warn(`[pdf] skipped image optimization: ${path.basename(file)}`);
23
- }
24
- }
25
- }
26
-
27
- async function listImageFiles(dir) {
28
- let entries;
29
- try {
30
- entries = await fs.readdir(dir, { withFileTypes: true });
31
- } catch {
32
- return [];
33
- }
34
-
35
- const files = [];
36
- for (const entry of entries) {
37
- const fullPath = path.join(dir, entry.name);
38
- if (entry.isDirectory()) {
39
- files.push(...(await listImageFiles(fullPath)));
40
- } else if (/\.(jpe?g|png)$/i.test(entry.name)) {
41
- files.push(fullPath);
42
- }
43
- }
44
- return files;
45
- }
@@ -1,19 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
3
- import { loadConfig } from "../runtime/config.mjs";
4
- import { copyDirectory, writeComponentsCss, writeContentCss } from "../runtime/file-utils.mjs";
5
- import { copyThemeFonts } from "./fonts.mjs";
6
- import { copyKatexFonts } from "./katex-assets.mjs";
7
-
8
- export async function syncPublicAssets(root, publicOutputDir, config) {
9
- config ??= await loadConfig(root);
10
- await fs.rm(path.join(publicOutputDir, "report.css"), { force: true });
11
- for (const name of ["tokens.css"]) {
12
- await fs.copyFile(path.join(config.paths.themeDir, name), path.join(publicOutputDir, name));
13
- }
14
- await writeContentCss(root, publicOutputDir, config);
15
- await copyThemeFonts(root, publicOutputDir, config);
16
- await copyKatexFonts(publicOutputDir);
17
- await writeComponentsCss(root, publicOutputDir, config);
18
- await copyDirectory(config.paths.mediaDir, path.join(publicOutputDir, "media"));
19
- }