mermaid-server/debug_quadrant_conversion.js

66 lines
2.3 KiB
JavaScript
Raw Normal View History

2025-05-24 12:08:37 +02:00
// Debug script to test different approaches for quadrant charts
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 };
}
// For quadrant charts, try different encoding methods
const firstWord = normalizedDiagram.split(/\s+/)[0];
if (firstWord === 'quadrantChart') {
console.log("Quadrant chart detected - trying special encoding methods");
// Method 1: Try different newline encoding
const method1 = normalizedDiagram.replace(/\n/g, '\\n');
console.log("Method 1 (\\n encoding):", method1);
// Method 2: Try different marker
const method2 = normalizedDiagram.replace(/\n/g, ' | ');
console.log("Method 2 (pipe separator):", method2);
// Method 3: Try preserving original with different content-type hint
const method3 = `PRESERVE_NEWLINES:${normalizedDiagram}`;
console.log("Method 3 (preserve hint):", method3);
// Let's try method 2 first (pipe separator)
return { rawDsl: method2 };
}
// Convert other types to single line with semicolons
const convertibleTypes = ['graph', 'flowchart', 'sequenceDiagram', 'pie', 'gitGraph'];
if (convertibleTypes.some(type => firstWord.startsWith(type))) {
const lines = normalizedDiagram.split('\n');
const firstLine = lines[0];
const remainingLines = lines.slice(1)
.filter(line => line.trim())
.map(line => line.trim());
const singleLine = firstLine + '; ' + remainingLines.join('; ');
console.log("Converted to single line:");
console.log(singleLine);
return { rawDsl: singleLine };
}
// For other non-convertible types, use NEWLINE encoding
const encodedDiagram = normalizedDiagram.replace(/\n/g, ' NEWLINE ');
console.log("Using NEWLINE encoding:");
console.log(encodedDiagram);
return { rawDsl: encodedDiagram };
};