diff --git a/internal/utils/frontmatter_value_test.go b/internal/utils/frontmatter_value_test.go index 1d426d5b8..c7cc2e887 100644 --- a/internal/utils/frontmatter_value_test.go +++ b/internal/utils/frontmatter_value_test.go @@ -35,12 +35,12 @@ func TestSetFrontmatterValue(t *testing.T) { { name: "quoted key is the same field", in: "---\n\"name\": prototype\n---\nBody", - want: "---\nname: emil-design-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", + want: "---\nname : emil-design-prototype\n---\nBody", }, { name: "block scalar is replaced whole, comment before the next key kept", @@ -87,6 +87,16 @@ func TestSetFrontmatterValue(t *testing.T) { 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: "no frontmatter gets one", in: "# Just a body\n", diff --git a/internal/utils/frontmatter_write.go b/internal/utils/frontmatter_write.go index b978d1978..7bef28bef 100644 --- a/internal/utils/frontmatter_write.go +++ b/internal/utils/frontmatter_write.go @@ -169,9 +169,18 @@ func SetFrontmatterValue(filePath, key, value string) error { } lines = append(lines[:fm.open+1], append(block, lines[fm.end:]...)...) } else if i, j := fm.keySpan(m, key); i >= 0 { - if anchor := valueAnchor(m, key); anchor != "" { - // Aliases elsewhere may point at the replaced value's anchor. - line = strings.Replace(line, key+": ", key+": &"+anchor+" ", 1) + if v := mappingValue(m, key); v != nil { + // Keep everything before the value as written (the key, its anchor, quoting, + // spacing and indent) and replace only the value, keeping its own anchor for + // aliases elsewhere. Column counts characters and points at an anchor or tag. + i = fm.open + v.Line + before := []rune(lines[i]) + before = before[:min(v.Column-1, len(before))] + anchor := "" + if v.Anchor != "" { + anchor = "&" + v.Anchor + " " + } + line = string(before) + anchor + value + fm.cr } lines = append(lines[:i], append([]string{line}, lines[j:]...)...) } else { @@ -233,14 +242,14 @@ func (fm frontmatterLines) keyLine(key string) int { return -1 } -// valueAnchor returns the anchor on key's value in mapping node m, or "". -func valueAnchor(m *yaml.Node, key string) string { +// mappingValue returns key's value node in mapping node m, or nil. +func mappingValue(m *yaml.Node, key string) *yaml.Node { for k := 0; m != nil && k+1 < len(m.Content); k += 2 { if m.Content[k].Value == key { - return m.Content[k+1].Anchor + return m.Content[k+1] } } - return "" + return nil } // mapping parses the frontmatter into its top-level mapping node, or nil when