mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
fix(sync): read a block-scalar skill name
ParseSkillName stopped at "name: >-" and decoded only that header, so the name was empty and the skill was skipped under standard and prefixed naming. Keep reading the indented lines of a block scalar before decoding it.
This commit is contained in:
@@ -12,14 +12,23 @@ import (
|
||||
)
|
||||
|
||||
// ParseSkillName reads the SKILL.md and extracts the top-level "name" from frontmatter.
|
||||
// It stops at the name line, so only a flow-style block ({...}) is read whole. Top-level
|
||||
// keys share the first key's indent; "name", 'name' and name : all count.
|
||||
// It stops after the name value (a block scalar's indented lines included), so only a
|
||||
// flow-style block ({...}) is read whole. Top-level keys share the first key's indent;
|
||||
// "name", 'name' and name : all count.
|
||||
func ParseSkillName(skillPath string) (string, error) {
|
||||
name, indent := "", -1
|
||||
var flow []string // the block's lines once it turns out to be a flow mapping
|
||||
var flow []string // the block's lines once it turns out to be a flow mapping
|
||||
var scalar []string // the name line and its indented lines when name is a block scalar
|
||||
err := scanLenientBlock(filepath.Join(skillPath, "SKILL.md"), func(raw []byte) bool {
|
||||
line := string(raw)
|
||||
trimmed := strings.TrimLeft(line, " \t")
|
||||
if scalar != nil {
|
||||
if trimmed == "" || len(line)-len(trimmed) > indent {
|
||||
scalar = append(scalar, line)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
if flow != nil {
|
||||
flow = append(flow, line)
|
||||
return true
|
||||
@@ -37,6 +46,10 @@ func ParseSkillName(skillPath string) (string, error) {
|
||||
if len(line)-len(trimmed) != indent || !ok || strings.Trim(strings.TrimSpace(key), `"'`) != "name" {
|
||||
return true
|
||||
}
|
||||
if v := strings.TrimSpace(value); v != "" && (v[0] == '|' || v[0] == '>') {
|
||||
scalar = []string{"name:" + value}
|
||||
return true
|
||||
}
|
||||
// The line alone decodes quotes and a trailing comment; a value YAML rejects is read as is.
|
||||
var fm map[string]any
|
||||
if yaml.Unmarshal([]byte("name:"+value), &fm) == nil {
|
||||
@@ -51,8 +64,8 @@ func ParseSkillName(skillPath string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if flow != nil {
|
||||
name, _ = decodeFrontmatter([]byte(strings.Join(flow, "\n")))["name"].(string)
|
||||
if lines := append(flow, scalar...); lines != nil {
|
||||
name, _ = decodeFrontmatter([]byte(strings.Join(lines, "\n")))["name"].(string)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
@@ -373,6 +373,7 @@ func TestParseSkillName_ValidYAMLForms(t *testing.T) {
|
||||
`name: prototype # upstream`,
|
||||
`{name: prototype, description: d}`,
|
||||
"metadata:\n name: other\nname: prototype",
|
||||
"name: >-\n prototype\ndescription: d",
|
||||
"name: prototype\nbad: [unclosed", // not YAML: the name: line is still read
|
||||
} {
|
||||
dir, _ := writeSkill(t, []byte("---\n"+fm+"\n---\nbody\n"))
|
||||
|
||||
Reference in New Issue
Block a user