diff --git a/internal/utils/frontmatter_value_test.go b/internal/utils/frontmatter_value_test.go index c02d11fc4..b8e64b897 100644 --- a/internal/utils/frontmatter_value_test.go +++ b/internal/utils/frontmatter_value_test.go @@ -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", diff --git a/internal/utils/frontmatter_write.go b/internal/utils/frontmatter_write.go index 59b25001b..0286af535 100644 --- a/internal/utils/frontmatter_write.go +++ b/internal/utils/frontmatter_write.go @@ -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)