mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
New utility: SetFrontmatterList() writes/removes YAML list fields
in SKILL.md frontmatter with dot-notation support (metadata.targets).
Normalizes legacy top-level targets field. Preserves body content.
New endpoints:
- POST /api/skills/batch/targets — set targets for all skills in folder
- PATCH /api/skills/{name}/targets — set target for single skill
Both use s.mu.Lock() for safe concurrent access.
Includes 15 unit tests covering set/remove/traverse/root-only/not-found.
Part of issue #115.
126 lines
2.6 KiB
Go
126 lines
2.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// SetFrontmatterList writes a YAML list field into a SKILL.md file's frontmatter.
|
|
// The field parameter uses dot notation: "metadata.targets" operates on fm["metadata"]["targets"].
|
|
// When values is nil, the field is removed. When non-nil, the field is set.
|
|
// For "metadata.targets", any legacy top-level "targets" field is also removed.
|
|
// All other frontmatter fields and the body content are preserved.
|
|
func SetFrontmatterList(filePath string, field string, values []string) error {
|
|
data, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
content := string(data)
|
|
fmRaw, body := splitFrontmatterAndBody(content)
|
|
|
|
var fm map[string]any
|
|
if fmRaw != "" {
|
|
if err := yaml.Unmarshal([]byte(fmRaw), &fm); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if fm == nil {
|
|
fm = make(map[string]any)
|
|
}
|
|
|
|
parts := strings.SplitN(field, ".", 2)
|
|
|
|
if len(parts) == 2 && parts[0] == "metadata" {
|
|
subField := parts[1]
|
|
md, _ := fm["metadata"].(map[string]any)
|
|
if md == nil {
|
|
md = make(map[string]any)
|
|
}
|
|
|
|
if values == nil {
|
|
delete(md, subField)
|
|
} else {
|
|
list := make([]any, len(values))
|
|
for i, v := range values {
|
|
list[i] = v
|
|
}
|
|
md[subField] = list
|
|
}
|
|
|
|
if len(md) == 0 {
|
|
delete(fm, "metadata")
|
|
} else {
|
|
fm["metadata"] = md
|
|
}
|
|
|
|
// Always remove legacy top-level field when operating on metadata.<field>
|
|
delete(fm, subField)
|
|
} else {
|
|
topField := parts[0]
|
|
if values == nil {
|
|
delete(fm, topField)
|
|
} else {
|
|
list := make([]any, len(values))
|
|
for i, v := range values {
|
|
list[i] = v
|
|
}
|
|
fm[topField] = list
|
|
}
|
|
}
|
|
|
|
fmBytes, err := yaml.Marshal(fm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var sb strings.Builder
|
|
sb.WriteString("---\n")
|
|
sb.Write(fmBytes)
|
|
sb.WriteString("---\n")
|
|
if body != "" {
|
|
sb.WriteString(body)
|
|
}
|
|
|
|
return os.WriteFile(filePath, []byte(sb.String()), 0644)
|
|
}
|
|
|
|
// splitFrontmatterAndBody splits SKILL.md content into raw frontmatter YAML
|
|
// and the remaining body. Returns ("", fullContent) if no frontmatter found.
|
|
func splitFrontmatterAndBody(content string) (string, string) {
|
|
if !strings.HasPrefix(strings.TrimSpace(content), "---") {
|
|
return "", content
|
|
}
|
|
|
|
lines := strings.Split(content, "\n")
|
|
inFrontmatter := false
|
|
fmStart := -1
|
|
fmEnd := -1
|
|
|
|
for i, line := range lines {
|
|
if strings.TrimSpace(line) == "---" {
|
|
if !inFrontmatter {
|
|
inFrontmatter = true
|
|
fmStart = i + 1
|
|
} else {
|
|
fmEnd = i
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if fmEnd < 0 {
|
|
return "", content
|
|
}
|
|
|
|
fmRaw := strings.Join(lines[fmStart:fmEnd], "\n")
|
|
body := ""
|
|
if fmEnd+1 < len(lines) {
|
|
body = strings.Join(lines[fmEnd+1:], "\n")
|
|
}
|
|
|
|
return fmRaw, body
|
|
}
|