@open-press/cli 1.0.0 → 1.1.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 (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,571 +0,0 @@
1
- import fs from "node:fs/promises";
2
- import http from "node:http";
3
- import path from "node:path";
4
- import { spawn } from "node:child_process";
5
- import { loadConfig, publicPdfHref } from "../runtime/config.mjs";
6
- import { searchSourceText } from "../runtime/source-text-tools.mjs";
7
- import { handleProjectAssetRequest } from "../react/project-asset-endpoint.mjs";
8
- import { handleSourceEditRequest } from "../react/source-edit-endpoint.mjs";
9
-
10
- const [rootArg = "dist", ...rest] = process.argv.slice(2);
11
- const host = valueAfter(rest, "--host") ?? "127.0.0.1";
12
- const port = Number(valueAfter(rest, "--port") ?? "8765");
13
- const root = path.resolve(rootArg);
14
- const workspace = path.resolve(valueAfter(rest, "--workspace") ?? await inferWorkspaceRoot(root));
15
- const config = await loadConfig(workspace);
16
-
17
- const mimeTypes = {
18
- ".html": "text/html; charset=utf-8",
19
- ".css": "text/css; charset=utf-8",
20
- ".js": "application/javascript; charset=utf-8",
21
- ".json": "application/json; charset=utf-8",
22
- ".png": "image/png",
23
- ".jpg": "image/jpeg",
24
- ".jpeg": "image/jpeg",
25
- ".gif": "image/gif",
26
- ".svg": "image/svg+xml",
27
- ".pdf": "application/pdf",
28
- };
29
-
30
- const server = http.createServer(async (req, res) => {
31
- try {
32
- const url = new URL(req.url ?? "/", `http://${host}:${port}`);
33
- if (url.pathname === "/__openpress/status") {
34
- await handleStatusRequest(req, res);
35
- return;
36
- }
37
- if (url.pathname === "/__openpress/search") {
38
- await handleSearchRequest(req, res, url);
39
- return;
40
- }
41
- if (url.pathname === "/__openpress/source-edit") {
42
- await handleSourceEditRequest(req, res, { root: workspace });
43
- return;
44
- }
45
- if (url.pathname === "/__openpress/local-pdf-export") {
46
- await handleLocalPdfExportRequest(req, res);
47
- return;
48
- }
49
- if (url.pathname === "/__openpress/local-pdf-file") {
50
- await handleLocalPdfFileRequest(req, res);
51
- return;
52
- }
53
- if (url.pathname === "/__openpress/deploy") {
54
- await handleDeployRequest(req, res);
55
- return;
56
- }
57
- if (url.pathname === "/__openpress/media-upload") {
58
- await handleMediaUploadRequest(req, res);
59
- return;
60
- }
61
- if (url.pathname === "/__openpress/project-asset") {
62
- await handleProjectAssetRequest(req, res, { root: workspace });
63
- return;
64
- }
65
- if (url.pathname.startsWith("/openpress/media/")) {
66
- await handleMediaFileRequest(req, res, url);
67
- return;
68
- }
69
- const requested = decodeURIComponent(url.pathname === "/" ? "/index.html" : url.pathname);
70
- const target = path.resolve(root, `.${requested}`);
71
- if (!target.startsWith(root)) {
72
- res.writeHead(403);
73
- res.end("Forbidden");
74
- return;
75
- }
76
- try {
77
- const stat = await fs.stat(target);
78
- const filePath = stat.isDirectory() ? path.join(target, "index.html") : target;
79
- const body = await fs.readFile(filePath);
80
- res.writeHead(200, { "Content-Type": mimeTypes[path.extname(filePath)] ?? "application/octet-stream" });
81
- res.end(body);
82
- } catch (err) {
83
- // SPA fallback: when a path doesn't map to a real file AND it
84
- // looks like a client-side route (no extension, not under a
85
- // reserved namespace), serve index.html so the reader's URL-based
86
- // routing can take over. This lets /cheatsheet / /proposal etc.
87
- // reload correctly without needing host-level rewrite rules.
88
- if (err?.code === "ENOENT" && shouldFallbackToIndex(url.pathname)) {
89
- const indexBody = await fs.readFile(path.join(root, "index.html"));
90
- res.writeHead(200, { "Content-Type": "text/html" });
91
- res.end(indexBody);
92
- return;
93
- }
94
- throw err;
95
- }
96
- } catch {
97
- res.writeHead(404);
98
- res.end("Not found");
99
- }
100
- });
101
-
102
- function shouldFallbackToIndex(pathname) {
103
- // Reserved namespaces — real resources whose 404s should stay 404.
104
- if (pathname.startsWith("/openpress/")) return false;
105
- if (pathname.startsWith("/__openpress/")) return false;
106
- if (pathname.startsWith("/assets/")) return false;
107
- // Anything with a file extension is an asset miss; fall through.
108
- const lastSlash = pathname.lastIndexOf("/");
109
- const tail = pathname.slice(lastSlash + 1);
110
- if (tail.includes(".")) return false;
111
- // Otherwise: looks like a client-side route — serve the SPA shell.
112
- return true;
113
- }
114
-
115
- server.listen(port, host, () => {
116
- console.log(`OpenPress static preview: http://${host}:${port}/`);
117
- });
118
-
119
- async function handleStatusRequest(req, res) {
120
- if (req.method !== "GET") {
121
- writeJson(res, 405, { ok: false, message: "Status endpoint requires GET." });
122
- return;
123
- }
124
-
125
- const deployConfigured = isDeployConfigured();
126
- const deploymentInfo = deployConfigured
127
- ? await readDeploymentInfo()
128
- : { deployed_at: undefined, pdf: publicPdfHref(config), public_url: undefined };
129
- const dirty = deployConfigured ? await isDeploymentDirty(deploymentInfo.deployed_at) : false;
130
- writeJson(res, 200, {
131
- ok: true,
132
- deployed_at: deploymentInfo.deployed_at,
133
- pdf: deploymentInfo.pdf,
134
- public_url: deploymentInfo.public_url,
135
- dirty,
136
- deploy_configured: deployConfigured,
137
- deploy_adapter: config.deploy.adapter,
138
- deploy_source: config.deploy.source,
139
- deploy_project_name: config.deploy.projectName,
140
- deploy_setup_message: deploySetupMessage(),
141
- });
142
- }
143
-
144
- async function handleSearchRequest(req, res, url) {
145
- if (req.method !== "GET") {
146
- writeJson(res, 405, { ok: false, message: "Search endpoint requires GET." });
147
- return;
148
- }
149
-
150
- const query = (url.searchParams.get("q") ?? "").trim();
151
- if (!query) {
152
- writeJson(res, 400, { ok: false, message: "Search query is required." });
153
- return;
154
- }
155
-
156
- try {
157
- const report = await searchSourceText({
158
- config,
159
- query,
160
- scope: searchScopeFrom(url),
161
- caseSensitive: url.searchParams.get("caseSensitive") === "true",
162
- });
163
- writeJson(res, 200, { ok: true, ...report });
164
- } catch (error) {
165
- writeJson(res, 500, { ok: false, message: error instanceof Error ? error.message : String(error) });
166
- }
167
- }
168
-
169
- function searchScopeFrom(url) {
170
- return url.searchParams.get("scope") === "all" ? "all" : "content";
171
- }
172
-
173
- function valueAfter(args, flag) {
174
- const index = args.indexOf(flag);
175
- return index >= 0 ? args[index + 1] : undefined;
176
- }
177
-
178
- async function inferWorkspaceRoot(staticRoot) {
179
- for (const candidate of [staticRoot, path.dirname(staticRoot), path.dirname(path.dirname(staticRoot))]) {
180
- // 1.0 workspace markers: press/index.tsx (the document entry) or
181
- // package.json with an "openpress" field. Either is sufficient.
182
- if (await fileExists(path.join(candidate, "press", "index.tsx"))) return candidate;
183
- if (await hasOpenpressPackageField(candidate)) return candidate;
184
- }
185
- if (path.basename(path.dirname(staticRoot)) === ".deploy") {
186
- return path.dirname(path.dirname(staticRoot));
187
- }
188
- return process.cwd();
189
- }
190
-
191
- async function hasOpenpressPackageField(dir) {
192
- try {
193
- const text = await fs.readFile(path.join(dir, "package.json"), "utf8");
194
- const parsed = JSON.parse(text);
195
- return parsed?.openpress && typeof parsed.openpress === "object";
196
- } catch {
197
- return false;
198
- }
199
- }
200
-
201
- async function handleLocalPdfExportRequest(req, res) {
202
- if (req.method !== "POST") {
203
- writeJson(res, 405, { ok: false, message: "Local PDF export endpoint requires POST." });
204
- return;
205
- }
206
-
207
- const result = await runLocalPdfExport();
208
- const exists = await fileExists(config.paths.pdf);
209
- writeJson(res, result.code === 0 && exists ? 200 : 500, {
210
- ok: result.code === 0 && exists,
211
- code: result.code,
212
- pdf: `/__openpress/local-pdf-file?ts=${Date.now()}`,
213
- command: "node engine/cli.mjs pdf .",
214
- stdout: result.stdout,
215
- stderr: result.stderr,
216
- });
217
- }
218
-
219
- async function handleLocalPdfFileRequest(req, res) {
220
- if (req.method !== "GET") {
221
- writeJson(res, 405, { ok: false, message: "Local PDF file endpoint requires GET." });
222
- return;
223
- }
224
-
225
- try {
226
- const body = await fs.readFile(config.paths.pdf);
227
- res.writeHead(200, {
228
- "Content-Type": "application/pdf",
229
- "Content-Disposition": `inline; filename="${config.pdf.filename}"`,
230
- "Cache-Control": "no-store",
231
- });
232
- res.end(body);
233
- } catch {
234
- writeJson(res, 404, { ok: false, message: "Local PDF has not been generated yet." });
235
- }
236
- }
237
-
238
- async function handleDeployRequest(req, res) {
239
- if (req.method !== "POST") {
240
- writeJson(res, 405, { ok: false, message: "Deploy endpoint requires POST." });
241
- return;
242
- }
243
-
244
- if (!isDeployConfigured()) {
245
- writeJson(res, 400, {
246
- ok: false,
247
- code: 2,
248
- message: deploySetupMessage(),
249
- deploy_configured: false,
250
- deploy_adapter: config.deploy.adapter,
251
- deploy_source: config.deploy.source,
252
- deploy_project_name: config.deploy.projectName,
253
- command: "node engine/cli.mjs deploy . --confirm",
254
- });
255
- return;
256
- }
257
-
258
- const result = await runDeploy();
259
- const deployedUrl = extractDeployUrl(result.stdout);
260
- if (result.code === 0 && deployedUrl) {
261
- await writeDeploymentPublicUrl(deployedUrl);
262
- }
263
- const deploymentInfo = await readDeploymentInfo();
264
- const publicUrl = deployedUrl ?? deploymentInfo.public_url;
265
- writeJson(res, result.code === 0 ? 200 : 500, {
266
- ok: result.code === 0,
267
- code: result.code,
268
- deployed_at: deploymentInfo.deployed_at,
269
- pdf: deployedUrl ? `${deployedUrl}/${config.pdf.filename}` : deploymentInfo.pdf,
270
- public_url: publicUrl,
271
- dirty: false,
272
- command: "node engine/cli.mjs deploy . --confirm",
273
- stdout: result.stdout,
274
- stderr: result.stderr,
275
- });
276
- }
277
-
278
- async function handleMediaUploadRequest(req, res) {
279
- if (req.method !== "POST") {
280
- writeJson(res, 405, { ok: false, message: "Media upload endpoint requires POST." });
281
- return;
282
- }
283
-
284
- const rawFileName = headerValue(req.headers["x-openpress-file-name"]);
285
- const decodedFileName = rawFileName ? safeDecodeURIComponent(rawFileName) : "";
286
- const fileName = sanitizeMediaFileName(decodedFileName);
287
- if (!fileName) {
288
- writeJson(res, 400, { ok: false, message: "Media upload requires a valid file name." });
289
- return;
290
- }
291
- if (!isAllowedMediaFile(fileName)) {
292
- writeJson(res, 400, { ok: false, message: "Only png, jpg, jpeg, gif, svg, and webp files can be uploaded." });
293
- return;
294
- }
295
-
296
- try {
297
- const body = await readRequestBuffer(req, 30 * 1024 * 1024);
298
- if (body.length === 0) {
299
- writeJson(res, 400, { ok: false, message: "Uploaded media file is empty." });
300
- return;
301
- }
302
- await fs.mkdir(config.paths.mediaDir, { recursive: true });
303
- const uniqueFileName = await uniqueMediaFileName(config.paths.mediaDir, fileName);
304
- const targetPath = path.join(config.paths.mediaDir, uniqueFileName);
305
- await fs.writeFile(targetPath, body);
306
- const relativePath = path.relative(workspace, targetPath).replaceAll("\\", "/");
307
- writeJson(res, 200, {
308
- ok: true,
309
- asset: {
310
- fileName: uniqueFileName,
311
- src: `/openpress/media/${encodeURIComponent(uniqueFileName)}`,
312
- path: relativePath,
313
- mention: `@media/${uniqueFileName}`,
314
- },
315
- });
316
- } catch (error) {
317
- writeJson(res, 500, { ok: false, message: error instanceof Error ? error.message : String(error) });
318
- }
319
- }
320
-
321
- async function handleMediaFileRequest(req, res, url) {
322
- if (req.method !== "GET" && req.method !== "HEAD") {
323
- writeJson(res, 405, { ok: false, message: "Media file endpoint requires GET." });
324
- return;
325
- }
326
-
327
- try {
328
- const fileName = sanitizeMediaFileName(safeDecodeURIComponent(url.pathname.replace(/^\/openpress\/media\/?/, "")));
329
- if (!fileName) {
330
- writeJson(res, 404, { ok: false, message: "Media file not found." });
331
- return;
332
- }
333
- const targetPath = path.join(config.paths.mediaDir, fileName);
334
- const resolvedTarget = path.resolve(targetPath);
335
- const mediaRoot = path.resolve(config.paths.mediaDir);
336
- if (!resolvedTarget.startsWith(`${mediaRoot}${path.sep}`) && resolvedTarget !== mediaRoot) {
337
- writeJson(res, 403, { ok: false, message: "Forbidden." });
338
- return;
339
- }
340
- const body = await fs.readFile(resolvedTarget);
341
- res.writeHead(200, {
342
- "Content-Type": mediaMimeType(fileName),
343
- "Cache-Control": "no-store",
344
- });
345
- if (req.method === "HEAD") {
346
- res.end();
347
- } else {
348
- res.end(body);
349
- }
350
- } catch {
351
- writeJson(res, 404, { ok: false, message: "Media file not found." });
352
- }
353
- }
354
-
355
- function runLocalPdfExport() {
356
- return new Promise((resolve) => {
357
- const child = spawn("node", ["engine/cli.mjs", "pdf", "."], {
358
- cwd: workspace,
359
- shell: false,
360
- });
361
- let stdout = "";
362
- let stderr = "";
363
- child.stdout.on("data", (chunk) => {
364
- stdout += String(chunk);
365
- });
366
- child.stderr.on("data", (chunk) => {
367
- stderr += String(chunk);
368
- });
369
- child.on("error", (error) => {
370
- resolve({ code: 1, stdout, stderr: `${stderr}${error.message}\n` });
371
- });
372
- child.on("close", (code) => {
373
- resolve({ code: code ?? 1, stdout, stderr });
374
- });
375
- });
376
- }
377
-
378
- function runDeploy() {
379
- return new Promise((resolve) => {
380
- const child = spawn("node", ["engine/cli.mjs", "deploy", ".", "--confirm"], {
381
- cwd: workspace,
382
- shell: false,
383
- });
384
- let stdout = "";
385
- let stderr = "";
386
- child.stdout.on("data", (chunk) => {
387
- stdout += String(chunk);
388
- });
389
- child.stderr.on("data", (chunk) => {
390
- stderr += String(chunk);
391
- });
392
- child.on("error", (error) => {
393
- resolve({ code: 1, stdout, stderr: `${stderr}${error.message}\n` });
394
- });
395
- child.on("close", (code) => {
396
- resolve({ code: code ?? 1, stdout, stderr });
397
- });
398
- });
399
- }
400
-
401
- function isDeployConfigured() {
402
- if (config.deploy.adapter === "cloudflare-pages") {
403
- return typeof config.deploy.projectName === "string" && config.deploy.projectName.trim().length > 0;
404
- }
405
- return true;
406
- }
407
-
408
- function deploySetupMessage() {
409
- if (isDeployConfigured()) return undefined;
410
- if (config.deploy.adapter === "cloudflare-pages") {
411
- return 'Cloudflare Pages deployment requires `openpress.deploy.projectName` in package.json.';
412
- }
413
- return `Deployment adapter \`${config.deploy.adapter}\` is not configured.`;
414
- }
415
-
416
- async function fileExists(filePath) {
417
- try {
418
- await fs.access(filePath);
419
- return true;
420
- } catch {
421
- return false;
422
- }
423
- }
424
-
425
- function writeJson(res, status, body) {
426
- res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
427
- res.end(`${JSON.stringify(body, null, 2)}\n`);
428
- }
429
-
430
- async function readDeploymentInfo() {
431
- try {
432
- const text = await fs.readFile(config.paths.deployMetadata, "utf8");
433
- const deployConfig = JSON.parse(text);
434
- return {
435
- deployed_at: typeof deployConfig.deployed_at === "string" ? deployConfig.deployed_at : undefined,
436
- pdf: typeof deployConfig.pdf === "string" ? deployConfig.pdf : publicPdfHref(config),
437
- public_url: typeof deployConfig.public_url === "string" ? deployConfig.public_url : undefined,
438
- };
439
- } catch {
440
- return { deployed_at: undefined, pdf: publicPdfHref(config), public_url: undefined };
441
- }
442
- }
443
-
444
- async function writeDeploymentPublicUrl(publicUrl) {
445
- let deployConfig = {};
446
- try {
447
- deployConfig = JSON.parse(await fs.readFile(config.paths.deployMetadata, "utf8"));
448
- } catch {
449
- deployConfig = {};
450
- }
451
- await fs.mkdir(path.dirname(config.paths.deployMetadata), { recursive: true });
452
- await fs.writeFile(
453
- config.paths.deployMetadata,
454
- `${JSON.stringify({ ...deployConfig, pdf: `${publicUrl}/${config.pdf.filename}`, public_url: publicUrl }, null, 2)}\n`,
455
- "utf8",
456
- );
457
- }
458
-
459
- async function isDeploymentDirty(deployedAt) {
460
- if (!deployedAt) return false;
461
- const deployedTime = new Date(deployedAt).getTime();
462
- if (Number.isNaN(deployedTime)) return false;
463
- const newestSourceMtime = await findNewestSourceMtime(getDeploymentSourcePaths());
464
- return newestSourceMtime > deployedTime + 1000;
465
- }
466
-
467
- function getDeploymentSourcePaths() {
468
- return [
469
- config.paths.sourceDir,
470
- config.paths.mediaDir,
471
- config.paths.themeDir,
472
- config.paths.designDoc,
473
- config.paths.componentsDir,
474
- path.join(workspace, "src"),
475
- path.join(workspace, "index.html"),
476
- path.join(workspace, "package.json"),
477
- path.join(workspace, "vite.config.ts"),
478
- ];
479
- }
480
-
481
- async function findNewestSourceMtime(paths) {
482
- const times = await Promise.all(paths.map((sourcePath) => findNewestMtime(sourcePath)));
483
- return Math.max(0, ...times);
484
- }
485
-
486
- async function findNewestMtime(sourcePath) {
487
- try {
488
- const stat = await fs.stat(sourcePath);
489
- if (!stat.isDirectory()) return stat.mtimeMs;
490
- const entries = await fs.readdir(sourcePath, { withFileTypes: true });
491
- const times = await Promise.all(entries.map((entry) => findNewestMtime(path.join(sourcePath, entry.name))));
492
- return Math.max(stat.mtimeMs, ...times);
493
- } catch {
494
- return 0;
495
- }
496
- }
497
-
498
- function headerValue(value) {
499
- return Array.isArray(value) ? value[0] : value;
500
- }
501
-
502
- function safeDecodeURIComponent(value) {
503
- try {
504
- return decodeURIComponent(value);
505
- } catch {
506
- return value;
507
- }
508
- }
509
-
510
- function sanitizeMediaFileName(value) {
511
- const baseName = path.basename(value).trim();
512
- if (!baseName) return "";
513
- const ext = path.extname(baseName);
514
- const stem = path.basename(baseName, ext)
515
- .replace(/[\\/:*?"<>|#%{}^~[\]`]/g, "-")
516
- .replace(/\s+/g, "-")
517
- .replace(/-+/g, "-")
518
- .replace(/^-|-$/g, "");
519
- if (!stem || !ext) return "";
520
- return `${stem}${ext.toLowerCase()}`;
521
- }
522
-
523
- function isAllowedMediaFile(fileName) {
524
- return /\.(png|jpe?g|gif|svg|webp)$/i.test(fileName);
525
- }
526
-
527
- function mediaMimeType(fileName) {
528
- const ext = path.extname(fileName).toLowerCase();
529
- if (ext === ".png") return "image/png";
530
- if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
531
- if (ext === ".gif") return "image/gif";
532
- if (ext === ".svg") return "image/svg+xml";
533
- if (ext === ".webp") return "image/webp";
534
- return "application/octet-stream";
535
- }
536
-
537
- async function uniqueMediaFileName(mediaDir, fileName) {
538
- const ext = path.extname(fileName);
539
- const stem = path.basename(fileName, ext);
540
- let candidate = fileName;
541
- let counter = 2;
542
- while (await fileExists(path.join(mediaDir, candidate))) {
543
- candidate = `${stem}-${counter}${ext}`;
544
- counter += 1;
545
- }
546
- return candidate;
547
- }
548
-
549
- function readRequestBuffer(req, maxBytes) {
550
- return new Promise((resolve, reject) => {
551
- const chunks = [];
552
- let total = 0;
553
- req.on("data", (chunk) => {
554
- const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
555
- total += buffer.length;
556
- if (total > maxBytes) {
557
- reject(new Error("Uploaded media file is too large."));
558
- req.destroy();
559
- return;
560
- }
561
- chunks.push(buffer);
562
- });
563
- req.on("end", () => resolve(Buffer.concat(chunks)));
564
- req.on("error", reject);
565
- });
566
- }
567
-
568
- function extractDeployUrl(output) {
569
- const match = output.match(/https:\/\/[^\s]+\.pages\.dev/);
570
- return match?.[0]?.replace(/\/$/, "");
571
- }
@@ -1,73 +0,0 @@
1
- const DEFAULT_CAPTION_NUMBERING = {
2
- figure: "Figure",
3
- table: "Table",
4
- separator: " ",
5
- };
6
-
7
- export function normalizeCaptionNumbering(value = {}) {
8
- const input = value && typeof value === "object" && !Array.isArray(value) ? value : {};
9
- return {
10
- figure: stringOption(input.figure, DEFAULT_CAPTION_NUMBERING.figure),
11
- table: stringOption(input.table, DEFAULT_CAPTION_NUMBERING.table),
12
- separator: typeof input.separator === "string" ? input.separator : DEFAULT_CAPTION_NUMBERING.separator,
13
- };
14
- }
15
-
16
- export function createCaptionNumberingState() {
17
- return {
18
- figure: 0,
19
- table: 0,
20
- seenTables: new Set(),
21
- };
22
- }
23
-
24
- export function numberCaptionsInHtml(html, numbering, state = createCaptionNumberingState()) {
25
- if (!html) return html;
26
- const options = normalizeCaptionNumbering(numbering);
27
- let out = String(html);
28
- out = numberTableCaptions(out, options, state);
29
- out = numberFigureCaptions(out, options, state);
30
- return out;
31
- }
32
-
33
- function numberTableCaptions(html, options, state) {
34
- return html.replace(/<table\b([^>]*)>([\s\S]*?<caption\b([^>]*)>)([\s\S]*?)(<\/caption>[\s\S]*?<\/table>)/g, (match, tableAttrs, beforeCaptionText, captionAttrs, captionText, afterCaptionText) => {
35
- if (captionText.includes("data-openpress-caption-label=")) return match;
36
- const tableId = attrValue(tableAttrs, "data-openpress-table-id");
37
- if (tableId && state.seenTables.has(tableId)) return match;
38
- if (tableId) state.seenTables.add(tableId);
39
- state.table += 1;
40
- const label = captionLabel(options.table, state.table, options.separator);
41
- return `<table${tableAttrs}>${beforeCaptionText}${captionLabelSpan("table", state.table, label)} ${captionText}${afterCaptionText}`;
42
- });
43
- }
44
-
45
- function numberFigureCaptions(html, options, state) {
46
- return html.replace(/<figure\b([^>]*)>([\s\S]*?<figcaption\b([^>]*)>)([\s\S]*?)(<\/figcaption>[\s\S]*?<\/figure>)/g, (match, figureAttrs, beforeCaptionText, captionAttrs, captionText, afterCaptionText) => {
47
- if (captionText.includes("data-openpress-caption-label=")) return match;
48
- state.figure += 1;
49
- const label = captionLabel(options.figure, state.figure, options.separator);
50
- return `<figure${figureAttrs}>${beforeCaptionText}${captionLabelSpan("figure", state.figure, label)} ${captionText}${afterCaptionText}`;
51
- });
52
- }
53
-
54
- function captionLabel(noun, number, separator) {
55
- return `${noun}${separator}${number}`;
56
- }
57
-
58
- function captionLabelSpan(kind, number, label) {
59
- return `<span class="openpress-caption-label" data-openpress-caption-label="${kind}" data-openpress-caption-number="${number}">${escapeHtml(label)}</span>`;
60
- }
61
-
62
- function attrValue(attrs, name) {
63
- const pattern = new RegExp(`${name}=(["'])(.*?)\\1`);
64
- return attrs.match(pattern)?.[2] ?? "";
65
- }
66
-
67
- function stringOption(value, fallback) {
68
- return typeof value === "string" && value.trim() ? value.trim() : fallback;
69
- }
70
-
71
- function escapeHtml(value) {
72
- return String(value).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
73
- }
@@ -1,11 +0,0 @@
1
- import type { IncomingMessage, ServerResponse } from "node:http";
2
-
3
- export function handleCommentRequest(
4
- req: IncomingMessage,
5
- res: ServerResponse,
6
- options?: {
7
- root?: string;
8
- id?: string;
9
- timestamp?: string;
10
- },
11
- ): Promise<void>;