mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
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:
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user