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" }));