Location of Item in a Tree Created from Tag

There are some tiddlers organized in a tree like structure by tag root as below

L1
|---L11
L2 
|--- L21
|--- L22
L3
|--- L31
|--- L32
|----- L321
|----- L322
|----- L323
L4 
|--- L41
|--- L42

I am looking for a simple macro to show the location of a tiddler in the tree. For example I give L321 and it returns

root/L3/L32/L321

or I give L41, macro should returns

root/L4/L41

Note that the names above are arbitrary and tiddlers can have any names!

Welcome atronoush!

I’ve been thinking about a similar problem using the TOC macro.

I think my overall strategy will be to use the kin plugin to determine all the tiddlers in the tree. Then apply a search criteria to find tiddlers that match. Then walk back up the tree for each of those tiddlers until reaching the root. Actually, the kin plugin will let you walk up a tree as well, so maybe that step can be simplified.

In my case though, I hope to be using the selective expanded mode, and open the branches that are pathways to the tiddler matching the criteria.

Good luck!

1 Like

Thank you Mark.
Actually I use this for navigation! So, it can be used with TOC also!
I am not familiar with kin plugin but will have a look to see how it works!

This is a bit rough, but should be tweakable to what you need if you don’t want to use the plugin @atronoush. It’s just a general recursive loop I tweaked from a post on “recursive” in Google Groups.

\define parent-loop() <$list filter="[<currentTiddler>tags[]]"><<currentTiddler>>#<$macrocall $name="parent-loop">></$list>

\define parents() <$tiddler tiddler="$(currentTiddler)$"><<parent-loop>></$tiddler>

That’s the meat of the work, calling parents which calls parent-loop reclusively.

If you want to show in order of “up” the chain as it would be discovered, it’d be something like:

<$tiddler tiddler="child-tiddler">
<<parents>>
</$tiddler>

And since you showed it instead in order of top down, you’d have to wikify it, split, reverse order, and then re-join it like this:

<$tiddler tiddler="child-tiddler">
<$wikify name="myparents" text=<<parents>>>{{{ [<myparents>split[#]butlast[1]reverse[]join[/]] }}}</$wikify>
</$tiddler>

Note that I “lazily” used the # character as a delimiter, so if you use that in your titles, change it to another character. Note the child-tiddler is the name of the tiddler at the end of the chain so-to-speak.

1 Like

@stobot thank you!
works like a charm!

I gonna to test on a wiki with several hundreds tiddlers in a tree like structure (hierarchical)
I will return and tell you how the performance is!

@atronoush welcome to discourse.

There will be new opportunities in the prerelease 5.2.0 using the counter variable. However as @stobot points out others (and I) have published extensively on recursive solutions on GG. Basically when you start to “iterate hierarchies” (other than using the TOC macros) it really pays to make your own nested or recursive macros.

Clever solution @stobot
There is only one issue, if a child tiddler has more than one tag, how the code will work?
I think it should see which tag returns to parent or grand parent root

1 Like

If you use several tags for 1 tiddler the mechanism will probably show the first tag it finds and the “path” isn’t consistent.

You could use my tocP plugin and use a similar mechanism. Since ther can only be 1 parent, the “path” will be consistent.

BUT the whole TOC structure will use a completely different concept. It’s field-based instead of tag based.

see: tocP — Parent based - Table of Content

Thank you for all inputs! The @stobot solution works great, except when a child tiddler has more than one tag!

I have heard about kin plugin! and I will give a try!

1 Like

Good to hear @atronoush - yeah I was targeting more of a “teach a man to fish” solution to append to the “give a man a fish” solution that came from the plugins that were already suggested. I think in a forum like this, if you can give both, then that’s the best. I’ve learned most of what I know from people like @EricShulman and others giving really detailed answers with mock code so I try to pay that forward.

To take it slightly further per the “gotchas” mentioned above, I’ll say there are a few relatively easy ways to enhance the core/base pattern I posted. I’ll point out just for reference:

  1. If you have extra tags and you know what they are:
    For example, if all of those tiddlers in the hierarchy were also “People” and therefore also had a “People” tag, then you could add a !tag[People] into the filter in the right place (right after the tags[] part of the parent-loop macro) to ignore those.

  2. If you will have multiple ways of getting to the same place due to just a jagged hierarchy:
    If you happen to not really show all of the ways to get from the top to the bottom, you could just arbitrarily pick one by adding a limit[1] to the same spot (right after the tags[] part of the parent-loop macro). This way it still “works” in a way in that you still get a hierarchy, it’s just not all of the possible hierarchy paths. That might be fine. If you needed all, you’d want to think about how you’d want that returned. Happy to help brainstorm if that’s the case.

  3. If you’d rather use fields instead of tags:
    You’d have to change a few places, but the pattern would be the same. For instance, if you had a field for each of these called myparent for instance and it only stored a single possible parent, then you’d only need to change the tags[] to get[myparent] in the parent-loop macro and it should work the same I think.

Hope that’s helpful,

I am in the process of responding to the original post in full and taking account of the issues that already exist.

Basically its a simpler rewrite of the TOC macros based on new methods and the counter widget of 5.2.0. It currently includes the ability to feed it different filters to determine membership and order.

I intend to include the ability for nodes to specify their own filter for their children.

I need to finalise the checking of loops. Not displayed is the full branch is displayed on mouse over the list icon.

I can wrap the same solution in a button, and capture the state of the hierarchy indefinitely.

Then this could be used to search for a specific item eg; 0.1.5.5. then link to the tiddler. You can see how this satisfies the “Location of Item”.

I have attached by work in progress, warning it is only that.
toc-positions.json (1.7 KB)

Later I will add different expandable and tabbed features.

Thank you all! Thank you @stobot!
Your code is a good starting point!