Function to return a hierarchical tree of tagging tiddlers - Any way to optimise?

Hello

I have written the below functions to recursively generate a tree of tiddlers descended from a single tag, on the condition that each of them also has a field parent_tag which contains the title of the tiddler directly above it in the hierarchy.

\function tag.tree()
[all[]]
:map:flat[tagging[]field:parent_tag<currentTiddler>]
+[!is[blank]]
:map:flat[function[tag.tree.include.self]]
\end

\function tag.tree.include.self()
[all[]]
[function[tag.tree]]
\end

Is there any way to optimise or neaten this up, ideally to be contained within a single function? I have thought about it for a while, but I think I need the second filter expression to make sure that each subsequent call to the first function also returns the tiddler that triggered the call…

Any suggestions would be appreciated!

@saqimtiaz demonstrated a recursive function for a similar problem but I can’t find it.

There is a javascript operator written to build a list based on tags and recursion that may do it faster. Recursive filter operators to show all tiddlers beneath a tag and all tags above a tiddler

  • Personally I would not be too hung up reducing it to one filter because recursion is by nature one process calling itself after an initial function.
  • Using the above operator you could store the list of tiddlers and later act on them for, example if you wanted to sum values across all tiddlers, and only cared about the order not the structure.
    • This in effect separates the act of finding the tiddlers from the processing of them.
1 Like

I’m not 100% sure, but may be my tocP parent based toc may be an option.
See: tocP — Parent based - Table of Content

-mario