cursor-canvas-export 0.0.0-stage → 0.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.
- package/LICENSE +21 -0
- package/README.md +97 -2
- package/bin/canvas-to-html.js +4 -0
- package/bin/canvas-to-pdf.js +4 -0
- package/dist/cli.js +903 -0
- package/package.json +52 -4
- package/src/sdk/charts.tsx +411 -0
- package/src/sdk/extras.tsx +461 -0
- package/src/sdk/hooks.tsx +28 -0
- package/src/sdk/index.tsx +55 -0
- package/src/sdk/tokens.ts +261 -0
- package/src/sdk/ui.tsx +779 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,903 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import path4 from "node:path";
|
|
5
|
+
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
6
|
+
|
|
7
|
+
// src/convert.ts
|
|
8
|
+
import * as esbuild from "esbuild";
|
|
9
|
+
import { createRequire as createRequire2 } from "node:module";
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import path2 from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
|
|
14
|
+
// src/browser.ts
|
|
15
|
+
import { spawn } from "node:child_process";
|
|
16
|
+
import { createRequire } from "node:module";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { chromium } from "playwright";
|
|
19
|
+
var require2 = createRequire(import.meta.url);
|
|
20
|
+
var BrowserNotReady = class extends Error {
|
|
21
|
+
gate;
|
|
22
|
+
constructor(gate) {
|
|
23
|
+
super(gate.state === "failed" ? gate.message : "Chromium is still downloading");
|
|
24
|
+
this.name = "BrowserNotReady";
|
|
25
|
+
this.gate = gate;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var browserPromise;
|
|
29
|
+
var users = 0;
|
|
30
|
+
var hold = false;
|
|
31
|
+
var phase = "idle";
|
|
32
|
+
var failureMessage = "";
|
|
33
|
+
var downloadListeners = [];
|
|
34
|
+
var forcedGate;
|
|
35
|
+
var launchGeneration = 0;
|
|
36
|
+
var installChild;
|
|
37
|
+
function holdBrowserProcess() {
|
|
38
|
+
hold = true;
|
|
39
|
+
}
|
|
40
|
+
function warmBrowser() {
|
|
41
|
+
if (forcedGate) return;
|
|
42
|
+
if (phase === "failed" || browserPromise) return;
|
|
43
|
+
startLaunch();
|
|
44
|
+
}
|
|
45
|
+
function browserGate() {
|
|
46
|
+
if (forcedGate) return Promise.resolve(forcedGate);
|
|
47
|
+
if (phase === "failed") {
|
|
48
|
+
const message = failureMessage;
|
|
49
|
+
startLaunch();
|
|
50
|
+
return Promise.resolve({ state: "failed", message });
|
|
51
|
+
}
|
|
52
|
+
if (phase === "downloading") return Promise.resolve({ state: "downloading" });
|
|
53
|
+
if (!browserPromise) startLaunch();
|
|
54
|
+
if (phase === "ready") return Promise.resolve({ state: "ready" });
|
|
55
|
+
return waitForLaunch(browserPromise);
|
|
56
|
+
}
|
|
57
|
+
function acquireBrowser() {
|
|
58
|
+
users += 1;
|
|
59
|
+
if (!browserPromise) startLaunch();
|
|
60
|
+
return browserPromise;
|
|
61
|
+
}
|
|
62
|
+
async function releaseBrowser() {
|
|
63
|
+
users = Math.max(0, users - 1);
|
|
64
|
+
if (users === 0 && !hold) await shutdown();
|
|
65
|
+
}
|
|
66
|
+
async function closeBrowser() {
|
|
67
|
+
hold = false;
|
|
68
|
+
if (users === 0) await shutdown();
|
|
69
|
+
}
|
|
70
|
+
function startLaunch() {
|
|
71
|
+
if (browserPromise) return browserPromise;
|
|
72
|
+
const generation = launchGeneration;
|
|
73
|
+
phase = "launching";
|
|
74
|
+
failureMessage = "";
|
|
75
|
+
const pending = launchChromium(generation).then(
|
|
76
|
+
(browser) => {
|
|
77
|
+
if (browserPromise === pending) phase = "ready";
|
|
78
|
+
return browser;
|
|
79
|
+
},
|
|
80
|
+
(error) => {
|
|
81
|
+
if (browserPromise !== pending) throw error;
|
|
82
|
+
browserPromise = void 0;
|
|
83
|
+
phase = "failed";
|
|
84
|
+
failureMessage = error instanceof Error ? error.message : String(error);
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
void pending.catch(() => {
|
|
89
|
+
});
|
|
90
|
+
browserPromise = pending;
|
|
91
|
+
return pending;
|
|
92
|
+
}
|
|
93
|
+
function waitForLaunch(pending) {
|
|
94
|
+
return new Promise((resolve) => {
|
|
95
|
+
let settled = false;
|
|
96
|
+
const finish = (gate) => {
|
|
97
|
+
if (settled) return;
|
|
98
|
+
settled = true;
|
|
99
|
+
downloadListeners = downloadListeners.filter((listener) => listener !== onDownload);
|
|
100
|
+
resolve(gate);
|
|
101
|
+
};
|
|
102
|
+
const onDownload = () => finish({ state: "downloading" });
|
|
103
|
+
downloadListeners.push(onDownload);
|
|
104
|
+
if (phase === "downloading") {
|
|
105
|
+
finish({ state: "downloading" });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
pending.then(
|
|
109
|
+
() => finish({ state: "ready" }),
|
|
110
|
+
(error) => {
|
|
111
|
+
const message = failureMessage || (error instanceof Error ? error.message : String(error));
|
|
112
|
+
const alreadySettled = settled;
|
|
113
|
+
finish({ state: "failed", message });
|
|
114
|
+
if (!alreadySettled && phase === "failed" && !browserPromise) startLaunch();
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function notifyDownloading() {
|
|
120
|
+
phase = "downloading";
|
|
121
|
+
const listeners = downloadListeners;
|
|
122
|
+
downloadListeners = [];
|
|
123
|
+
for (const listener of listeners) listener();
|
|
124
|
+
}
|
|
125
|
+
async function shutdown() {
|
|
126
|
+
if (users > 0 || hold || !browserPromise) return;
|
|
127
|
+
launchGeneration += 1;
|
|
128
|
+
stopInstallChild();
|
|
129
|
+
const pending = browserPromise;
|
|
130
|
+
const wasReady = phase === "ready";
|
|
131
|
+
browserPromise = void 0;
|
|
132
|
+
downloadListeners = [];
|
|
133
|
+
if (!wasReady) {
|
|
134
|
+
phase = "idle";
|
|
135
|
+
failureMessage = "";
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const browser = await pending.catch(() => void 0);
|
|
139
|
+
if (users > 0 && browser) {
|
|
140
|
+
browserPromise = Promise.resolve(browser);
|
|
141
|
+
phase = "ready";
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
await browser?.close().catch(() => {
|
|
145
|
+
});
|
|
146
|
+
if (!browserPromise && phase !== "failed") phase = "idle";
|
|
147
|
+
}
|
|
148
|
+
async function launchChromium(generation) {
|
|
149
|
+
try {
|
|
150
|
+
const browser = await chromium.launch({ headless: true });
|
|
151
|
+
if (generation !== launchGeneration) {
|
|
152
|
+
await browser.close().catch(() => {
|
|
153
|
+
});
|
|
154
|
+
throw new Error("browser launch cancelled");
|
|
155
|
+
}
|
|
156
|
+
return browser;
|
|
157
|
+
} catch (error) {
|
|
158
|
+
if (generation !== launchGeneration) throw error;
|
|
159
|
+
if (!isMissingBrowser(error)) throw error;
|
|
160
|
+
notifyDownloading();
|
|
161
|
+
if (generation !== launchGeneration) throw new Error("browser launch cancelled");
|
|
162
|
+
await installChromium();
|
|
163
|
+
if (generation !== launchGeneration) throw new Error("browser launch cancelled");
|
|
164
|
+
return await chromium.launch({ headless: true });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function isMissingBrowser(error) {
|
|
168
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
169
|
+
return message.includes("Executable doesn't exist") || message.includes("Please run");
|
|
170
|
+
}
|
|
171
|
+
function installChromium() {
|
|
172
|
+
const cli = path.join(path.dirname(require2.resolve("playwright/package.json")), "cli.js");
|
|
173
|
+
process.stderr.write("Downloading headless Chromium. No browser window will open.\n");
|
|
174
|
+
return new Promise((resolve, reject) => {
|
|
175
|
+
const child = spawn(process.execPath, [cli, "install", "chromium"], {
|
|
176
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
177
|
+
});
|
|
178
|
+
installChild = child;
|
|
179
|
+
let settled = false;
|
|
180
|
+
const finish = (error) => {
|
|
181
|
+
if (settled) return;
|
|
182
|
+
settled = true;
|
|
183
|
+
if (installChild === child) installChild = void 0;
|
|
184
|
+
if (error) reject(error);
|
|
185
|
+
else resolve();
|
|
186
|
+
};
|
|
187
|
+
child.stdout.on("data", (chunk) => process.stderr.write(chunk));
|
|
188
|
+
child.stderr.on("data", (chunk) => process.stderr.write(chunk));
|
|
189
|
+
child.on("error", (error) => finish(error));
|
|
190
|
+
child.on("close", (code) => {
|
|
191
|
+
if (code === 0) finish();
|
|
192
|
+
else finish(new Error(`playwright install chromium exited with code ${code ?? "unknown"}`));
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
function stopInstallChild() {
|
|
197
|
+
const child = installChild;
|
|
198
|
+
installChild = void 0;
|
|
199
|
+
if (!child?.pid || child.killed) return;
|
|
200
|
+
if (process.platform === "win32") {
|
|
201
|
+
spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore" });
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
child.kill("SIGKILL");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// src/sdk/tokens.ts
|
|
208
|
+
var chartPalette = {
|
|
209
|
+
green: "#1F8A65E8",
|
|
210
|
+
darkGreen: "#0D855AE0",
|
|
211
|
+
lightGreen: "#52B896E0",
|
|
212
|
+
mintGreen: "#7DCAB0E0",
|
|
213
|
+
blue: "#2E79B5E0",
|
|
214
|
+
lightBlue: "#70B0D8E0",
|
|
215
|
+
indigo: "#5A6CC0F0",
|
|
216
|
+
lightIndigo: "#9AAADCE0",
|
|
217
|
+
purple: "#7B64B8F0",
|
|
218
|
+
lightPurple: "#AA98D8E0",
|
|
219
|
+
warmPink: "#C85898E0",
|
|
220
|
+
lightPink: "#E8A0C4E0",
|
|
221
|
+
brightOrange: "#F0A040E0",
|
|
222
|
+
deepOrange: "#C06028E0",
|
|
223
|
+
goldenYellow: "#E8C030E0",
|
|
224
|
+
darkAmber: "#C04848E0",
|
|
225
|
+
warmPeach: "#F0A088E0",
|
|
226
|
+
vibrantTeal: "#2A9A8AE0",
|
|
227
|
+
muted: "#8888A8E0",
|
|
228
|
+
neutralLine: "#888899D0"
|
|
229
|
+
};
|
|
230
|
+
var chartColorSequence = [
|
|
231
|
+
chartPalette.green,
|
|
232
|
+
chartPalette.blue,
|
|
233
|
+
chartPalette.purple,
|
|
234
|
+
chartPalette.brightOrange,
|
|
235
|
+
chartPalette.lightBlue,
|
|
236
|
+
chartPalette.warmPink,
|
|
237
|
+
chartPalette.goldenYellow,
|
|
238
|
+
chartPalette.vibrantTeal,
|
|
239
|
+
chartPalette.indigo,
|
|
240
|
+
chartPalette.deepOrange
|
|
241
|
+
];
|
|
242
|
+
var categoryDark = {
|
|
243
|
+
gray: "#F0F0F099",
|
|
244
|
+
purple: "#7B64B8",
|
|
245
|
+
green: "#1F8A65",
|
|
246
|
+
yellow: "#E8C030",
|
|
247
|
+
cyan: "#2A9A8A",
|
|
248
|
+
pink: "#C85898",
|
|
249
|
+
blue: "#2E79B5",
|
|
250
|
+
orange: "#F0A040",
|
|
251
|
+
red: "#C04848"
|
|
252
|
+
};
|
|
253
|
+
var categoryLight = {
|
|
254
|
+
gray: "#14141499",
|
|
255
|
+
purple: "#6A54A8",
|
|
256
|
+
green: "#1A7A58",
|
|
257
|
+
yellow: "#8A6A10",
|
|
258
|
+
cyan: "#1E7A6C",
|
|
259
|
+
pink: "#A84880",
|
|
260
|
+
blue: "#2E79B5",
|
|
261
|
+
orange: "#C06028",
|
|
262
|
+
red: "#A83838"
|
|
263
|
+
};
|
|
264
|
+
function mix(base, percent) {
|
|
265
|
+
const alpha = Math.round(percent / 100 * 255).toString(16).padStart(2, "0");
|
|
266
|
+
return `${base}${alpha}`;
|
|
267
|
+
}
|
|
268
|
+
function palette(kind) {
|
|
269
|
+
const dark = kind === "dark";
|
|
270
|
+
const ink = dark ? "#F0F0F0" : "#141414";
|
|
271
|
+
const editor = dark ? "#181818" : "#FCFCFC";
|
|
272
|
+
const chrome = dark ? "#141414" : "#F3F3F3";
|
|
273
|
+
const accent = dark ? "#70B0D8" : "#2E79B5";
|
|
274
|
+
return {
|
|
275
|
+
foreground: ink,
|
|
276
|
+
foregroundSecondary: mix(ink, 74),
|
|
277
|
+
foregroundTertiary: mix(ink, 60),
|
|
278
|
+
foregroundQuaternary: mix(ink, 36),
|
|
279
|
+
editor,
|
|
280
|
+
chrome,
|
|
281
|
+
sidebar: chrome,
|
|
282
|
+
elevated: editor,
|
|
283
|
+
fillPrimary: mix(ink, 20),
|
|
284
|
+
fillSecondary: mix(ink, 14),
|
|
285
|
+
fillTertiary: mix(ink, 8),
|
|
286
|
+
fillQuaternary: mix(ink, 6),
|
|
287
|
+
strokePrimary: mix(ink, 20),
|
|
288
|
+
strokeSecondary: mix(ink, 12),
|
|
289
|
+
strokeTertiary: mix(ink, 8),
|
|
290
|
+
strokeFocused: accent,
|
|
291
|
+
accent,
|
|
292
|
+
buttonBackground: accent,
|
|
293
|
+
buttonForeground: dark ? "#141414" : "#FCFCFC",
|
|
294
|
+
buttonHoverBackground: dark ? "#8EC0E4" : "#24689F",
|
|
295
|
+
link: accent,
|
|
296
|
+
diffInsertedLine: dark ? "#1F8A652E" : "#1F8A6524",
|
|
297
|
+
diffRemovedLine: dark ? "#C048482E" : "#C0484824",
|
|
298
|
+
diffStripAdded: "#1F8A65",
|
|
299
|
+
diffStripRemoved: "#C04848"
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function buildTheme(kind) {
|
|
303
|
+
const p = palette(kind);
|
|
304
|
+
const category = kind === "dark" ? categoryDark : categoryLight;
|
|
305
|
+
const tokens = {
|
|
306
|
+
bg: { editor: p.editor, chrome: p.chrome, elevated: p.elevated },
|
|
307
|
+
text: {
|
|
308
|
+
primary: p.foreground,
|
|
309
|
+
secondary: p.foregroundSecondary,
|
|
310
|
+
tertiary: p.foregroundTertiary,
|
|
311
|
+
quaternary: p.foregroundQuaternary,
|
|
312
|
+
link: p.link,
|
|
313
|
+
onAccent: p.buttonForeground
|
|
314
|
+
},
|
|
315
|
+
stroke: {
|
|
316
|
+
primary: p.strokePrimary,
|
|
317
|
+
secondary: p.strokeSecondary,
|
|
318
|
+
tertiary: p.strokeTertiary,
|
|
319
|
+
focused: p.strokeFocused
|
|
320
|
+
},
|
|
321
|
+
fill: {
|
|
322
|
+
primary: p.fillPrimary,
|
|
323
|
+
secondary: p.fillSecondary,
|
|
324
|
+
tertiary: p.fillTertiary,
|
|
325
|
+
quaternary: p.fillQuaternary
|
|
326
|
+
},
|
|
327
|
+
accent: {
|
|
328
|
+
primary: p.accent,
|
|
329
|
+
control: p.buttonBackground,
|
|
330
|
+
controlHover: p.buttonHoverBackground
|
|
331
|
+
},
|
|
332
|
+
diff: {
|
|
333
|
+
insertedLine: p.diffInsertedLine,
|
|
334
|
+
removedLine: p.diffRemovedLine,
|
|
335
|
+
stripAdded: p.diffStripAdded,
|
|
336
|
+
stripRemoved: p.diffStripRemoved
|
|
337
|
+
},
|
|
338
|
+
category
|
|
339
|
+
};
|
|
340
|
+
return {
|
|
341
|
+
kind,
|
|
342
|
+
...tokens,
|
|
343
|
+
tokens,
|
|
344
|
+
palette: p,
|
|
345
|
+
category
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
var canvasPaletteDark = palette("dark");
|
|
349
|
+
var canvasPaletteLight = palette("light");
|
|
350
|
+
var canvasTokens = buildTheme("dark").tokens;
|
|
351
|
+
var canvasTokensLight = buildTheme("light").tokens;
|
|
352
|
+
var fontStack = '"Segoe UI Variable", "Segoe UI", system-ui, sans-serif';
|
|
353
|
+
|
|
354
|
+
// src/convert.ts
|
|
355
|
+
var require3 = createRequire2(import.meta.url);
|
|
356
|
+
var PDF_MAX_PX = 200 * 96;
|
|
357
|
+
function pdfTooLarge(width, height) {
|
|
358
|
+
return width > PDF_MAX_PX || height + 1 > PDF_MAX_PX;
|
|
359
|
+
}
|
|
360
|
+
function pdfLimitWarning(width, height) {
|
|
361
|
+
return `PDF skipped: page ${width}x${height}px exceeds Chromium's 200 inch limit.`;
|
|
362
|
+
}
|
|
363
|
+
function readState(input) {
|
|
364
|
+
const sidecar = input.replace(/\.canvas\.tsx$/, ".canvas.data.json");
|
|
365
|
+
if (!fs.existsSync(sidecar)) return {};
|
|
366
|
+
const parsed = JSON.parse(fs.readFileSync(sidecar, "utf8"));
|
|
367
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
368
|
+
return parsed;
|
|
369
|
+
}
|
|
370
|
+
return {};
|
|
371
|
+
}
|
|
372
|
+
function centerHtmlForBrowser(html) {
|
|
373
|
+
const head = html.lastIndexOf("</head>");
|
|
374
|
+
if (head === -1) return html;
|
|
375
|
+
return `${html.slice(0, head)}<style>#root{margin-left:auto;margin-right:auto}</style>${html.slice(head)}`;
|
|
376
|
+
}
|
|
377
|
+
function outputStem(out, input) {
|
|
378
|
+
if (!out) return input.replace(/\.canvas\.tsx$/, "");
|
|
379
|
+
const resolved = path2.resolve(out);
|
|
380
|
+
if (/\.(html|pdf)$/i.test(resolved)) return resolved.replace(/\.(html|pdf)$/i, "");
|
|
381
|
+
return resolved;
|
|
382
|
+
}
|
|
383
|
+
async function convert(options) {
|
|
384
|
+
const input = path2.resolve(options.input);
|
|
385
|
+
if (!fs.existsSync(input)) throw new Error(`Canvas file not found: ${input}`);
|
|
386
|
+
const theme = options.theme ?? "dark";
|
|
387
|
+
const width = options.width ?? 800;
|
|
388
|
+
const format = options.format ?? "both";
|
|
389
|
+
const tokens = buildTheme(theme);
|
|
390
|
+
const specifier = input.replaceAll("\\", "/");
|
|
391
|
+
const wrapper = [
|
|
392
|
+
'import { createRoot } from "react-dom/client";',
|
|
393
|
+
'import { setCanvasStateSeed, setExportTheme } from "cursor/canvas";',
|
|
394
|
+
`import Canvas from ${JSON.stringify(specifier)};`,
|
|
395
|
+
`setExportTheme(${JSON.stringify(theme)});`,
|
|
396
|
+
`setCanvasStateSeed(${JSON.stringify(readState(input))});`,
|
|
397
|
+
'const root = document.getElementById("root");',
|
|
398
|
+
'if (!root) throw new Error("Canvas root element was not found.");',
|
|
399
|
+
"createRoot(root).render(<Canvas />);",
|
|
400
|
+
""
|
|
401
|
+
].join("\n");
|
|
402
|
+
let built;
|
|
403
|
+
try {
|
|
404
|
+
built = await esbuild.build({
|
|
405
|
+
stdin: {
|
|
406
|
+
contents: wrapper,
|
|
407
|
+
resolveDir: path2.dirname(input),
|
|
408
|
+
sourcefile: "canvas-entry.tsx",
|
|
409
|
+
loader: "tsx"
|
|
410
|
+
},
|
|
411
|
+
bundle: true,
|
|
412
|
+
format: "iife",
|
|
413
|
+
platform: "browser",
|
|
414
|
+
write: false,
|
|
415
|
+
jsx: "automatic",
|
|
416
|
+
target: "es2022",
|
|
417
|
+
alias: { "cursor/canvas": sdkEntryPath() },
|
|
418
|
+
plugins: [reactFromPackage()],
|
|
419
|
+
define: { "process.env.NODE_ENV": '"production"' },
|
|
420
|
+
logLevel: "silent"
|
|
421
|
+
});
|
|
422
|
+
} catch (error) {
|
|
423
|
+
const formatted = formatEsbuildError(error);
|
|
424
|
+
if (formatted) throw new Error(formatted);
|
|
425
|
+
throw error;
|
|
426
|
+
}
|
|
427
|
+
const script = built.outputFiles?.[0]?.text?.replaceAll("</script", "<\\/script");
|
|
428
|
+
if (!script) throw new Error("Canvas build produced no code.");
|
|
429
|
+
const shell = `<!DOCTYPE html>
|
|
430
|
+
<html lang="ru">
|
|
431
|
+
<head>
|
|
432
|
+
<meta charset="utf-8" />
|
|
433
|
+
<meta name="viewport" content="width=${width}" />
|
|
434
|
+
<title>Canvas</title>
|
|
435
|
+
<style>
|
|
436
|
+
:root { color-scheme: ${theme}; }
|
|
437
|
+
html, body { margin: 0; padding: 0; background: ${tokens.bg.editor}; }
|
|
438
|
+
body { font-family: ${fontStack}; }
|
|
439
|
+
#root { width: ${width}px; box-sizing: border-box; padding: 24px 28px 32px; }
|
|
440
|
+
</style>
|
|
441
|
+
</head>
|
|
442
|
+
<body>
|
|
443
|
+
<div id="root"></div>
|
|
444
|
+
<script>${script}</script>
|
|
445
|
+
</body>
|
|
446
|
+
</html>`;
|
|
447
|
+
if (options.deferBrowser) {
|
|
448
|
+
const gate = await browserGate();
|
|
449
|
+
if (gate.state !== "ready") throw new BrowserNotReady(gate);
|
|
450
|
+
}
|
|
451
|
+
const browser = await acquireBrowser();
|
|
452
|
+
let page;
|
|
453
|
+
try {
|
|
454
|
+
page = await browser.newPage({ viewport: { width, height: 900 }, deviceScaleFactor: 1 });
|
|
455
|
+
const errors = [];
|
|
456
|
+
let settleRuntime = () => {
|
|
457
|
+
};
|
|
458
|
+
const runtimeFailure = new Promise((resolve, reject) => {
|
|
459
|
+
settleRuntime = (error) => error ? reject(error) : resolve();
|
|
460
|
+
});
|
|
461
|
+
void runtimeFailure.catch(() => {
|
|
462
|
+
});
|
|
463
|
+
const reportRuntime = (message) => {
|
|
464
|
+
errors.push(message);
|
|
465
|
+
settleRuntime(new Error(errors.join("\n")));
|
|
466
|
+
};
|
|
467
|
+
page.on("pageerror", (error) => reportRuntime(error.message));
|
|
468
|
+
page.on("console", (message) => {
|
|
469
|
+
if (message.type() === "error") reportRuntime(message.text());
|
|
470
|
+
});
|
|
471
|
+
try {
|
|
472
|
+
await Promise.race([
|
|
473
|
+
(async () => {
|
|
474
|
+
await page.setContent(shell, { waitUntil: "load" });
|
|
475
|
+
await page.waitForSelector("#root > *", { timeout: 15e3 });
|
|
476
|
+
await page.evaluate(async () => {
|
|
477
|
+
await document.fonts.ready;
|
|
478
|
+
await new Promise((resolve) => {
|
|
479
|
+
requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
})(),
|
|
483
|
+
runtimeFailure
|
|
484
|
+
]);
|
|
485
|
+
} catch (error) {
|
|
486
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
487
|
+
const extra = errors.filter((entry) => !message.includes(entry));
|
|
488
|
+
throw new Error(extra.length > 0 ? `${message}
|
|
489
|
+
${extra.join("\n")}` : message);
|
|
490
|
+
} finally {
|
|
491
|
+
settleRuntime();
|
|
492
|
+
}
|
|
493
|
+
if (errors.length > 0) throw new Error(errors.join("\n"));
|
|
494
|
+
await page.evaluate(() => {
|
|
495
|
+
const heading = document.querySelector("h1");
|
|
496
|
+
if (heading?.textContent) document.title = heading.textContent.trim();
|
|
497
|
+
document.querySelectorAll("script").forEach((node) => node.remove());
|
|
498
|
+
});
|
|
499
|
+
const size = await page.evaluate(() => ({
|
|
500
|
+
width: document.documentElement.scrollWidth,
|
|
501
|
+
height: document.documentElement.scrollHeight
|
|
502
|
+
}));
|
|
503
|
+
const warnings = [];
|
|
504
|
+
const wantsPdf = !options.preview && (format === "pdf" || format === "both");
|
|
505
|
+
const tooLarge = wantsPdf && pdfTooLarge(size.width, size.height);
|
|
506
|
+
if (!tooLarge) {
|
|
507
|
+
await page.setViewportSize({ width: size.width, height: Math.max(size.height, 1) });
|
|
508
|
+
}
|
|
509
|
+
const stem = outputStem(options.out, input);
|
|
510
|
+
const result = { width: size.width, height: size.height, warnings };
|
|
511
|
+
if (!options.preview) fs.mkdirSync(path2.dirname(stem), { recursive: true });
|
|
512
|
+
if (!options.preview && (format === "html" || format === "both")) {
|
|
513
|
+
const file = `${stem}.html`;
|
|
514
|
+
fs.writeFileSync(file, centerHtmlForBrowser(await page.content()), "utf8");
|
|
515
|
+
result.html = file;
|
|
516
|
+
}
|
|
517
|
+
if (options.preview) {
|
|
518
|
+
const limit = 1600;
|
|
519
|
+
result.previewClipped = size.height > limit;
|
|
520
|
+
result.preview = await page.screenshot(
|
|
521
|
+
size.height > limit ? { clip: { x: 0, y: 0, width: size.width, height: limit } } : { fullPage: true }
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
if (options.screenshot) {
|
|
525
|
+
const shot = path2.resolve(options.screenshot);
|
|
526
|
+
fs.mkdirSync(path2.dirname(shot), { recursive: true });
|
|
527
|
+
await page.screenshot({ path: shot, fullPage: true });
|
|
528
|
+
result.screenshot = shot;
|
|
529
|
+
}
|
|
530
|
+
if (wantsPdf && tooLarge) {
|
|
531
|
+
warnings.push(pdfLimitWarning(size.width, size.height));
|
|
532
|
+
if (format === "pdf") throw new Error(warnings[0]);
|
|
533
|
+
} else if (wantsPdf) {
|
|
534
|
+
const file = `${stem}.pdf`;
|
|
535
|
+
try {
|
|
536
|
+
await page.emulateMedia({ media: "screen" });
|
|
537
|
+
await page.pdf({
|
|
538
|
+
path: file,
|
|
539
|
+
width: `${size.width}px`,
|
|
540
|
+
height: `${size.height + 1}px`,
|
|
541
|
+
printBackground: true,
|
|
542
|
+
margin: { top: "0", right: "0", bottom: "0", left: "0" }
|
|
543
|
+
});
|
|
544
|
+
result.pdf = file;
|
|
545
|
+
} catch (error) {
|
|
546
|
+
fs.rmSync(file, { force: true });
|
|
547
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
548
|
+
if (result.html) warnings.push(`PDF was not written: ${message}`);
|
|
549
|
+
else throw error;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return result;
|
|
553
|
+
} finally {
|
|
554
|
+
await page?.close().catch(() => {
|
|
555
|
+
});
|
|
556
|
+
await releaseBrowser();
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
function sdkEntryPath() {
|
|
560
|
+
const here = path2.dirname(fileURLToPath(import.meta.url));
|
|
561
|
+
const candidates = [
|
|
562
|
+
path2.join(here, "sdk", "index.tsx"),
|
|
563
|
+
path2.join(here, "../src/sdk/index.tsx")
|
|
564
|
+
];
|
|
565
|
+
const found = candidates.find((candidate) => fs.existsSync(candidate));
|
|
566
|
+
if (!found) throw new Error("Canvas SDK sources were not found in the package.");
|
|
567
|
+
return found;
|
|
568
|
+
}
|
|
569
|
+
function reactFromPackage() {
|
|
570
|
+
return {
|
|
571
|
+
name: "react-from-package",
|
|
572
|
+
setup(build2) {
|
|
573
|
+
build2.onResolve({ filter: /^react(-dom)?(\/|$)/ }, (args) => ({
|
|
574
|
+
path: require3.resolve(args.path)
|
|
575
|
+
}));
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
function formatEsbuildError(error) {
|
|
580
|
+
if (!error || typeof error !== "object" || !("errors" in error)) return void 0;
|
|
581
|
+
const errors = error.errors;
|
|
582
|
+
if (!Array.isArray(errors)) return void 0;
|
|
583
|
+
return errors.map((item) => {
|
|
584
|
+
const entry = item;
|
|
585
|
+
const loc = entry.location;
|
|
586
|
+
const where = loc?.file ? `${loc.file}:${loc.line ?? 1}:${loc.column ?? 0}` : "build";
|
|
587
|
+
return `${where}: ${entry.text ?? "build failed"}`;
|
|
588
|
+
}).join("\n");
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// src/mcp.ts
|
|
592
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
593
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
594
|
+
import { z } from "zod";
|
|
595
|
+
|
|
596
|
+
// src/discover.ts
|
|
597
|
+
import fs2 from "node:fs";
|
|
598
|
+
import os from "node:os";
|
|
599
|
+
import path3 from "node:path";
|
|
600
|
+
function projectsRoot() {
|
|
601
|
+
return process.env.CANVAS_PROJECTS_DIR ?? path3.join(os.homedir(), ".cursor", "projects");
|
|
602
|
+
}
|
|
603
|
+
function listCanvases(workspace, root = projectsRoot()) {
|
|
604
|
+
if (!fs2.existsSync(root)) return [];
|
|
605
|
+
const needle = workspace ? slug(workspace) : "";
|
|
606
|
+
const hits = [];
|
|
607
|
+
for (const project of fs2.readdirSync(root)) {
|
|
608
|
+
if (needle && !slug(project).includes(needle) && !needle.includes(slug(project))) continue;
|
|
609
|
+
const dir = path3.join(root, project, "canvases");
|
|
610
|
+
if (!fs2.existsSync(dir)) continue;
|
|
611
|
+
for (const file of fs2.readdirSync(dir)) {
|
|
612
|
+
if (!file.endsWith(".canvas.tsx")) continue;
|
|
613
|
+
const full = path3.join(dir, file);
|
|
614
|
+
const stat = fs2.statSync(full);
|
|
615
|
+
if (!stat.isFile()) continue;
|
|
616
|
+
hits.push({
|
|
617
|
+
name: file.slice(0, -".canvas.tsx".length),
|
|
618
|
+
path: full,
|
|
619
|
+
project,
|
|
620
|
+
mtimeMs: stat.mtimeMs
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
hits.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
625
|
+
return hits;
|
|
626
|
+
}
|
|
627
|
+
function resolveCanvas(query, root) {
|
|
628
|
+
const direct = path3.resolve(query);
|
|
629
|
+
if (fs2.existsSync(direct) && direct.endsWith(".canvas.tsx")) {
|
|
630
|
+
return { match: hitFromFile(direct), matches: [] };
|
|
631
|
+
}
|
|
632
|
+
if (query.includes("/") || query.includes("\\") || query.endsWith(".canvas.tsx")) {
|
|
633
|
+
return { matches: [] };
|
|
634
|
+
}
|
|
635
|
+
const all = listCanvases(void 0, root);
|
|
636
|
+
const exact = all.filter((hit) => hit.name === query);
|
|
637
|
+
if (exact.length === 1) return { match: exact[0], matches: [] };
|
|
638
|
+
if (exact.length > 1) return { matches: exact };
|
|
639
|
+
const partial = all.filter((hit) => hit.name.includes(query));
|
|
640
|
+
if (partial.length === 1) return { match: partial[0], matches: [] };
|
|
641
|
+
return { matches: partial };
|
|
642
|
+
}
|
|
643
|
+
function hitFromFile(file) {
|
|
644
|
+
const stat = fs2.statSync(file);
|
|
645
|
+
return {
|
|
646
|
+
name: path3.basename(file).slice(0, -".canvas.tsx".length),
|
|
647
|
+
path: file,
|
|
648
|
+
project: path3.basename(path3.dirname(path3.dirname(file))),
|
|
649
|
+
mtimeMs: stat.mtimeMs
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function slug(value) {
|
|
653
|
+
return value.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// src/mcp.ts
|
|
657
|
+
var themeSchema = z.enum(["dark", "light"]).describe("Canvas theme. Defaults to dark.");
|
|
658
|
+
var widthSchema = z.number().int().min(320).optional().describe("Page width in pixels. Defaults to 800.");
|
|
659
|
+
var browserWait = "If the result tells you to wait because Chromium is downloading, that is not an error. Wait about 30 seconds and call this tool again with the same arguments. Nothing is written until a later call finishes the render.";
|
|
660
|
+
function createServer() {
|
|
661
|
+
holdBrowserProcess();
|
|
662
|
+
warmBrowser();
|
|
663
|
+
const server = new McpServer(
|
|
664
|
+
{ name: "cursor-canvas-export", version: "0.1.1" },
|
|
665
|
+
{
|
|
666
|
+
instructions: "If export_canvas or preview_canvas tells you to wait because Chromium is downloading, that is not an error. Wait about 30 seconds and call the same tool again with the same arguments. The file or preview is ready only when that later call returns it."
|
|
667
|
+
}
|
|
668
|
+
);
|
|
669
|
+
server.registerTool(
|
|
670
|
+
"list_canvases",
|
|
671
|
+
{
|
|
672
|
+
title: "List canvases",
|
|
673
|
+
description: "Find Cursor canvas files (.canvas.tsx) under ~/.cursor/projects. Newest first. Pass workspace to keep only that project's canvases.",
|
|
674
|
+
inputSchema: {
|
|
675
|
+
workspace: z.string().optional().describe("Workspace path or name fragment. Omit to list every local canvas.")
|
|
676
|
+
}
|
|
677
|
+
},
|
|
678
|
+
async ({ workspace }) => {
|
|
679
|
+
const hits = listCanvases(workspace);
|
|
680
|
+
return text(
|
|
681
|
+
hits.length === 0 ? "No canvases found." : hits.map((hit) => `${hit.name} ${hit.path}`).join("\n")
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
);
|
|
685
|
+
server.registerTool(
|
|
686
|
+
"export_canvas",
|
|
687
|
+
{
|
|
688
|
+
title: "Export canvas",
|
|
689
|
+
description: `Render a Cursor canvas to HTML and/or a one-page PDF. Pass a full path or a canvas name. Files are written next to the canvas unless out is set. Returns output paths, page size, and warnings. The browser runs headless. ${browserWait}`,
|
|
690
|
+
inputSchema: {
|
|
691
|
+
canvas: z.string().describe("Absolute path to a .canvas.tsx file, or its name without the extension."),
|
|
692
|
+
format: z.enum(["html", "pdf", "both"]).optional().describe("Defaults to both."),
|
|
693
|
+
theme: themeSchema.optional(),
|
|
694
|
+
width: widthSchema,
|
|
695
|
+
out: z.string().optional().describe("Output stem. Extension is ignored. Defaults to beside the canvas.")
|
|
696
|
+
}
|
|
697
|
+
},
|
|
698
|
+
async ({ canvas, format, theme, width, out }) => {
|
|
699
|
+
const resolved = resolveCanvas(canvas);
|
|
700
|
+
if (!resolved.match) return unresolvedCanvas(canvas, resolved.matches);
|
|
701
|
+
let result;
|
|
702
|
+
try {
|
|
703
|
+
result = await convert({
|
|
704
|
+
input: resolved.match.path,
|
|
705
|
+
out,
|
|
706
|
+
format: format ?? "both",
|
|
707
|
+
theme,
|
|
708
|
+
width,
|
|
709
|
+
deferBrowser: true
|
|
710
|
+
});
|
|
711
|
+
} catch (error) {
|
|
712
|
+
const waiting = browserWaitReply("export_canvas", error);
|
|
713
|
+
if (waiting) return waiting;
|
|
714
|
+
throw error;
|
|
715
|
+
}
|
|
716
|
+
const lines = [
|
|
717
|
+
result.html ? `html: ${result.html}` : "",
|
|
718
|
+
result.pdf ? `pdf: ${result.pdf}` : "",
|
|
719
|
+
`page: ${result.width}x${result.height}px`,
|
|
720
|
+
...result.warnings.map((warning) => `warning: ${warning}`)
|
|
721
|
+
].filter(Boolean);
|
|
722
|
+
return text(lines.join("\n"));
|
|
723
|
+
}
|
|
724
|
+
);
|
|
725
|
+
server.registerTool(
|
|
726
|
+
"preview_canvas",
|
|
727
|
+
{
|
|
728
|
+
title: "Preview canvas",
|
|
729
|
+
description: `Render a canvas and return a PNG of the page so you can check layout before handing the file over. Tall pages are clipped to the top 1600px. Nothing is written next to the canvas. ${browserWait}`,
|
|
730
|
+
inputSchema: {
|
|
731
|
+
canvas: z.string().describe("Absolute path to a .canvas.tsx file, or its name without the extension."),
|
|
732
|
+
theme: themeSchema.optional(),
|
|
733
|
+
width: widthSchema
|
|
734
|
+
}
|
|
735
|
+
},
|
|
736
|
+
async ({ canvas, theme, width }) => {
|
|
737
|
+
const resolved = resolveCanvas(canvas);
|
|
738
|
+
if (!resolved.match) return unresolvedCanvas(canvas, resolved.matches);
|
|
739
|
+
let result;
|
|
740
|
+
try {
|
|
741
|
+
result = await convert({
|
|
742
|
+
input: resolved.match.path,
|
|
743
|
+
preview: true,
|
|
744
|
+
theme,
|
|
745
|
+
width,
|
|
746
|
+
deferBrowser: true
|
|
747
|
+
});
|
|
748
|
+
} catch (error) {
|
|
749
|
+
const waiting = browserWaitReply("preview_canvas", error);
|
|
750
|
+
if (waiting) return waiting;
|
|
751
|
+
throw error;
|
|
752
|
+
}
|
|
753
|
+
const note = result.previewClipped ? `Preview clipped to the top 1600px of a ${result.height}px page.` : `Preview of ${resolved.match.name}, ${result.width}x${result.height}px.`;
|
|
754
|
+
return {
|
|
755
|
+
content: [
|
|
756
|
+
{ type: "text", text: note },
|
|
757
|
+
{
|
|
758
|
+
type: "image",
|
|
759
|
+
data: (result.preview ?? Buffer.alloc(0)).toString("base64"),
|
|
760
|
+
mimeType: "image/png"
|
|
761
|
+
}
|
|
762
|
+
]
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
);
|
|
766
|
+
return server;
|
|
767
|
+
}
|
|
768
|
+
async function serveStdio() {
|
|
769
|
+
const server = createServer();
|
|
770
|
+
process.stdin.on("end", () => {
|
|
771
|
+
void closeBrowser();
|
|
772
|
+
});
|
|
773
|
+
await server.connect(new StdioServerTransport());
|
|
774
|
+
}
|
|
775
|
+
function unresolvedCanvas(canvas, matches) {
|
|
776
|
+
if (matches.length === 0) {
|
|
777
|
+
throw new Error(`No canvas found for "${canvas}". Call list_canvases to see available files.`);
|
|
778
|
+
}
|
|
779
|
+
return text(
|
|
780
|
+
`Several canvases match "${canvas}". Pass one full path:
|
|
781
|
+
${matches.map((hit) => hit.path).join("\n")}`
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
function browserWaitReply(tool, error) {
|
|
785
|
+
if (!(error instanceof BrowserNotReady)) return;
|
|
786
|
+
return text(error.gate.state === "downloading" ? downloadingMessage(tool) : failedDownloadMessage(tool, error.gate.message));
|
|
787
|
+
}
|
|
788
|
+
function downloadingMessage(tool) {
|
|
789
|
+
const until = tool === "export_canvas" ? "it returns the output paths" : "it returns the preview image";
|
|
790
|
+
return [
|
|
791
|
+
`Wait about 30 seconds, then call ${tool} again with the same arguments. Chromium is still downloading.`,
|
|
792
|
+
"This is not an error. The canvas and the arguments are fine. Nothing was rendered or written yet.",
|
|
793
|
+
"The download is already running. Calling the tool again does not start a second download.",
|
|
794
|
+
`After waiting, call ${tool} once more with the same arguments. Repeat until ${until}.`
|
|
795
|
+
].join("\n");
|
|
796
|
+
}
|
|
797
|
+
function failedDownloadMessage(tool, message) {
|
|
798
|
+
return [
|
|
799
|
+
`Chromium download failed: ${message}`,
|
|
800
|
+
`Wait about 30 seconds, then call ${tool} again with the same arguments. A new download is already running.`,
|
|
801
|
+
"This is not a problem with the canvas or the arguments.",
|
|
802
|
+
"If the same failure comes back, install Chromium from a terminal and call the tool again:",
|
|
803
|
+
"npx playwright install chromium"
|
|
804
|
+
].join("\n");
|
|
805
|
+
}
|
|
806
|
+
function text(value) {
|
|
807
|
+
return { content: [{ type: "text", text: value }] };
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// src/cli.ts
|
|
811
|
+
async function main(argv = process.argv.slice(2)) {
|
|
812
|
+
const { flags, positionals } = readArgs(argv);
|
|
813
|
+
if (positionals[0] === "mcp") {
|
|
814
|
+
await serveStdio();
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
if (flags.has("help") || positionals.length === 0) {
|
|
818
|
+
process.stdout.write(help());
|
|
819
|
+
process.exit(positionals.length === 0 && !flags.has("help") ? 1 : 0);
|
|
820
|
+
}
|
|
821
|
+
const themeFlag = flags.get("theme");
|
|
822
|
+
if (themeFlag != null && themeFlag !== true && themeFlag !== "dark" && themeFlag !== "light") {
|
|
823
|
+
process.stderr.write("Theme must be dark or light.\n");
|
|
824
|
+
process.exit(1);
|
|
825
|
+
}
|
|
826
|
+
const theme = themeFlag === "light" ? "light" : "dark";
|
|
827
|
+
const envFormat = process.env.CANVAS_EXPORT_FORMAT;
|
|
828
|
+
const formatFlag = flags.get("format") ?? (envFormat === "html" || envFormat === "pdf" ? envFormat : "both");
|
|
829
|
+
if (formatFlag !== "html" && formatFlag !== "pdf" && formatFlag !== "both") {
|
|
830
|
+
process.stderr.write("Format must be html, pdf, or both.\n");
|
|
831
|
+
process.exit(1);
|
|
832
|
+
}
|
|
833
|
+
const widthFlag = flags.get("width");
|
|
834
|
+
const width = widthFlag == null || widthFlag === true ? 800 : Number(widthFlag);
|
|
835
|
+
if (!Number.isFinite(width) || width < 320) {
|
|
836
|
+
process.stderr.write("Width must be a number of pixels, at least 320.\n");
|
|
837
|
+
process.exit(1);
|
|
838
|
+
}
|
|
839
|
+
const outFlag = flags.get("out");
|
|
840
|
+
const shotFlag = flags.get("screenshot");
|
|
841
|
+
try {
|
|
842
|
+
const result = await convert({
|
|
843
|
+
input: positionals[0],
|
|
844
|
+
out: outFlag === true ? void 0 : outFlag,
|
|
845
|
+
theme,
|
|
846
|
+
width,
|
|
847
|
+
format: formatFlag,
|
|
848
|
+
screenshot: shotFlag === true ? void 0 : shotFlag
|
|
849
|
+
});
|
|
850
|
+
if (result.html) process.stdout.write(`${result.html}
|
|
851
|
+
`);
|
|
852
|
+
if (result.pdf) process.stdout.write(`${result.pdf}
|
|
853
|
+
`);
|
|
854
|
+
if (result.screenshot) process.stdout.write(`${result.screenshot}
|
|
855
|
+
`);
|
|
856
|
+
for (const warning of result.warnings) process.stderr.write(`warning: ${warning}
|
|
857
|
+
`);
|
|
858
|
+
} catch (error) {
|
|
859
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}
|
|
860
|
+
`);
|
|
861
|
+
process.exit(1);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
function help() {
|
|
865
|
+
return `cursor-canvas-export <file.canvas.tsx> [options]
|
|
866
|
+
cursor-canvas-export mcp
|
|
867
|
+
|
|
868
|
+
--out <path> output stem; .html and .pdf are stripped
|
|
869
|
+
--theme dark|light default dark
|
|
870
|
+
--width <px> page width, default 800
|
|
871
|
+
--format html|pdf|both default both
|
|
872
|
+
--screenshot <png> full-page PNG
|
|
873
|
+
|
|
874
|
+
canvas-to-html and canvas-to-pdf are the same command with --format defaulting to html or pdf.
|
|
875
|
+
The browser runs headless. Exports are written next to the canvas.
|
|
876
|
+
`;
|
|
877
|
+
}
|
|
878
|
+
function readArgs(tokens) {
|
|
879
|
+
const flags = /* @__PURE__ */ new Map();
|
|
880
|
+
const positionals = [];
|
|
881
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
882
|
+
const token = tokens[index];
|
|
883
|
+
if (!token.startsWith("--")) {
|
|
884
|
+
positionals.push(token);
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
const key = token.slice(2);
|
|
888
|
+
const next = tokens[index + 1];
|
|
889
|
+
if (next == null || next.startsWith("--")) flags.set(key, true);
|
|
890
|
+
else {
|
|
891
|
+
flags.set(key, next);
|
|
892
|
+
index += 1;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
return { flags, positionals };
|
|
896
|
+
}
|
|
897
|
+
var invoked = process.argv[1] ? path4.resolve(process.argv[1]) : "";
|
|
898
|
+
if (invoked === fileURLToPath2(import.meta.url)) {
|
|
899
|
+
await main();
|
|
900
|
+
}
|
|
901
|
+
export {
|
|
902
|
+
main
|
|
903
|
+
};
|