@open-press/cli 0.8.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.
- package/README.md +33 -23
- package/dist/cli.js +320 -252
- package/package.json +9 -8
- package/template/core/AGENTS.md +0 -126
- package/template/core/CHANGELOG.md +0 -215
- package/template/core/README.md +0 -40
- package/template/core/engine/cli.mjs +0 -96
- package/template/core/engine/commands/_shared.mjs +0 -177
- package/template/core/engine/commands/deploy.mjs +0 -31
- package/template/core/engine/commands/dev.mjs +0 -49
- package/template/core/engine/commands/doctor.mjs +0 -229
- package/template/core/engine/commands/export.mjs +0 -8
- package/template/core/engine/commands/init.mjs +0 -24
- package/template/core/engine/commands/inspect.mjs +0 -35
- package/template/core/engine/commands/pdf.mjs +0 -26
- package/template/core/engine/commands/preview.mjs +0 -26
- package/template/core/engine/commands/render.mjs +0 -17
- package/template/core/engine/commands/replace.mjs +0 -41
- package/template/core/engine/commands/search.mjs +0 -33
- package/template/core/engine/commands/typecheck.mjs +0 -5
- package/template/core/engine/commands/upgrade.mjs +0 -159
- package/template/core/engine/commands/validate.mjs +0 -17
- package/template/core/engine/document-export.mjs +0 -15
- package/template/core/engine/init.mjs +0 -90
- package/template/core/engine/output/chrome-pdf.d.mts +0 -34
- package/template/core/engine/output/chrome-pdf.mjs +0 -358
- package/template/core/engine/output/deploy-sync.mjs +0 -15
- package/template/core/engine/output/fonts.mjs +0 -62
- package/template/core/engine/output/katex-assets.mjs +0 -45
- package/template/core/engine/output/page-block.mjs +0 -30
- package/template/core/engine/output/pdf-media.mjs +0 -45
- package/template/core/engine/output/public-assets.mjs +0 -19
- package/template/core/engine/output/static-server.mjs +0 -532
- package/template/core/engine/react/caption-numbering.mjs +0 -73
- package/template/core/engine/react/comment-endpoint.d.mts +0 -11
- package/template/core/engine/react/comment-endpoint.mjs +0 -102
- package/template/core/engine/react/comment-marker.mjs +0 -374
- package/template/core/engine/react/document-entry.mjs +0 -324
- package/template/core/engine/react/document-export.mjs +0 -373
- package/template/core/engine/react/http-json.mjs +0 -24
- package/template/core/engine/react/mdx-compile.mjs +0 -599
- package/template/core/engine/react/measurement-css.mjs +0 -136
- package/template/core/engine/react/object-entities.mjs +0 -119
- package/template/core/engine/react/pagination/allocator.mjs +0 -122
- package/template/core/engine/react/pagination/regions.mjs +0 -81
- package/template/core/engine/react/pagination-constants.mjs +0 -3
- package/template/core/engine/react/pagination.mjs +0 -9
- package/template/core/engine/react/pipeline/allocate.mjs +0 -251
- package/template/core/engine/react/pipeline/final-render.mjs +0 -94
- package/template/core/engine/react/pipeline/frame-measurement.mjs +0 -302
- package/template/core/engine/react/pipeline/press-tree.mjs +0 -135
- package/template/core/engine/react/project-asset-endpoint.d.mts +0 -10
- package/template/core/engine/react/project-asset-endpoint.mjs +0 -361
- package/template/core/engine/react/section-css.mjs +0 -56
- package/template/core/engine/react/source-edit-endpoint.d.mts +0 -10
- package/template/core/engine/react/source-edit-endpoint.mjs +0 -75
- package/template/core/engine/react/sources/heading-numbering.mjs +0 -132
- package/template/core/engine/react/sources/mdx-resolver.mjs +0 -439
- package/template/core/engine/react/style-discovery.mjs +0 -142
- package/template/core/engine/runtime/config.d.mts +0 -40
- package/template/core/engine/runtime/config.mjs +0 -175
- package/template/core/engine/runtime/file-utils.mjs +0 -106
- package/template/core/engine/runtime/file-walk.mjs +0 -22
- package/template/core/engine/runtime/inspection.mjs +0 -328
- package/template/core/engine/runtime/issue-report.mjs +0 -44
- package/template/core/engine/runtime/path-utils.mjs +0 -20
- package/template/core/engine/runtime/source-text-tools.d.mts +0 -102
- package/template/core/engine/runtime/source-text-tools.mjs +0 -832
- package/template/core/engine/runtime/source-workspace.mjs +0 -159
- package/template/core/engine/runtime/validation.mjs +0 -174
- package/template/core/index.html +0 -13
- package/template/core/openpress.config.mjs +0 -12
- package/template/core/package.json +0 -91
- package/template/core/src/main.tsx +0 -16
- package/template/core/src/openpress/app/OpenPressApp.tsx +0 -140
- package/template/core/src/openpress/app/OpenPressRuntime.tsx +0 -94
- package/template/core/src/openpress/app/index.ts +0 -2
- package/template/core/src/openpress/core/Frame.tsx +0 -78
- package/template/core/src/openpress/core/FrameContext.tsx +0 -24
- package/template/core/src/openpress/core/MdxArea.tsx +0 -34
- package/template/core/src/openpress/core/Press.tsx +0 -34
- package/template/core/src/openpress/core/cn.ts +0 -4
- package/template/core/src/openpress/core/index.tsx +0 -40
- package/template/core/src/openpress/core/primitives.tsx +0 -44
- package/template/core/src/openpress/core/types.ts +0 -191
- package/template/core/src/openpress/core/useSource.ts +0 -28
- package/template/core/src/openpress/document-model/anchorMapModel.ts +0 -27
- package/template/core/src/openpress/document-model/documentIndexes.ts +0 -329
- package/template/core/src/openpress/document-model/documentTypes.ts +0 -138
- package/template/core/src/openpress/document-model/index.ts +0 -6
- package/template/core/src/openpress/document-model/objectEntityModel.ts +0 -51
- package/template/core/src/openpress/document-model/projectIdentityModel.ts +0 -15
- package/template/core/src/openpress/document-model/reactDocumentMetadataModel.ts +0 -27
- package/template/core/src/openpress/manuscript/index.tsx +0 -238
- package/template/core/src/openpress/mdx/index.ts +0 -88
- package/template/core/src/openpress/numbering/index.ts +0 -294
- package/template/core/src/openpress/reader/PublicReaderPage.tsx +0 -267
- package/template/core/src/openpress/reader/ReaderNavigationPanel.tsx +0 -123
- package/template/core/src/openpress/reader/index.ts +0 -10
- package/template/core/src/openpress/reader/pageViewportScaleModel.ts +0 -73
- package/template/core/src/openpress/reader/readerPageRegistry.ts +0 -41
- package/template/core/src/openpress/reader/readerPageRoute.ts +0 -21
- package/template/core/src/openpress/reader/readerScroll.ts +0 -92
- package/template/core/src/openpress/reader/readerStateModel.ts +0 -15
- package/template/core/src/openpress/reader/readerTypes.ts +0 -4
- package/template/core/src/openpress/reader/usePageViewportScale.ts +0 -119
- package/template/core/src/openpress/reader/usePanelState.ts +0 -56
- package/template/core/src/openpress/reader/useReaderHashSync.ts +0 -61
- package/template/core/src/openpress/reader/useReaderKeyboardNav.ts +0 -48
- package/template/core/src/openpress/reader/useReaderRuntime.ts +0 -146
- package/template/core/src/openpress/reader/useReaderScrollAnchor.ts +0 -64
- package/template/core/src/openpress/shared/Panel.tsx +0 -77
- package/template/core/src/openpress/shared/frameScheduler.ts +0 -32
- package/template/core/src/openpress/shared/index.ts +0 -4
- package/template/core/src/openpress/shared/numberUtils.ts +0 -3
- package/template/core/src/openpress/shared/runtimeMode.ts +0 -11
- package/template/core/src/openpress/workbench/Workbench.tsx +0 -407
- package/template/core/src/openpress/workbench/actions/DeploymentControl.tsx +0 -157
- package/template/core/src/openpress/workbench/actions/PageZoomControl.tsx +0 -182
- package/template/core/src/openpress/workbench/actions/SearchControl.tsx +0 -345
- package/template/core/src/openpress/workbench/actions/deploymentStatusModel.ts +0 -112
- package/template/core/src/openpress/workbench/actions/index.ts +0 -5
- package/template/core/src/openpress/workbench/actions/useDeploymentWorkbench.ts +0 -136
- package/template/core/src/openpress/workbench/dialog/WorkbenchDialog.tsx +0 -72
- package/template/core/src/openpress/workbench/dialog/index.ts +0 -1
- package/template/core/src/openpress/workbench/document/components/DocumentPanel.tsx +0 -127
- package/template/core/src/openpress/workbench/document/components/InlineSourceEditorLayer.tsx +0 -207
- package/template/core/src/openpress/workbench/document/components/ReaderStage.tsx +0 -9
- package/template/core/src/openpress/workbench/document/hooks/useDocumentWorkbenchModel.ts +0 -34
- package/template/core/src/openpress/workbench/document/hooks/useInlineDocumentEditor.ts +0 -525
- package/template/core/src/openpress/workbench/document/index.ts +0 -10
- package/template/core/src/openpress/workbench/index.ts +0 -2
- package/template/core/src/openpress/workbench/inspector/InlineInspectorLayer.tsx +0 -459
- package/template/core/src/openpress/workbench/inspector/index.ts +0 -5
- package/template/core/src/openpress/workbench/inspector/inlineCommentModel.ts +0 -125
- package/template/core/src/openpress/workbench/inspector/inspectorGeometryModel.ts +0 -160
- package/template/core/src/openpress/workbench/inspector/inspectorModel.ts +0 -408
- package/template/core/src/openpress/workbench/inspector/useInspectorComments.ts +0 -248
- package/template/core/src/openpress/workbench/mentions/MentionSuggestionList.tsx +0 -41
- package/template/core/src/openpress/workbench/mentions/index.ts +0 -2
- package/template/core/src/openpress/workbench/mentions/useComposerMentions.ts +0 -185
- package/template/core/src/openpress/workbench/panels/Panel.tsx +0 -1
- package/template/core/src/openpress/workbench/panels/PendingCommentsPanel.tsx +0 -76
- package/template/core/src/openpress/workbench/panels/WorkbenchControlPanel.tsx +0 -29
- package/template/core/src/openpress/workbench/panels/index.ts +0 -3
- package/template/core/src/openpress/workbench/project/ProjectEntryPanel.tsx +0 -523
- package/template/core/src/openpress/workbench/project/ProjectPreviewDialog.tsx +0 -35
- package/template/core/src/openpress/workbench/project/index.ts +0 -2
- package/template/core/src/openpress/workbench/project/projectPreviewTypes.ts +0 -11
- package/template/core/src/openpress/workbench/project/projectSourceModel.ts +0 -24
- package/template/core/src/openpress/workbench/shell/WorkbenchShell.tsx +0 -167
- package/template/core/src/openpress/workbench/shell/index.ts +0 -1
- package/template/core/src/openpress/workbench/workbenchFormatters.ts +0 -120
- package/template/core/src/openpress/workbench/workbenchTypes.ts +0 -35
- package/template/core/src/styles/openpress/app-shell.css +0 -251
- package/template/core/src/styles/openpress/media-workspace.css +0 -230
- package/template/core/src/styles/openpress/print-route.css +0 -184
- package/template/core/src/styles/openpress/project-preview-panel.css +0 -924
- package/template/core/src/styles/openpress/public-viewer.css +0 -688
- package/template/core/src/styles/openpress/reader-runtime.css +0 -980
- package/template/core/src/styles/openpress/responsive.css +0 -245
- package/template/core/src/styles/openpress/workbench-panels.css +0 -594
- package/template/core/src/styles/openpress/workbench.css +0 -1255
- package/template/core/src/styles/openpress.css +0 -14
- package/template/core/src/vite-env.d.ts +0 -9
- package/template/core/tsconfig.json +0 -40
- package/template/core/vite.config.ts +0 -584
- package/template/packs/academic-paper/document/chapters/01-introduction/content/01-introduction.mdx +0 -35
- package/template/packs/academic-paper/document/chapters/02-methods/content/01-methods.mdx +0 -50
- package/template/packs/academic-paper/document/chapters/03-results-and-discussion/content/01-results.mdx +0 -47
- package/template/packs/academic-paper/document/chapters/04-acknowledgment/content/01-acknowledgment.mdx +0 -26
- package/template/packs/academic-paper/document/chapters/05-references/content/01-references.mdx +0 -32
- package/template/packs/academic-paper/document/components/ChapterOpenerVisual/index.tsx +0 -76
- package/template/packs/academic-paper/document/components/Page.tsx +0 -60
- package/template/packs/academic-paper/document/components/TokenSwatchGrid/index.tsx +0 -46
- package/template/packs/academic-paper/document/components/TokenSwatchGrid/style.css +0 -63
- package/template/packs/academic-paper/document/components/TypeSpecimen/index.tsx +0 -38
- package/template/packs/academic-paper/document/components/TypeSpecimen/style.css +0 -111
- package/template/packs/academic-paper/document/design.md +0 -279
- package/template/packs/academic-paper/document/index.tsx +0 -123
- package/template/packs/academic-paper/document/media/README.md +0 -13
- package/template/packs/academic-paper/document/media/figure-placeholder.svg +0 -9
- package/template/packs/academic-paper/document/openpress.config.mjs +0 -26
- package/template/packs/academic-paper/document/theme/README.md +0 -11
- package/template/packs/academic-paper/document/theme/base/page-contract.css +0 -522
- package/template/packs/academic-paper/document/theme/base/print.css +0 -93
- package/template/packs/academic-paper/document/theme/base/typography.css +0 -333
- package/template/packs/academic-paper/document/theme/fonts.css +0 -3
- package/template/packs/academic-paper/document/theme/page-surfaces/back-cover.css +0 -43
- package/template/packs/academic-paper/document/theme/page-surfaces/chapter-opener.css +0 -205
- package/template/packs/academic-paper/document/theme/page-surfaces/cover.css +0 -294
- package/template/packs/academic-paper/document/theme/page-surfaces/toc.css +0 -149
- package/template/packs/academic-paper/document/theme/patterns/_chart-frame.css +0 -49
- package/template/packs/academic-paper/document/theme/patterns/figure-grid.css +0 -68
- package/template/packs/academic-paper/document/theme/patterns/table-utilities.css +0 -66
- package/template/packs/academic-paper/document/theme/shell/reader-controls.css +0 -761
- package/template/packs/academic-paper/document/theme/tokens.css +0 -80
- package/template/packs/academic-paper/openpress.config.mjs +0 -5
- package/template/packs/claude-document/document/chapters/01-document-shape/content/01-document-shape.mdx +0 -51
- package/template/packs/claude-document/document/chapters/02-review-loop/content/01-review-loop.mdx +0 -31
- package/template/packs/claude-document/document/components/ChapterOpenerVisual.tsx +0 -96
- package/template/packs/claude-document/document/components/Page.tsx +0 -37
- package/template/packs/claude-document/document/design.md +0 -142
- package/template/packs/claude-document/document/index.tsx +0 -94
- package/template/packs/claude-document/document/media/README.md +0 -13
- package/template/packs/claude-document/document/openpress.config.mjs +0 -26
- package/template/packs/claude-document/document/theme/README.md +0 -15
- package/template/packs/claude-document/document/theme/base/page-contract.css +0 -525
- package/template/packs/claude-document/document/theme/base/print.css +0 -93
- package/template/packs/claude-document/document/theme/base/typography.css +0 -612
- package/template/packs/claude-document/document/theme/fonts.css +0 -4
- package/template/packs/claude-document/document/theme/page-surfaces/back-cover.css +0 -72
- package/template/packs/claude-document/document/theme/page-surfaces/chapter-opener.css +0 -236
- package/template/packs/claude-document/document/theme/page-surfaces/cover.css +0 -309
- package/template/packs/claude-document/document/theme/page-surfaces/toc.css +0 -225
- package/template/packs/claude-document/document/theme/patterns/_chart-frame.css +0 -53
- package/template/packs/claude-document/document/theme/patterns/figure-grid.css +0 -68
- package/template/packs/claude-document/document/theme/patterns/table-utilities.css +0 -66
- package/template/packs/claude-document/document/theme/shell/reader-controls.css +0 -789
- package/template/packs/claude-document/document/theme/tokens.css +0 -89
- package/template/packs/claude-document/openpress.config.mjs +0 -5
- package/template/packs/editorial-monograph/document/chapters/01-product-and-use-cases/content/01-product-and-use-cases.mdx +0 -31
- package/template/packs/editorial-monograph/document/chapters/02-workflow/content/01-workflow.mdx +0 -89
- package/template/packs/editorial-monograph/document/chapters/03-agent-skills-contributors/content/01-agent-skills-contributors.mdx +0 -51
- package/template/packs/editorial-monograph/document/chapters/04-validation-deploy/content/01-validation-deploy.mdx +0 -39
- package/template/packs/editorial-monograph/document/components/ChapterOpenerVisual/index.tsx +0 -76
- package/template/packs/editorial-monograph/document/components/Page.tsx +0 -37
- package/template/packs/editorial-monograph/document/components/TokenSwatchGrid/index.tsx +0 -46
- package/template/packs/editorial-monograph/document/components/TokenSwatchGrid/style.css +0 -63
- package/template/packs/editorial-monograph/document/components/TypeSpecimen/index.tsx +0 -38
- package/template/packs/editorial-monograph/document/components/TypeSpecimen/style.css +0 -111
- package/template/packs/editorial-monograph/document/design.md +0 -279
- package/template/packs/editorial-monograph/document/index.tsx +0 -97
- package/template/packs/editorial-monograph/document/media/README.md +0 -13
- package/template/packs/editorial-monograph/document/openpress.config.mjs +0 -26
- package/template/packs/editorial-monograph/document/theme/README.md +0 -11
- package/template/packs/editorial-monograph/document/theme/base/page-contract.css +0 -505
- package/template/packs/editorial-monograph/document/theme/base/print.css +0 -93
- package/template/packs/editorial-monograph/document/theme/base/typography.css +0 -336
- package/template/packs/editorial-monograph/document/theme/fonts.css +0 -3
- package/template/packs/editorial-monograph/document/theme/page-surfaces/back-cover.css +0 -43
- package/template/packs/editorial-monograph/document/theme/page-surfaces/chapter-opener.css +0 -205
- package/template/packs/editorial-monograph/document/theme/page-surfaces/cover.css +0 -147
- package/template/packs/editorial-monograph/document/theme/page-surfaces/toc.css +0 -149
- package/template/packs/editorial-monograph/document/theme/patterns/_chart-frame.css +0 -49
- package/template/packs/editorial-monograph/document/theme/patterns/figure-grid.css +0 -68
- package/template/packs/editorial-monograph/document/theme/patterns/table-utilities.css +0 -66
- package/template/packs/editorial-monograph/document/theme/shell/reader-controls.css +0 -761
- package/template/packs/editorial-monograph/document/theme/tokens.css +0 -80
- package/template/packs/editorial-monograph/openpress.config.mjs +0 -5
|
@@ -1,532 +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
|
-
const stat = await fs.stat(target);
|
|
77
|
-
const filePath = stat.isDirectory() ? path.join(target, "index.html") : target;
|
|
78
|
-
const body = await fs.readFile(filePath);
|
|
79
|
-
res.writeHead(200, { "Content-Type": mimeTypes[path.extname(filePath)] ?? "application/octet-stream" });
|
|
80
|
-
res.end(body);
|
|
81
|
-
} catch {
|
|
82
|
-
res.writeHead(404);
|
|
83
|
-
res.end("Not found");
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
server.listen(port, host, () => {
|
|
88
|
-
console.log(`OpenPress static preview: http://${host}:${port}/`);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
async function handleStatusRequest(req, res) {
|
|
92
|
-
if (req.method !== "GET") {
|
|
93
|
-
writeJson(res, 405, { ok: false, message: "Status endpoint requires GET." });
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const deployConfigured = isDeployConfigured();
|
|
98
|
-
const deploymentInfo = deployConfigured
|
|
99
|
-
? await readDeploymentInfo()
|
|
100
|
-
: { deployed_at: undefined, pdf: publicPdfHref(config), public_url: undefined };
|
|
101
|
-
const dirty = deployConfigured ? await isDeploymentDirty(deploymentInfo.deployed_at) : false;
|
|
102
|
-
writeJson(res, 200, {
|
|
103
|
-
ok: true,
|
|
104
|
-
deployed_at: deploymentInfo.deployed_at,
|
|
105
|
-
pdf: deploymentInfo.pdf,
|
|
106
|
-
public_url: deploymentInfo.public_url,
|
|
107
|
-
dirty,
|
|
108
|
-
deploy_configured: deployConfigured,
|
|
109
|
-
deploy_adapter: config.deploy.adapter,
|
|
110
|
-
deploy_source: config.deploy.source,
|
|
111
|
-
deploy_project_name: config.deploy.projectName,
|
|
112
|
-
deploy_setup_message: deploySetupMessage(),
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async function handleSearchRequest(req, res, url) {
|
|
117
|
-
if (req.method !== "GET") {
|
|
118
|
-
writeJson(res, 405, { ok: false, message: "Search endpoint requires GET." });
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const query = (url.searchParams.get("q") ?? "").trim();
|
|
123
|
-
if (!query) {
|
|
124
|
-
writeJson(res, 400, { ok: false, message: "Search query is required." });
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
try {
|
|
129
|
-
const report = await searchSourceText({
|
|
130
|
-
config,
|
|
131
|
-
query,
|
|
132
|
-
scope: searchScopeFrom(url),
|
|
133
|
-
caseSensitive: url.searchParams.get("caseSensitive") === "true",
|
|
134
|
-
});
|
|
135
|
-
writeJson(res, 200, { ok: true, ...report });
|
|
136
|
-
} catch (error) {
|
|
137
|
-
writeJson(res, 500, { ok: false, message: error instanceof Error ? error.message : String(error) });
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function searchScopeFrom(url) {
|
|
142
|
-
return url.searchParams.get("scope") === "all" ? "all" : "content";
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function valueAfter(args, flag) {
|
|
146
|
-
const index = args.indexOf(flag);
|
|
147
|
-
return index >= 0 ? args[index + 1] : undefined;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async function inferWorkspaceRoot(staticRoot) {
|
|
151
|
-
for (const candidate of [staticRoot, path.dirname(staticRoot), path.dirname(path.dirname(staticRoot))]) {
|
|
152
|
-
if (await fileExists(path.join(candidate, "openpress.config.mjs"))) return candidate;
|
|
153
|
-
}
|
|
154
|
-
if (path.basename(path.dirname(staticRoot)) === ".deploy") {
|
|
155
|
-
return path.dirname(path.dirname(staticRoot));
|
|
156
|
-
}
|
|
157
|
-
return process.cwd();
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async function handleLocalPdfExportRequest(req, res) {
|
|
161
|
-
if (req.method !== "POST") {
|
|
162
|
-
writeJson(res, 405, { ok: false, message: "Local PDF export endpoint requires POST." });
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const result = await runLocalPdfExport();
|
|
167
|
-
const exists = await fileExists(config.paths.pdf);
|
|
168
|
-
writeJson(res, result.code === 0 && exists ? 200 : 500, {
|
|
169
|
-
ok: result.code === 0 && exists,
|
|
170
|
-
code: result.code,
|
|
171
|
-
pdf: `/__openpress/local-pdf-file?ts=${Date.now()}`,
|
|
172
|
-
command: "node engine/cli.mjs pdf .",
|
|
173
|
-
stdout: result.stdout,
|
|
174
|
-
stderr: result.stderr,
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async function handleLocalPdfFileRequest(req, res) {
|
|
179
|
-
if (req.method !== "GET") {
|
|
180
|
-
writeJson(res, 405, { ok: false, message: "Local PDF file endpoint requires GET." });
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
const body = await fs.readFile(config.paths.pdf);
|
|
186
|
-
res.writeHead(200, {
|
|
187
|
-
"Content-Type": "application/pdf",
|
|
188
|
-
"Content-Disposition": `inline; filename="${config.pdf.filename}"`,
|
|
189
|
-
"Cache-Control": "no-store",
|
|
190
|
-
});
|
|
191
|
-
res.end(body);
|
|
192
|
-
} catch {
|
|
193
|
-
writeJson(res, 404, { ok: false, message: "Local PDF has not been generated yet." });
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async function handleDeployRequest(req, res) {
|
|
198
|
-
if (req.method !== "POST") {
|
|
199
|
-
writeJson(res, 405, { ok: false, message: "Deploy endpoint requires POST." });
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (!isDeployConfigured()) {
|
|
204
|
-
writeJson(res, 400, {
|
|
205
|
-
ok: false,
|
|
206
|
-
code: 2,
|
|
207
|
-
message: deploySetupMessage(),
|
|
208
|
-
deploy_configured: false,
|
|
209
|
-
deploy_adapter: config.deploy.adapter,
|
|
210
|
-
deploy_source: config.deploy.source,
|
|
211
|
-
deploy_project_name: config.deploy.projectName,
|
|
212
|
-
command: "node engine/cli.mjs deploy . --confirm",
|
|
213
|
-
});
|
|
214
|
-
return;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const result = await runDeploy();
|
|
218
|
-
const deployedUrl = extractDeployUrl(result.stdout);
|
|
219
|
-
if (result.code === 0 && deployedUrl) {
|
|
220
|
-
await writeDeploymentPublicUrl(deployedUrl);
|
|
221
|
-
}
|
|
222
|
-
const deploymentInfo = await readDeploymentInfo();
|
|
223
|
-
const publicUrl = deployedUrl ?? deploymentInfo.public_url;
|
|
224
|
-
writeJson(res, result.code === 0 ? 200 : 500, {
|
|
225
|
-
ok: result.code === 0,
|
|
226
|
-
code: result.code,
|
|
227
|
-
deployed_at: deploymentInfo.deployed_at,
|
|
228
|
-
pdf: deployedUrl ? `${deployedUrl}/${config.pdf.filename}` : deploymentInfo.pdf,
|
|
229
|
-
public_url: publicUrl,
|
|
230
|
-
dirty: false,
|
|
231
|
-
command: "node engine/cli.mjs deploy . --confirm",
|
|
232
|
-
stdout: result.stdout,
|
|
233
|
-
stderr: result.stderr,
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
async function handleMediaUploadRequest(req, res) {
|
|
238
|
-
if (req.method !== "POST") {
|
|
239
|
-
writeJson(res, 405, { ok: false, message: "Media upload endpoint requires POST." });
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const rawFileName = headerValue(req.headers["x-openpress-file-name"]);
|
|
244
|
-
const decodedFileName = rawFileName ? safeDecodeURIComponent(rawFileName) : "";
|
|
245
|
-
const fileName = sanitizeMediaFileName(decodedFileName);
|
|
246
|
-
if (!fileName) {
|
|
247
|
-
writeJson(res, 400, { ok: false, message: "Media upload requires a valid file name." });
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
if (!isAllowedMediaFile(fileName)) {
|
|
251
|
-
writeJson(res, 400, { ok: false, message: "Only png, jpg, jpeg, gif, svg, and webp files can be uploaded." });
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
try {
|
|
256
|
-
const body = await readRequestBuffer(req, 30 * 1024 * 1024);
|
|
257
|
-
if (body.length === 0) {
|
|
258
|
-
writeJson(res, 400, { ok: false, message: "Uploaded media file is empty." });
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
await fs.mkdir(config.paths.mediaDir, { recursive: true });
|
|
262
|
-
const uniqueFileName = await uniqueMediaFileName(config.paths.mediaDir, fileName);
|
|
263
|
-
const targetPath = path.join(config.paths.mediaDir, uniqueFileName);
|
|
264
|
-
await fs.writeFile(targetPath, body);
|
|
265
|
-
const relativePath = path.relative(workspace, targetPath).replaceAll("\\", "/");
|
|
266
|
-
writeJson(res, 200, {
|
|
267
|
-
ok: true,
|
|
268
|
-
asset: {
|
|
269
|
-
fileName: uniqueFileName,
|
|
270
|
-
src: `/openpress/media/${encodeURIComponent(uniqueFileName)}`,
|
|
271
|
-
path: relativePath,
|
|
272
|
-
mention: `@media/${uniqueFileName}`,
|
|
273
|
-
},
|
|
274
|
-
});
|
|
275
|
-
} catch (error) {
|
|
276
|
-
writeJson(res, 500, { ok: false, message: error instanceof Error ? error.message : String(error) });
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
async function handleMediaFileRequest(req, res, url) {
|
|
281
|
-
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
282
|
-
writeJson(res, 405, { ok: false, message: "Media file endpoint requires GET." });
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
try {
|
|
287
|
-
const fileName = sanitizeMediaFileName(safeDecodeURIComponent(url.pathname.replace(/^\/openpress\/media\/?/, "")));
|
|
288
|
-
if (!fileName) {
|
|
289
|
-
writeJson(res, 404, { ok: false, message: "Media file not found." });
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
const targetPath = path.join(config.paths.mediaDir, fileName);
|
|
293
|
-
const resolvedTarget = path.resolve(targetPath);
|
|
294
|
-
const mediaRoot = path.resolve(config.paths.mediaDir);
|
|
295
|
-
if (!resolvedTarget.startsWith(`${mediaRoot}${path.sep}`) && resolvedTarget !== mediaRoot) {
|
|
296
|
-
writeJson(res, 403, { ok: false, message: "Forbidden." });
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
const body = await fs.readFile(resolvedTarget);
|
|
300
|
-
res.writeHead(200, {
|
|
301
|
-
"Content-Type": mediaMimeType(fileName),
|
|
302
|
-
"Cache-Control": "no-store",
|
|
303
|
-
});
|
|
304
|
-
if (req.method === "HEAD") {
|
|
305
|
-
res.end();
|
|
306
|
-
} else {
|
|
307
|
-
res.end(body);
|
|
308
|
-
}
|
|
309
|
-
} catch {
|
|
310
|
-
writeJson(res, 404, { ok: false, message: "Media file not found." });
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
function runLocalPdfExport() {
|
|
315
|
-
return new Promise((resolve) => {
|
|
316
|
-
const child = spawn("node", ["engine/cli.mjs", "pdf", "."], {
|
|
317
|
-
cwd: workspace,
|
|
318
|
-
shell: false,
|
|
319
|
-
});
|
|
320
|
-
let stdout = "";
|
|
321
|
-
let stderr = "";
|
|
322
|
-
child.stdout.on("data", (chunk) => {
|
|
323
|
-
stdout += String(chunk);
|
|
324
|
-
});
|
|
325
|
-
child.stderr.on("data", (chunk) => {
|
|
326
|
-
stderr += String(chunk);
|
|
327
|
-
});
|
|
328
|
-
child.on("error", (error) => {
|
|
329
|
-
resolve({ code: 1, stdout, stderr: `${stderr}${error.message}\n` });
|
|
330
|
-
});
|
|
331
|
-
child.on("close", (code) => {
|
|
332
|
-
resolve({ code: code ?? 1, stdout, stderr });
|
|
333
|
-
});
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
function runDeploy() {
|
|
338
|
-
return new Promise((resolve) => {
|
|
339
|
-
const child = spawn("node", ["engine/cli.mjs", "deploy", ".", "--confirm"], {
|
|
340
|
-
cwd: workspace,
|
|
341
|
-
shell: false,
|
|
342
|
-
});
|
|
343
|
-
let stdout = "";
|
|
344
|
-
let stderr = "";
|
|
345
|
-
child.stdout.on("data", (chunk) => {
|
|
346
|
-
stdout += String(chunk);
|
|
347
|
-
});
|
|
348
|
-
child.stderr.on("data", (chunk) => {
|
|
349
|
-
stderr += String(chunk);
|
|
350
|
-
});
|
|
351
|
-
child.on("error", (error) => {
|
|
352
|
-
resolve({ code: 1, stdout, stderr: `${stderr}${error.message}\n` });
|
|
353
|
-
});
|
|
354
|
-
child.on("close", (code) => {
|
|
355
|
-
resolve({ code: code ?? 1, stdout, stderr });
|
|
356
|
-
});
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
function isDeployConfigured() {
|
|
361
|
-
if (config.deploy.adapter === "cloudflare-pages") {
|
|
362
|
-
return typeof config.deploy.projectName === "string" && config.deploy.projectName.trim().length > 0;
|
|
363
|
-
}
|
|
364
|
-
return true;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
function deploySetupMessage() {
|
|
368
|
-
if (isDeployConfigured()) return undefined;
|
|
369
|
-
if (config.deploy.adapter === "cloudflare-pages") {
|
|
370
|
-
return "Cloudflare Pages deployment requires `deploy.projectName` in openpress.config.mjs.";
|
|
371
|
-
}
|
|
372
|
-
return `Deployment adapter \`${config.deploy.adapter}\` is not configured.`;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
async function fileExists(filePath) {
|
|
376
|
-
try {
|
|
377
|
-
await fs.access(filePath);
|
|
378
|
-
return true;
|
|
379
|
-
} catch {
|
|
380
|
-
return false;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
function writeJson(res, status, body) {
|
|
385
|
-
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
|
386
|
-
res.end(`${JSON.stringify(body, null, 2)}\n`);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
async function readDeploymentInfo() {
|
|
390
|
-
try {
|
|
391
|
-
const text = await fs.readFile(config.paths.deployMetadata, "utf8");
|
|
392
|
-
const deployConfig = JSON.parse(text);
|
|
393
|
-
return {
|
|
394
|
-
deployed_at: typeof deployConfig.deployed_at === "string" ? deployConfig.deployed_at : undefined,
|
|
395
|
-
pdf: typeof deployConfig.pdf === "string" ? deployConfig.pdf : publicPdfHref(config),
|
|
396
|
-
public_url: typeof deployConfig.public_url === "string" ? deployConfig.public_url : undefined,
|
|
397
|
-
};
|
|
398
|
-
} catch {
|
|
399
|
-
return { deployed_at: undefined, pdf: publicPdfHref(config), public_url: undefined };
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
async function writeDeploymentPublicUrl(publicUrl) {
|
|
404
|
-
let deployConfig = {};
|
|
405
|
-
try {
|
|
406
|
-
deployConfig = JSON.parse(await fs.readFile(config.paths.deployMetadata, "utf8"));
|
|
407
|
-
} catch {
|
|
408
|
-
deployConfig = {};
|
|
409
|
-
}
|
|
410
|
-
await fs.mkdir(path.dirname(config.paths.deployMetadata), { recursive: true });
|
|
411
|
-
await fs.writeFile(
|
|
412
|
-
config.paths.deployMetadata,
|
|
413
|
-
`${JSON.stringify({ ...deployConfig, pdf: `${publicUrl}/${config.pdf.filename}`, public_url: publicUrl }, null, 2)}\n`,
|
|
414
|
-
"utf8",
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
async function isDeploymentDirty(deployedAt) {
|
|
419
|
-
if (!deployedAt) return false;
|
|
420
|
-
const deployedTime = new Date(deployedAt).getTime();
|
|
421
|
-
if (Number.isNaN(deployedTime)) return false;
|
|
422
|
-
const newestSourceMtime = await findNewestSourceMtime(getDeploymentSourcePaths());
|
|
423
|
-
return newestSourceMtime > deployedTime + 1000;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
function getDeploymentSourcePaths() {
|
|
427
|
-
return [
|
|
428
|
-
config.paths.sourceDir,
|
|
429
|
-
config.paths.mediaDir,
|
|
430
|
-
config.paths.themeDir,
|
|
431
|
-
config.paths.designDoc,
|
|
432
|
-
config.paths.componentsDir,
|
|
433
|
-
path.join(workspace, "src"),
|
|
434
|
-
path.join(workspace, "index.html"),
|
|
435
|
-
path.join(workspace, "package.json"),
|
|
436
|
-
path.join(workspace, "openpress.config.mjs"),
|
|
437
|
-
config.configPath,
|
|
438
|
-
path.join(workspace, "vite.config.ts"),
|
|
439
|
-
];
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
async function findNewestSourceMtime(paths) {
|
|
443
|
-
const times = await Promise.all(paths.map((sourcePath) => findNewestMtime(sourcePath)));
|
|
444
|
-
return Math.max(0, ...times);
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
async function findNewestMtime(sourcePath) {
|
|
448
|
-
try {
|
|
449
|
-
const stat = await fs.stat(sourcePath);
|
|
450
|
-
if (!stat.isDirectory()) return stat.mtimeMs;
|
|
451
|
-
const entries = await fs.readdir(sourcePath, { withFileTypes: true });
|
|
452
|
-
const times = await Promise.all(entries.map((entry) => findNewestMtime(path.join(sourcePath, entry.name))));
|
|
453
|
-
return Math.max(stat.mtimeMs, ...times);
|
|
454
|
-
} catch {
|
|
455
|
-
return 0;
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
function headerValue(value) {
|
|
460
|
-
return Array.isArray(value) ? value[0] : value;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
function safeDecodeURIComponent(value) {
|
|
464
|
-
try {
|
|
465
|
-
return decodeURIComponent(value);
|
|
466
|
-
} catch {
|
|
467
|
-
return value;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
function sanitizeMediaFileName(value) {
|
|
472
|
-
const baseName = path.basename(value).trim();
|
|
473
|
-
if (!baseName) return "";
|
|
474
|
-
const ext = path.extname(baseName);
|
|
475
|
-
const stem = path.basename(baseName, ext)
|
|
476
|
-
.replace(/[\\/:*?"<>|#%{}^~[\]`]/g, "-")
|
|
477
|
-
.replace(/\s+/g, "-")
|
|
478
|
-
.replace(/-+/g, "-")
|
|
479
|
-
.replace(/^-|-$/g, "");
|
|
480
|
-
if (!stem || !ext) return "";
|
|
481
|
-
return `${stem}${ext.toLowerCase()}`;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function isAllowedMediaFile(fileName) {
|
|
485
|
-
return /\.(png|jpe?g|gif|svg|webp)$/i.test(fileName);
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
function mediaMimeType(fileName) {
|
|
489
|
-
const ext = path.extname(fileName).toLowerCase();
|
|
490
|
-
if (ext === ".png") return "image/png";
|
|
491
|
-
if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
|
|
492
|
-
if (ext === ".gif") return "image/gif";
|
|
493
|
-
if (ext === ".svg") return "image/svg+xml";
|
|
494
|
-
if (ext === ".webp") return "image/webp";
|
|
495
|
-
return "application/octet-stream";
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
async function uniqueMediaFileName(mediaDir, fileName) {
|
|
499
|
-
const ext = path.extname(fileName);
|
|
500
|
-
const stem = path.basename(fileName, ext);
|
|
501
|
-
let candidate = fileName;
|
|
502
|
-
let counter = 2;
|
|
503
|
-
while (await fileExists(path.join(mediaDir, candidate))) {
|
|
504
|
-
candidate = `${stem}-${counter}${ext}`;
|
|
505
|
-
counter += 1;
|
|
506
|
-
}
|
|
507
|
-
return candidate;
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
function readRequestBuffer(req, maxBytes) {
|
|
511
|
-
return new Promise((resolve, reject) => {
|
|
512
|
-
const chunks = [];
|
|
513
|
-
let total = 0;
|
|
514
|
-
req.on("data", (chunk) => {
|
|
515
|
-
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
516
|
-
total += buffer.length;
|
|
517
|
-
if (total > maxBytes) {
|
|
518
|
-
reject(new Error("Uploaded media file is too large."));
|
|
519
|
-
req.destroy();
|
|
520
|
-
return;
|
|
521
|
-
}
|
|
522
|
-
chunks.push(buffer);
|
|
523
|
-
});
|
|
524
|
-
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
525
|
-
req.on("error", reject);
|
|
526
|
-
});
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
function extractDeployUrl(output) {
|
|
530
|
-
const match = output.match(/https:\/\/[^\s]+\.pages\.dev/);
|
|
531
|
-
return match?.[0]?.replace(/\/$/, "");
|
|
532
|
-
}
|
|
@@ -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, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
73
|
-
}
|