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:
\define toc-list(here,max,exclude,level:"1",field:"tags")
<!-- SHOW FLAT LIST -->
<$list filter="""[contains:$field$[$here$]sortby{$here$!!list}] -[subfilter<__exclude__>] -[[$here$]]""">
<$text text="[["/><<currentTiddler>><$text text="]]"/><br>
<$reveal default="$level$" type="nomatch" text="$max$">
<$macrocall $name="toc-list" here=<<currentTiddler>> max="$max$" exclude="""$exclude$ [[$here$]]""" level={{{ [[$level$]add[1]] }}} field=<<__field__>>/>
</$reveal>
</$list>
\end
Notes:
- Only the
here
parameter is required, wherehere
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 withmax
) 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:
\define toc-list(here,exclude)
<$list filter="""[tag[$here$]sortby{$here$!!list}] -[subfilter<__exclude__>] -[[$here$]]""">
<$text text="[["/><<currentTiddler>><$text text="]]"/><br>
<$macrocall $name="toc-list" here=<<currentTiddler>> exclude="""$exclude$ [[$here$]]"""/>
</$list>
\end
enjoy,
-e