[READ-ONLY] a fast, modern browser for the npm registry
at main 31 lines 1.0 kB view raw
1export function parseBasicFrontmatter(fileContent: string): Record<string, unknown> { 2 const match = fileContent.match(/^---[ \t]*\n([\s\S]*?)\n---[ \t]*(?:\n|$)/) 3 if (!match?.[1]) return {} 4 5 return match[1].split('\n').reduce<Record<string, unknown>>((acc, line) => { 6 const idx = line.indexOf(':') 7 if (idx === -1) return acc 8 9 const key = line.slice(0, idx).trim() 10 11 // Remove surrounding quotes 12 let value = line 13 .slice(idx + 1) 14 .trim() 15 .replace(/^["']|["']$/g, '') 16 17 // Type coercion (handles 123, 45.6, boolean, arrays) 18 if (value === 'true') acc[key] = true 19 else if (value === 'false') acc[key] = false 20 else if (/^-?\d+$/.test(value)) acc[key] = parseInt(value, 10) 21 else if (/^-?\d+\.\d+$/.test(value)) acc[key] = parseFloat(value) 22 else if (value.startsWith('[') && value.endsWith(']')) { 23 acc[key] = value 24 .slice(1, -1) 25 .split(',') 26 .map(s => s.trim().replace(/^["']|["']$/g, '')) 27 } else acc[key] = value 28 29 return acc 30 }, {}) 31}