fix(sync): keep the indent of an indented root mapping when renaming

ParseSkillName accepts frontmatter whose root mapping is indented as a
whole, but SetFrontmatterValue wrote name: at column zero beside the
indented keys, so a prefixed copy got malformed YAML. Write the key at
the column the mapping's keys use.
This commit is contained in:
Willie
2026-10-09 11:56:12 +08:00
parent 86ff94fa8a
commit adf375d3e2
2 changed files with 13 additions and 0 deletions
+10
View File
@@ -57,6 +57,16 @@ func TestSetFrontmatterValue(t *testing.T) {
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 description: d\n name: emil-design-prototype\n---\nBody",
},
{
name: "no frontmatter gets one",
in: "# Just a body\n",
+3
View File
@@ -153,6 +153,9 @@ func SetFrontmatterValue(filePath, key, value string) error {
lines := fm.lines
m := fm.mapping()
if m != nil && len(m.Content) > 0 {
line = strings.Repeat(" ", m.Content[0].Column-1) + line // a root mapping may be indented as a whole
}
if m != nil && m.Style&yaml.FlowStyle != 0 {
// Flow-style keys share lines, so the mapping is written back whole.
setMappingScalar(m, key, value)