// Extracts the first h1 or h2 heading from a Markdown body — used as the canonical title fallback.

// Returns the text of the first H1 or H2 heading encountered in body, skipping fenced code blocks. Returns null if none found.
export function firstHeading(body) {
  const lines = body.split('\n')
  let inFence = false
  for (const line of lines) {
    if (line.startsWith('```')) { inFence = !inFence; continue }
    if (inFence) continue
    const m = line.match(/^#{1,2}\s+(.+?)\s*$/)
    if (m) return m[1]
  }
  return null
}
