Expand to Current Tiddler in Table of Contents

So far I haven’t encountered such a thing, but it would be SUPER helpful if when I do a search, and open a tiddler, my TOC (I’m using the standard TOC macros – toc-selective-expandable

I use the TOC extensively and often when I search for a topic, it’s difficult to track it down in the TOC (If it happens to be a ‘folder’ tiddler etc).

Has anyone created such a macro/plugin?

Thanks!

@EricShulman has a custom TOC on Inside TiddlyWiki that looks like the sort of thing you’re looking for. Try navigating through his guide and see how the drop-down TOC changes.

I’m not sure whether he’s documented it for general use, though. I took a quick look around https://tiddlytools.com/ and didn’t see it among the packages, although you can find some relevant tiddlers if you search for “TOC”.

Although I haven’t written proper documentation for it, the TiddlyTools TOC macros are suitable for general use.

Just import https://tiddlytools.com/#TiddlyTools%2FTOC into your TiddlyWiki (via drag-and-drop). You can then use

<<toc-tree roottagname>>

to display a TOC that is very similar to the TWCore <<toc-selective-expandable roottagname>>, some very useful added features:

  • As @etardiff noted, TiddlyTools <<toc-tree>> includes handling for auto-opening branches that contain the current tiddler (as defined by the $:/HistoryList!!current-tiddler field contents).
  • TiddlyTools <<toc-tree>> also has handling for drag-and-drop re-organization of the tree items. Drag any tree item (or linked title in tiddler content) and drop it onto an existing tree item and it will be automatically inserted as a “child” of the tree item it was dropped on. If you hold the CTRL key when dropping, the dropped item will be added as a “sibling” BEFORE the item dropped upon. If you hold the SHIFT key when dropping, the dropped item will be added as a sibling AFTER the item dropped upon.
  • The <<toc-all>> macro shows the entire tree without the “selective expandable” handling
  • The <<toc-list>> macro outputs the entire tree as a “flat list” of tiddler titles. You can then use
    <$wikify name="tids" text="<<toc-list roottagname>>">
    to capture this list for further processing (see InsideTiddlyWiki TiddlyTools/NavBar/Bottom for an example of previous/next navigation links)

enjoy,
-e

3 Likes

The main problem with most TOCs is, that some tiddlers are tagged with several tags, that may be part of different branches in the TOC-tree. So it may be the “wrong” branch that is auto-selected. …

I did rewrite the TOC macros. They should contain enough information to allow auto-selecting and expanding. … But I did not reliably resolve the problem described above.

As a workaround you can try my trails-plugin to see the “context” a tiddler is in. eg: Trails-Plugin imported to tw-com looks like this.

1 Like

This works BEAUTIFULLY!
I used my existing TOC - with expand/collapse buttons, and just updated the macro call to toc-tree and BOOM! It worked, with all of your added features. Thank you SO much!

I like it! I’ll play around with trails! Thank you!

One tweak I wonder if you can help me with - if the selected node in the tree is a folder, and it is clicked, is there a tweak I can make to your macro that deselects it and collapses it? So far I don’t see a way to deselect/collapse the currently selected node.

Thanks!

Perhaps you could define folder, do you mean a tiddler that tags other tiddlers, thus has children? Ie one that is typically expandable?

1 Like

It’s not exactly the behavior that you asked for, but perhaps the following will suit your purposes a bit better than the current behavior:

In TiddlyTools/TOC macro definition for toc-show-if-parent(here,exclude), change this line:

<$list filter="[<__here__>!match<item>]" emptyMessage="<<toc-show-branch>>">

to this:

<$list filter="[<__here__>!match<item>]"
   emptyMessage="<$list filter='[{$:/HistoryList!!current-tiddler}!match<item>]'><<toc-show-branch>></$list>">

The effect will be that when the “selected node in the tree” (i.e., the $:HistoryList!!current-tiddler) is a “folder” (i.e., has children), then it will no longer automatically open the branch to show it’s children, but it will still automatically open a branch if the selected node IS one of that branch’s descendents. Of course, you can always manually open a folder branch by using the “>” button next to that folder name.

Let me know how it goes…

-e

1 Like

Thank you for taking the time to offer a suggestion!
When I tried that my ‘folders’ quit opening at all hehe.
I’ll continue working on learning TW coding etc, and may try experimenting.
I love this epanded TOC macro. Thanks again for letting me know about it!

Sorry - by ‘folder’ I mean a parent within the TOC. I like to keep my TOC collapsed when I’m not digging through it (I’m weird), and the ONLY issue I have with the TiddlyTools/TOC is once a ‘folder’ is selected, it won’t collapse, unless I click on a different ‘folder’, but then that one won’t collapse.

That’s not true lol - sometimes I can click on a ‘folder’ label, then click the arrow next to it and it will collapse, but not always.

BUT - Having a TOC that shows me where my current tiddler is within the TOC is absolutely amazing!

The reason for this inconsistency is that the ToC path to the most recently opened tiddler insists on staying open.

If you open a tiddler that has no place in the ToC, then there won’t be a “foot in the door” preventing the collapse of the ToC. (And that’s probably the situation where you noticed what looked like inconsistent behavior.)

In theory, you could modify the chevron close-button action in the TiddlyTools/TOC so that it first clears the $:/HistoryList tiddler (or adds a dummy value at the top) before attempting to close that branch.

1 Like

Thank you SO much for the insights. When I have some time, I’ll try experimenting with that.

It’s been a while since I’ve been able to mess with this. Maybe you can help me, I see where the history is looked at:

\define toc-item()
\whitespace trim
<$tiddler tiddler=<<item>>>
@@.tt-toc-item <nobr>
<$reveal state=<<toc-open-all>> type="nomatch" text="yes"><<toc-toggle-branch>></$reveal>
<$transclude tiddler=<<toc-template>>/> <!-- item template extras -->
<$button class={{{ [[tc-btn-invisible ]] [[tt-toc-button]] [{$:/HistoryList!!current-tiddler}match<item>then[-selected]] +[join[]] }}}
   style="outline:none;" to=<<currentTiddler>> tooltip=<<item>>><<toc-caption>> <$list filter="[<item>has[draft.of]]">(draft)</$list></$button>
<$list filter="[{$:/HistoryList!!current-tiddler}match<item>]"><div style="height:0;width:0;visibility:hidden;"><$scrollhere/></div></$list>
<div style="clear:both;"/>
\end

And I THINK I see where the branch item is being toggled:

\define toc-show-if-parent(here,exclude)
<!-- AUTO SHOW BRANCH IF IT CONTAINS CURRENT TIDDLER -->
<$list filter="[<__here__>!match<item>]" emptyMessage="<<toc-show-branch>>">
   <$list filter="""[[$here$]get<field>enlist-input[]] -[subfilter<__exclude__>] -[[$here$]]""">
      <$macrocall $name="toc-show-if-parent" here=<<currentTiddler>> exclude="""$exclude$ [[$here$]]"""/>
   </$list>
</$list>
\end

\define toc-toggle-branch()
\whitespace trim
<span class="tt-toc-toggle" style="display:inline-block;width:1.2em;">
<$list filter=<<toc-item-is-branch>> emptyMessage="&emsp;">
   <$reveal state=<<toc-branch-is-open>> type="match" text="open">
       <$button class="tc-btn-invisible" tooltip="collapse this item"> {{$:/core/images/down-arrow}}
         <$action-setfield $tiddler=<<toc-config>> $index="""$(path)$/$(item)$"""/>
      </$button>
   </$reveal>
   <$reveal state=<<toc-branch-is-open>> type="nomatch" text="open">
      <$button class="tc-btn-invisible" tooltip="expand this item"> {{$:/core/images/right-arrow}}
         <$action-setfield $tiddler=<<toc-config>> $index="""$(path)$/$(item)$""" $value="open"/>
      </$button>
  </$reveal>
</$list>
</span>
\end

There are a lot of cool features in there (Expand all/Collapse all, Drag and Drop etc) that make it tough to identify JUST the folder toggle. I’ve tried tweaking things here and there, to see what would happen - It breaks lol

I would like to maybe just have a toggle that says if you’re collapsing a folder, by clicking on the arrow, just ignore the current history - expand the branch stuff. Otherwise, do what it currently does.

Thank you!

To prevent the folder from expanding when it is the current tiddler, you need to change the emptyMessage logic in toc-show-if-parent() as I have already explained in my previous post. So… change this:

\define toc-show-if-parent(here,exclude)
<!-- AUTO SHOW BRANCH IF IT CONTAINS CURRENT TIDDLER -->
<$list filter="[<__here__>!match<item>]" emptyMessage="<<toc-show-branch>>">
   <$list filter="""[[$here$]get<field>enlist-input[]] -[subfilter<__exclude__>] -[[$here$]]""">
      <$macrocall $name="toc-show-if-parent" here=<<currentTiddler>> exclude="""$exclude$ [[$here$]]"""/>
   </$list>
</$list>
\end

to this:

\define toc-show-if-parent(here,exclude)
<!-- AUTO SHOW BRANCH IF IT CONTAINS CURRENT TIDDLER -->
<$list filter="[<__here__>!match<item>]"
   emptyMessage="<$list filter='[{$:/HistoryList!!current-tiddler}!match<item>]'><<toc-show-branch>></$list>">
   <$list filter="""[[$here$]get<field>enlist-input[]] -[subfilter<__exclude__>] -[[$here$]]""">
      <$macrocall $name="toc-show-if-parent" here=<<currentTiddler>> exclude="""$exclude$ [[$here$]]"""/>
   </$list>
</$list>
\end

Note: I have done quite a lot of “code cleanup” and small fixes to TiddlyTools/TOC. I strongly suggest getting the latest update from https://tiddlytools.com/#TiddlyTools%2FTOC, which already includes the above change that enables the “folder toggle” handling that you want.

-e

Thank you for re-explaining this. I think I had tried your earlier update, but missed where you explained clicking the folder itself (vs the arrow) would collapse it. So I had moved on to trying other stuff.

Sorry about that.

I’ve grabbed your latest and will use that.
Thanks again for the explanation!