From 6cac4d16e0885e55902373d99045a3a7e39df34d Mon Sep 17 00:00:00 2001 From: Alex Zaw Date: Fri, 10 Apr 2026 12:20:49 -0700 Subject: [PATCH] Add curl-friendly /api/convert endpoint Accepts raw markdown body, format via query param: curl --data-binary @file.md host/api/convert > out.html curl --data-binary @file.md host/api/convert?format=pdf -o out.pdf Co-Authored-By: Claude Opus 4.6 (1M context) --- server.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/server.js b/server.js index d519d84..157f566 100644 --- a/server.js +++ b/server.js @@ -237,6 +237,43 @@ const server = http.createServer(async (req, res) => { return; } + // curl-friendly API: accepts raw markdown body, format via query param or Accept header + // Usage: + // curl -X POST --data-binary @file.md https://host/api/convert > output.html + // curl -X POST --data-binary @file.md https://host/api/convert?format=pdf -o output.pdf + if (req.method === "POST" && req.url.startsWith("/api/convert")) { + try { + const url = new URL(req.url, `http://${req.headers.host}`); + const formatParam = url.searchParams.get("format") || "html"; + const markdown = await parseBody(req); + + if (!markdown.trim()) { + res.writeHead(400, { "Content-Type": "text/plain" }); + res.end("Empty body — POST raw markdown text"); + return; + } + + const html = markdownToHtml(markdown); + + if (formatParam === "pdf") { + const pdfBuffer = await htmlToPdf(html); + res.writeHead(200, { + "Content-Type": "application/pdf", + "Content-Disposition": "attachment; filename=output.pdf", + }); + res.end(pdfBuffer); + } else { + res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }); + res.end(html); + } + } catch (err) { + console.error("API convert error:", err); + res.writeHead(500, { "Content-Type": "text/plain" }); + res.end("Conversion failed: " + err.message); + } + return; + } + if (req.method === "GET" && req.url === "/health") { res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ status: "ok" }));