( content: string, )
| 51 | * Returns an object with title and optional instructions, or null if not a magic doc |
| 52 | */ |
| 53 | export function detectMagicDocHeader( |
| 54 | content: string, |
| 55 | ): { title: string; instructions?: string } | null { |
| 56 | const match = content.match(MAGIC_DOC_HEADER_PATTERN) |
| 57 | if (!match || !match[1]) { |
| 58 | return null |
| 59 | } |
| 60 | |
| 61 | const title = match[1].trim() |
| 62 | |
| 63 | // Look for italics on the next line after the header (allow one optional blank line) |
| 64 | const headerEndIndex = match.index! + match[0].length |
| 65 | const afterHeader = content.slice(headerEndIndex) |
| 66 | // Match: newline, optional blank line, then content line |
| 67 | const nextLineMatch = afterHeader.match(/^\s*\n(?:\s*\n)?(.+?)(?:\n|$)/) |
| 68 | |
| 69 | if (nextLineMatch && nextLineMatch[1]) { |
| 70 | const nextLine = nextLineMatch[1] |
| 71 | const italicsMatch = nextLine.match(ITALICS_PATTERN) |
| 72 | if (italicsMatch && italicsMatch[1]) { |
| 73 | const instructions = italicsMatch[1].trim() |
| 74 | return { |
| 75 | title, |
| 76 | instructions, |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return { title } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Register a file as a Magic Doc when it's read |
no outgoing calls
no test coverage detected