YAML: Stack-Fix, best-effort Output auch bei nicht-parsebarem YAML

This commit is contained in:
2026-05-30 00:04:55 +02:00
parent f4f1889dce
commit e8e1ca9fae

View File

@@ -120,31 +120,40 @@ function collectPaths(data, path='root', paths=[]) {
return paths;
}
// ── YAML Auto-Fix: normalisiert Einrückung bevor js-yaml parst ───────────────
// ── YAML Auto-Fix: Stack-basierter Strukturerhalter ──────────────────────────
function fixYamlIndentation(text) {
const lines = text.split('\n');
const lines = text.split('\n').map(l => l.replace(/\t/g, ' '));
const stack = [{ indent: -1, depth: -1 }];
const result = [];
// 1. Tabs → Spaces (4 oder 2)
const detabbed = lines.map(l => l.replace(/\t/g, ' '));
for (const line of lines) {
if (!line.trim()) { result.push(''); continue; }
// 2. Finde die kleinste vorkommende Einrückung > 0 (das ist die Einheit)
const indents = detabbed
.filter(l => l.trim() && !l.trim().startsWith('#'))
.map(l => l.search(/\S/))
.filter(n => n > 0);
const unit = indents.length ? Math.min(...indents) : 2;
const indent = line.search(/\S/);
const content = line.trim();
// 3. Remap: jede Einrückung → Niveau * 2 Spaces
const remapped = detabbed.map(line => {
if (!line.trim()) return '';
if (line.trim().startsWith('#')) return line.trimEnd();
const spaces = line.search(/\S/);
if (spaces <= 0) return line.trimEnd();
const level = Math.round(spaces / unit);
return ' '.repeat(level) + line.trimStart().trimEnd();
});
if (content.startsWith('#')) {
result.push(' '.repeat(Math.max(0, stack[stack.length-1].depth + 1)) + content);
continue;
}
return remapped.join('\n');
// Pop stack-Einträge die >= aktuelle Einrückung haben
while (stack.length > 1 && stack[stack.length-1].indent >= indent) {
stack.pop();
}
const depth = stack[stack.length-1].depth + 1;
stack.push({ indent, depth });
result.push(' '.repeat(depth) + content);
}
return result.join('\n');
}
// Versucht einen lesbaren Fehler aus dem js-yaml Fehler zu bauen
function shortError(e) {
const first = e.message.split('\n')[0];
return first.length > 120 ? first.slice(0,120)+'…' : first;
}
// ── YAML Tool ─────────────────────────────────────────────────────────────────
@@ -167,17 +176,18 @@ function YamlTool({ mobile }) {
try { obj = jsyaml.load(text); }
catch(e) { parseError = e; }
// Versuch 2: wenn Fehler → Auto-Fix, dann nochmal
// Versuch 2: Auto-Fix, dann nochmal
if (parseError) {
try {
const fixed = fixYamlIndentation(text);
try {
obj = jsyaml.load(fixed);
setWarning(`⚠ Einrückung automatisch korrigiert: ${parseError.message.split('\n')[0]}`);
setWarning(`⚠ Einrückung automatisch korrigiert bitte Ergebnis prüfen`);
} catch(e2) {
// Fix hat auch nicht geholfen originalen Fehler zeigen
setError(parseError.message);
setWarning('');
setParsed(null); setFormatted('');
// Auch nach Fix nicht parsebar → best-effort Text trotzdem anzeigen
setError(shortError(parseError));
setWarning('Struktur zu stark beschädigt nur Einrückung wurde normalisiert. Bitte manuell korrigieren.');
setParsed(null);
setFormatted(fixed); // zeige korrigierten Text zum manuellen Nachbessern
return;
}
} else {
@@ -260,17 +270,26 @@ function YamlTool({ mobile }) {
</div>
</div>
{error ? (
{error && !formatted ? (
<div style={{background:'rgba(255,107,157,0.08)',border:'1px solid rgba(255,107,157,0.2)',
borderRadius:8,padding:14,color:'#ff6b9d',fontFamily:'monospace',fontSize:11,lineHeight:1.7}}>
{error}
</div>
) : (<>
{warning && (
<div style={{background:'rgba(255,230,109,0.07)',border:'1px solid rgba(255,230,109,0.2)',
borderRadius:7,padding:'7px 12px',color:'#ffe66d',fontFamily:'monospace',
fontSize:10,lineHeight:1.6,marginBottom:8}}>
{warning}
<div style={{background: error ? 'rgba(255,230,109,0.07)' : 'rgba(78,205,196,0.07)',
border:`1px solid ${error ? 'rgba(255,230,109,0.25)' : 'rgba(78,205,196,0.2)'}`,
borderRadius:7,padding:'7px 12px',
color: error ? '#ffe66d' : '#4ecdc4',
fontFamily:'monospace',fontSize:10,lineHeight:1.6,marginBottom:8}}>
{error ? '⚠ ' : '✓ '}{warning}
</div>
)}
{error && formatted && (
<div style={{background:'rgba(255,107,157,0.06)',border:'1px solid rgba(255,107,157,0.15)',
borderRadius:7,padding:'6px 12px',color:'rgba(255,107,157,0.7)',
fontFamily:'monospace',fontSize:9,lineHeight:1.6,marginBottom:8}}>
{error}
</div>
)}
{view==='raw' ? (