Skip to content

fix: keep other frontmatter properties as written when editing or deleting a key - #148

Open
jayhemnani9910 wants to merge 1 commit into
Yakitrak:mainfrom
jayhemnani9910:fix/frontmatter-edit-keeps-other-properties
Open

jayhemnani9910 wants to merge 1 commit into
Yakitrak:mainfrom
jayhemnani9910:fix/frontmatter-edit-keeps-other-properties

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

Description

frontmatter --edit and --delete now change only the key they are given. The frontmatter is edited as a YAML node, so the other properties keep their values, key order and comments. Indentation is written as 2 spaces, the Obsidian default, so a note with another indentation is re-indented.

Motivation and Context

Editing one key rewrote the whole frontmatter and changed the other properties:

country: NO
visa needed: no
options: [y, n, on, off]
version: 1.10
zip: 02134

After notesmd-cli frontmatter Trip --edit --key status --value done:

country: false
options:
    - true
    - false
    - true
    - false
status: done
version: 1.1
visa needed: false
zip: 1116

Parse reads the frontmatter through adrg/frontmatter, which uses yaml.v2 (YAML 1.1): NO, no, y and on are booleans there, and 02134 is octal. SetKey and DeleteKey then marshal the whole map again, so every value goes through that round trip, keys get sorted, and comments are dropped. Obsidian reads these values as the strings and numbers they were.

SetKey and DeleteKey now parse the frontmatter into a yaml.v3 node, replace, add or remove only the target key, and encode the node. yaml.v3 is already a dependency. A replaced value keeps its comment. A duplicated key ends up with one entry, as the old map did. Aliases to a removed or replaced value are replaced by a copy of it, so the frontmatter stays valid. Creating frontmatter on a note without any is unchanged.

--print still goes through Parse, so it shows NO as false. It doesn't write anything, so I left it out of this change.

Checklist:

  • I have written unit tests for my changes.
  • I have updated the documentation accordingly.
  • All new and existing tests passed.

New TestEditKeepsOtherProperties (exact output for SetKey and DeleteKey) fails without the change. TestEditEdgeCases covers the comment, duplicate-key and alias cases. go test ./... passes. Ran the example above with a built binary: before, the file came out as shown, and with the change diff shows only the added status: done line.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant