diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fddb9f..5f8f89b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Initial release. -- **Export to PDF** code action: posts the current Markdown buffer to `/api/v1/md` and writes `.pdf` next to the source file, overwriting it. Images referenced by a local relative or absolute path are embedded as `data:` URIs first (the service cannot reach the user's filesystem); unreadable images and images over the 5MB per-image limit are left as references and named in one non-fatal warning. +- **Export to PDF** code action: posts the current Markdown buffer to `/api/v1/md` and writes `.pdf` next to the source file, overwriting it. Images referenced by a local relative or absolute path are embedded as `data:` URIs first (the service cannot reach the user's filesystem); if any local image is unreadable or over the 5MB per-image limit, the export stops without writing a PDF and an error dialog that stays until dismissed names each failed reference. - **Validate accessibility** code action: posts to `/api/v1/md/validate` and shows the issue summary (errors, warnings, first messages), logging each issue. Offered only when an `apiToken` is configured, since the endpoint has no anonymous access. It does not embed local images. - **Report problem** on a failed export: posts the failure fingerprints (HTTP status, code-shaped error code, page size, font family, font size, input bytes) to `/api/v1/feedback`; never the document text or file name. - Settings under `lsp.makespdf.settings` mirror the VS Code plugin's defaults, including `serviceUrl`, `apiToken`, `pageSize`, `fontFamily`, `fontSize` and `margins`. diff --git a/README.md b/README.md index 51eec1d..89b2b1b 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Export the Markdown file you're editing to an accessible, archival-quality PDF ( Zed's extension API has no command palette, event hooks or webviews, so makesPDF ships as a **language server for Markdown** whose code actions do the work: -- **Export to PDF** — posts the current buffer (not the file on disk) to makesPDF and writes the PDF next to the source file, overwriting `.pdf`. Images referenced by a local relative or absolute path are embedded as `data:` URIs first, so they appear in the PDF. A message tells you where it went and how many pages it rendered. +- **Export to PDF** — posts the current buffer (not the file on disk) to makesPDF and writes the PDF next to the source file, overwriting `.pdf`. Images referenced by a local relative or absolute path are embedded as `data:` URIs first, so they appear in the PDF; if one cannot be embedded, the export stops and names it. A message tells you where it went and how many pages it rendered. - **Validate accessibility** — runs the same PDF/UA-1 content checks as the renderer (heading hierarchy, image alt text) without producing a PDF. The summary shows the error and warning counts plus the first few issues; every issue is written to the log. - **Report problem** — if an export fails, the error dialog offers **Report problem**, which sends the failure's fingerprints (never your text) to makesPDF. @@ -74,7 +74,7 @@ Your Markdown crosses the network only when you run **Export to PDF** or **Valid Anonymous renders are not persisted to an account; named renders (with an API key) are stored so you can re-download them. See the [privacy policy](https://makespdf.com/legal/privacy). -**Local images are embedded on export.** An image referenced by a relative path (`![diagram](diagram.png)`) or an absolute path is read and embedded as a `data:` URI before the Markdown is sent, so the service can render it without filesystem access. Files larger than 5MB per image are left as plain references and named in a non-fatal warning; the same warning names references that could not be read. `http(s)` URLs are left for the service to fetch. +**Local images are embedded on export.** An image referenced by a relative path (`![diagram](diagram.png)`) or an absolute path is read and embedded as a `data:` URI before the Markdown is sent, so the service can render it without filesystem access. If any local image cannot be embedded (the file is missing or unreadable, or larger than 5MB), the export stops without writing a PDF and an error dialog names each failed reference, so you can fix or remove it and export again. `http(s)` URLs are left for the service to fetch. ## Development diff --git a/server/src/images.test.ts b/server/src/images.test.ts index f2591bb..cc471ee 100644 --- a/server/src/images.test.ts +++ b/server/src/images.test.ts @@ -7,7 +7,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { - formatImageNotice, + formatImageFailure, inlineLocalImages, MAX_IMAGE_BYTES, type ImageFailure, @@ -177,29 +177,30 @@ test("mixed outcomes keep document order in failures and rewrite the readable fi }); }); -test("formatImageNotice names failures in one non-fatal warning", () => { - assert.equal(formatImageNotice([]), null); +test("formatImageFailure names each failed reference and how to fix it", () => { + assert.equal(formatImageFailure([]), null); assert.equal( - formatImageNotice([{ src: "missing.png", reason: "unreadable" }]), - "Could not embed 1 local image: missing.png (could not be read). They were left as-is.", + formatImageFailure([{ src: "missing.png", reason: "unreadable" }]), + "Export stopped, no PDF was written: 1 local image could not be embedded: " + + "missing.png (could not be read). Fix or remove the reference and export again.", ); assert.equal( - formatImageNotice([ + formatImageFailure([ { src: "missing.png", reason: "unreadable" }, { src: "big.png", reason: "too-large" }, ]), - "Could not embed 2 local images: missing.png (could not be read), " + - "big.png (too large to embed, max 5MB). They were left as-is.", + "Export stopped, no PDF was written: 2 local images could not be embedded: " + + "missing.png (could not be read), big.png (too large to embed, max 5MB). " + + "Fix or remove these references and export again.", ); - const many: ImageFailure[] = ["a.png", "b.png", "c.png", "d.png"].map((src) => ({ - src, + const many: ImageFailure[] = Array.from({ length: 12 }, (_, i) => ({ + src: `${i}.png`, reason: "unreadable", })); - assert.equal( - formatImageNotice(many), - "Could not embed 4 local images: a.png (could not be read), " + - "b.png (could not be read), c.png (could not be read)…. They were left as-is.", - ); + const message = formatImageFailure(many)!; + assert.match(message, /^Export stopped, no PDF was written: 12 local images/); + assert.match(message, /9\.png \(could not be read\) and 2 more\./); + assert.ok(!message.includes("10.png"), "names past the tenth are counted, not listed"); }); test("failures across markdown and HTML references keep document order", async () => { diff --git a/server/src/images.ts b/server/src/images.ts index a17574f..633ee88 100644 --- a/server/src/images.ts +++ b/server/src/images.ts @@ -6,7 +6,8 @@ // mirrors the VS Code plugin's `inlineLocalImages`: remote and `data:` URLs // are left alone, code blocks and inline spans are masked so literal // image-like text is untouched, and unreadable or oversized files are -// reported non-fatally and left as references. +// returned as failures. The caller stops the export on any failure: a PDF +// that silently lacks an image is worse than no PDF. // // References are found with left-to-right scans rather than the VS Code // plugin's backtracking regexes: the document is arbitrary input, and runs of @@ -407,21 +408,26 @@ const FAILURE_REASON_TEXT: Record = { "too-large": "too large to embed, max 5MB", }; +/** At most this many failed references are named; the rest are counted. */ +const MAX_NAMED_FAILURES = 10; + /** - * One non-fatal warning naming the images that were left as references, or - * `null` when nothing failed. Mirrors the VS Code plugin's notice: at most - * three names are listed, then an ellipsis. + * The error that stops an export when a local image could not be embedded, + * or `null` when nothing failed. Names each failed reference (up to + * MAX_NAMED_FAILURES) with its reason, so the user can fix or remove it. */ -export function formatImageNotice(failures: ImageFailure[]): string | null { +export function formatImageFailure(failures: ImageFailure[]): string | null { if (failures.length === 0) return null; const names = failures - .slice(0, 3) + .slice(0, MAX_NAMED_FAILURES) .map((failure) => `${failure.src} (${FAILURE_REASON_TEXT[failure.reason]})`); - const ellipsis = failures.length > 3 ? "…" : ""; - const plural = failures.length === 1 ? "" : "s"; + const more = failures.length - names.length; + const listed = more > 0 ? `${names.join(", ")} and ${more} more` : names.join(", "); + const one = failures.length === 1; return ( - `Could not embed ${failures.length} local image${plural}: ` + - `${names.join(", ")}${ellipsis}. They were left as-is.` + `Export stopped, no PDF was written: ${failures.length} local ` + + `image${one ? "" : "s"} could not be embedded: ${listed}. ` + + `Fix or remove ${one ? "the reference" : "these references"} and export again.` ); } diff --git a/server/src/server.ts b/server/src/server.ts index f4ae9cb..1d9e122 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -46,7 +46,7 @@ import { sendFeedback, type RenderFailureFacts, } from "./feedback.ts"; -import { formatImageNotice, inlineLocalImages } from "./images.ts"; +import { formatImageFailure, inlineLocalImages } from "./images.ts"; import { fileDir, isFileUri, pdfTarget } from "./paths.ts"; import { DEFAULT_SETTINGS, @@ -57,6 +57,7 @@ import { import { SERVER_VERSION } from "./version.ts"; const REPORT_PROBLEM = "Report problem"; +const DISMISS = "Dismiss"; const ISSUES_URL = "https://github.com/makesPDF/makespdf-zed-extension/issues"; const REQUEST_TIMEOUT_MS = 120_000; @@ -71,8 +72,9 @@ let anonymousTipShown = false; let withheldTokenWarned = false; // Plain `window/showMessage` notifications for outcomes with nothing to click. -// (`connection.window.showInformationMessage` would send a showMessageRequest, -// which the task reserves for the "Report problem" action.) +// Zed auto-dismisses these after a few seconds, so a message the user must +// act on goes through `connection.window.show*Message` with a button instead +// (a showMessageRequest, which stays until dismissed). function showMessage(type: MessageType, message: string): void { void connection.sendNotification(ShowMessageNotification.type, { type, message }); } @@ -211,8 +213,14 @@ async function exportToPdf(uri: string): Promise { markdown, fileDir(uri), ); - const imageNotice = formatImageNotice(failures); - if (imageNotice) showWarning(`makesPDF: ${imageNotice}`); + // A missing image would otherwise leave a PDF that looks finished but is + // not, so the export stops. The error carries a button because Zed + // auto-dismisses any message without one, and this one must not be missed. + const imageFailure = formatImageFailure(failures); + if (imageFailure) { + await connection.window.showErrorMessage(`makesPDF: ${imageFailure}`, { title: DISMISS }); + return; + } const { title, pdfPath } = pdfTarget(uri); const options = optionsFromSettings(settings, title); diff --git a/server/test/lsp.integration.test.ts b/server/test/lsp.integration.test.ts index d3ef34e..f72f352 100644 --- a/server/test/lsp.integration.test.ts +++ b/server/test/lsp.integration.test.ts @@ -246,7 +246,7 @@ test("codeAction offers Export (and Validate only with a token); Export writes t test("Export inlines local images as data: URIs while Validate sends the plain reference", async () => { const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 7, 7, 7]); const dataUri = `data:image/png;base64,${png.toString("base64")}`; - const markdown = "# Diagram\n\n![diagram](diagram.png)\n\n![gone](missing.png)\n"; + const markdown = "# Diagram\n\n![diagram](diagram.png)\n"; const harness = await startHarness({ markdown, files: { "diagram.png": png }, @@ -274,26 +274,11 @@ test("Export inlines local images as data: URIs while Validate sends the plain r const render = harness.api.requestsFor("/api/v1/md"); assert.equal(render.length, 1); - assert.equal( - render[0]!.json.markdown, - `# Diagram\n\n![diagram](${dataUri})\n\n![gone](missing.png)\n`, - "the readable image is embedded and the unreadable one stays a reference", - ); - - // The unreadable image is named in one non-fatal warning; export still - // ran and wrote the PDF. + assert.equal(render[0]!.json.markdown, `# Diagram\n\n![diagram](${dataUri})\n`); assert.deepEqual( await readFile(join(harness.dir, "report.pdf")), Buffer.from("%PDF-1.4 image mock\n"), ); - const warning = messages(harness.client).find((params) => - /local image/.test(params.message), - ); - assert.ok(warning, "an unreadable image produces a non-fatal warning"); - assert.equal(warning.type, 2, "the notice is a Warning (2)"); - assert.match(warning.message, /missing\.png/); - assert.match(warning.message, /left as-is/); - assert.ok(!warning.message.includes("diagram.png"), "the embedded image is not named"); // Validate checks the Markdown, not the image bytes, so it must not inline. await execute(harness, COMMAND_VALIDATE); @@ -305,6 +290,44 @@ test("Export inlines local images as data: URIs while Validate sends the plain r } }); +test("Export stops with a persistent error when a local image cannot be embedded", async () => { + const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 7, 7, 7]); + const harness = await startHarness({ + markdown: "# Diagram\n\n![diagram](diagram.png)\n\n![gone](missing.png)\n", + files: { "diagram.png": png }, + routes: [ + { + path: "/api/v1/md", + reply: { status: 200, body: Buffer.from("%PDF-1.4 should not be written\n") }, + }, + ], + }); + try { + await execute(harness, COMMAND_EXPORT); + + assert.equal(harness.api.requestsFor("/api/v1/md").length, 0, "nothing is sent"); + await assert.rejects(readFile(join(harness.dir, "report.pdf")), { code: "ENOENT" }); + + // A dialog with a button, not a plain notification: Zed auto-dismisses + // messages without actions, and this one must stay until read. + const dialogs = harness.client.serverRequestsFor("window/showMessageRequest") as any[]; + assert.equal(dialogs.length, 1); + const [dialog] = dialogs; + assert.equal(dialog.type, 1, "the dialog is an Error (1)"); + assert.deepEqual(dialog.actions, [{ title: "Dismiss" }]); + assert.match(dialog.message, /no PDF was written/); + assert.match(dialog.message, /missing\.png \(could not be read\)/); + assert.ok(!dialog.message.includes("diagram.png"), "the embeddable image is not named"); + assert.equal( + messages(harness.client).filter((params) => /local image/.test(params.message)).length, + 0, + "no auto-dismissing notification is sent", + ); + } finally { + await harness.dispose(); + } +}); + test("Export sends no Authorization without a token and Bearer with one", async () => { const harness = await startHarness({ routes: [