MetaEdit icon indicating copy to clipboard operation
MetaEdit copied to clipboard

Update Subproperty

Open sigmoid-azoth opened this issue 3 years ago • 6 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

---

sigmoid-azoth avatar Jul 22 '21 21:07 sigmoid-azoth

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

Archie-2021 avatar Jul 24 '21 10:07 Archie-2021

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

sigmoid-azoth avatar Jul 25 '21 12:07 sigmoid-azoth

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

thorlucas avatar Aug 06 '21 07:08 thorlucas

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. :)

chhoumann avatar Aug 06 '21 08:08 chhoumann

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

mayurankv avatar Nov 23 '22 12:11 mayurankv

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 ]
}})

Cybolic avatar Jan 03 '23 13:01 Cybolic