How to include missing tiddlers in the Tree Macro?

If you have tiddlers in your wiki named something like $:/level_1/level_2 or $:/level_a/level_b, then the tree macro generates a hierarchical tree like this:

  • level_1
    • level_2
  • level_a
    • level_b

I’m wondering if there are any tricks to get the macro to include missing tiddlers as well? For example say that somewhere in my wiki I also had a link to a tiddler $:/alpha/beta, but had not actually created the tiddler. Could I get the tree macro to show something like the following?

  • level_1
    • level_2
  • level_a
    • level_b
  • alpha
    • beta

To do this, you’ll need to hack the tree macro. But since you don’t want to interfere with future upgrades, better to clone $:/core/macros/tree and call it something else. Then change tree to tree2 and invoke as <<tree2>> .

The code below worked for me. Mostly everywhere that it said “shadows+tiddlers” I changed to “shadows+tiddlers+missing” . Also, added

[<__prefix__>addsuffix<__chunk__>is[missing]]

to the leaf-node macro’s filter at the top. And of course changed tree to tree2 .

tree2

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

\define leaf-node(prefix,chunk)
<li>
<$list filter="[<__prefix__>addsuffix<__chunk__>is[shadow]] [<__prefix__>addsuffix<__chunk__>is[missing]]  [<__prefix__>addsuffix<__chunk__>is[tiddler]]" variable="full-title">
<$list filter="[<full-title>removeprefix<__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: "/")
<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+missing]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__>>/>
</$reveal>
</$set>
</li>
\end

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

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

Thanks a lot Mark, works great!