Useful macro to build and display/act-on a "linked-list" of tiddlers

84 views
Skip to first unread message

Diego Mesa

unread,
Feb 12, 2018, 1:14:42 PM2/12/18
to TiddlyWiki
Hey all,

While working on my Spaced Repetition/Anki system, I've developed this general link-list macro which might be useful. Its purpose is to take a subfilter, eg: "tag[Flash Card]", sort them someway, eg: created, an ending link (eg: "Results"), and create a series of links from one tiddler to the next, all the way to the end. The "next" tiddler in the link is saved in a field, whose name you can also specify.

Right now, it also has the list navigation built in (action-navigate), though Im sure we can just add an additional actions parameter to this macro in the future.

\define createAndShowLinkedList(subfilter lastlink sortby:created linkfieldname:next)
<!-- for the n many cards, we return n-1 -->
<$formula-vars counter="count([$subfilter$])-1">
   
<!-- iter goes from 0 to n-2  -->
   
<$list filter="[range<counter>butlast[]]" variable="iter">
       
<!-- arrays need to be accessed from 1 to n -->        
       
<$formula-vars current="nth([$subfilter$sort[$sortby$]], <<iter>> + 1)" next="nth([$subfilter$sort[$sortby$]], <<iter>> + 2)">
           
<$action-setfield $tiddler=<<current>> $field=$linkfieldname$ $value=<<next>>/>
            <!--<Current: <<current>>, Next: <<next>> <br/
> -->
       
</$formula-vars>
    </
$list>
   
<!-- take last element with no forward link -->
   
<$list filter="[range<counter>last[]]" variable="iter">
       
<$formula-vars current="nth([$subfilter$sort[$sortby$]], <<iter>> + 1)">
           
<$action-setfield $tiddler=<<current>> $field=$linkfieldname$ $value=$lastlink$/>
       
</$formula-vars>
    </
$list>
</$formula-vars>

<!-- begin list navigation -->
<$list filter="[$subfilter$sort[$sortby$]first[]]">
    <$action-navigate $to=<<currentTiddler>>/
>
</$list>
\end

It currently uses Evan's excellent formula plugin. I'm pretty sure someone could do all of the counting, adding, etc without it, but Im not sure how to get the range using just core features. I was thinking of trying to make this my first core PR, but I couldnt see a way around the use of range.

If anyone has any feedback, critcisms, etc please let me know!

Best,
Diego
Reply all
Reply to author
Forward
0 new messages