53 lines
2.0 KiB
JavaScript
53 lines
2.0 KiB
JavaScript
// Smart mermaid conversion script that converts multiline to single-line format
|
|
export const code = async (inputs) => {
|
|
const raw = String(inputs.diagram || "").trim();
|
|
if (!raw) throw new Error("No Mermaid input provided");
|
|
|
|
// Normalize line endings first
|
|
let normalizedDiagram = raw
|
|
.replace(/\r\n/g, '\n') // Convert Windows line endings
|
|
.replace(/\r/g, '\n'); // Convert old Mac line endings
|
|
|
|
console.log("Original diagram:");
|
|
console.log(normalizedDiagram);
|
|
|
|
// Check if it's already single line
|
|
if (!normalizedDiagram.includes('\n')) {
|
|
console.log("Already single line, returning as-is");
|
|
return { rawDsl: normalizedDiagram };
|
|
}
|
|
|
|
// Convert multiline to single line with semicolons
|
|
// This works for: graph, flowchart, sequenceDiagram, pie, gitGraph
|
|
const convertibleTypes = ['graph', 'flowchart', 'sequenceDiagram', 'pie', 'gitGraph'];
|
|
const firstWord = normalizedDiagram.split(/\s+/)[0];
|
|
|
|
if (convertibleTypes.some(type => firstWord.startsWith(type))) {
|
|
// Convert newlines to semicolons, preserving the first line structure
|
|
const lines = normalizedDiagram.split('\n');
|
|
const firstLine = lines[0];
|
|
const remainingLines = lines.slice(1)
|
|
.filter(line => line.trim()) // Remove empty lines
|
|
.map(line => line.trim()); // Remove indentation
|
|
|
|
const singleLine = firstLine + '; ' + remainingLines.join('; ');
|
|
|
|
console.log("Converted to single line:");
|
|
console.log(singleLine);
|
|
|
|
return { rawDsl: singleLine };
|
|
}
|
|
|
|
// For non-convertible types (journey, stateDiagram, classDiagram),
|
|
// try replacing newlines with specific delimiters that might work
|
|
console.log("Non-convertible type detected, using special encoding");
|
|
|
|
// Try using a special marker that we can detect and replace on server side
|
|
const encodedDiagram = normalizedDiagram.replace(/\n/g, ' NEWLINE ');
|
|
|
|
console.log("Encoded diagram:");
|
|
console.log(encodedDiagram);
|
|
|
|
return { rawDsl: encodedDiagram };
|
|
};
|