chhoumann / MetaEdit

MetaEdit for Obsidian

Home Page:https://bagerbach.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Update Subproperty

sigmoid-azoth opened this issue Β· comments

Is there a way to update a yaml subproperty via something like update("metadata.description", "test", "πŸ“‚ nonhierarchical/Untitled.md") ? If not, could this be added?

Example yaml

---
metadata:
    aliases     : []
    datetime    : { created     : 2021-07-22T17:30:05-04:00 }
    description : 
    scope       : personal
    status      : { reviewed    : false }
    tags        : [NOTE/OBJECT]
    title       : Untitled
    type        : 
    uuid        : 1bd559d1-2d3d-4416-9109-082f5ebb03e6
source  :
    template: "[[πŸ“ templates/πŸ“„ new uri πŸ”—.md]]"
    version : 1626988597970
uri     : 
    label   : 
    data    : scheme://www.example.null
    type    : bookmark

---

I couldn't find a way to see value to sub nested yaml fields neither.

Also I can't figure out how it is supposed to work with dataview I was hoping to update these values from tables in data view. Can you please explain a bit on that?

Thanks for this great plugin btw. It is just what I was hoping for

I've been looking into this package to do the heavy lifting https://www.npmjs.com/package/js-yaml

I think this is quite an important feature and should be a top priority.

Thank you for your suggestions! It is most appreciated. This is most definitely something I want to add.

Just to give an update:
Right now, it is on the 'next-up' list for MetaEdit. There are some technical challenges which takes time. I hope that I can leverage packages such as the one @sigmoid-azoth mentioned to reduce the workload. :)

Can't wait for this! Tysm for the plugin, its changed my days of trying to use templater dynamic commands in frontmatter (thank god).

I found a way to hack around this for now.
Since MetaEdit can only deal with single-line properties, we can use the inline block YAML format for the associative array to get around this limitation.

It won't work great for the example in this issue due to needing to collapse all those sub-keys, but for things like daily tracker functionality, it works just fine.

YAML frontmatter

---
track: {shopping: 0, exercise: 0, cinema: 0}
---

dataviewjs code-block

const { update } = this.app.plugins.plugins["metaedit"].api;

const stringifyYamlInline = (obj) =>
  JSON.stringify(obj)
    .replace(/^\{(.*)\}$/, '$1') // remove surrounding curly braces
    .replace(/"(,)?([^"]*?)":/g, '$1 $2: ') // remove quotion marks around keys and add spacing after colon and comma
    .replace(/^ (.*)$/, '{$1}'); // add back the curly braces

const mergeProperty = (propertyName, propertyValue, filePath) =>
  update(
    propertyName,
    stringifyYamlInline({
      ...(dv.page(filePath)[propertyName] ?? {}),
      ...propertyValue
    }),
    filePath
  );

// update the `shopping` and `cinema` sub-keys in the `track` associative array in the current file
mergeProperty('track', { shopping: 1, cinema: 1 }, this.currentFilePath);

// or, as a button
const { createButton } = app.plugins.plugins["buttons"];
createButton({ app, el: this.container, args: { name: "Mall Party!" }, clickOverride: {
  click: mergeProperty, params: [ 'track', { shopping: 1, cinema: 1 }, this.currentFilePath ]
}})