fix(sync): rewrite only the name value, keeping its key as written

Rewriting the whole name line dropped whatever sat on the key side: an
anchor on the key (&f name:) left its aliases undefined, and quoting,
"name :" spacing or an explicit "? name" key were rewritten. Keep the
text before the value as written, located from the YAML node, and
replace only the value span, keeping the value's own anchor.
This commit is contained in:
Willie
2026-10-09 12:44:24 +08:00
parent 183beeba54
commit 666df43edd
2 changed files with 28 additions and 9 deletions
+12 -2
View File
@@ -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",
+16 -7
View File
@@ -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