/** * Patches @astrojs/starlight to handle undefined `data.head` in frontmatter. * * Starlight 0.37.x assumes docsSchema() defaults `head` to [] via Zod, but * the content loader doesn't always apply this default, causing: * "Cannot read properties of undefined (reading 'some')" * * This adds a nullish coalescing fallback: `data.head ?? []` */ import { readFileSync, writeFileSync } from 'node:fs'; const file = 'node_modules/@astrojs/starlight/utils/head.ts'; try { let src = readFileSync(file, 'utf8'); const target = 'config.head, data.head)'; const replacement = 'config.head, data.head ?? [])'; if (src.includes(replacement)) { console.log('[patch] head.ts already patched, skipping.'); process.exit(0); } if (!src.includes(target)) { console.warn('[patch] Could not find target string in head.ts — Starlight may have been updated.'); process.exit(0); } src = src.replace(target, replacement); writeFileSync(file, src, 'utf8'); console.log('[patch] Patched head.ts: data.head ?? []'); } catch (err) { console.warn('[patch] Could not patch head.ts:', err.message); }