Parse extracts YAML frontmatter from a SKILL.md file. Frontmatter is delimited by --- on its own lines.
(content string)
| 29 | // Parse extracts YAML frontmatter from a SKILL.md file. |
| 30 | // Frontmatter is delimited by --- on its own lines. |
| 31 | func Parse(content string) (*ParseResult, error) { |
| 32 | trimmed := strings.TrimLeft(content, "\r\n") |
| 33 | if !strings.HasPrefix(trimmed, delimiter) { |
| 34 | return &ParseResult{Body: content}, nil |
| 35 | } |
| 36 | |
| 37 | rest := trimmed[len(delimiter):] |
| 38 | rest = strings.TrimLeft(rest, "\r\n") |
| 39 | endIdx := strings.Index(rest, "\n"+delimiter) |
| 40 | if endIdx == -1 { |
| 41 | return &ParseResult{Body: content}, nil |
| 42 | } |
| 43 | |
| 44 | yamlContent := rest[:endIdx] |
| 45 | body := rest[endIdx+len("\n"+delimiter):] |
| 46 | body = strings.TrimLeft(body, "\r\n") |
| 47 | |
| 48 | var rawYAML map[string]interface{} |
| 49 | if err := yaml.Unmarshal([]byte(yamlContent), &rawYAML); err != nil { |
| 50 | return nil, fmt.Errorf("invalid frontmatter YAML: %w", err) |
| 51 | } |
| 52 | |
| 53 | var meta Metadata |
| 54 | if err := yaml.Unmarshal([]byte(yamlContent), &meta); err != nil { |
| 55 | return nil, fmt.Errorf("invalid frontmatter YAML: %w", err) |
| 56 | } |
| 57 | |
| 58 | return &ParseResult{ |
| 59 | Metadata: meta, |
| 60 | Body: body, |
| 61 | RawYAML: rawYAML, |
| 62 | }, nil |
| 63 | } |
| 64 | |
| 65 | // InjectGitHubMetadata adds GitHub tracking metadata to the spec-defined |
| 66 | // "metadata" map in frontmatter. Keys are prefixed with "github-" to avoid |