How to auto-increament a property value based on another note's property

What I’m trying to do

How to establish a sense of sequence among my notes.

Each of my notes have these properties

  • previous
  • order

If A and B are notes, and
if B.previous = A, then B.order = A.order + 1
if B.previous is empty, then B.order = 1

If I have a long sequence of notes
A(previous = “”, order = 1),
B(previous = [[A]], order = 2)

Z(previous = [[Y]], order = 26)

And if I insert a new note, let’s say, between E and F, and update the “previous” property of the relevant notes manually, I want the order property of everything from the new note to the last note, Z in this case, to update automatically

Is there a way?

Things I have tried

I tried with Metadata Menu. I added a new audto-update formula field “order” with this formula -

current["previous"] ? Math.max.apply(null, current["previous"].map(a=>dv.page(a.path)).map(a=>a["order"]).filter(a=>typeof(a)=="number")) + 1 : 1

This seems to work sometimes, but not always. The little icon at the bottom status bar shows Metadata Menu is indexing, which I’m guessing means, it’s going through the files and updating the fields. It keeps on flickering between check-mark icon and refresh icon.

I have about 50 notes and I will be adding more as I use Obsidian

How am I updating the “previous” property of each note? - I have another field in Metadata Menu that gets updated based on edges on a canvas

Is my formula correct?
Is there a better way to accomplish this?

It’s much easier to use a previous & next concept. Then you won’t have to update all notes, but just the two immediate siblings of the note you’re working with or where you insert it.

It should also then be easy to traverse the list. And if you want to you could have a dedicated start of the list to get you going on traversals from an end.

I have that as well for traversing. I didn’t mention this because it wasn’t being used in the formula. I have “previous” and “next” properties, both getting updated as edges on a canvas. So, I can open a note and traverse up and down the sequence by clicking the links in these two properties.

But it doesn’t give me a sense of where I am in the list. That’s where the “order” property helps. If it says “order = 100”, I’ll know there are 99 notes before this one

If you know when you insert a new note, I’d say that the best method would be to trigger Templater to have a template run through all relevant notes, and update those in need of that. Maybe you should use this template to also insert the new note into the chain.

GIven you write the new note, and link it to the correct previous entry, you could then trigger a template with the following logic:

  • With the previous document do the following:
    • Get the order and next
    • Set the next to be the current document
  • Back in the current document do the following:
    • Set this documents next to be the one we got earlier
    • Set the order to be an increment of the order we got earlier
  • Now continue to the first next document and do:
    • Set the previous to be current document
  • Start the loop doing the following:
    • Increment the order of current document
    • Go to next as long as there is one, and repeat the loop

All of this should be doable using Templater functions to read and modify files. The same kind of logic could also be done using either QuickAdd or possibly a command defined using Commander. It could be written in dataviewjs also, but you would need to encapsulate it somehow/somewhere to trigger as a onetime run, so the other options are better.

I don’t know how to do this in templater. Could you please give an example snippet?

I might have misremembered about Templater read/write functionality, and you might need to sprinkle in some of the main Obsidian API to read and process file content. The logical would still be sound though, you just need to use other functions.

There are examples within this forum on how to modify files using like in this answer for one approach: Updating frontmater programatically - #4 by ninjineer

Using `app.fileManager.processFrontMatter() is also a valid approach.

And no, I’m not going to do all the work for you. In order for you to have an ownership to what’s happening in your vault, you ought to try to implement this (preferably in a test vault for starters). So produce some code, and someone will most likely give you pointers along the way.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.