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) <noreply@anthropic.com>
This commit is contained in:
37
server.js
37
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" }));
|
||||
|
||||
Reference in New Issue
Block a user