@open-press/core 0.3.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.
- package/LICENSE +21 -0
- package/README.md +36 -0
- package/engine/chrome-pdf.d.mts +34 -0
- package/engine/chrome-pdf.mjs +344 -0
- package/engine/cli.mjs +93 -0
- package/engine/commands/_shared.mjs +170 -0
- package/engine/commands/deploy.mjs +31 -0
- package/engine/commands/dev.mjs +26 -0
- package/engine/commands/export.mjs +8 -0
- package/engine/commands/init.mjs +24 -0
- package/engine/commands/inspect.mjs +35 -0
- package/engine/commands/migrate-to-react.mjs +27 -0
- package/engine/commands/pdf.mjs +26 -0
- package/engine/commands/preview.mjs +26 -0
- package/engine/commands/render.mjs +17 -0
- package/engine/commands/replace.mjs +41 -0
- package/engine/commands/search.mjs +33 -0
- package/engine/commands/typecheck.mjs +5 -0
- package/engine/commands/validate.mjs +17 -0
- package/engine/config.d.mts +40 -0
- package/engine/config.mjs +160 -0
- package/engine/deploy-sync.mjs +15 -0
- package/engine/document-export.mjs +15 -0
- package/engine/file-utils.mjs +106 -0
- package/engine/fonts.mjs +62 -0
- package/engine/init.mjs +90 -0
- package/engine/inspection.mjs +348 -0
- package/engine/issue-report.mjs +44 -0
- package/engine/katex-assets.mjs +45 -0
- package/engine/page-block.mjs +30 -0
- package/engine/page-renderer.mjs +217 -0
- package/engine/pdf-media.mjs +45 -0
- package/engine/public-assets.mjs +19 -0
- package/engine/react/chapter-css.mjs +53 -0
- package/engine/react/comment-endpoint.d.mts +11 -0
- package/engine/react/comment-endpoint.mjs +128 -0
- package/engine/react/comment-marker.mjs +306 -0
- package/engine/react/document-entry.mjs +253 -0
- package/engine/react/document-export.mjs +392 -0
- package/engine/react/mdx-compile.mjs +295 -0
- package/engine/react/measurement-css.mjs +44 -0
- package/engine/react/migrate-to-react.mjs +355 -0
- package/engine/react/pagination-constants.mjs +3 -0
- package/engine/react/pagination.mjs +121 -0
- package/engine/react/project-asset-endpoint.d.mts +10 -0
- package/engine/react/project-asset-endpoint.mjs +379 -0
- package/engine/react/workspace-discovery.mjs +156 -0
- package/engine/source-text-tools.mjs +280 -0
- package/engine/source-workspace.mjs +76 -0
- package/engine/static-server.mjs +493 -0
- package/engine/validation.mjs +172 -0
- package/index.html +13 -0
- package/package.json +86 -0
- package/src/openpress/App.tsx +127 -0
- package/src/openpress/composerMentions.ts +188 -0
- package/src/openpress/core/basePages.tsx +87 -0
- package/src/openpress/core/index.tsx +20 -0
- package/src/openpress/core/types.ts +71 -0
- package/src/openpress/frameScheduler.ts +32 -0
- package/src/openpress/indexes.ts +329 -0
- package/src/openpress/inspector.ts +282 -0
- package/src/openpress/pageRoute.ts +21 -0
- package/src/openpress/pagination.ts +845 -0
- package/src/openpress/projectIdentity.ts +15 -0
- package/src/openpress/projectSources.ts +24 -0
- package/src/openpress/projectWorkspace.tsx +919 -0
- package/src/openpress/publicPage.tsx +469 -0
- package/src/openpress/reactDocumentMetadata.ts +41 -0
- package/src/openpress/readerPageRegistry.ts +41 -0
- package/src/openpress/readerRuntime.ts +230 -0
- package/src/openpress/readerScroll.ts +92 -0
- package/src/openpress/readerState.ts +15 -0
- package/src/openpress/renderer.tsx +91 -0
- package/src/openpress/runtimeMode.ts +22 -0
- package/src/openpress/types.ts +112 -0
- package/src/openpress/workbench.tsx +1299 -0
- package/src/openpress/workbenchPanels.tsx +122 -0
- package/src/openpress/workbenchTypes.ts +4 -0
- package/src/styles/openpress/app-shell.css +251 -0
- package/src/styles/openpress/media-workspace.css +230 -0
- package/src/styles/openpress/print-route.css +186 -0
- package/src/styles/openpress/project-workspace.css +1318 -0
- package/src/styles/openpress/public-viewer.css +983 -0
- package/src/styles/openpress/reader-runtime.css +792 -0
- package/src/styles/openpress/responsive.css +384 -0
- package/src/styles/openpress/workbench-panels.css +558 -0
- package/src/styles/openpress/workbench.css +720 -0
- package/src/styles/openpress.css +14 -0
- package/tsconfig.json +37 -0
- package/vite.config.ts +512 -0
|
@@ -0,0 +1,493 @@
|
|
|
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 "./config.mjs";
|
|
6
|
+
import { handleProjectAssetRequest } from "./react/project-asset-endpoint.mjs";
|
|
7
|
+
|
|
8
|
+
const [rootArg = "dist", ...rest] = process.argv.slice(2);
|
|
9
|
+
const host = valueAfter(rest, "--host") ?? "127.0.0.1";
|
|
10
|
+
const port = Number(valueAfter(rest, "--port") ?? "8765");
|
|
11
|
+
const root = path.resolve(rootArg);
|
|
12
|
+
const workspace = path.resolve(valueAfter(rest, "--workspace") ?? await inferWorkspaceRoot(root));
|
|
13
|
+
const config = await loadConfig(workspace);
|
|
14
|
+
|
|
15
|
+
const mimeTypes = {
|
|
16
|
+
".html": "text/html; charset=utf-8",
|
|
17
|
+
".css": "text/css; charset=utf-8",
|
|
18
|
+
".js": "application/javascript; charset=utf-8",
|
|
19
|
+
".json": "application/json; charset=utf-8",
|
|
20
|
+
".png": "image/png",
|
|
21
|
+
".jpg": "image/jpeg",
|
|
22
|
+
".jpeg": "image/jpeg",
|
|
23
|
+
".gif": "image/gif",
|
|
24
|
+
".svg": "image/svg+xml",
|
|
25
|
+
".pdf": "application/pdf",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const server = http.createServer(async (req, res) => {
|
|
29
|
+
try {
|
|
30
|
+
const url = new URL(req.url ?? "/", `http://${host}:${port}`);
|
|
31
|
+
if (url.pathname === "/__openpress/status") {
|
|
32
|
+
await handleStatusRequest(req, res);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (url.pathname === "/__openpress/local-pdf-export") {
|
|
36
|
+
await handleLocalPdfExportRequest(req, res);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (url.pathname === "/__openpress/local-pdf-file") {
|
|
40
|
+
await handleLocalPdfFileRequest(req, res);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (url.pathname === "/__openpress/deploy") {
|
|
44
|
+
await handleDeployRequest(req, res);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (url.pathname === "/__openpress/media-upload") {
|
|
48
|
+
await handleMediaUploadRequest(req, res);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (url.pathname === "/__openpress/project-asset") {
|
|
52
|
+
await handleProjectAssetRequest(req, res, { root: workspace });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (url.pathname.startsWith("/openpress/media/")) {
|
|
56
|
+
await handleMediaFileRequest(req, res, url);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const requested = decodeURIComponent(url.pathname === "/" ? "/index.html" : url.pathname);
|
|
60
|
+
const target = path.resolve(root, `.${requested}`);
|
|
61
|
+
if (!target.startsWith(root)) {
|
|
62
|
+
res.writeHead(403);
|
|
63
|
+
res.end("Forbidden");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const stat = await fs.stat(target);
|
|
67
|
+
const filePath = stat.isDirectory() ? path.join(target, "index.html") : target;
|
|
68
|
+
const body = await fs.readFile(filePath);
|
|
69
|
+
res.writeHead(200, { "Content-Type": mimeTypes[path.extname(filePath)] ?? "application/octet-stream" });
|
|
70
|
+
res.end(body);
|
|
71
|
+
} catch {
|
|
72
|
+
res.writeHead(404);
|
|
73
|
+
res.end("Not found");
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
server.listen(port, host, () => {
|
|
78
|
+
console.log(`OpenPress static preview: http://${host}:${port}/`);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
async function handleStatusRequest(req, res) {
|
|
82
|
+
if (req.method !== "GET") {
|
|
83
|
+
writeJson(res, 405, { ok: false, message: "Status endpoint requires GET." });
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const deployConfigured = isDeployConfigured();
|
|
88
|
+
const deploymentInfo = deployConfigured
|
|
89
|
+
? await readDeploymentInfo()
|
|
90
|
+
: { deployed_at: undefined, pdf: publicPdfHref(config), public_url: undefined };
|
|
91
|
+
const dirty = deployConfigured ? await isDeploymentDirty(deploymentInfo.deployed_at) : false;
|
|
92
|
+
writeJson(res, 200, {
|
|
93
|
+
ok: true,
|
|
94
|
+
deployed_at: deploymentInfo.deployed_at,
|
|
95
|
+
pdf: deploymentInfo.pdf,
|
|
96
|
+
public_url: deploymentInfo.public_url,
|
|
97
|
+
dirty,
|
|
98
|
+
deploy_configured: deployConfigured,
|
|
99
|
+
deploy_adapter: config.deploy.adapter,
|
|
100
|
+
deploy_source: config.deploy.source,
|
|
101
|
+
deploy_project_name: config.deploy.projectName,
|
|
102
|
+
deploy_setup_message: deploySetupMessage(),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function valueAfter(args, flag) {
|
|
107
|
+
const index = args.indexOf(flag);
|
|
108
|
+
return index >= 0 ? args[index + 1] : undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function inferWorkspaceRoot(staticRoot) {
|
|
112
|
+
for (const candidate of [staticRoot, path.dirname(staticRoot), path.dirname(path.dirname(staticRoot))]) {
|
|
113
|
+
if (await fileExists(path.join(candidate, "openpress.config.mjs"))) return candidate;
|
|
114
|
+
}
|
|
115
|
+
if (path.basename(path.dirname(staticRoot)) === ".deploy") {
|
|
116
|
+
return path.dirname(path.dirname(staticRoot));
|
|
117
|
+
}
|
|
118
|
+
return process.cwd();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function handleLocalPdfExportRequest(req, res) {
|
|
122
|
+
if (req.method !== "POST") {
|
|
123
|
+
writeJson(res, 405, { ok: false, message: "Local PDF export endpoint requires POST." });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const result = await runLocalPdfExport();
|
|
128
|
+
const exists = await fileExists(config.paths.pdf);
|
|
129
|
+
writeJson(res, result.code === 0 && exists ? 200 : 500, {
|
|
130
|
+
ok: result.code === 0 && exists,
|
|
131
|
+
code: result.code,
|
|
132
|
+
pdf: `/__openpress/local-pdf-file?ts=${Date.now()}`,
|
|
133
|
+
command: "node engine/cli.mjs pdf .",
|
|
134
|
+
stdout: result.stdout,
|
|
135
|
+
stderr: result.stderr,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function handleLocalPdfFileRequest(req, res) {
|
|
140
|
+
if (req.method !== "GET") {
|
|
141
|
+
writeJson(res, 405, { ok: false, message: "Local PDF file endpoint requires GET." });
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
const body = await fs.readFile(config.paths.pdf);
|
|
147
|
+
res.writeHead(200, {
|
|
148
|
+
"Content-Type": "application/pdf",
|
|
149
|
+
"Content-Disposition": `inline; filename="${config.pdf.filename}"`,
|
|
150
|
+
"Cache-Control": "no-store",
|
|
151
|
+
});
|
|
152
|
+
res.end(body);
|
|
153
|
+
} catch {
|
|
154
|
+
writeJson(res, 404, { ok: false, message: "Local PDF has not been generated yet." });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function handleDeployRequest(req, res) {
|
|
159
|
+
if (req.method !== "POST") {
|
|
160
|
+
writeJson(res, 405, { ok: false, message: "Deploy endpoint requires POST." });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!isDeployConfigured()) {
|
|
165
|
+
writeJson(res, 400, {
|
|
166
|
+
ok: false,
|
|
167
|
+
code: 2,
|
|
168
|
+
message: deploySetupMessage(),
|
|
169
|
+
deploy_configured: false,
|
|
170
|
+
deploy_adapter: config.deploy.adapter,
|
|
171
|
+
deploy_source: config.deploy.source,
|
|
172
|
+
deploy_project_name: config.deploy.projectName,
|
|
173
|
+
command: "node engine/cli.mjs deploy . --confirm",
|
|
174
|
+
});
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const result = await runDeploy();
|
|
179
|
+
const deployedUrl = extractDeployUrl(result.stdout);
|
|
180
|
+
if (result.code === 0 && deployedUrl) {
|
|
181
|
+
await writeDeploymentPublicUrl(deployedUrl);
|
|
182
|
+
}
|
|
183
|
+
const deploymentInfo = await readDeploymentInfo();
|
|
184
|
+
const publicUrl = deployedUrl ?? deploymentInfo.public_url;
|
|
185
|
+
writeJson(res, result.code === 0 ? 200 : 500, {
|
|
186
|
+
ok: result.code === 0,
|
|
187
|
+
code: result.code,
|
|
188
|
+
deployed_at: deploymentInfo.deployed_at,
|
|
189
|
+
pdf: deployedUrl ? `${deployedUrl}/${config.pdf.filename}` : deploymentInfo.pdf,
|
|
190
|
+
public_url: publicUrl,
|
|
191
|
+
dirty: false,
|
|
192
|
+
command: "node engine/cli.mjs deploy . --confirm",
|
|
193
|
+
stdout: result.stdout,
|
|
194
|
+
stderr: result.stderr,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function handleMediaUploadRequest(req, res) {
|
|
199
|
+
if (req.method !== "POST") {
|
|
200
|
+
writeJson(res, 405, { ok: false, message: "Media upload endpoint requires POST." });
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const rawFileName = headerValue(req.headers["x-openpress-file-name"]);
|
|
205
|
+
const decodedFileName = rawFileName ? safeDecodeURIComponent(rawFileName) : "";
|
|
206
|
+
const fileName = sanitizeMediaFileName(decodedFileName);
|
|
207
|
+
if (!fileName) {
|
|
208
|
+
writeJson(res, 400, { ok: false, message: "Media upload requires a valid file name." });
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (!isAllowedMediaFile(fileName)) {
|
|
212
|
+
writeJson(res, 400, { ok: false, message: "Only png, jpg, jpeg, gif, svg, and webp files can be uploaded." });
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
const body = await readRequestBuffer(req, 30 * 1024 * 1024);
|
|
218
|
+
if (body.length === 0) {
|
|
219
|
+
writeJson(res, 400, { ok: false, message: "Uploaded media file is empty." });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
await fs.mkdir(config.paths.mediaDir, { recursive: true });
|
|
223
|
+
const uniqueFileName = await uniqueMediaFileName(config.paths.mediaDir, fileName);
|
|
224
|
+
const targetPath = path.join(config.paths.mediaDir, uniqueFileName);
|
|
225
|
+
await fs.writeFile(targetPath, body);
|
|
226
|
+
const relativePath = path.relative(workspace, targetPath).replaceAll("\\", "/");
|
|
227
|
+
writeJson(res, 200, {
|
|
228
|
+
ok: true,
|
|
229
|
+
asset: {
|
|
230
|
+
fileName: uniqueFileName,
|
|
231
|
+
src: `/openpress/media/${encodeURIComponent(uniqueFileName)}`,
|
|
232
|
+
path: relativePath,
|
|
233
|
+
mention: `@media/${uniqueFileName}`,
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
} catch (error) {
|
|
237
|
+
writeJson(res, 500, { ok: false, message: error instanceof Error ? error.message : String(error) });
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function handleMediaFileRequest(req, res, url) {
|
|
242
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
243
|
+
writeJson(res, 405, { ok: false, message: "Media file endpoint requires GET." });
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
try {
|
|
248
|
+
const fileName = sanitizeMediaFileName(safeDecodeURIComponent(url.pathname.replace(/^\/openpress\/media\/?/, "")));
|
|
249
|
+
if (!fileName) {
|
|
250
|
+
writeJson(res, 404, { ok: false, message: "Media file not found." });
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const targetPath = path.join(config.paths.mediaDir, fileName);
|
|
254
|
+
const resolvedTarget = path.resolve(targetPath);
|
|
255
|
+
const mediaRoot = path.resolve(config.paths.mediaDir);
|
|
256
|
+
if (!resolvedTarget.startsWith(`${mediaRoot}${path.sep}`) && resolvedTarget !== mediaRoot) {
|
|
257
|
+
writeJson(res, 403, { ok: false, message: "Forbidden." });
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const body = await fs.readFile(resolvedTarget);
|
|
261
|
+
res.writeHead(200, {
|
|
262
|
+
"Content-Type": mediaMimeType(fileName),
|
|
263
|
+
"Cache-Control": "no-store",
|
|
264
|
+
});
|
|
265
|
+
if (req.method === "HEAD") {
|
|
266
|
+
res.end();
|
|
267
|
+
} else {
|
|
268
|
+
res.end(body);
|
|
269
|
+
}
|
|
270
|
+
} catch {
|
|
271
|
+
writeJson(res, 404, { ok: false, message: "Media file not found." });
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function runLocalPdfExport() {
|
|
276
|
+
return new Promise((resolve) => {
|
|
277
|
+
const child = spawn("node", ["engine/cli.mjs", "pdf", "."], {
|
|
278
|
+
cwd: workspace,
|
|
279
|
+
shell: false,
|
|
280
|
+
});
|
|
281
|
+
let stdout = "";
|
|
282
|
+
let stderr = "";
|
|
283
|
+
child.stdout.on("data", (chunk) => {
|
|
284
|
+
stdout += String(chunk);
|
|
285
|
+
});
|
|
286
|
+
child.stderr.on("data", (chunk) => {
|
|
287
|
+
stderr += String(chunk);
|
|
288
|
+
});
|
|
289
|
+
child.on("error", (error) => {
|
|
290
|
+
resolve({ code: 1, stdout, stderr: `${stderr}${error.message}\n` });
|
|
291
|
+
});
|
|
292
|
+
child.on("close", (code) => {
|
|
293
|
+
resolve({ code: code ?? 1, stdout, stderr });
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function runDeploy() {
|
|
299
|
+
return new Promise((resolve) => {
|
|
300
|
+
const child = spawn("node", ["engine/cli.mjs", "deploy", ".", "--confirm"], {
|
|
301
|
+
cwd: workspace,
|
|
302
|
+
shell: false,
|
|
303
|
+
});
|
|
304
|
+
let stdout = "";
|
|
305
|
+
let stderr = "";
|
|
306
|
+
child.stdout.on("data", (chunk) => {
|
|
307
|
+
stdout += String(chunk);
|
|
308
|
+
});
|
|
309
|
+
child.stderr.on("data", (chunk) => {
|
|
310
|
+
stderr += String(chunk);
|
|
311
|
+
});
|
|
312
|
+
child.on("error", (error) => {
|
|
313
|
+
resolve({ code: 1, stdout, stderr: `${stderr}${error.message}\n` });
|
|
314
|
+
});
|
|
315
|
+
child.on("close", (code) => {
|
|
316
|
+
resolve({ code: code ?? 1, stdout, stderr });
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function isDeployConfigured() {
|
|
322
|
+
if (config.deploy.adapter === "cloudflare-pages") {
|
|
323
|
+
return typeof config.deploy.projectName === "string" && config.deploy.projectName.trim().length > 0;
|
|
324
|
+
}
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function deploySetupMessage() {
|
|
329
|
+
if (isDeployConfigured()) return undefined;
|
|
330
|
+
if (config.deploy.adapter === "cloudflare-pages") {
|
|
331
|
+
return "Cloudflare Pages deployment requires `deploy.projectName` in openpress.config.mjs.";
|
|
332
|
+
}
|
|
333
|
+
return `Deployment adapter \`${config.deploy.adapter}\` is not configured.`;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function fileExists(filePath) {
|
|
337
|
+
try {
|
|
338
|
+
await fs.access(filePath);
|
|
339
|
+
return true;
|
|
340
|
+
} catch {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function writeJson(res, status, body) {
|
|
346
|
+
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
|
347
|
+
res.end(`${JSON.stringify(body, null, 2)}\n`);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async function readDeploymentInfo() {
|
|
351
|
+
try {
|
|
352
|
+
const text = await fs.readFile(config.paths.deployMetadata, "utf8");
|
|
353
|
+
const deployConfig = JSON.parse(text);
|
|
354
|
+
return {
|
|
355
|
+
deployed_at: typeof deployConfig.deployed_at === "string" ? deployConfig.deployed_at : undefined,
|
|
356
|
+
pdf: typeof deployConfig.pdf === "string" ? deployConfig.pdf : publicPdfHref(config),
|
|
357
|
+
public_url: typeof deployConfig.public_url === "string" ? deployConfig.public_url : undefined,
|
|
358
|
+
};
|
|
359
|
+
} catch {
|
|
360
|
+
return { deployed_at: undefined, pdf: publicPdfHref(config), public_url: undefined };
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
async function writeDeploymentPublicUrl(publicUrl) {
|
|
365
|
+
let deployConfig = {};
|
|
366
|
+
try {
|
|
367
|
+
deployConfig = JSON.parse(await fs.readFile(config.paths.deployMetadata, "utf8"));
|
|
368
|
+
} catch {
|
|
369
|
+
deployConfig = {};
|
|
370
|
+
}
|
|
371
|
+
await fs.mkdir(path.dirname(config.paths.deployMetadata), { recursive: true });
|
|
372
|
+
await fs.writeFile(
|
|
373
|
+
config.paths.deployMetadata,
|
|
374
|
+
`${JSON.stringify({ ...deployConfig, pdf: `${publicUrl}/${config.pdf.filename}`, public_url: publicUrl }, null, 2)}\n`,
|
|
375
|
+
"utf8",
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async function isDeploymentDirty(deployedAt) {
|
|
380
|
+
if (!deployedAt) return false;
|
|
381
|
+
const deployedTime = new Date(deployedAt).getTime();
|
|
382
|
+
if (Number.isNaN(deployedTime)) return false;
|
|
383
|
+
const newestSourceMtime = await findNewestSourceMtime(getDeploymentSourcePaths());
|
|
384
|
+
return newestSourceMtime > deployedTime + 1000;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function getDeploymentSourcePaths() {
|
|
388
|
+
return [
|
|
389
|
+
config.paths.sourceDir,
|
|
390
|
+
config.paths.mediaDir,
|
|
391
|
+
config.paths.themeDir,
|
|
392
|
+
config.paths.designDoc,
|
|
393
|
+
config.paths.componentsDir,
|
|
394
|
+
path.join(workspace, "src"),
|
|
395
|
+
path.join(workspace, "index.html"),
|
|
396
|
+
path.join(workspace, "package.json"),
|
|
397
|
+
path.join(workspace, "openpress.config.mjs"),
|
|
398
|
+
config.configPath,
|
|
399
|
+
path.join(workspace, "vite.config.ts"),
|
|
400
|
+
];
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
async function findNewestSourceMtime(paths) {
|
|
404
|
+
const times = await Promise.all(paths.map((sourcePath) => findNewestMtime(sourcePath)));
|
|
405
|
+
return Math.max(0, ...times);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
async function findNewestMtime(sourcePath) {
|
|
409
|
+
try {
|
|
410
|
+
const stat = await fs.stat(sourcePath);
|
|
411
|
+
if (!stat.isDirectory()) return stat.mtimeMs;
|
|
412
|
+
const entries = await fs.readdir(sourcePath, { withFileTypes: true });
|
|
413
|
+
const times = await Promise.all(entries.map((entry) => findNewestMtime(path.join(sourcePath, entry.name))));
|
|
414
|
+
return Math.max(stat.mtimeMs, ...times);
|
|
415
|
+
} catch {
|
|
416
|
+
return 0;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function headerValue(value) {
|
|
421
|
+
return Array.isArray(value) ? value[0] : value;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function safeDecodeURIComponent(value) {
|
|
425
|
+
try {
|
|
426
|
+
return decodeURIComponent(value);
|
|
427
|
+
} catch {
|
|
428
|
+
return value;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function sanitizeMediaFileName(value) {
|
|
433
|
+
const baseName = path.basename(value).trim();
|
|
434
|
+
if (!baseName) return "";
|
|
435
|
+
const ext = path.extname(baseName);
|
|
436
|
+
const stem = path.basename(baseName, ext)
|
|
437
|
+
.replace(/[\\/:*?"<>|#%{}^~[\]`]/g, "-")
|
|
438
|
+
.replace(/\s+/g, "-")
|
|
439
|
+
.replace(/-+/g, "-")
|
|
440
|
+
.replace(/^-|-$/g, "");
|
|
441
|
+
if (!stem || !ext) return "";
|
|
442
|
+
return `${stem}${ext.toLowerCase()}`;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function isAllowedMediaFile(fileName) {
|
|
446
|
+
return /\.(png|jpe?g|gif|svg|webp)$/i.test(fileName);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function mediaMimeType(fileName) {
|
|
450
|
+
const ext = path.extname(fileName).toLowerCase();
|
|
451
|
+
if (ext === ".png") return "image/png";
|
|
452
|
+
if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
|
|
453
|
+
if (ext === ".gif") return "image/gif";
|
|
454
|
+
if (ext === ".svg") return "image/svg+xml";
|
|
455
|
+
if (ext === ".webp") return "image/webp";
|
|
456
|
+
return "application/octet-stream";
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
async function uniqueMediaFileName(mediaDir, fileName) {
|
|
460
|
+
const ext = path.extname(fileName);
|
|
461
|
+
const stem = path.basename(fileName, ext);
|
|
462
|
+
let candidate = fileName;
|
|
463
|
+
let counter = 2;
|
|
464
|
+
while (await fileExists(path.join(mediaDir, candidate))) {
|
|
465
|
+
candidate = `${stem}-${counter}${ext}`;
|
|
466
|
+
counter += 1;
|
|
467
|
+
}
|
|
468
|
+
return candidate;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function readRequestBuffer(req, maxBytes) {
|
|
472
|
+
return new Promise((resolve, reject) => {
|
|
473
|
+
const chunks = [];
|
|
474
|
+
let total = 0;
|
|
475
|
+
req.on("data", (chunk) => {
|
|
476
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
477
|
+
total += buffer.length;
|
|
478
|
+
if (total > maxBytes) {
|
|
479
|
+
reject(new Error("Uploaded media file is too large."));
|
|
480
|
+
req.destroy();
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
chunks.push(buffer);
|
|
484
|
+
});
|
|
485
|
+
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
486
|
+
req.on("error", reject);
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function extractDeployUrl(output) {
|
|
491
|
+
const match = output.match(/https:\/\/[^\s]+\.pages\.dev/);
|
|
492
|
+
return match?.[0]?.replace(/\/$/, "");
|
|
493
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { loadConfig } from "./config.mjs";
|
|
4
|
+
import { createIssue, createIssueReport } from "./issue-report.mjs";
|
|
5
|
+
import { collectSourceTextFiles } from "./source-text-tools.mjs";
|
|
6
|
+
import { collectActiveContentFiles, resolveActiveSourceWorkspace, sourceDirectoryExists } from "./source-workspace.mjs";
|
|
7
|
+
|
|
8
|
+
// Adapters that publish the document to a URL anyone on the internet can reach.
|
|
9
|
+
// `deploy.requiresConfirmation: true` is mandatory for these so an automated
|
|
10
|
+
// pipeline (or a careless command) cannot ship without an explicit human step.
|
|
11
|
+
// Adapters not in this set (local file copy, custom in-house tooling, null)
|
|
12
|
+
// can opt out of the confirmation gate.
|
|
13
|
+
const PUBLIC_DEPLOY_ADAPTERS = new Set([
|
|
14
|
+
"cloudflare-pages",
|
|
15
|
+
"github-pages",
|
|
16
|
+
"netlify",
|
|
17
|
+
"vercel",
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
export async function discoverWorkspace(startPath = ".") {
|
|
21
|
+
let current = path.resolve(startPath);
|
|
22
|
+
try {
|
|
23
|
+
const stat = await fs.stat(current);
|
|
24
|
+
if (!stat.isDirectory()) current = path.dirname(current);
|
|
25
|
+
} catch {
|
|
26
|
+
current = path.dirname(current);
|
|
27
|
+
}
|
|
28
|
+
while (true) {
|
|
29
|
+
const configPath = path.join(current, "openpress.config.mjs");
|
|
30
|
+
try {
|
|
31
|
+
await fs.access(configPath);
|
|
32
|
+
return current;
|
|
33
|
+
} catch {
|
|
34
|
+
const parent = path.dirname(current);
|
|
35
|
+
if (parent === current) throw new Error(`No OpenPress workspace found from ${startPath}`);
|
|
36
|
+
current = parent;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function validateWorkspace(root) {
|
|
42
|
+
const config = await loadConfig(root);
|
|
43
|
+
const sourceWorkspace = await resolveActiveSourceWorkspace(config);
|
|
44
|
+
const activeConfig = sourceWorkspace.config;
|
|
45
|
+
const issues = [];
|
|
46
|
+
const checked = [];
|
|
47
|
+
const mark = (name) => {
|
|
48
|
+
if (!checked.includes(name)) checked.push(name);
|
|
49
|
+
};
|
|
50
|
+
const add = (level, code, message, filePath = null, detail = undefined) => issues.push(createIssue({ level, code, message, path: filePath, detail }));
|
|
51
|
+
|
|
52
|
+
mark("config");
|
|
53
|
+
for (const [key, target] of [
|
|
54
|
+
["sourceDir", sourceWorkspace.sourceDir],
|
|
55
|
+
["mediaDir", activeConfig.paths.mediaDir],
|
|
56
|
+
["themeDir", activeConfig.paths.themeDir],
|
|
57
|
+
["designDoc", activeConfig.paths.designDoc],
|
|
58
|
+
["componentsDir", activeConfig.paths.componentsDir],
|
|
59
|
+
]) {
|
|
60
|
+
if (!(await exists(target))) add("error", `config.${key}`, `Configured OpenPress path \`${key}\` does not exist.`, target);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
mark("design-doc");
|
|
64
|
+
const designDoc = activeConfig.paths.designDoc;
|
|
65
|
+
if (!(await exists(designDoc))) {
|
|
66
|
+
add("error", "design-doc.missing", "Design document must exist.", designDoc);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
mark("deploy-gate");
|
|
70
|
+
if (PUBLIC_DEPLOY_ADAPTERS.has(config.deploy.adapter) && config.deploy.requiresConfirmation !== true) {
|
|
71
|
+
add(
|
|
72
|
+
"error",
|
|
73
|
+
"deploy.confirmation",
|
|
74
|
+
`Public deploy adapter \`${config.deploy.adapter}\` must require user confirmation (set deploy.requiresConfirmation: true).`,
|
|
75
|
+
config.configPath,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
mark(sourceWorkspace.checkedName);
|
|
80
|
+
if (!(typeof activeConfig.title === "string" && activeConfig.title.trim())) {
|
|
81
|
+
add("warning", "config.title", "openpress.config.mjs `title` is empty; the workbench will show the default placeholder.", activeConfig.configPath);
|
|
82
|
+
}
|
|
83
|
+
if (!(await sourceDirectoryExists(sourceWorkspace))) {
|
|
84
|
+
add("warning", sourceWorkspace.missingCode, sourceWorkspace.missingMessage, sourceWorkspace.sourceDir);
|
|
85
|
+
} else {
|
|
86
|
+
const contentFiles = await collectActiveContentFiles(sourceWorkspace, { skipUnderscoreFiles: true });
|
|
87
|
+
if (contentFiles.length === 0) {
|
|
88
|
+
add("warning", sourceWorkspace.emptyCode, sourceWorkspace.emptyMessage, sourceWorkspace.sourceDir);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (sourceWorkspace.kind === "react-mdx") {
|
|
93
|
+
mark("react-comments");
|
|
94
|
+
const sourceFiles = await collectSourceTextFiles(activeConfig, { scope: "all" });
|
|
95
|
+
for (const file of sourceFiles) {
|
|
96
|
+
for (const marker of findCommentMarkers(file.text)) {
|
|
97
|
+
add(
|
|
98
|
+
"warning",
|
|
99
|
+
"react-comments.pending",
|
|
100
|
+
`Pending OpenPress comment \`${marker.id}\` remains in React source; run apply-comments or resolve it manually before publishing.`,
|
|
101
|
+
file.absolutePath,
|
|
102
|
+
{
|
|
103
|
+
id: marker.id,
|
|
104
|
+
line: marker.line,
|
|
105
|
+
path: file.path ?? file.relativePath,
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
mark("react-pagination");
|
|
113
|
+
const documentJsonPath = path.join(activeConfig.paths.publicDir, "document.json");
|
|
114
|
+
const exportedDocument = await readJsonIfExists(documentJsonPath);
|
|
115
|
+
const paginationWarnings = exportedDocument?.source?.pagination?.warnings;
|
|
116
|
+
if (Array.isArray(paginationWarnings)) {
|
|
117
|
+
for (const warning of paginationWarnings) {
|
|
118
|
+
if (warning?.code !== "block-overflows-page") continue;
|
|
119
|
+
const warningPath = typeof warning.path === "string" && warning.path
|
|
120
|
+
? path.resolve(activeConfig.root, warning.path)
|
|
121
|
+
: documentJsonPath;
|
|
122
|
+
add(
|
|
123
|
+
"warning",
|
|
124
|
+
"react-pagination.block-overflows-page",
|
|
125
|
+
`Block \`${warning.blockId ?? "(unknown)"}\` exceeds the configured page safe area during React pagination.`,
|
|
126
|
+
warningPath,
|
|
127
|
+
{
|
|
128
|
+
blockId: warning.blockId,
|
|
129
|
+
height: warning.height,
|
|
130
|
+
pageSafeHeightPx: warning.pageSafeHeightPx,
|
|
131
|
+
source: warning.source,
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return createIssueReport({
|
|
138
|
+
kind: "validation",
|
|
139
|
+
checked,
|
|
140
|
+
issues,
|
|
141
|
+
okMessage: "OpenPress validation OK",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function findCommentMarkers(text) {
|
|
146
|
+
const markers = [];
|
|
147
|
+
const lines = String(text ?? "").split(/\r?\n/);
|
|
148
|
+
for (const [index, line] of lines.entries()) {
|
|
149
|
+
const match = line.match(/@openpress-comment\b[^}]*\bid="([^"]+)"/);
|
|
150
|
+
if (!match) continue;
|
|
151
|
+
markers.push({ id: match[1], line: index + 1 });
|
|
152
|
+
}
|
|
153
|
+
return markers;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function readJsonIfExists(filePath) {
|
|
157
|
+
try {
|
|
158
|
+
return JSON.parse(await fs.readFile(filePath, "utf8"));
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if (error?.code === "ENOENT") return null;
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function exists(filePath) {
|
|
166
|
+
try {
|
|
167
|
+
await fs.access(filePath);
|
|
168
|
+
return true;
|
|
169
|
+
} catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
}
|
package/index.html
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-Hant">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
|
|
7
|
+
<title>OpenPress Workspace</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div id="root"></div>
|
|
11
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|