37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
// Simple POST fix - back to basics with proper headers
|
|
export const code = async (inputs) => {
|
|
const { diagram, format } = inputs;
|
|
|
|
if (!diagram || !diagram.trim()) {
|
|
throw new Error("No Mermaid input provided");
|
|
}
|
|
|
|
// Ensure proper line endings (just normalize, don't over-process)
|
|
const cleanDiagram = diagram.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
|
|
console.log("Sending diagram (length: " + cleanDiagram.length + "):");
|
|
console.log(JSON.stringify(cleanDiagram));
|
|
|
|
const url = `https://diagrams.starbit.cloud/generate?type=${format}`;
|
|
|
|
const resp = await fetch(url, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "text/plain",
|
|
"Content-Length": cleanDiagram.length.toString()
|
|
},
|
|
body: cleanDiagram
|
|
});
|
|
|
|
if (!resp.ok) {
|
|
const txt = await resp.text();
|
|
throw new Error(`Mermaid-server error ${resp.status}: ${txt}`);
|
|
}
|
|
|
|
const buffer = Buffer.from(await resp.arrayBuffer());
|
|
const mime = format === "png" ? "image/png" : "image/svg+xml";
|
|
const file = `data:${mime};base64,${buffer.toString("base64")}`;
|
|
|
|
return { file };
|
|
};
|