1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
function hexoFrontMatterToJSON(yamlStr) { if (!yamlStr || typeof yamlStr !== 'string') { return {}; }
const result = {}; const lines = yamlStr .split(/\r?\n/) .map(line => line.trimEnd()) .filter(line => { const trimmed = line.trim(); return trimmed !== '' && !trimmed.startsWith('#'); });
let currentKey = null; let currentIndent = 0;
for (const line of lines) { const indentMatch = line.match(/^(\s+)/); const indent = indentMatch ? indentMatch[1].length : 0;
const keyValueMatch = line.match(/^\s*([^:#]+?)\s*:\s*(.*)/);
if (keyValueMatch) { const key = keyValueMatch[1].trim(); let value = keyValueMatch[2].trim();
if (value === 'true' || value === 'false') { value = value === 'true'; } else if (!isNaN(value) && value !== '') { value = Number(value); } else if (value === '' || value === 'null') { value = null; } else { value = value.replace(/^["']|["']$/g, ''); }
result[key] = value; currentKey = key; currentIndent = indent; }
else if (line.trim().startsWith('-')) { const arrayValue = line.replace(/^\s*-\s*/, '').replace(/^["']|["']$/g, ''); if (!Array.isArray(result[currentKey])) { result[currentKey] = []; } result[currentKey].push(arrayValue); }
else if (indent > currentIndent && currentKey) { const text = line.trim(); if (text) { result[currentKey] = (result[currentKey] || '') + ' ' + text; } } }
return result; }
|