Here’s my TiddlyTools <<toc-list>> macro (see https://tiddlytools.com/#TiddlyTools%2FTOC), which walks a TOC tree structure in the manner you have described (“depth-first traversal”) and produces a “flat list” output of links to all tiddlers in the TOC:
Only the here parameter is required, where here is the top-level starting tag.
Example: <<toc-list "TableOfContents">>
max (optional) allows you to limit the depth of traversal.
Example: <<toc-list here:"TableOfContents" max:3>>
exclude (optional) can be used to provide a filter that ignores specified tiddlers. It is also used internally to prevent infinite recursive loops by ignoring any tiddlers that have already been visited in the current TOC tree branch.
level is used internally (along with max) to track the current depth.
field:fieldname (optional, default=“tags”) allows you to define the tree structure using a field other than “tags” (e.g., field:parent)
You can use <$wikify name="flatlist" text="<<toc-list TableOfContents>>">...</$wikify> to capture the output into a variable that can then be used via [enlist<flatlist>] to perform operations on the resultant tiddler list (e.g., previous/next navigation via <$link to={{{ [enlist<flatlist>before<currentTiddler>] }}}>previous</$link> and <$link to={{{ [enlist<flatlist>after<currentTiddler>] }}}>next</$link>
The macro can also be simplified a bit by removing the handling for max, level and field parameters, like this:
Thanks @EricShulman you have documented how to achieve what I was asking for, however I was aware of this approach, and was wondering if the taggingtree filter operator could return the flat list, making it a lot simpler. It is so close to doing this already and with 5.3.0 it could be used in a function.
Until taggingtree has this output order, the way you show is the only way to achive this so I am sure it will help others finding this thread by search.
Good catch! IT’S A BUG IN MY CODE!!! There were TWO problems:
The first line of the toc-list() macro:
<$list filter="[<__level__>!match<__max__>]">
checks for “maximum tree depth”, but had the side-effect of setting “currentTiddler” to the value of <__level__> (which is initially “0”). I’ve changed this to:
The result of these two errors was that instead of recursively outputting the tiddler titles, it was outputting the level number (as [[0]]) for each item at the first level of the “TableOfContents” tree (for which there are 13 tiddlers, and thus 13 [[0]] were displayed.
Well, when you open a tiddler by the next/previous button the TOC in the sidebar automatically opens the branch and highlights the nodes!
I did not try the new code to see if a node (tiddler) is opened by other means it will open the branch or not!
TiddlyTools/TOC <<toc-tree>>'s “auto-open” feature relies upon the value in $:/HistoryList!!current-tiddler, so it should work as long opening a tiddler updates the $:/HistoryList!!current-tiddler value. This should be the case for any action that uses tm-navigate, including clicking on links directly embedded in tiddler content.
Note, however, that if some custom code directly changes the title list contained in $:/StoryList!!listbut does not also update the$:/HistoryList!!current-tiddler value, then the <<toc-tree>> display won’t auto-open nor highlight the “current” tiddler.
Here’s some code you can try on https://TiddlyWiki.com to illustrate the different handling:
Direct link to [[Other Tiddler]] (missing)
<$button> tm-navigate open of "Other Tiddler"
<$action-navigate $to="Other Tiddler"/>
</$button>
<$button> test custom open of "Other Tiddler"
<$action-setfield $tiddler="$:/StoryList" $field="list" $value={{{ [[Other Tiddler]] [list[$:/StoryList]] +[format:titlelist[]join[ ]] }}}/>
</$button>
<$button> close "Other Tiddler"
<$action-sendmessage $message="tm-close-tiddler" $param="Other Tiddler"/>
</$button>
$:/StoryList!!list = <blockquote>{{$:/StoryList!!list}}</blockquote>
$:/HistoryList!!current-tiddler = <blockquote>{{$:/HistoryList!!current-tiddler}}</blockquote>
TiddlyTools <<toc-all>> (which is a special case of <<toc-tree>>) automatically shows the caption field of each tiddler but links to the actual tiddler title values. This is consistent with the handling in the TWCore’s <<toc-*>> macros, which also show the captions but link to the titles.
The <<toc-caption>> macro, defined in the $:/core/macros/toc shadow tiddler, looks like this:
If you want to always show the actual tiddler title, you can define a modified version of <<toc-caption>> that precedes your use of <<toc-all>> (or <<toc-tree>>) and replaces
In contrast, <<toc-list>> always outputs the actual title value of each tiddler. It is left up to the calling wikitext context to decide how to handle that list of titles, either by showing/linking to them “as-is”, or by showing the caption text but linking to the title value.
So, while it might not be “expected”, it is the “intended” behavior.
Thank you for your explanation. I know understand why the links in toc-all and toc-list are different.
However, I still don’t understand why there are extra [[]] add to the outside of title by toc-link where there are blankspace in the title. As a result of this behaviour, those links are not pointing to the actual tiddler with title title. Instead they are pointing to [[title]], which is different from the tiddler indexed.
The output of <<toc-list>> uses the TWCore standard “titlelist” format, which automatically surrounds titles that contain spaces inside [[ and ]]. This allows you to use the enlist filter operator to process the list of titles. If the square brackets were not present, then each WORD of a title that contains spaces would be processed as a separate tiddler title.
The $wikify widget “captures” the <<toc-list>> output in a variable, which is then used as a parameter for the enlist filter operator to iterate over the titles and output a link to each.
Both the TiddlyTools TOC macros (<<toc-tree>> and <<toc-list>>) and the standard TWCore TOC macros use the list field to apply a manually defined sort order in which child tiddlers are shown in the tree (in contrast to the default alpha-sorted order that is used for “unlisted” child tiddlers).
The list field is also used by the TiddlyTools <<toc-tree>> macro to handle drag-and-drop re-arrangment of tree items, which allows interactive redefinition of parent-child relationships as well as “sibling re-ordering”. Dropping an item on a parent makes it a child of the item it is dropped on. This drag-and-drop behavior is also affected by “modifier” keys, where alt-drop creates a child copy of an item, and ctrl-drop or shift-drop moves an item before or after the item it is dropped on (i.e., as a sibling of the item it is dropped on).
The list field WAS also being used to allow the creation of “top down” tree definitions, where each parent could contain an explicit list of their respective children, as compared to usual “bottom up” tree definitions, where each child has a field (default tags) that contains a list of one or more parent tiddlers.
However… as you’ve correctly pointed out… this produced unexpected results by conflating the top down list field handling and the “sort order” list field handling.
Although top down tree definitions are still interesting, the implementation (which was essentially an undocumented experiment) was, at best, flawed… and been temporarily REMOVED from the TiddlyTools/TOC code until such time as I can come up with a better approach to adding this functionality.