mermaid-server/fixed_api_call.js

40 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2025-05-24 11:45:48 +02:00
// Fixed API call script for your workflow
export const code = async (inputs) => {
const { diagram, format } = inputs;
// diagram is now your exact multiline DSL (including any %%init%% lines)
if (!diagram || !diagram.trim()) {
throw new Error("No Mermaid input provided");
}
// Ensure proper newlines are preserved and normalize line endings
const normalizedDiagram = diagram
.replace(/\r\n/g, '\n') // Convert Windows line endings
.replace(/\r/g, '\n') // Convert old Mac line endings
.trim(); // Remove leading/trailing whitespace
// Log for debugging
console.log("Sending diagram:");
console.log(JSON.stringify(normalizedDiagram));
const url = `https://diagrams.starbit.cloud/generate?type=${encodeURIComponent(format)}`;
const resp = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "text/plain; charset=utf-8" // Explicit charset
},
body: normalizedDiagram, // <-- send the normalized, multi-line DSL
});
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 };
};