From 14d804648b8d01f822ddcf0454b0ad9fa74f53cc Mon Sep 17 00:00:00 2001 From: Jason Galea Date: Fri, 2 Oct 2026 12:23:22 +1000 Subject: [PATCH 1/4] feat: inline local images as data: URIs before export Port the VS Code plugin's inlineLocalImages into the sidecar: Export to PDF now reads images referenced by local relative or absolute path and embeds them as data: URIs, since the service cannot reach the user's filesystem. Remote/data:/file: and protocol-relative URLs, unknown extensions and code-block text are left alone; unreadable files and files over the server's 5MB image limit are left as references and named in one non-fatal warning. Validate accessibility does not inline. --- server/src/images.test.ts | 203 +++++++++++++++++++++++++++++++++++ server/src/images.ts | 216 ++++++++++++++++++++++++++++++++++++++ server/src/paths.ts | 5 + server/src/server.ts | 15 ++- 4 files changed, 437 insertions(+), 2 deletions(-) create mode 100644 server/src/images.test.ts create mode 100644 server/src/images.ts diff --git a/server/src/images.test.ts b/server/src/images.test.ts new file mode 100644 index 0000000..2fcf12f --- /dev/null +++ b/server/src/images.test.ts @@ -0,0 +1,203 @@ +// Unit tests for the src -> data: URI rewrite used by Export to PDF. + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { mkdtemp, rm, truncate, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import { + formatImageNotice, + inlineLocalImages, + MAX_IMAGE_BYTES, + type ImageFailure, +} from "./images.ts"; + +const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 1, 2, 3]); +const PNG_URI = `data:image/png;base64,${PNG.toString("base64")}`; + +/** Run `fn` in a fresh temp directory pre-populated with `files`. */ +async function withDir( + files: Record, + fn: (dir: string) => Promise, +): Promise { + const dir = await mkdtemp(join(tmpdir(), "makespdf-images-")); + try { + for (const [name, contents] of Object.entries(files)) { + await writeFile(join(dir, name), contents); + } + return await fn(dir); + } finally { + await rm(dir, { recursive: true, force: true }); + } +} + +/** Create an empty file of `size` bytes without materialising its contents. */ +async function sparseFile(path: string, size: number): Promise { + await writeFile(path, ""); + await truncate(path, size); +} + +test("rewrites relative and absolute local references, preserving alt text and title", async () => { + await withDir({ "diagram.png": PNG }, async (dir) => { + const source = `![diagram](diagram.png "The diagram")\n\n![abs](${join(dir, "diagram.png")})\n`; + const result = await inlineLocalImages(source, dir); + assert.deepEqual(result.failures, []); + assert.equal( + result.markdown, + `![diagram](${PNG_URI} "The diagram")\n\n![abs](${PNG_URI})\n`, + ); + }); +}); + +test("rewrites in single and double quotes, preserving other attributes", async () => { + await withDir({ "diagram.png": PNG }, async (dir) => { + const source = `x\n\n`; + const result = await inlineLocalImages(source, dir); + assert.deepEqual(result.failures, []); + assert.equal( + result.markdown, + `x\n\n`, + ); + }); +}); + +test("rewrites every reference to the same file and angle-bracketed or percent-encoded srcs", async () => { + await withDir({ "my diagram.png": PNG }, async (dir) => { + const source = + "![one]()\n![two](my%20diagram.png)\n![three]()\n"; + const result = await inlineLocalImages(source, dir); + assert.deepEqual(result.failures, []); + assert.equal(result.markdown, `![one](${PNG_URI})\n![two](${PNG_URI})\n![three](${PNG_URI})\n`); + }); +}); + +test("leaves remote, data:, file:, protocol-relative, fragment and unknown-extension references alone", async () => { + await withDir({}, async (dir) => { + const source = [ + "![remote](https://example.com/x.png)", + "![plain](http://example.com/x.png)", + "![inline](data:image/png;base64,AAAA)", + "![file](file:///tmp/x.png)", + "![proto](//example.com/x.png)", + "![fragment](#diagram)", + "![doc](notes.txt)", + "", + ].join("\n"); + const result = await inlineLocalImages(source, dir); + assert.deepEqual(result.failures, []); + assert.equal(result.markdown, source); + }); +}); + +test("leaves image-like text inside fenced blocks and inline code untouched", async () => { + await withDir({ "diagram.png": PNG }, async (dir) => { + const source = [ + "![real](diagram.png)", + "", + "```md", + "![fenced](diagram.png)", + '~~~', + '', + "```", + "", + "~~~", + "", + "~~~", + "", + "and `![inline](diagram.png)` too, plus ````.", + "", + ].join("\n"); + const result = await inlineLocalImages(source, dir); + assert.deepEqual(result.failures, []); + assert.equal( + result.markdown, + [ + `![real](${PNG_URI})`, + "", + "```md", + "![fenced](diagram.png)", + '~~~', + '', + "```", + "", + "~~~", + "", + "~~~", + "", + "and `![inline](diagram.png)` too, plus ````.", + "", + ].join("\n"), + ); + }); +}); + +test("an unreadable file is left as-is and reported as unreadable", async () => { + await withDir({}, async (dir) => { + const source = "# Missing\n\n![gone](missing.png)\n"; + const result = await inlineLocalImages(source, dir); + assert.equal(result.markdown, source); + assert.deepEqual(result.failures, [{ src: "missing.png", reason: "unreadable" }]); + }); +}); + +test("a file over 5MB is left as-is and reported as too-large", async () => { + await withDir({}, async (dir) => { + await sparseFile(join(dir, "big.png"), MAX_IMAGE_BYTES + 1); + const source = "![big](big.png)\n"; + const result = await inlineLocalImages(source, dir); + assert.equal(result.markdown, source); + assert.deepEqual(result.failures, [{ src: "big.png", reason: "too-large" }]); + }); +}); + +test("a file exactly at the 5MB limit is inlined", async () => { + await withDir({}, async (dir) => { + await sparseFile(join(dir, "limit.png"), MAX_IMAGE_BYTES); + const result = await inlineLocalImages("![limit](limit.png)\n", dir); + assert.deepEqual(result.failures, []); + assert.ok(result.markdown.startsWith("![limit](data:image/png;base64,")); + assert.ok(!result.markdown.includes("limit.png)")); + }); +}); + +test("mixed outcomes keep document order in failures and rewrite the readable files", async () => { + await withDir({ "ok.png": PNG }, async (dir) => { + await sparseFile(join(dir, "big.png"), MAX_IMAGE_BYTES + 1); + const source = "![ok](ok.png)\n![big](big.png)\n![gone](gone.jpg)\n"; + const result = await inlineLocalImages(source, dir); + assert.equal( + result.markdown, + `![ok](${PNG_URI})\n![big](big.png)\n![gone](gone.jpg)\n`, + ); + assert.deepEqual(result.failures, [ + { src: "big.png", reason: "too-large" }, + { src: "gone.jpg", reason: "unreadable" }, + ]); + }); +}); + +test("formatImageNotice names failures in one non-fatal warning", () => { + assert.equal(formatImageNotice([]), 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.", + ); + assert.equal( + formatImageNotice([ + { 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.", + ); + const many: ImageFailure[] = ["a.png", "b.png", "c.png", "d.png"].map((src) => ({ + src, + 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.", + ); +}); diff --git a/server/src/images.ts b/server/src/images.ts new file mode 100644 index 0000000..6577591 --- /dev/null +++ b/server/src/images.ts @@ -0,0 +1,216 @@ +// Inline local image references as `data:` URIs before an export. +// +// The makesPDF service fetches only `http(s)` and `data:` URLs — it has no +// access to the user's filesystem — so an image referenced by a local path +// (`![diagram](diagram.png)`) would be silently dropped from the PDF. This +// 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. + +import { readFile, stat } from "node:fs/promises"; +import { extname, isAbsolute, resolve } from "node:path"; + +/** + * The service's per-image cap (MAX_IMAGE_SIZE in its image-utils). A file + * above it would be skipped server-side, so its bytes are never uploaded. + */ +export const MAX_IMAGE_BYTES = 5 * 1024 * 1024; + +/** MIME types for image extensions, matching the VS Code plugin. */ +export const IMAGE_MIME_BY_EXT: Record = { + ".png": "image/png", + ".jpg": "image/jpeg", + ".jpeg": "image/jpeg", + ".gif": "image/gif", + ".webp": "image/webp", + ".svg": "image/svg+xml", + ".bmp": "image/bmp", + ".avif": "image/avif", + ".ico": "image/x-icon", + ".tif": "image/tiff", + ".tiff": "image/tiff", + ".apng": "image/apng", +}; + +// Markdown image: ![alt](src "optional title"). Group 2 is the src (possibly +// wrapped in <…>); group 3 is the optional title with its leading whitespace. +const MD_IMAGE = + /!\[([^\]]*)\]\(\s*(<[^>]+>|[^)\s]+)((?:\s+(?:"[^"]*"|'[^']*'))?)\s*\)/g; +// HTML . Group 1 is everything up to and including `src=`, +// group 3/4 is the quoted value (double/single). +const HTML_IMAGE_SRC = /(]*?\bsrc\s*=\s*)("([^"]*)"|'([^']*)')/gi; + +export type ImageFailureReason = "unreadable" | "too-large"; + +export interface ImageFailure { + /** The reference exactly as written in the Markdown. */ + src: string; + reason: ImageFailureReason; +} + +export interface InlineResult { + markdown: string; + /** References left as-is, in document order; empty when all were inlined. */ + failures: ImageFailure[]; +} + +/** + * Replace local image references in `source` with base64 `data:` URIs read + * from disk, resolving relative paths against `baseDir`. Remote URLs (http, + * https, data, file, protocol-relative), unknown extensions, and references + * inside code blocks/spans are left untouched. Unreadable files and files + * over MAX_IMAGE_BYTES are left as references and reported in `failures`. + */ +export async function inlineLocalImages( + source: string, + baseDir: string, +): Promise { + // Protect code so we never rewrite image-like text inside fenced blocks or + // inline spans (where it's literal text the user wants to see verbatim). + const { masked, restore } = maskCode(source); + + const srcs = new Set(); + for (const m of masked.matchAll(MD_IMAGE)) srcs.add(cleanSrc(m[2]!)); + for (const m of masked.matchAll(HTML_IMAGE_SRC)) { + srcs.add(cleanSrc(m[3] ?? m[4] ?? "")); + } + + const candidates = [...srcs].filter( + (src) => isInlinableLocalSrc(src) && IMAGE_MIME_BY_EXT[extname(src).toLowerCase()], + ); + + interface Resolved { + src: string; + dataUri?: string; + reason?: ImageFailureReason; + } + const resolved = await Promise.all( + candidates.map(async (src): Promise => { + const mime = IMAGE_MIME_BY_EXT[extname(src).toLowerCase()]!; + const path = toFsPath(src, baseDir); + try { + // `stat` first so an oversized file's bytes are never read; the + // length check after reading covers a file that grew in between. + if ((await stat(path)).size > MAX_IMAGE_BYTES) { + return { src, reason: "too-large" }; + } + const bytes = await readFile(path); + if (bytes.length > MAX_IMAGE_BYTES) { + return { src, reason: "too-large" }; + } + return { src, dataUri: `data:${mime};base64,${bytes.toString("base64")}` }; + } catch { + return { src, reason: "unreadable" }; + } + }), + ); + + const dataUris = new Map(); + const failures: ImageFailure[] = []; + for (const entry of resolved) { + if (entry.dataUri !== undefined) dataUris.set(entry.src, entry.dataUri); + else if (entry.reason !== undefined) { + failures.push({ src: entry.src, reason: entry.reason }); + } + } + + if (dataUris.size === 0) return { markdown: source, failures }; + + let out = masked.replace( + MD_IMAGE, + (whole, alt: string, url: string, title?: string) => { + const uri = dataUris.get(cleanSrc(url)); + return uri ? `![${alt}](${uri}${title ?? ""})` : whole; + }, + ); + out = out.replace( + HTML_IMAGE_SRC, + (whole, prefix: string, _quoted: string, dq?: string, sq?: string) => { + const uri = dataUris.get(cleanSrc(dq ?? sq ?? "")); + if (!uri) return whole; + const quote = dq !== undefined ? '"' : "'"; + return `${prefix}${quote}${uri}${quote}`; + }, + ); + + return { markdown: restore(out), failures }; +} + +const FAILURE_REASON_TEXT: Record = { + unreadable: "could not be read", + "too-large": "too large to embed, max 5MB", +}; + +/** + * 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. + */ +export function formatImageNotice(failures: ImageFailure[]): string | null { + if (failures.length === 0) return null; + const names = failures + .slice(0, 3) + .map((failure) => `${failure.src} (${FAILURE_REASON_TEXT[failure.reason]})`); + const ellipsis = failures.length > 3 ? "…" : ""; + const plural = failures.length === 1 ? "" : "s"; + return ( + `Could not embed ${failures.length} local image${plural}: ` + + `${names.join(", ")}${ellipsis}. They were left as-is.` + ); +} + +/** Strip surrounding <…> angle brackets and whitespace from a src token. */ +function cleanSrc(raw: string): string { + const s = raw.trim(); + return s.startsWith("<") && s.endsWith(">") ? s.slice(1, -1).trim() : s; +} + +/** + * Whether a src is a local path we should inline. Skips anything with a URL + * scheme of two or more characters (http:, https:, data:, file:) so that + * Windows drive paths like `C:\img.png` are still treated as local, plus + * protocol-relative (`//host/x`) and fragment-only refs. + */ +function isInlinableLocalSrc(src: string): boolean { + if (!src) return false; + if (/^[a-z][a-z0-9+.-]+:/i.test(src)) return false; + if (src.startsWith("//") || src.startsWith("#")) return false; + return true; +} + +/** Resolve a (possibly percent-encoded) src to an absolute filesystem path. */ +function toFsPath(src: string, baseDir: string): string { + let p = src; + try { + p = decodeURIComponent(src); + } catch { + /* malformed escapes — fall back to the raw string */ + } + return isAbsolute(p) ? p : resolve(baseDir, p); +} + +/** + * Replace fenced code blocks and inline code spans with NUL-delimited + * sentinel tokens so the image scan never touches them, returning a + * `restore` to swap them back in. NUL cannot occur in document text. + */ +function maskCode(input: string): { + masked: string; + restore: (s: string) => string; +} { + const stash: string[] = []; + const keep = (m: string) => { + const token = `\u0000CODE${stash.length}\u0000`; + stash.push(m); + return token; + }; + const masked = input + .replace(/```[\s\S]*?```/g, keep) + .replace(/~~~[\s\S]*?~~~/g, keep) + .replace(/``[^`]*``/g, keep) + .replace(/`[^`\n]*`/g, keep); + const restore = (s: string) => + s.replace(/\u0000CODE(\d+)\u0000/g, (_t, i: string) => stash[Number(i)]!); + return { masked, restore }; +} diff --git a/server/src/paths.ts b/server/src/paths.ts index 16ae1c0..25d0e54 100644 --- a/server/src/paths.ts +++ b/server/src/paths.ts @@ -9,6 +9,11 @@ export function isFileUri(uri: string): boolean { return uri.startsWith("file://"); } +/** Directory containing the document, for resolving relative image paths. */ +export function fileDir(uri: string): string { + return dirname(fileURLToPath(uri)); +} + export interface PdfTarget { /** Output path: `/.pdf`. */ pdfPath: string; diff --git a/server/src/server.ts b/server/src/server.ts index b706675..f4ae9cb 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -46,7 +46,8 @@ import { sendFeedback, type RenderFailureFacts, } from "./feedback.ts"; -import { isFileUri, pdfTarget } from "./paths.ts"; +import { formatImageNotice, inlineLocalImages } from "./images.ts"; +import { fileDir, isFileUri, pdfTarget } from "./paths.ts"; import { DEFAULT_SETTINGS, mergeSettings, @@ -203,6 +204,16 @@ async function exportToPdf(uri: string): Promise { return; } + // Inline images referenced by local path so they survive the trip to the + // server, which can only fetch http(s) URLs — it has no filesystem access. + // Remote URLs and existing data: URIs are left untouched. + const { markdown: markdownToSend, failures } = await inlineLocalImages( + markdown, + fileDir(uri), + ); + const imageNotice = formatImageNotice(failures); + if (imageNotice) showWarning(`makesPDF: ${imageNotice}`); + const { title, pdfPath } = pdfTarget(uri); const options = optionsFromSettings(settings, title); const apiToken = requestToken(); @@ -220,7 +231,7 @@ async function exportToPdf(uri: string): Promise { response = await fetch(apiUrl(settings.serviceUrl, RENDER_PATH), { method: "POST", headers: requestHeaders(SERVER_VERSION, apiToken), - body: markdownBody(markdown, options), + body: markdownBody(markdownToSend, options), signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), }); } catch (error) { From 0fb4c789afbf510365abe3adac9b584f8308605d Mon Sep 17 00:00:00 2001 From: Jason Galea Date: Fri, 2 Oct 2026 12:24:43 +1000 Subject: [PATCH 2/4] test: cover image inlining end-to-end; document the new contract The stdio LSP integration test now exports a document referencing a local image and a missing one, asserting the render body carries the data: URI while the validate body keeps the plain reference, and that the missing image produces a non-fatal warning. README's v1 limitation is replaced by the embedding behavior and the privacy line states that referenced image bytes are sent with an export; CHANGELOG updated. --- CHANGELOG.md | 4 +- README.md | 6 +-- server/test/lsp.integration.test.ts | 72 ++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 092c92d..3fddb9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ 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. -- **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. +- **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. +- **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`. - Every request sends `X-MakesPDF-Client: zed/`; with an `apiToken` configured, requests also carry `Authorization: Bearer `, but only to makesPDF's own origins and loopback addresses. diff --git a/README.md b/README.md index b3de54e..fb14b01 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`. 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. 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. @@ -70,11 +70,11 @@ Settings live under `lsp.makespdf.settings` in your Zed `settings.json` (same na Documents up to 20 pages render out of the box, rate-limited to 60/hour and 200/day per IP. An API key removes the per-IP rate limit and the per-render page cap (the 200KB Markdown input cap still applies) and persists your PDFs to [makespdf.com/settings/renders](https://makespdf.com/settings/renders) for re-download. See [makespdf.com/pricing](https://makespdf.com/pricing); PDFs on the Free and Hobbyist plans include a small `makespdf.com` link at the bottom of the page. -Your Markdown crosses the network only when you run **Export to PDF** or **Validate accessibility**. When you use **Report problem**, the message is built from the failure shape (HTTP status, error code, page size, font family, font size, document size in bytes) — never your Markdown, file name or path. The service stores feedback with a daily-salted hash of your IP address and your country code and keeps it until the makesPDF team deletes it; your IP address is also used, unhashed, for rate limiting. +Your Markdown crosses the network only when you run **Export to PDF** or **Validate accessibility**. An export also carries the bytes of the local images your Markdown references by path, embedded as `data:` URIs so the service can render them; **Validate accessibility** sends the Markdown alone, without image bytes. When you use **Report problem**, the message is built from the failure shape (HTTP status, error code, page size, font family, font size, document size in bytes) — never your Markdown, file name or path. The service stores feedback with a daily-salted hash of your IP address and your country code and keeps it until the makesPDF team deletes it; your IP address is also used, unhashed, for rate limiting. 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 not embedded in v1.** The service can fetch `http(s)` URLs and `data:` URIs only, so an image referenced by a local relative path (`![diagram](diagram.png)`) is left as a plain reference and will be missing from the PDF. Host it somewhere reachable or inline it as a `data:` URI if it must appear. +**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. ## Development diff --git a/server/test/lsp.integration.test.ts b/server/test/lsp.integration.test.ts index 87aa15a..d3ef34e 100644 --- a/server/test/lsp.integration.test.ts +++ b/server/test/lsp.integration.test.ts @@ -51,10 +51,16 @@ interface Harness { async function startHarness(options: { routes: MockRoute[]; settings?: Record; + markdown?: string; + files?: Record; }): Promise { const dir = await mkdtemp(join(tmpdir(), "makespdf-zed-")); + const markdown = options.markdown ?? MARKDOWN; const mdPath = join(dir, "report.md"); - await writeFile(mdPath, MARKDOWN); + await writeFile(mdPath, markdown); + for (const [name, contents] of Object.entries(options.files ?? {})) { + await writeFile(join(dir, name), contents); + } const uri = pathToFileURL(mdPath).toString(); const api = await startMockApi(options.routes); @@ -90,7 +96,7 @@ async function startHarness(options: { // happened so the push below is applied on top of a completed pull. await client.waitForServerRequest("workspace/configuration"); client.notify("textDocument/didOpen", { - textDocument: { uri, languageId: "markdown", version: 1, text: MARKDOWN }, + textDocument: { uri, languageId: "markdown", version: 1, text: markdown }, }); // Push the same settings; the push is applied synchronously by the command // handlers, which removes any race with the initial asynchronous pull. @@ -237,6 +243,68 @@ 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 harness = await startHarness({ + markdown, + files: { "diagram.png": png }, + settings: { apiToken: "key_v" }, + routes: [ + { + path: "/api/v1/md", + reply: { status: 200, body: Buffer.from("%PDF-1.4 image mock\n") }, + }, + { + path: "/api/v1/md/validate", + reply: { + status: 200, + body: JSON.stringify({ + valid: true, + issues: [], + summary: { errors: 0, warnings: 0 }, + }), + }, + }, + ], + }); + try { + await execute(harness, COMMAND_EXPORT); + + 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.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); + const validate = harness.api.requestsFor("/api/v1/md/validate"); + assert.equal(validate.length, 1); + assert.equal(validate[0]!.json.markdown, markdown, "Validate posts the buffer verbatim"); + } finally { + await harness.dispose(); + } +}); + test("Export sends no Authorization without a token and Bearer with one", async () => { const harness = await startHarness({ routes: [ From 75bdba4baa022fc96b861de57d85434a85ddec78 Mon Sep 17 00:00:00 2001 From: Jason Galea Date: Fri, 2 Oct 2026 12:25:25 +1000 Subject: [PATCH 3/4] docs: the 200KB cap applies to text, not inlined image bytes --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fb14b01..51eec1d 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Settings live under `lsp.makespdf.settings` in your Zed `settings.json` (same na ## Limits and privacy -Documents up to 20 pages render out of the box, rate-limited to 60/hour and 200/day per IP. An API key removes the per-IP rate limit and the per-render page cap (the 200KB Markdown input cap still applies) and persists your PDFs to [makespdf.com/settings/renders](https://makespdf.com/settings/renders) for re-download. See [makespdf.com/pricing](https://makespdf.com/pricing); PDFs on the Free and Hobbyist plans include a small `makespdf.com` link at the bottom of the page. +Documents up to 20 pages render out of the box, rate-limited to 60/hour and 200/day per IP. An API key removes the per-IP rate limit and the per-render page cap (the 200KB cap still applies to the Markdown text; embedded image bytes are bounded by the service's image limits) and persists your PDFs to [makespdf.com/settings/renders](https://makespdf.com/settings/renders) for re-download. See [makespdf.com/pricing](https://makespdf.com/pricing); PDFs on the Free and Hobbyist plans include a small `makespdf.com` link at the bottom of the page. Your Markdown crosses the network only when you run **Export to PDF** or **Validate accessibility**. An export also carries the bytes of the local images your Markdown references by path, embedded as `data:` URIs so the service can render them; **Validate accessibility** sends the Markdown alone, without image bytes. When you use **Report problem**, the message is built from the failure shape (HTTP status, error code, page size, font family, font size, document size in bytes) — never your Markdown, file name or path. The service stores feedback with a daily-salted hash of your IP address and your country code and keeps it until the makesPDF team deletes it; your IP address is also used, unhashed, for rate limiting. From 8cd7730bff8e44111589437f5cd269f3fe784f20 Mon Sep 17 00:00:00 2001 From: Jason Galea Date: Fri, 2 Oct 2026 12:41:47 +1000 Subject: [PATCH 4/4] fix: scan image references linearly, not with backtracking regexes The VS Code regexes ported in 14d8046 backtrack quadratically on runs of unterminated image references: measured ~3s for 64KB of `![` and ~13s for 160KB of `![a](`, in a single-threaded language server that blocks every export and validate until the scan ends. Replace both scans and both replace passes with left-to-right scanners that memoise the delimiter lookups they would otherwise repeat, and fix two review findings: - merge markdown and HTML failures into document order (the doc comment promised it; running the markdown pass first did not); - require an attribute-name boundary for { "b.png (could not be read), c.png (could not be read)…. They were left as-is.", ); }); + +test("failures across markdown and HTML references keep document order", async () => { + await withDir({}, async (dir) => { + const source = '\n\n![second](second-missing.jpg)\n'; + const result = await inlineLocalImages(source, dir); + assert.deepEqual(result.failures, [ + { src: "first-missing.png", reason: "unreadable" }, + { src: "second-missing.jpg", reason: "unreadable" }, + ]); + }); +}); + +test("an lazy-loading from data-src has its real src inlined, not data-src", async () => { + await withDir({ "diagram.png": PNG }, async (dir) => { + const source = 'x\n'; + const result = await inlineLocalImages(source, dir); + assert.deepEqual(result.failures, []); + assert.equal( + result.markdown, + `x\n`, + ); + }); +}); + +test("a valid reference after a malformed one is still inlined", async () => { + await withDir({ "ok.png": PNG }, async (dir) => { + const result = await inlineLocalImages("![bad](x ![good](ok.png)\n", dir); + assert.deepEqual(result.failures, []); + assert.equal(result.markdown, `![bad](x ![good](${PNG_URI})\n`); + }); +}); + +test("pathological runs of unterminated references are scanned in linear time", async () => { + await withDir({}, async (dir) => { + const sources = [ + "![a](".repeat(200_000), + " = { ".apng": "image/apng", }; -// Markdown image: ![alt](src "optional title"). Group 2 is the src (possibly -// wrapped in <…>); group 3 is the optional title with its leading whitespace. -const MD_IMAGE = - /!\[([^\]]*)\]\(\s*(<[^>]+>|[^)\s]+)((?:\s+(?:"[^"]*"|'[^']*'))?)\s*\)/g; -// HTML . Group 1 is everything up to and including `src=`, -// group 3/4 is the quoted value (double/single). -const HTML_IMAGE_SRC = /(]*?\bsrc\s*=\s*)("([^"]*)"|'([^']*)')/gi; - export type ImageFailureReason = "unreadable" | "too-large"; export interface ImageFailure { - /** The reference exactly as written in the Markdown. */ + /** The reference as written, minus `<…>` brackets and surrounding space. */ src: string; reason: ImageFailureReason; } @@ -70,10 +70,14 @@ export async function inlineLocalImages( // inline spans (where it's literal text the user wants to see verbatim). const { masked, restore } = maskCode(source); + // The two syntaxes are scanned independently, as the regexes were, so a + // reference nested inside another reference's text is still found. + const markdownMatches = scanMarkdownImages(masked); + const htmlMatches = scanHtmlImages(masked); + const srcs = new Set(); - for (const m of masked.matchAll(MD_IMAGE)) srcs.add(cleanSrc(m[2]!)); - for (const m of masked.matchAll(HTML_IMAGE_SRC)) { - srcs.add(cleanSrc(m[3] ?? m[4] ?? "")); + for (const match of [...markdownMatches, ...htmlMatches].sort((a, b) => a.start - b.start)) { + srcs.add(cleanSrc(match.src)); } const candidates = [...srcs].filter( @@ -117,26 +121,287 @@ export async function inlineLocalImages( if (dataUris.size === 0) return { markdown: source, failures }; - let out = masked.replace( - MD_IMAGE, - (whole, alt: string, url: string, title?: string) => { - const uri = dataUris.get(cleanSrc(url)); - return uri ? `![${alt}](${uri}${title ?? ""})` : whole; - }, - ); - out = out.replace( - HTML_IMAGE_SRC, - (whole, prefix: string, _quoted: string, dq?: string, sq?: string) => { - const uri = dataUris.get(cleanSrc(dq ?? sq ?? "")); - if (!uri) return whole; - const quote = dq !== undefined ? '"' : "'"; - return `${prefix}${quote}${uri}${quote}`; - }, - ); - + // Markdown replacements first, then a fresh HTML scan of the result — + // exactly the two `String.replace` passes this replaces. + let out = replaceMatches(masked, markdownMatches, dataUris); + out = replaceMatches(out, scanHtmlImages(out), dataUris); return { markdown: restore(out), failures }; } +/** A well-formed image reference found in the masked document. */ +interface ImageMatch { + /** Index of the opening `![` or `` only: index of the value's opening quote and the quote char. */ + html?: { quoteStart: number; quote: string }; +} + +/** `![alt](src "title")` matches, in document order and in linear time. */ +function scanMarkdownImages(s: string): ImageMatch[] { + const matches: ImageMatch[] = []; + const runs = new RunMemo(); + const quotes = new QuoteMemo(); + const gts = new GtMemo(); + let cursor = 0; + for (;;) { + const start = s.indexOf("![", cursor); + if (start === -1) break; + const altEnd = s.indexOf("]", start + 2); + if (altEnd === -1) break; + if (s[altEnd + 1] !== "(") { + // Every `![` between `start` and this `]` closes its alt here too and + // fails identically; resume past the `]` instead of retrying each one. + cursor = altEnd + 1; + continue; + } + let k = altEnd + 2; + while (k < s.length && isSpace(s[k]!)) k++; + + const match = matchMarkdownSrc(s, start, altEnd, k, runs, quotes, gts); + if (match) { + matches.push(match); + cursor = match.end; + } else { + // Resume past the alt close: later `![` starts after it get their own + // alt close and are parsed normally. + cursor = altEnd + 1; + } + } + return matches; +} + +/** Parse the `src "title")` tail of a markdown image opening at `start`. */ +function matchMarkdownSrc( + s: string, + start: number, + altEnd: number, + k: number, + runs: RunMemo, + quotes: QuoteMemo, + gts: GtMemo, +): ImageMatch | null { + const alt = s.slice(start + 2, altEnd); + + if (s[k] === "<") { + const gt = gts.firstAtOrAfter(s, k + 1); + if (gt > k + 1) { + const title = coverTitle(s, gt + 1, quotes); + if (title !== null) { + return { + start, + end: title.end, + src: s.slice(k, gt + 1), + markdown: { alt, title: title.text }, + }; + } + } + } + + const t = runs.endOfRun(s, k); + if (t > k) { + const title = coverTitle(s, t, quotes); + if (title !== null) { + return { + start, + end: title.end, + src: s.slice(k, t), + markdown: { alt, title: title.text }, + }; + } + } + return null; +} + +/** + * Parse the part after a src: optional whitespace, then either `)` or a + * quoted title followed by optional whitespace and `)`. Returns the match + * end (one past `)`) and the title suffix as the regex replacement kept it. + */ +function coverTitle( + s: string, + pos: number, + quotes: QuoteMemo, +): { end: number; text: string } | null { + let q = pos; + while (q < s.length && isSpace(s[q]!)) q++; + if (s[q] === ")") return { end: q + 1, text: "" }; + if (q === pos) return null; + const quote = s[q]; + if (quote !== '"' && quote !== "'") return null; + const close = quotes.firstAtOrAfter(s, quote, q + 1); + if (close === -1) return null; + let r = close + 1; + while (r < s.length && isSpace(s[r]!)) r++; + if (s[r] !== ")") return null; + return { end: r + 1, text: s.slice(pos, close + 1) }; +} + +/** `` matches, in document order and in linear time. */ +function scanHtmlImages(s: string): ImageMatch[] { + const matches: ImageMatch[] = []; + const gts = new GtMemo(); + let cursor = 0; + for (;;) { + const start = indexOfCI(s, "` was rejected, and a later ``: no later tag can close either). + cursor = gt === -1 ? s.length : gt + 1; + } + } + return matches; +} + +/** Replace every matched reference whose src resolved, preserving the rest. */ +function replaceMatches( + source: string, + matches: ImageMatch[], + dataUris: Map, +): string { + let out = ""; + let cursor = 0; + let changed = false; + for (const match of matches) { + const uri = dataUris.get(cleanSrc(match.src)); + if (uri === undefined) continue; + out += source.slice(cursor, match.start); + if (match.markdown) { + out += `![${match.markdown.alt}](${uri}${match.markdown.title})`; + } else { + const { quoteStart, quote } = match.html!; + out += `${source.slice(match.start, quoteStart)}${quote}${uri}${quote}`; + } + cursor = match.end; + changed = true; + } + return changed ? out + source.slice(cursor) : source; +} + +/** + * The end of a plain src run (the first `)` or whitespace at/after `k`), + * memoised so a run of failing attempts that start inside the same run is + * not rescanned. Queried with non-decreasing `k`. + */ +class RunMemo { + private k = -1; + private t = -1; + + endOfRun(s: string, k: number): number { + if (this.k !== -1 && k >= this.k && k <= this.t) return this.t; + let t = k; + while (t < s.length && s[t] !== ")" && !isSpace(s[t]!)) t++; + this.k = k; + this.t = t; + return t; + } +} + +/** The first `>` at or after a position, memoised across failed candidates. */ +class GtMemo { + private from = -1; + private gt = -1; + + firstAtOrAfter(s: string, from: number): number { + if (this.from !== -1 && from >= this.from && (this.gt === -1 || from <= this.gt)) { + return this.gt; + } + this.from = from; + this.gt = s.indexOf(">", from); + return this.gt; + } +} + +/** The first quote character at or after a position, memoised per quote. */ +class QuoteMemo { + private readonly quotes = new Map(); + + firstAtOrAfter(s: string, quote: string, from: number): number { + const memo = this.quotes.get(quote); + if (memo && from >= memo.from && (memo.index === -1 || from <= memo.index)) { + return memo.index; + } + const index = s.indexOf(quote, from); + this.quotes.set(quote, { from, index }); + return index; + } +} + +const SPACE = /\s/; +const WORD = /[A-Za-z0-9_]/; + +function isSpace(c: string): boolean { + return SPACE.test(c); +} + +function isWordChar(c: string): boolean { + return WORD.test(c); +} + +/** Case-insensitive `indexOf`, searching starts in `[from, to - needle.length]`. */ +function indexOfCI(s: string, needle: string, from: number, to = s.length): number { + const lower = needle.toLowerCase(); + const last = Math.min(to, s.length) - lower.length; + for (let i = Math.max(0, from); i <= last; i++) { + let hit = true; + for (let j = 0; j < lower.length; j++) { + if (s[i + j]!.toLowerCase() !== lower[j]) { + hit = false; + break; + } + } + if (hit) return i; + } + return -1; +} + const FAILURE_REASON_TEXT: Record = { unreadable: "could not be read", "too-large": "too large to embed, max 5MB",