Tree macro that uses a different field for structure

I like the way the tree macro produces a nice TOC to entries. But I don’t like the fact that it requires you to change the title to create the tree structure. The title field is over-worked as it is!

This was a quick hack. It probably doesn’t work under all conditions, and will be somewhat less performant than tree. But it allows you to specify a different field other than title when making a tree. So you can structure your tree without changing the title. Tag as a global macro and call as

<<tree2 prefix separator field>>

\define leaf-link(full-title,chunk,separator: "/")
<$link to=<<__full-title__>>><$text text=<<__chunk__>>/></$link>
\end

\define leaf-node(prefix,chunk,field)
<li>
<$list filter="[<__prefix__>addsuffix<__chunk__>is[shadow]] [all[tiddlers]]:filter[get<__field__>prefix<__prefix__>suffix<__chunk__>]" variable="full-title">
<$list filter="[<full-title>get<__field__>trim<__prefix__>]" variable="chunk">
<span>{{$:/core/images/file}}</span> <$macrocall $name="leaf-link" full-title=<<full-title>> chunk=<<chunk>>/>
</$list>
</$list>
</li>
\end

\define branch-node(prefix,chunk,separator: "/", field: "title")
<li>
<$set name="reveal-state" value={{{ [[$:/state/tree/]addsuffix<__prefix__>addsuffix<__chunk__>] }}}>
<$reveal type="nomatch" stateTitle=<<reveal-state>> text="show">
<$button setTitle=<<reveal-state>> setTo="show" class="tc-btn-invisible">
{{$:/core/images/folder}} <$text text=<<__chunk__>>/>
</$button>
</$reveal>
<$reveal type="match" stateTitle=<<reveal-state>> text="show">
<$button setTitle=<<reveal-state>> setTo="hide" class="tc-btn-invisible">
{{$:/core/images/folder}} <$text text=<<__chunk__>>/>
</$button>
</$reveal>
<span>(<$count filter="[all[shadows+tiddlers]get<__field__>removeprefix<__prefix__>removeprefix<__chunk__>] -[<__prefix__>addsuffix<__chunk__>]"/>)</span>
<$reveal type="match" stateTitle=<<reveal-state>> text="show">
<$macrocall $name="tree-node" prefix={{{ [<__prefix__>addsuffix<__chunk__>] }}} separator=<<__separator__>> field=<<__field__>>/>
</$reveal>
</$set>
</li>
\end

\define tree-node(prefix,separator: "/", field="title")
<ol>
<$list filter="[all[shadows+tiddlers]get<__field__>removeprefix<__prefix__>splitbefore<__separator__>sort[]!suffix<__separator__>]" variable="chunk">
<$macrocall $name="leaf-node" prefix=<<__prefix__>> chunk=<<chunk>> separator=<<__separator__>> field=<<__field__>>/>
</$list>
<$list filter="[all[shadows+tiddlers]get<__field__>removeprefix<__prefix__>splitbefore<__separator__>sort[]suffix<__separator__>]" variable="chunk">
<$macrocall $name="branch-node" prefix=<<__prefix__>> chunk=<<chunk>> separator=<<__separator__>> field=<<__field__>>/>
</$list>
</ol>
\end

\define tree2(prefix: "$:/",separator: "/", field: "title")
<div class="tc-tree">
<span><$text text=<<__prefix__>>/></span>
<div>
<$macrocall $name="tree-node" prefix=<<__prefix__>> separator=<<__separator__>> field=<<__field__>>/>
</div>
</div>
\end

2 Likes

I think it a really interesting post!

Though there seem to be teething problems 'cause once I put the macro in a fresh wiki the “Explorer” under “More” went blank??? Just for me?

But the general idea to “Push The Tree” in new ways I think is a v. good one.

Just FYI “@morosanuae” did a tree variant some (long) time ago that would take a search filter–though I think he retired it after @jeremyruston updated the tree mechanism?

Just a comment
TT

I wanted to comment more on this post as it implies a lot.

It is true that using the “tree structure”, inherent to TW, is a great storage route.
Why?
Because it is a visual analogue of PC file systems familiar to many.
It is cognitively intuitive on Round One.

But ADDING new ways to represent “tiddler arrangements” I think a very good strategy to enhance simple nested logic (based on file similes) with additional cross-cutting meanings on Round Two.

TT