Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

Initial release.

- **Export to PDF** code action: posts the current Markdown buffer to `/api/v1/md` and writes `<name>.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 `<name>.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/<version>`; with an `apiToken` configured, requests also carry `Authorization: Bearer <token>`, but only to makesPDF's own origins and loopback addresses.
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<name>.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 `<name>.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.

Expand Down Expand Up @@ -68,13 +68,13 @@ 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**. 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

Expand Down
255 changes: 255 additions & 0 deletions server/src/images.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
// 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<T>(
files: Record<string, string | Buffer>,
fn: (dir: string) => Promise<T>,
): Promise<T> {
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<void> {
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 <img src> in single and double quotes, preserving other attributes", async () => {
await withDir({ "diagram.png": PNG }, async (dir) => {
const source = `<img src="diagram.png" alt="x" width="10">\n<img src='diagram.png'>\n`;
const result = await inlineLocalImages(source, dir);
assert.deepEqual(result.failures, []);
assert.equal(
result.markdown,
`<img src="${PNG_URI}" alt="x" width="10">\n<img src='${PNG_URI}'>\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](<my diagram.png>)\n![two](my%20diagram.png)\n![three](<my diagram.png>)\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)",
'~~~',
'<img src="diagram.png">',
"```",
"",
"~~~",
"<img src='diagram.png'>",
"~~~",
"",
"and `![inline](diagram.png)` too, plus ``<img src=\"diagram.png\">``.",
"",
].join("\n");
const result = await inlineLocalImages(source, dir);
assert.deepEqual(result.failures, []);
assert.equal(
result.markdown,
[
`![real](${PNG_URI})`,
"",
"```md",
"![fenced](diagram.png)",
'~~~',
'<img src="diagram.png">',
"```",
"",
"~~~",
"<img src='diagram.png'>",
"~~~",
"",
"and `![inline](diagram.png)` too, plus ``<img src=\"diagram.png\">``.",
"",
].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.",
);
});

test("failures across markdown and HTML references keep document order", async () => {
await withDir({}, async (dir) => {
const source = '<img src="first-missing.png">\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 <img> lazy-loading from data-src has its real src inlined, not data-src", async () => {
await withDir({ "diagram.png": PNG }, async (dir) => {
const source = '<img data-src="diagram.png" src="diagram.png" alt="x">\n';
const result = await inlineLocalImages(source, dir);
assert.deepEqual(result.failures, []);
assert.equal(
result.markdown,
`<img data-src="diagram.png" src="${PNG_URI}" alt="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),
"<img ".repeat(200_000),
`${"![a](x".repeat(200_000)} y)`,
];
for (const source of sources) {
const started = performance.now();
const result = await inlineLocalImages(source, dir);
const elapsed = performance.now() - started;
assert.equal(result.markdown, source, "nothing is rewritten");
assert.deepEqual(result.failures, []);
// The backtracking regexes this replaced took ~13s at 160KB on the
// first pattern and scaled quadratically, so this bound fails loudly
// on any regression to super-linear scanning.
assert.ok(elapsed < 2_000, `scanning ${source.length} chars took ${elapsed}ms`);
}
});
});
Loading
Loading