mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
* fix(frontmatter): read and rewrite SKILL.md that starts with a UTF-8 BOM Windows editors such as Notepad write a BOM before the opening ---. The lenient readers (ParseSkillName, ParseFrontmatterField/Fields/List, ReadSkillBody) did not skip it, so under target_naming standard/prefixed the skill had no readable name and was silently skipped. Strip the BOM in the shared frontmatter locator and the line scanner, and in the hub, search, audit and sync/context readers that split lines themselves (utils.TrimBOM). The writers kept working on the unstripped text, so SetFrontmatterValue prepended a second block and ToggleFrontmatterFlag/RewriteFrontmatterList could leave a BOM in the middle of the file. They now split the BOM off and put it back in front, rewriting only the name value (or flag/list) as before. * fix(frontmatter): cover the dashboard parser and stop auditing a leading BOM A consumer sweep after the first BOM commit found two more places that mishandled a SKILL.md written by Notepad: - audit reported the leading BOM as a HIGH hidden-unicode finding (U+FEFF is in the zero-width rule), so every such skill showed a risk score. A BOM at the very start of a file is its encoding mark and hides nothing; one past the start is still reported. - the dashboard's parseSkillMarkdown saw no frontmatter, so the skill editor would treat the whole file as body and write a second block on save. The audit-engine page and its four translations note the exemption. * fix(ui): keep the BOM when the skill editor saves a SKILL.md parseSkillMarkdown dropped the BOM so the editor could see the frontmatter, but SkillEditor writes the composed text back through PUT .../content, so every dashboard edit silently removed the marker from a Notepad-written file. The parse result now carries the BOM and composeSkillMarkdown puts it back in front, matching the Go rewrite paths.
137 lines
5.0 KiB
Go
137 lines
5.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestSetFrontmatterValue(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{
|
|
name: "existing value is replaced in place, everything else untouched",
|
|
in: "---\n# keep this comment\nname: prototype\ndescription: d\n---\n# Body\nname: not frontmatter\n",
|
|
want: "---\n# keep this comment\nname: emil-design-prototype\ndescription: d\n---\n# Body\nname: not frontmatter\n",
|
|
},
|
|
{
|
|
name: "quoted value and trailing comment are replaced",
|
|
in: "---\nname: \"prototype\" # upstream\n---\nBody",
|
|
want: "---\nname: emil-design-prototype\n---\nBody",
|
|
},
|
|
{
|
|
name: "nested key of the same name is not the field",
|
|
in: "---\nmetadata:\n name: prototype\n---\nBody",
|
|
want: "---\nname: emil-design-prototype\nmetadata:\n name: prototype\n---\nBody",
|
|
},
|
|
{
|
|
name: "CRLF line endings are kept",
|
|
in: "---\r\nname: prototype\r\ndescription: d\r\n---\r\nBody\r\n",
|
|
want: "---\r\nname: emil-design-prototype\r\ndescription: d\r\n---\r\nBody\r\n",
|
|
},
|
|
{
|
|
name: "quoted key is the same field",
|
|
in: "---\n\"name\": prototype\n---\nBody",
|
|
want: "---\n\"name\": emil-design-prototype\n---\nBody",
|
|
},
|
|
{
|
|
name: "space before the colon is the same field",
|
|
in: "---\nname : prototype\n---\nBody",
|
|
want: "---\nname : emil-design-prototype\n---\nBody",
|
|
},
|
|
{
|
|
name: "block scalar is replaced whole, comment before the next key kept",
|
|
in: "---\nname: >-\n proto\n\n type\n# next\ndescription: d\n---\nBody",
|
|
want: "---\nname: emil-design-prototype\n# next\ndescription: d\n---\nBody",
|
|
},
|
|
{
|
|
name: "flow-style mapping keeps its other fields",
|
|
in: "---\n{name: prototype, description: text}\n---\nBody",
|
|
want: "---\n{name: emil-design-prototype, description: text}\n---\nBody",
|
|
},
|
|
{
|
|
name: "flow-style mapping with name last keeps the fields before it",
|
|
in: "---\n{description: text, name: prototype}\n---\nBody",
|
|
want: "---\n{description: text, name: emil-design-prototype}\n---\nBody",
|
|
},
|
|
{
|
|
name: "indented root mapping keeps its indent",
|
|
in: "---\n name: prototype\n description: d\n---\nBody",
|
|
want: "---\n name: emil-design-prototype\n description: d\n---\nBody",
|
|
},
|
|
{
|
|
name: "indented root mapping without name gets it at the same indent",
|
|
in: "---\n description: d\n---\nBody",
|
|
want: "---\n name: emil-design-prototype\n description: d\n---\nBody",
|
|
},
|
|
{
|
|
name: "inherited name is set inside the mapping, before a document end marker",
|
|
in: "---\ndefaults: &d {name: prototype}\n<<: *d\n...\n---\nBody",
|
|
want: "---\nname: emil-design-prototype\ndefaults: &d {name: prototype}\n<<: *d\n...\n---\nBody",
|
|
},
|
|
{
|
|
name: "document end marker with a comment, and indented comments after it",
|
|
in: "---\n# head\ndefaults: &d {name: prototype}\n<<: *d\n... # end\n # trailing\n---\nBody",
|
|
want: "---\n# head\nname: emil-design-prototype\ndefaults: &d {name: prototype}\n<<: *d\n... # end\n # trailing\n---\nBody",
|
|
},
|
|
{
|
|
name: "anchor on the name is kept for its aliases",
|
|
in: "---\nname: &n prototype\ndescription: *n\n---\nBody",
|
|
want: "---\nname: &n emil-design-prototype\ndescription: *n\n---\nBody",
|
|
},
|
|
{
|
|
name: "anchor on the name is kept in a flow mapping",
|
|
in: "---\n{name: &n prototype, description: *n}\n---\nBody",
|
|
want: "---\n{name: &n emil-design-prototype, description: *n}\n---\nBody",
|
|
},
|
|
{
|
|
name: "anchor on the name key is kept",
|
|
in: "---\n&f name: prototype\ndescription: *f\n---\nBody",
|
|
want: "---\n&f name: emil-design-prototype\ndescription: *f\n---\nBody",
|
|
},
|
|
{
|
|
name: "explicit key keeps its key line",
|
|
in: "---\n? name\n: prototype\ndescription: d\n---\nBody",
|
|
want: "---\n? name\n: emil-design-prototype\ndescription: d\n---\nBody",
|
|
},
|
|
{
|
|
name: "inherited name under an explicit first key gets the root indent",
|
|
in: "---\n? defaults\n: &d {name: prototype}\n<<: *d\n---\nBody",
|
|
want: "---\nname: emil-design-prototype\n? defaults\n: &d {name: prototype}\n<<: *d\n---\nBody",
|
|
},
|
|
{
|
|
name: "no frontmatter gets one",
|
|
in: "# Just a body\n",
|
|
want: "---\nname: emil-design-prototype\n---\n# Just a body\n",
|
|
},
|
|
{
|
|
name: "BOM is kept and only the name value changes",
|
|
in: "\ufeff---\r\nname: prototype\r\ndescription: d\r\n---\r\nBody\r\n",
|
|
want: "\ufeff---\r\nname: emil-design-prototype\r\ndescription: d\r\n---\r\nBody\r\n",
|
|
},
|
|
{
|
|
name: "BOM without frontmatter stays first, the new block follows it",
|
|
in: "\ufeff# Just a body\n",
|
|
want: "\ufeff---\nname: emil-design-prototype\n---\n# Just a body\n",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "SKILL.md")
|
|
if err := os.WriteFile(path, []byte(tt.in), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SetFrontmatterValue(path, "name", "emil-design-prototype"); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
got, _ := os.ReadFile(path)
|
|
if string(got) != tt.want {
|
|
t.Errorf("content mismatch\n got: %q\nwant: %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|