48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
|
|
// Final fixed API call - handles URL encoding edge cases properly
|
||
|
|
export const code = async (inputs) => {
|
||
|
|
const { diagram, format } = inputs;
|
||
|
|
|
||
|
|
if (!diagram || !diagram.trim()) {
|
||
|
|
throw new Error("No Mermaid input provided");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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
|
||
|
|
|
||
|
|
// Use URLSearchParams for proper encoding of complex content
|
||
|
|
const params = new URLSearchParams();
|
||
|
|
params.set('type', format);
|
||
|
|
params.set('data', normalizedDiagram);
|
||
|
|
|
||
|
|
// Log for debugging
|
||
|
|
console.log("Original diagram:");
|
||
|
|
console.log(JSON.stringify(normalizedDiagram));
|
||
|
|
console.log("URL params:");
|
||
|
|
console.log(params.toString());
|
||
|
|
|
||
|
|
// Build URL manually to avoid double encoding issues
|
||
|
|
const url = `https://diagrams.starbit.cloud/generate?${params.toString()}`;
|
||
|
|
|
||
|
|
const resp = await fetch(url, {
|
||
|
|
method: "GET",
|
||
|
|
headers: {
|
||
|
|
'Accept': 'image/svg+xml, image/png, */*',
|
||
|
|
'User-Agent': 'Mermaid-Client/1.0'
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
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 };
|
||
|
|
};
|