| 5 | * Returns { frontmatter, content } where content is the markdown body without frontmatter. |
| 6 | */ |
| 7 | export function parseFrontmatter(raw: string): { frontmatter: SkillFrontmatter; content: string } { |
| 8 | const match = raw.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/) |
| 9 | if (!match) { |
| 10 | throw createError({ statusCode: 400, message: 'Invalid SKILL.md: missing YAML frontmatter' }) |
| 11 | } |
| 12 | |
| 13 | const yamlBlock = match[1]! |
| 14 | const content = match[2]! |
| 15 | |
| 16 | const frontmatter: Record<string, string | Record<string, string>> = {} |
| 17 | let currentKey = '' |
| 18 | let inMetadata = false |
| 19 | const metadata: Record<string, string> = {} |
| 20 | |
| 21 | for (const line of yamlBlock.split('\n')) { |
| 22 | const trimmed = line.trim() |
| 23 | if (!trimmed || trimmed.startsWith('#')) continue |
| 24 | |
| 25 | if (line.startsWith(' ') && inMetadata) { |
| 26 | const [key, ...valueParts] = trimmed.split(':') |
| 27 | if (key && valueParts.length) { |
| 28 | metadata[key.trim()] = valueParts |
| 29 | .join(':') |
| 30 | .trim() |
| 31 | .replace(/^["']|["']$/g, '') |
| 32 | } |
| 33 | } else { |
| 34 | const colonIndex = line.indexOf(':') |
| 35 | if (colonIndex !== -1) { |
| 36 | currentKey = line.slice(0, colonIndex).trim() |
| 37 | const value = line.slice(colonIndex + 1).trim() |
| 38 | inMetadata = currentKey === 'metadata' && !value |
| 39 | if (!inMetadata && value) { |
| 40 | frontmatter[currentKey] = value.replace(/^["']|["']$/g, '') |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (Object.keys(metadata).length > 0) { |
| 47 | frontmatter.metadata = metadata |
| 48 | } |
| 49 | |
| 50 | if (!frontmatter.name || !frontmatter.description) { |
| 51 | throw createError({ |
| 52 | statusCode: 400, |
| 53 | message: 'Invalid SKILL.md: missing required name or description', |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | return { frontmatter: frontmatter as unknown as SkillFrontmatter, content } |
| 58 | } |
| 59 | |
| 60 | export interface SkillDirInfo { |
| 61 | name: string |