Repository navigation
fix: keep other frontmatter properties as written when editing or deleting a key - #148
Open
jayhemnani9910 wants to merge 1 commit into
Open
jayhemnani9910 wants to merge 1 commit into
jayhemnani9910 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
frontmatter --editand--deletenow 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:
After
notesmd-cli frontmatter Trip --edit --key status --value done:Parsereads the frontmatter throughadrg/frontmatter, which uses yaml.v2 (YAML 1.1):NO,no,yandonare booleans there, and02134is octal.SetKeyandDeleteKeythen 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.SetKeyandDeleteKeynow parse the frontmatter into ayaml.v3node, 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.--printstill goes throughParse, so it showsNOasfalse. It doesn't write anything, so I left it out of this change.Checklist:
New
TestEditKeepsOtherProperties(exact output forSetKeyandDeleteKey) fails without the change.TestEditEdgeCasescovers 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 changediffshows only the addedstatus: doneline.