ParseSkillFrontmatter extracts name, description, and the remaining body from a skill meta file. The expected format is YAML frontmatter delimited by "---" lines: --- name: my-skill description: Does a thing --- Body text here...
(content string)
| 47 | // --- |
| 48 | // Body text here... |
| 49 | func ParseSkillFrontmatter(content string) (name, description, body string, err error) { |
| 50 | content = strings.TrimPrefix(content, "\xef\xbb\xbf") |
| 51 | lines := strings.Split(content, "\n") |
| 52 | if len(lines) == 0 || strings.TrimSpace(lines[0]) != "---" { |
| 53 | return "", "", "", xerrors.New( |
| 54 | "missing opening frontmatter delimiter", |
| 55 | ) |
| 56 | } |
| 57 | |
| 58 | closingIdx := -1 |
| 59 | for i := 1; i < len(lines); i++ { |
| 60 | if strings.TrimSpace(lines[i]) == "---" { |
| 61 | closingIdx = i |
| 62 | break |
| 63 | } |
| 64 | } |
| 65 | if closingIdx < 0 { |
| 66 | return "", "", "", xerrors.New( |
| 67 | "missing closing frontmatter delimiter", |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | frontmatterContent := strings.Join(lines[1:closingIdx], "\n") |
| 72 | var frontmatter map[string]any |
| 73 | if err := yaml.Unmarshal([]byte(frontmatterContent), &frontmatter); err != nil { |
| 74 | return "", "", "", xerrors.Errorf("parse frontmatter YAML: %w", err) |
| 75 | } |
| 76 | |
| 77 | name, ok, err := frontmatterStringField(frontmatter, "name") |
| 78 | if err != nil { |
| 79 | return "", "", "", xerrors.Errorf("%w: %v", ErrFrontmatterNameRequired, err) |
| 80 | } |
| 81 | if !ok || name == "" { |
| 82 | return "", "", "", xerrors.Errorf("%w", ErrFrontmatterNameRequired) |
| 83 | } |
| 84 | description, _, err = frontmatterStringField(frontmatter, "description") |
| 85 | if err != nil { |
| 86 | return "", "", "", err |
| 87 | } |
| 88 | |
| 89 | // Everything after the closing delimiter is the body. |
| 90 | body = strings.Join(lines[closingIdx+1:], "\n") |
| 91 | body = markdownCommentRe.ReplaceAllString(body, "") |
| 92 | body = strings.TrimSpace(body) |
| 93 | |
| 94 | return name, description, body, nil |
| 95 | } |